NumPy Array Attributes

Every NumPy array comes with built-in attributes that describe its structure - its dimensions, size, and the type of data it holds - which are essential to check whenever you're debugging shape mismatches or preparing data for a model.

Key Array Attributes

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

print(arr.shape)   # (2, 3) - 2 rows, 3 columns
print(arr.ndim)    # 2 - number of dimensions
print(arr.size)    # 6 - total number of elements
print(arr.dtype)   # int64 - the data type of the elements

Reshaping an Array

The reshape() method changes an array's shape without changing its data, as long as the total number of elements stays the same.

import numpy as np

arr = np.arange(12)          # [0, 1, 2, ..., 11]
reshaped = arr.reshape(3, 4)  # reshape into 3 rows, 4 columns
print(reshaped)

flattened = reshaped.flatten()   # convert back to a 1D array

Changing Data Type

import numpy as np

arr = np.array([1, 2, 3])
float_arr = arr.astype(float)
print(float_arr)   # [1. 2. 3.]
A huge number of NumPy errors in real projects come down to a shape mismatch between arrays - regularly checking .shape while debugging is one of the fastest ways to catch these issues.

Coming Up Next

To close out this section, the final topic covers Matrix Product - how NumPy performs matrix multiplication, a core operation behind many Machine Learning algorithms.

Strides and How Memory Is Laid Out

Beyond shape and dtype, every array has a strides attribute: the number of bytes to step in memory to move one element along each axis. Understanding strides explains why some operations are nearly free. Reshaping or transposing an array often just changes its shape and strides while pointing at the same data — no copying happens.

import numpy as np
arr = np.arange(6).reshape(2, 3)
print(arr.strides)        # e.g. (24, 8) on a 64-bit int array
print(arr.T.strides)      # transpose swaps the strides, data untouched
print(arr.itemsize)       # 8, bytes per element
print(arr.nbytes)         # 48, total bytes used

View vs Copy When You Reshape

This is where silent bugs hide. Many operations return a view that shares memory with the original, so changing the view changes the source too. Others return an independent copy. Slicing usually gives a view; fancy indexing and flatten() give a copy, while ravel() gives a view when it can.

a = np.array([1, 2, 3, 4])
b = a[1:3]        # view
b[0] = 99         # a is now [1, 99, 3, 4]  -- surprise!
c = a[1:3].copy() # independent copy, safe to edit

When in doubt whether you hold a view, call .copy() before modifying, or check arr.base — a non-None base means the array is a view onto something else.

Debugging Shape Errors Fast

Most real NumPy errors are shape mismatches during broadcasting or matrix operations. A quick routine saves time:

  • Print .shape of every array just before the failing line — the mismatch is usually obvious once both shapes are visible.
  • Remember broadcasting aligns shapes from the right; a (3,) array combines with a (2, 3) array but not a (3, 2) one.
  • Use reshape(-1, 1) or reshape(1, -1) to turn a 1-D array into an explicit column or row when an operation expects 2-D.
  • Check .ndim when a function demands a specific number of dimensions.

One more attribute saves memory and avoids surprises: dtype. NumPy fixes the element type when an array is created, so assigning a float into an integer array silently truncates it rather than widening the array. If you need decimals, create the array as a float type from the start or convert it with astype(float) first. Likewise, very large integer values can overflow a small integer dtype without any error, so for big counts an explicit wider type is safer than trusting the default.

Takeaway: these attributes are not trivia — shape and ndim catch mismatches, strides explains why reshaping is cheap, and knowing view versus copy prevents the baffling bug where editing one array quietly changes another.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course