Numpy (Wk 3 UCSD / DataCamp) Flashcards
How does a (numpy) array differ from a list?
You can perform mathematical operations element wise on the array (vector arithmetic)
How do you create a numpy array?
np_array = np.array(list) OR np.array([x, y, z])
What happens if you divide 2 np arrays (of the same size)
the output is the an array of the same size, w/ each element the respective quotients
What happens if you create a numpy array of multiple types?
it will convert all to one type (aka type coercion)
what’s the output of np_array > X
Array of Booleans element wise [True, False, etc]
What’s the output of np_array[ np_array > X]
Returns element wise values that meet the condition (product w/ Boolean result)
When do you use parentheses with an array?
When you’re instantiating only (eg, not when you’re setting or sub-setting
How do you create a multi-dimensional np array?
np_array = np.array(list1, list2) OR = np.array( [ [1,2,3], [4,5,6] ] )
How do you get the number of rows and columns of an array?
np_array.shape (attribute of the array)
How do return 2nd element from 3rd row of a 2d np array?
np_array[2,1] OR np_array[2][1]
How do you sub-set multi-dimensional np arrays?
np_array[ x:y, k:j ]
using subsetting, return the entire second row of a np array
np_array[ 1, : ]
How does arithmetic work with multi-dim arrays?
can do all operations. Array X Array, or Array X Vector(s)
Get the mean of the first column of data in a 2D np array
np.mean(np_array[ : , 0]
summary stats functions np arrays
np.mean(), np.median(), np.std()
check the corrleation between column 1 and column 2 in a 2D array
np.corrcoef( np_array[ : , 0] , np_array[ : , 1] )
How to generate an array with normally distributed random numbers
np.random.normal(mean, std, num of values to generate)
how to merge two 1D arrays into a single 2D array
np_2d = np.column_stack(( np1, np2))
create arrays full of 0s, 1s, diagonal 1
np.zeros, np.ones, np,eye
create 2d array with all value 9
np_array = np.full((2,2), 9.0)
create a 2d array with random floats between 0 and 1
np_array = np.random.random((2,2))
What does a sub-set of an array like a_slice = np_array[1:3 , 1:3 ]
a 2x2 array that you can reference with the new indices (eg 0,0; 0,1; etc), but points to the same data as the original array
how do you make a new data structure using a slice
new_slice = np.array(np_array[1:2, 2:3]
explicitly declare the data type in an array
x = np.array(list1, dtype=float64)»_space; forces to the dtype, can use various types
get the mean by row, or by col, of an array
arr.mean(axis=1)»_space; row; arr.mean(axis=2)»_space; column
print just the unique values of an array
print(np.unique(np_array))
what happens if you add a 1,3 array to a 3,3 array
broadcasting - eg, python adds the 1,3 row to each row (will works same column wise)