📐 Array Properties
Understanding array properties is crucial for working effectively with NumPy! Every array has characteristics like shape, dimensions, data type, and size that determine how it stores data and what operations you can perform. These properties help you understand your data structure and optimize your code.
Think of array properties as the "blueprint" of your data - they tell you everything about how your array is organized and stored in memory.
import numpy as np
# Create a sample array to explore properties
sample_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(f"Array: \n{sample_array}")
print(f"Shape: {sample_array.shape}")
print(f"Dimensions: {sample_array.ndim}")
print(f"Size: {sample_array.size}")
print(f"Data type: {sample_array.dtype}")
🔍 Why Array Properties Matter
Array properties are fundamental because they:
- Define data structure 📊: Shape tells you how data is organized (rows, columns, etc.)
- Control memory usage 💾: Data types determine how much memory each element uses
- Enable efficient operations ⚡: Understanding dimensions helps you write better code
- Prevent errors 🛡️: Knowing array properties helps avoid shape mismatches
- Optimize performance 🚀: Right data types and shapes lead to faster computations
Every NumPy operation depends on these properties!
📏 Shape and Dimensions Preview
Shape is probably the most important property - it defines how your data is structured:
import numpy as np
# Different array shapes
vector = np.array([1, 2, 3, 4]) # 1D array
matrix = np.array([[1, 2], [3, 4]]) # 2D array
cube = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # 3D array
print(f"Vector: {vector}")
print(f" Shape: {vector.shape}, Dimensions: {vector.ndim}")
print(f"Matrix: \n{matrix}")
print(f" Shape: {matrix.shape}, Dimensions: {matrix.ndim}")
print(f"Cube shape: {cube.shape}, Dimensions: {cube.ndim}")
print(f"Total elements in cube: {cube.size}")
🔢 Data Types and Memory Preview
Data types control how numbers are stored and how much memory they use:
import numpy as np
# Different data types
int_array = np.array([1, 2, 3], dtype=int)
float_array = np.array([1.0, 2.0, 3.0], dtype=float)
small_int = np.array([1, 2, 3], dtype=np.int8) # Saves memory
print(f"Integer array: {int_array}, dtype: {int_array.dtype}")
print(f"Float array: {float_array}, dtype: {float_array.dtype}")
print(f"Small int: {small_int}, dtype: {small_int.dtype}")
print(f"Memory usage:")
print(f" int64: {int_array.nbytes} bytes")
print(f" float64: {float_array.nbytes} bytes")
print(f" int8: {small_int.nbytes} bytes")
📚 What You'll Learn in This Section
This section covers all the essential array properties:
- 📐 Array Shape and Dimensions Master understanding and working with array shapes, from 1D vectors to multi-dimensional arrays.
- 🔢 Data Types and Memory Learn about NumPy data types, memory usage, and how to optimize storage efficiency.
- 📊 Array Size and Indexing Understand array sizing, indexing fundamentals, and how to access array properties.
🎯 Array Properties in Action
See how understanding properties helps in real scenarios:
import numpy as np
print("📊 Array Properties in Practice")
print("=" * 35)
# Example: Image data analysis
image_data = np.random.randint(0, 255, (100, 150, 3)) # 100x150 RGB image
print(f"Image properties:")
print(f" Dimensions: {image_data.ndim}D (height, width, channels)")
print(f" Shape: {image_data.shape}")
print(f" Total pixels: {image_data.shape[0] * image_data.shape[1]}")
print(f" Color channels: {image_data.shape[2]}")
print(f" Memory usage: {image_data.nbytes / 1024:.1f} KB")
# Example: Checking data compatibility
data1 = np.array([[1, 2], [3, 4]])
data2 = np.array([10, 20])
print(f"\nData compatibility check:")
print(f" Matrix shape: {data1.shape}")
print(f" Vector shape: {data2.shape}")
print(f" Can broadcast? {data1.shape[-1] == data2.shape[0]}")
💡 Why Properties Matter for Performance
Understanding array properties helps you write faster, more efficient code:
🔍 Quick Properties Reference
Here are the most commonly used array properties:
Property | Description | Example |
---|---|---|
.shape | Dimensions of array | (3, 4) for 3x4 matrix |
.ndim | Number of dimensions | 2 for 2D array |
.size | Total number of elements | 12 for 3x4 matrix |
.dtype | Data type of elements | int64 , float32 |
.itemsize | Bytes per element | 8 for int64 |
.nbytes | Total memory used | 96 bytes for 12 int64s |
🎯 Properties Inspection Example
Let's explore all properties of a real array:
import numpy as np
# Create a sample data array
sales_data = np.array([
[150, 200, 175], # Q1 sales
[180, 220, 195], # Q2 sales
[165, 210, 185], # Q3 sales
[190, 240, 205] # Q4 sales
], dtype=np.float32)
print("🏢 Sales Data Analysis")
print("=" * 25)
print(f"Data: \n{sales_data}")
print(f"\n📐 Structure Properties:")
print(f" Shape: {sales_data.shape} (quarters, products)")
print(f" Dimensions: {sales_data.ndim}D")
print(f" Total values: {sales_data.size}")
print(f"\n💾 Memory Properties:")
print(f" Data type: {sales_data.dtype}")
print(f" Bytes per number: {sales_data.itemsize}")
print(f" Total memory: {sales_data.nbytes} bytes")
print(f"\n📊 Data Insights:")
print(f" Quarters tracked: {sales_data.shape[0]}")
print(f" Products tracked: {sales_data.shape[1]}")
print(f" Average quarterly sales: {sales_data.mean(axis=1)}")
🎯 Key Takeaways
🚀 What's Next?
Ready to dive deep into array properties? Let's start by mastering array shape and dimensions - the foundation of array structure.
Continue to: Array Shape and Dimensions
Time to master array structure! 📐✨
Was this helpful?
Track Your Learning Progress
Sign in to bookmark tutorials and keep track of your learning journey.
Your progress is saved automatically as you read.