Numpy

// Numerical python

Import

import numpy as np

Create Array and Reshape

np.arange(0,10,1) # 0 to 9, steps of 1, returns np.array

np.zeros(5) # returns array of 5 0s
np.ones(5) # returns array of 5 1s
np.zeros((2,4)) # 2 rows by 5 cols

np.linspace(0,10,3) # returns [0, 5, 10]
np.linspace(0,3,4) # [0, 1, 2, 3]

np.eye(5) # creates a identity matrix, baso a bunch of 0s with horizontal 1s

###

np.random.rand(rows, cols) # picks a number between 0-0.9999

np.random.randn() # 0 highest likerly hood, negative numbers are possible

np.random.randint(0, 101, (rows, cols)) # 0 to 100, returns in array

np.random.seed(42) 
np.random.rand(4) # this will return the same set of numbers

###

arr.reshape(cols, rows) # changes the shape of an array into a new shape

arr.shape() # returns tuple of array shape

arr.max()
arr.min()

arr.argmax() # index of max number

arr.dtype() # data type of an array

Indexing and Selection

arr[8] # index 8

arr[1:5] # index 1 to index 5

arr[:5] # 0 to index 5

###

arr[0:5] = 100

partOfArray = arr[0:5] # partOfArray is a list of pointers

arrCopy = arr.copy() # does not use pointers

###

arr2d[0] # selects row at index 0
arr2d[row][col] or arr2d[row, col] # select specific element
arr2d[0:1, 1:] # select row 0 and 1, col 1 to end

###

arr > 4 # returns boolean array with true or false for each element

arr[arr > 4] # used to filter array (only shows values that are true)

Operations

arr + 5 # adds 5 to each element

arr + arr # matrix addition


np.sqrt(arr)
np.sin(arr)
np.cos(arr)
np.tan(arr)
# there is alot so not gonna list them all out

arr.sum(axis=0) # 0 sum down each col, 1 sum rows across each row
arr.mean()
arr.var()
arr.std()