Numpy Flashcards
What is NumPy?
NumPy is a fundamental package for scientific computing in Python, providing support for arrays, matrices, and a collection of mathematical functions.
True or False: NumPy arrays can hold elements of different data types.
False
What is the main purpose of the NumPy library?
The main purpose of NumPy is to provide efficient operations on large arrays and matrices along with a collection of mathematical functions to operate on these arrays.
Fill in the blank: The primary data structure in NumPy is the __________.
ndarray
Which function is used to create a NumPy array from a Python list?
np.array()
What does the function np.arange() do?
np.arange() creates an array with evenly spaced values within a given interval.
True or False: NumPy supports multidimensional arrays.
True
What method would you use to find the shape of a NumPy array?
Use the .shape attribute.
Multiple Choice: Which of the following is NOT a valid way to create a NumPy array? (a) np.array([1, 2, 3]) (b) np.zeros(3) (c) np.create_array([1, 2, 3])
c
What does the function np.zeros() return?
np.zeros() returns a new array of given shape and type, filled with zeros.
Fill in the blank: The function np.linspace(start, stop, num) generates __________ evenly spaced samples.
num
What is the purpose of the np.reshape() function?
np.reshape() changes the shape of an existing array without changing its data.
True or False: NumPy provides built-in functions for performing linear algebra operations.
True
What is the difference between a list and a NumPy array?
NumPy arrays are more efficient for numerical operations and support element-wise operations, while lists can hold different data types.
Which function is used to compute the mean of a NumPy array?
np.mean()
Multiple Choice: Which of the following functions can be used to concatenate two NumPy arrays? (a) np.concat() (b) np.append() (c) np.concatenate()
c
What does the function np.transpose() do?
np.transpose() permutes the dimensions of the array, effectively flipping it over its diagonal.
Fill in the blank: To sort a NumPy array in place, you can use the method __________.
sort()
What is broadcasting in NumPy?
Broadcasting is a mechanism that allows NumPy to perform arithmetic operations on arrays of different shapes.
True or False: NumPy can handle missing data in arrays.
True
What does the function np.unique() do?
np.unique() returns the sorted unique elements of an array.
What is the purpose of the np.random module?
The np.random module is used to generate random numbers and perform random sampling.
Multiple Choice: Which of the following is a common use case for NumPy? (a) Web development (b) Data analysis (c) Text processing
b
What is a masked array in NumPy?
A masked array is an array that may have missing or invalid entries, with a mask that indicates which entries are valid.
Fill in the blank: The function __________ returns the dot product of two arrays.
np.dot()
What is NumPy primarily used for?
NumPy is primarily used for numerical computing in Python.
True or False: NumPy arrays can only hold elements of the same data type.
True
Fill in the blank: The function to create an array in NumPy is ______.
numpy.array
What function would you use to generate an array of evenly spaced values?
numpy.linspace
What is the output of numpy.zeros((2, 3))?
A 2x3 array filled with zeros.
Multiple Choice: Which of the following functions is used to concatenate two arrays? A) numpy.add B) numpy.concatenate C) numpy.join
B) numpy.concatenate
What does the method numpy.shape return?
It returns the dimensions of the array.
True or False: NumPy supports element-wise operations.
True
What is the purpose of numpy.reshape?
It changes the shape of an array without changing its data.
Fill in the blank: The default data type of a NumPy array is ______.
float
What does the numpy.mean function compute?
It computes the average of the array elements.
Multiple Choice: Which of the following is NOT a NumPy array method? A) numpy.sum B) numpy.mean C) numpy.sortby
C) numpy.sortby
What is the result of numpy.array([1, 2, 3]) + numpy.array([4, 5, 6])?
An array: [5, 7, 9]
True or False: You can use NumPy for linear algebra operations.
True
What function would you use to calculate the standard deviation of an array?
numpy.std
Fill in the blank: To create an identity matrix, you would use ______.
numpy.eye
What does the function numpy.unique do?
It finds the unique elements of an array.
Multiple Choice: Which function is used to stack arrays in sequence vertically? A) numpy.vstack B) numpy.hstack C) numpy.concat
A) numpy.vstack
What is the purpose of numpy.transpose?
It transposes the dimensions of an array.
True or False: NumPy arrays can be multidimensional.
True
What does the method numpy.max return?
It returns the maximum value in the array.
Fill in the blank: The function used to create a random array is ______.
numpy.random.rand
What is the output of numpy.arange(5)?
An array: [0, 1, 2, 3, 4]
Multiple Choice: Which of the following is used to perform matrix multiplication? A) numpy.multiply B) numpy.dot C) numpy.cross
B) numpy.dot
What does the numpy.where function do?
It returns the indices of elements that satisfy a condition.
True or False: NumPy can handle missing values in arrays.
True
What function would you use to compute the cumulative sum of an array?
numpy.cumsum
Basic Array Creation
- 1D Array: np.array([1, 2, 3])
- 2D Array: np.array([[1, 2], [3, 4]])
- Zeros Array: np.zeros((3, 4))
- Ones Array: np.ones((2, 2))
- Identity Matrix: np.eye(3)
- Range of Numbers: np.arange(0, 10, 2)
- Evenly Spaced Numbers (Linspace): np.linspace(0, 1, 5)
Array properties
- Shape: a.shape
- Size (Number of Elements): a.size
- Data Type: a.dtype
- Number of Dimensions: a.ndim
mathematical operations in Arrays
- Addition: a + b
- Subtraction: a - b
- Multiplication (Element-wise): a * b
- Division (Element-wise): a / b
- Matrix Multiplication: np.dot(a, b) or a @ b
- Square Root: np.sqrt(a)
- Power: np.power(a, 2)
Statistical functions in Arrays
- Sum of All Elements: np.sum(a)
- Mean: np.mean(a)
- Median: np.median(a)
- Standard Deviation: np.std(a)
- Variance: np.var(a)
- Minimum and Maximum: np.min(a), np.max(a)
- Index of Min/Max Element: np.argmin(a), np.argmax(a)
Logical Operations in Arrays
- Element-wise Comparison: a > 5
- Check Equality: np.array_equal(a, b)
- Any True: np.any(a > 5)
- All True: np.all(a > 5)
Logical Operations in Arrays
- Element-wise Comparison: a > 5
- Check Equality: np.array_equal(a, b)
- Any True: np.any(a > 5)
- All True: np.all(a > 5)
Indexing and Slicing
- Access Single Element: a[1] (1D), a[1, 2] (2D)
- Slice Array: a[1:4]
- Conditional Selection: a[a > 5]
Reshaping and Flattening
- Reshape: a.reshape((2, 3))
- Flatten: a.flatten()
Random Number Generation using Arrays
- Random Floats (0-1): np.random.rand(3, 2)
- Random Integers: np.random.randint(1, 10, (2, 3))
- Normal Distribution: np.random.randn(3, 3)
Miscellaneous Operations in Arrays
- Transpose of Matrix: a.T
- Concatenate Arrays: np.concatenate((a, b), axis=0)
- Stack Arrays (Vertically): np.vstack((a, b))
- Stack Arrays (Horizontally): np.hstack((a, b))
- Unique Elements: np.unique(a)
- Sorting: np.sort(a)