Python NumPy2 Create, Restructure, Arrays Flashcards
How can you easily create a vector of zeroes?
Produce a vector of 3 zeros like this:
np.zeros(3)
How would you create a 2X3 matrix of zeros?
np.zeros((2,3))
Notice the second pair of parentheses
i.e. the argument is a tuple
OK, how to create a vector of four 1’s?
np.ones(4)
Notice there is no half-open interval business going on here. You are directly specifying how many elements you want.
And what about matrix of 1’s. Do a 4 X3
np.ones((4,3))
How would you create 11 equally spaced points between 0 and 5?
np.linspace(0,5,11)
You know the endpoints and number of points, it calculates the interval for you. Contrast with arange, which given the endpoints and interval, it deduces the number of points. «np.linspace(0,5,11) and np.arange(0,5,.5) return the same thing»
How would you create a 4X4 identity matrix?
Using the “eye” function
np.eye(4)
How do you reshape a vector or dimension?
Using the reshape method. For example, np.eye(4).reshape((2,8)), also see image
How do you get the minimum, maximum, and standard deviation of an array?
Never mind finding the maximum value of an array; how would you find the position of the maximum value of an array?
use the argmax function.
How can you query the shape or data type of an array?
Use the attributes shape and dtype. Note they do not have parentheses.