NumPy Flashcards
Numpy practice
How does a NumPy array display when it is printed?
[‘val1’ ‘val2’ ‘val3’]
strings with delimiters but no commas between.
What does a NumPy matrix look like when printed?
[[1 2 3]
[4 5 6]
[7 8 9]]
Stacked list of lists, no commas between the sublists.
What is the linspace calculation?
np.linspace(x1, x2, n)
(x2-x1)/(n-1)
i.e.
np.linspace(10, 20, 3)
(20-10)/(3-1)
10/2 = 5
print(np.linspace(10,20,3)) = [10. 15. 20.]
note: the output is decimals
Describe the np.arange(x, y, z) function.
arange or a range function creates a range where x is the starting value, y-1 is the ending value and z is the step.
Can you use the reshape(x,y) module on an array with an odd number of values?
No, it will throw an error.
What is the default base for the NumPy log function?
e
What does np.eye(x) do?
It creates an identity matrix with x by x rows and columns.
An identity matrix is a matrix where all values are 0 except for 1s running through the true diagonal.
Give an example of calling from a numpy array based on a conditional operation
var = np.array(1, 2, 4, 8, 9)
print(var[var>4])
[8 9]
Describe how to format a numpy array.
Take the numpy object, usually imported as np with the .array().
Within the (), put a list, with [] and commas between the values of the list.
Describe the format for splitting a numpy matrix.
The matrix variable will have [] behind it and those [] will hold the x and y range. The first value in the [] will be the x range, with similar principles of splitting lists in Python, i.e. x1:x2-1. The second value after a comma will be similar but for columns.
What are two numpy save formats?
.npy and .npz
in .npz, you can use the np.savez() command to save several arrays in a single uncompressed file.
Give an example of how to save a numpy array/matrix to a .npy file.
np.save(‘path’, matrix)