NumPy Arrays - Differences, Reshaping, and Transposing
Master NumPy arrays for advanced data manipulation
NumPy Arrays vs Python Lists
| Feature | Python Lists | NumPy Arrays |
|---|---|---|
| Display Format | Numbers with commas | Numbers without commas |
| Properties | No shape or ndim | Has shape and ndim properties |
| Reshaping | Manual clustering required | Built-in reshape method |
| Dimensions | Always 1D | Multi-dimensional support |
Essential NumPy Array Properties
shape
Returns dimensions as a tuple. For 1D arrays shows (n,) where n is length. For 2D shows (rows, columns).
ndim
Returns number of dimensions. Vector arrays have ndim=1, matrix arrays have ndim=2.
reshape
Transforms array structure while preserving total element count. Cannot change total number of elements.
Count the square brackets before seeing numbers to determine dimensions. One bracket = 1D vector, two brackets = 2D matrix.
Common Array Reshaping Patterns (12 Elements)
Reshaping Arrays Step by Step
Create Array
Convert Python list to NumPy array using np.array() function
Check Dimensions
Use .shape and .ndim properties to verify current structure
Apply Reshape
Use .reshape(rows, columns) method ensuring total elements match
Verify Result
Check new shape and visual representation of reshaped array
Reshape vs Transpose Operations
| Feature | Reshape | Transpose |
|---|---|---|
| Element Order | Preserves original order | Changes element positions |
| Purpose | Change array dimensions | Flip rows and columns |
| Method | .reshape(rows, cols) | .transpose() |
| Data Flow | Left-to-right, top-to-bottom | Rows become columns |
Transpose actually moves data elements to new positions, while reshape just changes how the same ordered elements are displayed in rows and columns.
Creating a Tic-Tac-Toe Board Array
Create List
Make a Python list with 9 elements (X's and O's)
Convert to Array
Use np.array() to convert list to NumPy array
Reshape to 3x3
Apply .reshape(3,3) to create game board layout
Modify Elements
Access specific positions using array[row,col] notation
Array Manipulation Checklist
New dimensions must multiply to original element count
Access elements with array[row, column] syntax
Confirm shape and ndim match expectations
Choose based on whether you need to preserve element order
This lesson is a preview from our Data Science & AI Certificate Online (includes software) and Python Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.
Key Takeaways