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
Do you feed 5,2 or (5,2) to np.ones()?
A tuple i.e. np.ones((5,2))
Can you get integers instead of floats in np.ones()?
Yes np.ones((5,2), dtype = int)
How do you get a array of numbers of any number?
np.full((3,5),13)
How do you create an arrange of items 0 to 4?
np.arange(5)
How many elements does lin space take and what are they?
start, end and num =
np.linspace(0,1,num = 5)
How do you specify the shape of an array using a range?
np.arange(0,21).reshape(4,5)
Is reshape in place?
No
If you do operators on a numpy array - is it in place?
No
Do you need to iterate over items to do things to an array?
No anarray * 2 works
Can you do augmented assignments in numpy?
Yes numbers += 10 adds 10 to every element
Can you multiply arrays together?
Yes
What happens if you multiply arrays together that are not the same shape?
Error if you use *. You can use * or np.multipy(array1, array2) if the arrays are different sizes but only if one is the same length as a row.
Can you do comparisons on numpy arrays?
Yes numbers >= 13 will produce an array of booleans
How do you see what methods are available for the array type?
arrayname. then press tab
What are some of the methods available for arrays?
sum() min() max() mean()
How do you calculate the mean of a column in an array?
arrayname.mean(axis = 0)
Axis = 0 is column or row?
column
How do you access universal functions eg. sqrt?
Using the module name so if you import numpy as np then np.sqrt()
what is the main difference between indexing lists and numpy in multidimensions?
list[1][2]
array [1,2]
How do you select not continuous rows / columns in an array?
myarray[[1,3]]
What is a shallow copy?
Where you create a new object in memory but it points to the original object where the data is actually sorted?
How do you create a shallow copy of an array?
myarray.view()
If you change the original object, does the new object change?
Yes as it’s pointing to the original still
If you take a slice of an array - is it a view or is it a completely new object?
It is a view, so if you change the original object it will change
How do you create a deep copy (i.e. new object) of an array?
x = myarray.copy()
Reshape vs resize, which is a view and which modifies the original array?
Reshape is a view
Is resize a view?
No
How many dimensions does this array have? np.array[[1,2,3,4]]
2 - double square brackets
How do you reduce dimensions of an array?
myarray.flatten()
Flatten or ravel, which is a view?
Ravel is a view
Can you filter an array?
Yes just like a dataframe arr[array % 2 != 0]
How can you determine if an array contains an element?
np.any(arrayname)
How do you reverse an array?
np.flip(arrayname, axis = )
How do you find elements that are non zero?
np.nonzero(arrayname)
How do you create an identity matrix?
np.identity(3)
How do you create a specific set of values on a diagonal?
np.diag([1,2,3],k = 0) k means the diagonal - 0 is the main - 1 is below
How do you get the coordinates from a flat index?
np.unravel_index(indexyouwant, [dimensions of array])
How do you do matrix multiplication
np.dot()
np.nonzero(arrayname) or arrayname.nonzero()
np.nonzero(arrayname)
What does np.where() do?
Evaluates if a condition is true, and if it is does something (or not)
What arguments does np.where() take?
np.where(x <4 , x, 5)
What is the output of np.where()
Typically an array of number unless you tell it booleaons
How can you test AND conditions in numpy?
np.logical_and(test1,test2)
np.where() or arrayname.where()
np.where()