0
0
Data Analysis Pythondata~10 mins

Array shapes and dimensions in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array shapes and dimensions
Create array
Check number of dimensions (ndim)
Check shape (size in each dimension)
Use shape and ndim for analysis or reshape
End
Start by creating an array, then check how many dimensions it has and the size in each dimension using shape. Use this info to understand or change the array.
Execution Sample
Data Analysis Python
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.ndim)
print(arr.shape)
Create a 2D array and print its number of dimensions and shape.
Execution Table
StepActionArray Contentndim (Dimensions)shape (Size per dimension)Output
1Create array arr[[1, 2, 3], [4, 5, 6]]2(2, 3)
2Print arr.ndim[[1, 2, 3], [4, 5, 6]]2(2, 3)2
3Print arr.shape[[1, 2, 3], [4, 5, 6]]2(2, 3)(2, 3)
4End[[1, 2, 3], [4, 5, 6]]2(2, 3)
💡 All steps completed, array shape and dimensions checked.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arrNone[[1, 2, 3], [4, 5, 6]][[1, 2, 3], [4, 5, 6]][[1, 2, 3], [4, 5, 6]][[1, 2, 3], [4, 5, 6]]
arr.ndimN/A2222
arr.shapeN/A(2, 3)(2, 3)(2, 3)(2, 3)
Key Moments - 3 Insights
Why does arr.ndim show 2 for this array?
Because the array has two levels: rows and columns, so it is 2-dimensional as shown in execution_table step 2.
What does the shape (2, 3) mean?
It means the array has 2 rows and 3 columns, as seen in execution_table step 3.
Can an array have shape with more than two numbers?
Yes, arrays can have many dimensions, each number in shape shows size in that dimension.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of arr.ndim at step 2?
A1
B2
C3
D0
💡 Hint
Check the 'Output' column at step 2 in the execution_table.
At which step does the code print the shape of the array?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the action 'Print arr.shape' in the execution_table.
If the array was 3D with shape (2, 3, 4), what would arr.ndim be?
A3
B4
C2
D1
💡 Hint
Number of dimensions equals the length of the shape tuple, see variable_tracker for shape info.
Concept Snapshot
Array shapes and dimensions:
- Use arr.ndim to get number of dimensions.
- Use arr.shape to get size in each dimension.
- Shape is a tuple showing length per dimension.
- Useful for understanding array structure.
- Can reshape arrays using this info.
Full Transcript
We start by creating a 2D array with two rows and three columns. Then we check the number of dimensions using arr.ndim, which is 2 because the array has rows and columns. Next, we check the shape using arr.shape, which shows (2, 3) meaning 2 rows and 3 columns. This information helps us understand the array's structure and is useful for further data analysis or reshaping.