Numpy Flashcards
How do you create a numpy array?
import numpy as np
np.array([1,2,3])
How do you create an array with a range of elements?
np.arrange(4)
How do you create an array with evenly spaced elements
np.arrange(2,9,2)
How do you sort an array?
np.sort(array)
How do you change the shape of an array?
array.reshape(rows,cols)
Can you select a subset of numpy arrays?
Yes; create a boolean and then insert it in the slicer
criteria = y < 3
y[criteria]
How do you create a matrix of zeros?
np.zeros((5,5))
How do you create a matrix of ones?
np.ones((2,10))
Does np.zeros(()) create integers or floats?
Floats
How do you create random numbers?J
np.random.randint(0,100)
How do you create random numbers in a matrix?
np.random.randint(0,100,(5,5))
How do you get numbers that are spaced evenly?
np.linspace(0,10,101)
What does np.linspace(0,10,101) mean?
101 numbers between 0 to 10 that are equally spaced
How do you get consistent random numbers?
np.random.seed(x)
Does seed persistent beyond the next row?
No
How do you get the maximum value in an array?
arrayname.max()
How do you find out where in an array the max value is?
arrayname.argmax()
How do you change the shape of an array?
arrayname.reshape(2,5)
How do you slice on rows / columns?
arrayname[5,2]
How does numpy format arrays?
makes each number the same size as the biggest number by adding spaces
What is faster, numpy or lists?
Numpy
How do you access the dimensions of an array?
name. ndim
How do you access the number of rows and columns in an array
name.shape
What does shape give you for a 1 dimensional array?
Gives the number of elements
What are .dtype and .shape
Attributes - has to be on an objective (i.e. they are not functions)
How do you get the number of elements in a 2 dimensional array?
name.size
How do you remove dimensionality from an array?
name.flat iterate over
How do you create an array?
np.array([]) - must be a list of lists if it is multidimensional