Numpy Flashcards
convert list to numpy array
np.array(list)
arange
np.arange(10,30)
Return evenly spaced values within a given interval.
zeros
np.zeros(count)
ones
np.ones(count)
linspace
np.linspace(0,10,3)
np.linspace(start,stop,count)
Return evenly spaced numbers over a specified interval.
eye
np.eye(4)
Creates an 4x4 identity matrix
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])
rand
np.random.rand(2)
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1) .
np.random,rand(5,5)
creates 5x5 matrix with random float values 0-1
randn
np.random.randn(2) –> 2 values
np.random.randn(5,5)
creates 5x5 matrix with random float values.
Return a sample (or samples) from the “standard normal” distribution. Unlike rand which is uniform:
randint
np.random.randint(1,10)
Return random integers from low (inclusive) to high (exclusive). similar to random module
Reshape
array.reshape(5,5)
Returns an array containing the same data with a new shape.
argmax
arr = np.array([[3, 2, 1], [6, 5, 4], [9, 8, 7]])
np.argmax(arr)
# Find the indices of the maximum element along the flattened array
np.argmax(arr,axis = 0)
# Find the indices of the maximum element along the first axis (rows)
np.argmxax(arr, axis = 1)
# Find the indices of the maximum element along the second axis (columns)
argmax is a function that returns the indices of the maximum values along a given axis.
Shape
Shape is an attribute that arrays have (not a method) returns the shape of an array
dtype
You can also grab the data type of the object in the array:
Fetching the values from numpy array
arr[index]
arr[start : stop]
Broadcasting
arr[0:5]=100