An ndarray is a multi-dimensional, fixed-size container of items of the same data type in NumPy. It is the core data structure used for numerical computations.
Click to reveal answer
beginner
How do you create a 2D ndarray with NumPy?
You can create a 2D ndarray by passing a list of lists to np.array(). For example: np.array([[1, 2], [3, 4]]) creates a 2x2 array.
Click to reveal answer
beginner
What does the shape attribute of an ndarray tell you?
The shape attribute shows the size of the array in each dimension. For example, a shape of (3, 4) means 3 rows and 4 columns.
Click to reveal answer
intermediate
Why must all elements in an ndarray have the same data type?
Having the same data type allows NumPy to store data efficiently in memory and perform fast mathematical operations.
Click to reveal answer
intermediate
How does an ndarray differ from a Python list?
An ndarray is fixed-size, multi-dimensional, and stores elements of the same type for fast computation. A Python list is flexible in size and can hold mixed data types but is slower for numerical tasks.
Click to reveal answer
What is the main advantage of using an ndarray over a Python list for numerical data?
AAutomatically resizes when adding elements
BCan store mixed data types
CSupports variable length elements
DFaster mathematical operations due to fixed data type and memory layout
✗ Incorrect
NumPy's ndarray stores data in a fixed type and contiguous memory, enabling fast math operations.
Which NumPy function is used to create an ndarray from a Python list?
Anp.array()
Bnp.create()
Cnp.ndarray()
Dnp.list_to_array()
✗ Incorrect
The np.array() function converts Python lists into NumPy ndarray objects.
If an ndarray has shape (5, 3), what does this mean?
A5 rows and 3 columns
B5 elements total
C5 columns and 3 rows
D3 elements total
✗ Incorrect
Shape (5, 3) means 5 rows and 3 columns in the array.
Can an ndarray contain elements of different data types?
AYes, it can mix types freely
BNo, all elements must have the same data type
COnly if you use special functions
DOnly for 1D arrays
✗ Incorrect
All elements in an ndarray must be of the same data type for efficiency.
What attribute of an ndarray tells you the number of dimensions it has?
Ashape
Bsize
Cndim
Ddtype
✗ Incorrect
The ndim attribute shows how many dimensions the array has.
Explain what an ndarray is and why it is important in NumPy.
Think about how NumPy handles numbers compared to Python lists.
You got /5 concepts.
Describe how you would create a 3x3 ndarray filled with zeros and check its shape.
NumPy has special functions to create arrays filled with zeros.
You got /4 concepts.
Practice
(1/5)
1.
What is the main purpose of a numpy ndarray?
easy
A. To display images on screen
B. To create text documents
C. To store numbers in one or more dimensions for fast math operations
D. To connect to databases
Solution
Step 1: Understand what ndarray stores
ndarray is designed to hold numbers arranged in rows and columns or more dimensions.
Step 2: Recognize its use for math
It allows fast math operations on many numbers at once, unlike regular Python lists.
Final Answer:
To store numbers in one or more dimensions for fast math operations -> Option C
Quick Check:
ndarray = fast number storage [OK]
Hint: ndarray = fast multi-dimensional number storage [OK]
Common Mistakes:
Thinking ndarray stores text or images
Confusing ndarray with database tools
Assuming ndarray is for display only
2.
Which of the following is the correct way to create a 1D ndarray from a Python list [1, 2, 3]?
import numpy as np
# Which line is correct?
easy
A. arr = np.array([1, 2, 3])
B. arr = np.ndarray([1, 2, 3])
C. arr = np.array(1, 2, 3)
D. arr = np.array{1, 2, 3}
Solution
Step 1: Recall correct syntax for np.array()
np.array() takes a list or nested lists inside parentheses.
Step 2: Check each option
arr = np.array([1, 2, 3]) uses correct syntax with square brackets inside parentheses. Others have wrong function calls or braces.
Final Answer:
arr = np.array([1, 2, 3]) -> Option A
Quick Check:
np.array(list) syntax = correct [OK]
Hint: Use np.array() with list inside parentheses [OK]