Member-only story

Cheatsheet for machine learning coding

Wencao Yang
1 min readNov 6, 2021

--

import numpy as np# linear 
np.arange(1,100,1) # stepsize
np.linsapce(1,100,100) # number
np.random.shuffle(a) #in-place
np.percentile(a,q)
# random
np.random.rand(d0,d1,…dn) # only return [0,1)
np.random.randint(low, high, size) # int
random.random() # (0,1)
random.randint(start,stop + 1)
# random distribution
np.random.choice(data, size = ,replace=True )
np.random.uniform(0,1)
np.random.normal(loc = ,scale = )
np.random.exponential(scale=) # scale = 1/lambda np.random.binomial(n, p, 1000)
# selection
x = x[x%2 ==1] # select odd number
x = x[::2] # select odd index
x = np.sqrt(x) # array is elementwise
x = x[x > 1] # keep x > 1
# inverse-cdf
scipy.stats.norm.ppf(q) # inverse-cdf,q is quantile scipy.stats.expon.ppf(q,scale=)
scipy.stats.binom.ppf(q,n,p)
scipy.stats.poisson.ppf(q,mu) # mu = lambda
# cdf
scipy.stats.norm.cdf(x)
scipy.special.erf(x) # cdf of normal
# test
stat, p = scipy.stats.ttest_ind(data1,data2) # t test
stat, p = scipy.stats.ttest_rel(data1,data2) # t test,paired
stat, p = scipy.stats.shapiro(data) # nomality
stat, p = scipy.stats.pearsonr(data1,data2) #correlation
stat, p = scipy.stats.f_oneway(data1,data2,data3) #anova
p = scipy.stats.binom_test(x,n,p)
# normalization
a = a/a.sum(axis = 0) # good for numpy matrix and pandas df
# optimize
scipy.optimize.minimize(func, param)…

--

--

Wencao Yang
Wencao Yang

Written by Wencao Yang

Data Scientist & Physics PhD

No responses yet