Bird
Raised Fist0
Data Analysis Pythondata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the shape of a NumPy array represent?
easy
A. The number of elements along each dimension
B. The total number of elements in the array
C. The data type of the elements
D. The memory size of the array

Solution

  1. Step 1: Understand the meaning of shape

    The shape of an array is a tuple that shows how many elements are in each dimension.
  2. Step 2: Differentiate shape from other properties

    Shape is not the total count, data type, or memory size, but the size per dimension.
  3. Final Answer:

    The number of elements along each dimension -> Option A
  4. Quick Check:

    Shape = elements per dimension [OK]
Hint: Shape = count of elements per axis [OK]
Common Mistakes:
  • Confusing shape with total number of elements
  • Mixing shape with data type
  • Thinking shape is memory size
2. Which of the following is the correct way to get the shape of a NumPy array arr?
easy
A. arr.shape
B. arr.shape()
C. shape(arr)
D. arr.get_shape()

Solution

  1. Step 1: Recall NumPy shape syntax

    In NumPy, shape is an attribute, not a method, so no parentheses are used.
  2. Step 2: Check each option

    arr.shape uses arr.shape correctly; others use incorrect syntax or functions.
  3. Final Answer:

    arr.shape -> Option A
  4. Quick Check:

    Use attribute, no parentheses [OK]
Hint: Use arr.shape without parentheses [OK]
Common Mistakes:
  • Adding parentheses after shape
  • Using non-existent functions like get_shape()
  • Trying to call shape as a function
3. Given the code:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)

What is the output?
medium
A. (3, 2)
B. (6,)
C. (2, 3)
D. (2,)

Solution

  1. Step 1: Analyze the array structure

    The array has 2 rows and 3 columns, so shape is (2, 3).
  2. Step 2: Confirm shape output

    Printing arr.shape returns the tuple (2, 3).
  3. Final Answer:

    (2, 3) -> Option C
  4. Quick Check:

    Rows=2, Columns=3 -> (2, 3) [OK]
Hint: Count rows then columns for shape [OK]
Common Mistakes:
  • Swapping rows and columns in shape
  • Thinking shape is total elements
  • Confusing shape with array size
4. What is wrong with this code snippet?
import numpy as np
arr = np.array([1, 2, 3])
print(arr.shape())
medium
A. print statement syntax is incorrect
B. arr is not a NumPy array
C. The array is empty
D. shape is an attribute, not a method; parentheses cause error

Solution

  1. Step 1: Identify the error source

    The code calls arr.shape() with parentheses, but shape is an attribute.
  2. Step 2: Understand attribute vs method

    Attributes do not use parentheses; calling with () causes a TypeError.
  3. Final Answer:

    shape is an attribute, not a method; parentheses cause error -> Option D
  4. Quick Check:

    Attributes no parentheses [OK]
Hint: Remember: shape is attribute, no () [OK]
Common Mistakes:
  • Using parentheses with shape
  • Assuming array is empty causes error
  • Misreading print syntax
5. You have a 3D NumPy array with shape (4, 3, 2). Which of the following operations will correctly reshape it into a 2D array with 4 rows and 6 columns?
hard
A. arr.reshape(12, 2)
B. arr.reshape(4, 6)
C. arr.reshape(6, 4)
D. arr.reshape(2, 12)

Solution

  1. Step 1: Calculate total elements

    Original shape (4, 3, 2) means 4*3*2 = 24 elements total.
  2. Step 2: Determine new shape dimensions

    New shape (4, 6) means 4*6 = 24 elements, matching total elements.
  3. Step 3: Verify other options

    arr.reshape(12, 2), arr.reshape(6, 4), and arr.reshape(2, 12) do not produce 4 rows and 6 columns.
  4. Final Answer:

    arr.reshape(4, 6) -> Option B
  5. Quick Check:

    4*6=24 matches total elements [OK]
Hint: New shape must keep total elements same [OK]
Common Mistakes:
  • Mixing up rows and columns in reshape
  • Choosing shapes with wrong total elements
  • Forgetting reshape must keep total size
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard