numpy - mrprov12/DSPrep GitHub Wiki

Numpy

numpy arrays

CAN ONLY INCLUDE A SINGLE DATA TYPE
when + doesnt concatenate, adds each instance to its respecive pair
ie. [1,2,3] + [1,2,3] = [2,4,6]
np.array()
ie. np_height_in = np.array(height_in)
can multiply, etc each entry by an operator
ie. np_height_m = np.array(height_in) * 0.0254
bmi = np_weight_kg/(np_height_m**2)
can create array of bools and subset it within another array, and itll only return the values that match the True values
ie. light = np.array(bmi<21). #creates bool array of true false for those that are <21
print(bmi[light]) #subsets in bmi array to only return bmis that fit the True values
can also subset like normal lists
** subsetting ([xyz])
ie. array([100:111]) returns enteries from index 100 to 110 inclusive

2D numpy arrays

.shape => returns (rows, cols); not a method, is an attribute
array[0][2] == array[0,2]
calling just cols or rows is easy -> array[r:c] with slicing telling python to include all
ie. np_baseball[:,1] select just the 2nd column
print(np_baseball[123,0]) print the 124th player's height
Can create an array of conversions by row/col and multiply
ie. array with 3 cols, new array with conversion factors in 3 cols, multiply to convert whole array

basic statistics

np.mean(array[:,0])
np.median(array[:])
np.corrcoef(array1, array2). see if 2 array rows/cols are correlaed
np.std(array) std dev
np.sum()
np.sort()
Very fast calculations because all 1 data type

⚠️ **GitHub.com Fallback** ⚠️