1/21
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
NumPy
NumPy (Numerical Python) is a foundational package for numerical computing in Python, providing efficient multidimensional arrays and tools for working with them.
ndarray
An ndarray is a multidimensional array object in NumPy that enables fast array-oriented arithmetic operations and flexible broadcasting capabilities.
Creating a NumPy array from a list
Use np.array(list). For example:
data = [1, 2, 3]
arr = np.array(data)
Shape attribute of an array
The shape attribute is a tuple indicating the size of each dimension of the array. For example, a 2x3 array has shape (2, 3).
Creating an array of zeros
Use np.zeros(shape). For example:
np.zeros((3, 4)) # Creates a 3x4 array of zeros.
Purpose of the dtype attribute
The dtype attribute describes the data type of the elements in the array, such as float64 or int32.
Element-wise arithmetic operations
Use standard operators (+, -, *, /). For example:
arr * 2 # Multiplies each element by 2.
Boolean indexing in NumPy
Boolean indexing allows you to select elements from an array using a boolean condition. For example:
arr[arr > 0] # Selects all positive elements.
Transposing a NumPy array
Use the T attribute or the transpose() method. For example:
arr.T # Transposes the array.
np.where function
np.where(condition, x, y) returns elements from x where condition is True and from y otherwise. For example:
np.where(arr > 0, 2, -2) # Replaces positives with 2 and negatives with -2.
Computing the mean of an array
Use the mean() method or np.mean(). For example:
arr.mean() # Computes the mean of all elements.
Purpose of the cumsum method
cumsum() computes the cumulative sum of elements along an axis. For example:
arr.cumsum() # Returns an array of cumulative sums.
Sorting a NumPy array
Use the sort() method. For example:
arr.sort() # Sorts the array in-place.
np.unique function
np.unique returns the sorted unique values in an array. For example:
np.unique([1, 2, 2, 3]) # Returns [1, 2, 3].
Saving and loading a NumPy array
Use np.save('filename.npy', arr) to save and np.load('filename.npy') to load.
Difference between np.dot and * operator
np.dot performs matrix multiplication, while * performs element-wise multiplication.
Generating random numbers in NumPy
Use functions from np.random, such as np.random.randn() for standard normal samples or np.random.randint() for integers.
Purpose of np.random.seed
np.random.seed sets the random number generator's seed for reproducibility. For example:
np.random.seed(1234) # Ensures the same random numbers are generated each time.
Computing the inverse of a matrix
Use np.linalg.inv(matrix). For example:
inv_mat = np.linalg.inv(mat)
argmax method
argmax returns the indices of the maximum values along an axis. For example:
arr.argmax() # Returns the index of the maximum value.
Computing the QR decomposition
Use np.linalg.qr(matrix). For example:
q, r = np.linalg.qr(mat)
Broadcasting in NumPy
Broadcasting allows NumPy to perform operations on arrays of different shapes by expanding dimensions as needed.