Skip to content

01

Import

import numpy as np

Basics

np.array([1, 2, 3, 4, 5])
np.arange(1, 100, 10) ## start, step, step
np.linspace(1, 100, 10) ## start, step, no of values

## idk
np.zeros(10)
np.ones(10)

## random
np.random.random(10)
np.random.randn(10)

Array Operations

## Element-wise
a+3
1/a

## Boolean
a > 4

Indexing

a[2]

a[2:]
a[-10:]

a[:10]
a[:-10]

a[::2] ## even rows
a[1::2] ## odd rows


## Masking
a[a > 4]

np.vectorize

Kinda like a for loop

names = ["Thahir", "Azhar"]
first_letter = np.vectorize(lambda x: x[0])(names) 

Stats

np.mean(a)
np.median(a)
np.std(a)
np.quantile(a, 0.90)
np.percenile(a, 90)

Calculus

## analytic calculus (for symbolic, use sympy)
dydx = np.gradient(y, x )
y_int = np.cumsum(y) * (x[1]-x[0])

Multi-Dimensional

a = np.array([
  [1, 2, 3],
  [4, 5, 6]
])
a = np.random.randn(3, 3)

a.ravel() ## returns a 1d array

a[0] ## first row
a[:,0] ## first column

Mesh Grid

xv, yv = np.meshgrid(x, y)
zv = xv**2 + yv**2
plt.contourf(xv, yv, zv, levels=100)
plt.colorbar()

Linear Algebra

Matrix

  a.T
  a*b ## element-wise operator
  a@b ## matrix multiplication
  a.dot(b)
  a.cross(b)

Solve systems of equations

  a = np.array([
    [3, 2, 1],
    [5, -5, 4],
    [6, 0, 1]
  ])
  b = np.array([
    4,
    3,
    0
  ])

  x = np.linalg.solve(a, b) ## ax = b

Eigenvalues

  temp = np.linalg.eig(A)
  eigen_values = temp[0]
  eigen_vector = temp[1][:, 0]

Find-Replace

if

  prediction['Rating'] = np.where(
    prediction['Rating'].to_numpy() > 100,
    100,
    prediction['Rating'].to_numpy()
  )

if-else

  prediction['Rating'] = np.where(
    prediction['Rating'].to_numpy() > 100,
    100,
    0
  )

if-elseif-else

  conditions = [
    prediction['Rating'].to_numpy() > 100,
    prediction['Rating'].to_numpy() > 50,
    prediction['Rating'].to_numpy() > 20
  ]

  values = [
    100,
    50,
    20  
  ]

  default = 0

  prediction['Rating'] = np.select(
    conditions,
    values,
    default = default
  )

nested

  conditions = [
    (prediction['Rating'].to_numpy() > 100 & prediction['Rating'].to_numpy() % 2 == 0),
    (prediction['Rating'].to_numpy() > 100 & prediction['Rating'].to_numpy() % 3 == 0),
    (prediction['Rating'].to_numpy() > 100 & prediction['Rating'].to_numpy() % 4 == 0),

    prediction['Rating'].to_numpy() > 50,
    prediction['Rating'].to_numpy() > 20
  ]

  values = [
    102,
    103,
    104,

    50,
    20  
  ]

  default = 0

  prediction['Rating'] = np.select(
    conditions,
    values,
    default = default
  )

Rounding

Round to Integer

  np.around(prediction)

  ## instead of
  ## prediction = ( round(element) for element in prediction )

Round to \(n\) places

  np.around(prediction, n)

Read data

data = np.loadtxt(
  "./data.csv",
  dtype = "object",
  delimiter = ",",
  unpack = True,
  skiprows = 1 
)

Save

np.savetxt(
    filename + ".csv",
  data,
  delimiter = ",",
  fmt = "%d",
  header = "Col1, Col2"
)
Last Updated: 2024-05-14 ; Contributors: AhmedThahir

Comments