#conda install numpy
#pip install numpy
import numpy as np
A homogeneous container of numerical element of a single type.
Arrays can have 1 dimension (similar to python lists) or 2 dimensions (similar to python matrix).
Axes are defined for arrays with more than one dimension: the first running vertically downwards across rows (axis 0), and the second running horizontally across columns (axis 1).
#From a Python list
my_list = [1,2,3]
my_list
arr = np.array(my_list)
arr
#From a Python matrix
my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
my_matrix
mat = np.array(my_matrix)
mat
np.array(object [, dtype=None, copy=True, order='K', subok=False, ndmin=0])
#create an array 1 dimension
arr = np.array([2,4,6])
arr
#create an array 2 dimensions (matrix)
mat = np.array([[2,4,6], [8,10,12], [14,16,18]])
mat
#generates arrays of zeros
arr0 = np.zeros(3)
mat0 = np.zeros((5,5))
mat0
#generates arrays of ones
arr1 = np.ones(3)
mat1 = np.ones((3,3))
mat1
np.arange(start, stop, [step, ]dtype=None)
#create sequences of numbers
arr = np.arange(0,10, 2, int ) #np.arange([start,] stop [, step, dtype])
arr
arr = np.arange( 0, 2, 0.3 ) #accepts float
arr
arr = np.arange(0,12).reshape(3,4) #can be reshaped in rows and columns
arr
np.linspace(start, stop, num=50[, endpoint=True, retstep=False, dtype=None, axis=0])
#Return evenly spaced numbers over a specified interval
np.linspace(0,10,21)
#creates an identity matrix
np.eye(4)
#Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1].
np.random.rand(2)
np.random.rand(5,5)
#Return a sample (or samples) from the "standard normal" distribution. Unlike rand which is uniform:
np.random.randn(2)
np.random.randn(5,5)
#Return random integers from `low` (inclusive) to `high` (exclusive).
np.random.randint(1,100)
np.random.randint(1,100,10)
arr = np.arange(25)
arr #show
ranarr = np.random.randint(0,50,10)
ranarr #show
ranarr.reshape(5,2) #modify the structure of the array
ranarr.max() #max value
ranarr.argmax() #max value position
ranarr.min()#min value
ranarr.argmin() #min value position
ranarr.ndim #number of axes(dimensions)
ranarr.size #number of elements
ranarr.shape #dimensions
ranarr.dtype #data type
ranarr.itemsize #size in bytes
ranarr.reshape(5,2).shape
#Get a value at an index
arr[8]
#Get values in a range (start included, end excluded)
arr[1:5]
#Copies and clones
arr1 = arr
arr2 = arr.copy
arr is arr1 #true, same object
arr is arr2 #false, different object
#Creating 2D array
arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))
arr_2d
#Indexing rows
arr_2d[1] #one row
arr_2d[[0,2]] #list of rows
#Getting individual element value
arr_2d[1][0]
#Getting multiple element values
arr_2d[:2,1:] #first row/s, second column/s
#Filter
arr = np.arange(1,11)
arr > 4 #filter condition
bool_arr = arr>4 #boolean of selection
arr[bool_arr] #filter
arr[arr>4] #direct filter
x = 4 #filter condition
arr[arr>x] #filter using condition
arr = np.arange(1,13).reshape(3,4)
print(arr)
arr + arr #sum
arr - arr #subtraction
arr * arr #product
arr/arr #division
1/arr #normalization
arr**2 #power
np.sqrt(arr) #square root
np.exp(arr) #exponent
np.sin(arr) #sin
np.log(arr) #logaritm
np.sum(arr) #total sum
np.sum(arr, axis = 0) #column sum
np.sum(arr, axis = 1) #row sum
np.cumsum(arr, axis = 0) #cumulative column sum
np.cumsum(arr, axis = 1) #cumulative row sum
np.diff(arr) #diference between a number and the following one
np.prod(arr) #total product
np.cumprod(arr) #acumulated product
np.max(arr) #maximum
np.min(arr) #minimum
np.average(arr) #average
np.mean(arr) #mean
np.median(arr) #median
np.var(arr) #variance
np.std(arr) #standard deviation
np.bincount(arr[0]) #values occurency
np.corrcoef(arr) #Pearson relation between vectprs
np.cov(arr) #covariance matrix
arrdem = np.arange(0,8,0.23)
arrdem
np.rint(arrdem) #to the nearest integer
np.round(arrdem, 1) #to the nearest number for the precision defined (decimals)
np.ceil(arrdem) #to te nearest upper integer
np.floor(arrdem) #to te nearest lower integer
np.trunc(arrdem) #truncates to integer
np.clip(arrdem, 2, 6) #clips lower and upper values according to the boundaries
np.sort(arr) #sort values ascending
-np.sort(-arr) #sort values descending
np.ravel(arr) #flattens the values to a single line
arr.T #transposes the matrix
arr.resize(4,3) #modifies arrays structure
arr
np.vstack((arr, arr)) #joins vertically (rows)
np.hstack((arr,arr)) #joins horizontally (columns)
np.split(arr, 2) #splits in equal parts the rows
np.hsplit(arr, 3) #splits in equal parts the columns
arr[0:3,0] = 100 #updates values
arr
slice_of_arr = arr[:2, :3] #cuts a portion of values
slice_of_arr
arr[arr > 6] = 20 #filter to update
arr