Array Attributes

Loading concept...

🎒 NumPy Array Attributes: Your Array’s ID Card


🌟 The Story Begins

Imagine you have a box of LEGO bricks. Before building anything cool, you’d want to know:

  • How many layers does this box have? (Is it flat or stacked?)
  • What’s the shape of the arrangement?
  • How many total bricks are there?
  • How big is each brick?
  • How much does the whole thing weigh?

NumPy arrays are just like LEGO boxes! They come with an “ID card” — special attributes that tell you everything about them. Let’s explore these superpowers! 🦸


📐 1. Array Dimensions with ndim

What Is It?

Think of dimensions as layers or directions.

  • A single row of candies = 1 dimension (1D)
  • A grid of chocolates (rows & columns) = 2 dimensions (2D)
  • A stack of grids = 3 dimensions (3D)

The .ndim attribute tells you: “How many directions can I move in this array?”

🍬 Simple Example

import numpy as np

candies = np.array([1, 2, 3, 4])
print(candies.ndim)
# Output: 1

This is a 1D array — just one row of candies!

chocolate_box = np.array([
    [1, 2, 3],
    [4, 5, 6]
])
print(chocolate_box.ndim)
# Output: 2

This is a 2D array — rows AND columns, like a chocolate box!

stacked_boxes = np.array([
    [[1, 2], [3, 4]],
    [[5, 6], [7, 8]]
])
print(stacked_boxes.ndim)
# Output: 3

This is a 3D array — multiple layers stacked up!

🧠 Key Insight

ndim counts how many “brackets” you need to reach the innermost number.

  • [1, 2, 3] → 1 bracket layer → ndim = 1
  • [[1, 2], [3, 4]] → 2 bracket layers → ndim = 2

📏 2. Array Shape Attribute

What Is It?

If ndim tells you how many directions, then .shape tells you how far in each direction.

Think of it as the blueprint of your LEGO box:

  • How many rows?
  • How many columns?
  • How many layers?

🏠 Simple Example

import numpy as np

row = np.array([10, 20, 30, 40, 50])
print(row.shape)
# Output: (5,)

Translation: 1 direction with 5 items.

grid = np.array([
    [1, 2, 3],
    [4, 5, 6]
])
print(grid.shape)
# Output: (2, 3)

Translation: 2 rows, 3 columns — like a table!

cube = np.array([
    [[1, 2], [3, 4]],
    [[5, 6], [7, 8]],
    [[9, 10], [11, 12]]
])
print(cube.shape)
# Output: (3, 2, 2)

Translation: 3 layers, each with 2 rows and 2 columns.

🎯 The Shape Formula

shape = (size_of_dim_0, size_of_dim_1, ...)

Pro Tip: The shape is always a tuple. Even 1D arrays have a comma: (5,)


🔢 3. Array Size Attribute

What Is It?

.size answers the simplest question: “How many items are in this entire array?”

It’s like counting every single LEGO brick in your box — no matter how they’re arranged.

🧮 Simple Example

import numpy as np

small = np.array([1, 2, 3])
print(small.size)
# Output: 3

Three items. Easy!

medium = np.array([
    [1, 2, 3],
    [4, 5, 6]
])
print(medium.size)
# Output: 6

2 rows × 3 columns = 6 items total.

big = np.array([
    [[1, 2], [3, 4]],
    [[5, 6], [7, 8]]
])
print(big.size)
# Output: 8

2 layers × 2 rows × 2 columns = 8 items.

💡 The Magic Formula

size = shape[0] × shape[1] × shape[2] × ...

Just multiply all the numbers in the shape tuple!


📦 4. itemsize — Size of Each Element

What Is It?

Every number in your array takes up some memory space (measured in bytes).

.itemsize tells you: “How many bytes does ONE number use?”

🎈 Simple Example

import numpy as np

integers = np.array([1, 2, 3])
print(integers.itemsize)
# Output: 8

Each integer uses 8 bytes (default int64).

floats = np.array([1.5, 2.5, 3.5])
print(floats.itemsize)
# Output: 8

Each float uses 8 bytes (default float64).

small_ints = np.array([1, 2, 3], dtype=np.int8)
print(small_ints.itemsize)
# Output: 1

Using int8 = only 1 byte per number!

📊 Common Item Sizes

Data Type Itemsize
int8 1 byte
int32 4 bytes
int64 8 bytes
float32 4 bytes
float64 8 bytes

💾 5. nbytes — Total Memory Used

What Is It?

.nbytes tells you the total memory your entire array consumes.

It’s like asking: “How heavy is my whole LEGO box?”

🏋️ Simple Example

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(arr.nbytes)
# Output: 40

5 items × 8 bytes each = 40 bytes total.

grid = np.array([
    [1, 2, 3],
    [4, 5, 6]
])
print(grid.nbytes)
# Output: 48

6 items × 8 bytes = 48 bytes.

🔗 The Relationship

nbytes = size × itemsize

This is always true!

arr = np.array([[1, 2], [3, 4]])
print(f"Size: {arr.size}")
print(f"Itemsize: {arr.itemsize}")
print(f"nbytes: {arr.nbytes}")
print(f"Check: {arr.size * arr.itemsize}")

# Output:
# Size: 4
# Itemsize: 8
# nbytes: 32
# Check: 32 ✓

🎨 Putting It All Together

Here’s a complete picture of an array’s “ID Card”:

import numpy as np

my_array = np.array([
    [1.0, 2.0, 3.0],
    [4.0, 5.0, 6.0],
    [7.0, 8.0, 9.0]
])

print(f"ndim:     {my_array.ndim}")
print(f"shape:    {my_array.shape}")
print(f"size:     {my_array.size}")
print(f"itemsize: {my_array.itemsize}")
print(f"nbytes:   {my_array.nbytes}")

Output:

ndim:     2
shape:    (3, 3)
size:     9
itemsize: 8
nbytes:   72

🗺️ Visual Summary

graph LR A[🎒 NumPy Array] --> B[ndim = 2] A --> C[shape = 3, 3] A --> D[size = 9] A --> E[itemsize = 8 bytes] A --> F[nbytes = 72 bytes] B --> G[How many directions?] C --> H[How big in each direction?] D --> I[Total element count] E --> J[Memory per element] F --> K[Total memory used]

🚀 You Did It!

Now you can describe any NumPy array like a pro:

Attribute Question Answered
.ndim How many dimensions?
.shape What’s the size in each dimension?
.size How many total elements?
.itemsize How many bytes per element?
.nbytes How much total memory?

🎯 Remember: These are read-only properties. They describe your array — you can’t change them directly!

You’re now ready to inspect any array’s ID card! 🎉

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.