We use array shapes and dimensions to understand the size and structure of data. This helps us organize and analyze data correctly.
Array shapes and dimensions in Data Analysis Python
import numpy as np # Create an array array = np.array([[1, 2, 3], [4, 5, 6]]) # Get the shape of the array shape = array.shape # Get the number of dimensions dimensions = array.ndim
shape returns a tuple showing the size in each dimension.
ndim returns an integer showing how many dimensions the array has.
import numpy as np empty_array = np.array([]) print(empty_array.shape) # Output: (0,) print(empty_array.ndim) # Output: 1
import numpy as np one_element_array = np.array([42]) print(one_element_array.shape) # Output: (1,) print(one_element_array.ndim) # Output: 1
import numpy as np matrix = np.array([[1, 2, 3], [4, 5, 6]]) print(matrix.shape) # Output: (2, 3) print(matrix.ndim) # Output: 2
import numpy as np tensor = np.array([[[1], [2]], [[3], [4]]]) print(tensor.shape) # Output: (2, 2, 1) print(tensor.ndim) # Output: 3
This program creates arrays with different shapes and dimensions. It prints their shapes and number of dimensions to show how data structure changes.
import numpy as np # Create different arrays empty_array = np.array([]) one_element_array = np.array([42]) matrix = np.array([[1, 2, 3], [4, 5, 6]]) tensor = np.array([[[1], [2]], [[3], [4]]]) # Print shapes and dimensions print(f"Empty array shape: {empty_array.shape}, dimensions: {empty_array.ndim}") print(f"One element array shape: {one_element_array.shape}, dimensions: {one_element_array.ndim}") print(f"Matrix shape: {matrix.shape}, dimensions: {matrix.ndim}") print(f"Tensor shape: {tensor.shape}, dimensions: {tensor.ndim}")
Time complexity to get shape or ndim is O(1) because it is stored as metadata.
Space complexity is not affected by accessing shape or ndim.
Common mistake: Confusing shape (size in each dimension) with total number of elements.
Use shape and ndim to check if arrays can be combined or reshaped safely.
Array shape tells how many elements are in each dimension.
Number of dimensions shows how many axes the data has.
Knowing shape and dimensions helps organize and analyze data correctly.
Practice
Solution
Step 1: Understand the meaning of shape
The shape of an array is a tuple that shows how many elements are in each dimension.Step 2: Differentiate shape from other properties
Shape is not the total count, data type, or memory size, but the size per dimension.Final Answer:
The number of elements along each dimension -> Option AQuick Check:
Shape = elements per dimension [OK]
- Confusing shape with total number of elements
- Mixing shape with data type
- Thinking shape is memory size
arr?Solution
Step 1: Recall NumPy shape syntax
In NumPy, shape is an attribute, not a method, so no parentheses are used.Step 2: Check each option
arr.shape usesarr.shapecorrectly; others use incorrect syntax or functions.Final Answer:
arr.shape -> Option AQuick Check:
Use attribute, no parentheses [OK]
- Adding parentheses after shape
- Using non-existent functions like get_shape()
- Trying to call shape as a function
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.shape)
What is the output?
Solution
Step 1: Analyze the array structure
The array has 2 rows and 3 columns, so shape is (2, 3).Step 2: Confirm shape output
Printingarr.shapereturns the tuple (2, 3).Final Answer:
(2, 3) -> Option CQuick Check:
Rows=2, Columns=3 -> (2, 3) [OK]
- Swapping rows and columns in shape
- Thinking shape is total elements
- Confusing shape with array size
import numpy as np arr = np.array([1, 2, 3]) print(arr.shape())
Solution
Step 1: Identify the error source
The code callsarr.shape()with parentheses, but shape is an attribute.Step 2: Understand attribute vs method
Attributes do not use parentheses; calling with () causes a TypeError.Final Answer:
shape is an attribute, not a method; parentheses cause error -> Option DQuick Check:
Attributes no parentheses [OK]
- Using parentheses with shape
- Assuming array is empty causes error
- Misreading print syntax
Solution
Step 1: Calculate total elements
Original shape (4, 3, 2) means 4*3*2 = 24 elements total.Step 2: Determine new shape dimensions
New shape (4, 6) means 4*6 = 24 elements, matching total elements.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.Final Answer:
arr.reshape(4, 6) -> Option BQuick Check:
4*6=24 matches total elements [OK]
- Mixing up rows and columns in reshape
- Choosing shapes with wrong total elements
- Forgetting reshape must keep total size
Ready to prove it?
Pass the test and this pattern moves unknown
