0 ratings0% found this document useful (0 votes) 126 views11 pagesMachine Learning Algorithms (Python & R) PDF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
CHEATSHEET
Machine Learning
@ Algorithms @
( Python and R Codes)
Types
Supervised Learning Unsupervised Learning Reinforcement Learning
-Decision Tree -Random Forest -Apriori algorithm - k-means Markov Decision Process
~KNN Logistic Regression - Hierarchical Clustering ~ Q LearningEle
=
2
A
ra
ry
‘4
>
Cy
[4
Python
Code
taport Library
taport other necessary Libraries Like pandas,
run
fron sklearn Inport Linear_nodel
Load Train and Test datasets
Tdentity feature and response variable(s) and
values must be numeric and numpy arrays
x traininput_variables_values training datasets
y-train-target variables values training datasets
x test=Lnput variables values test datasets
create Linear regression object
Linear = Linear_wodel. LinearRegression()
Train the model using the training sets and
check score
Linear-Fit(x train, y_tratn)
Linear-score(x_train, y_train)
equation coefficient and Intercept
print(*Coefficlent: \a", Linear.coef_)
print(*Iatercept: \n', Linear. Intercept_)
Predict output
predicted= Linear.predict(x test)
‘load Train and Test datasets
stdentify feature and response varlable(s) and
values must be muvee and nunpy arrays
a tealn <- input _vartables_values_training datasets
yiteain < tanget_vartables_values_tratning datasets
test c input variables values test datasets
cbind(x_train,y. train)
‘train the model using the training sets and
check score
Linear <= Im(y_tratn ~ .,
stumnary(Qinear)
predict output
pradicted= predict(inear,x test)
data = 0)Python
Code
‘iport Library
‘Assured you have, X (predictor) and ¥ (target)
‘for training data set and x test(orecietor)
oF test_sotaset
(4
rotel FLt(K, )
wotel.score(k, 9)
‘tention coetfictent ané Intercept
print(*coctticiant: \n', model.coof_)
print(Entercept: \n", nedel intercept)
sPredict Output
pradictede model.predict(x test)
rr
i=
s
cae
ro
2
uy
oy
‘rm sklearn.Linear_wodel inport Logistictegression
© cbind(x trsin,y_train)
‘rain the model using the training sets and check
logistic <- gln(y-train = ., data = x,fanily='binoniat)
surmary (logistic)
predictedn prodict( logistic x test)act}
=
=
2
ete
Python
Code
‘more Library
import other necessary Libraries Like pandas, nunpy.
from sklearn Snport tree
‘ssuned you have, X (predictor) and ¥ (tareet) for
training data set and x test (predictor) of
Acoeate tree object
wodel = tree.Decistontresclassifter(criterione'gint')
‘fon clasesfication, here you can change the
algorithm a5 gint or entropy (information gain) by
‘fault St 45 aint
rodel = tree.Qectstontredhagrestor() for
rain the model using the training Sets and check
model. fitix, y)
wodel.score(k, 1)
predicted nodel.predict(x test)
brary(rpart)
X& ebind(x train, train)
erow tree
41 & rpare(y_train ~ ., data = xnethode"class")
summary Fit)
trredict output
redict( Fie x test)s
2
=
5
A
=
5
EF
s
=
5
A
Ey
Ei
3B
=
EA
Python
Code
‘ingore Library
from skleaen snport sym
‘Asse you have, X (predictor) and Y (target) for
‘training data set and x test(predictor) of test dataset
screste Su classification object
rogel ~ syn.sve()
fthere are various options associates
vith St, this 15 sinple for classification,
‘train the nodel using the training sets and check
rodel.Fit(k ¥)
rodel.score(s, ¥)
Predict output
predieted- nogel.onedict(x test)
‘apart Library
Lbrary(e1971)
x < cbind(x train,y train)
‘aitting model
Fit cosum(y_train ~ ., data =x)
suamary Fit)
‘Arecict Output
predicteds predict(Fit,x testPETC ty
Python
Code
sapere Library
‘nssunes you have, X (predictor) and ¥ (target) for
‘training data set and x test(predictor) of test dataset
jn object node = Gaussiam()
‘here 45 otter distribution for multinomial classes
Like Bernoulls Naive Bayes
‘ain the model using the training sets and check
scoeate SiN classifi
rodel f18(K, y)
predictede nodel.predict(x test)
ampore Library
brary e1e71)
x © cbind(x train,y train)
srieting nose
it Cnatvesayes(y train ~ ., data =x)
summary (#42)
sPredict Output
predicted= pradict(fit,x test)Python
Code
fron slearn.neiehbors snport KtelghborsClassifier ibrary(knn)
XG ehind(x_tratn,y tain)
FE Chan(y train ~ ., date =
sunnary( fit)
weredice output
predictede prediet(fit,x test)
‘issuned you have, X (predictor) and ¥ (target) For
raining data set and x tost(predicto) of test_cataset
ereate Kueighbors cassi¢ier object model
el groorsCiassifier(anedghtarse6)
roael F880, 9)
‘sPredict tut
predicted nadel.pradict(x test)
A
3
k=
c=
3
2
4
5
Fy
Fs
=
7
2
=Python
Code
suport Laseary
‘rom sklearncluster taport means
assumed you have, X (attributes) for training cata
‘and x test(attriutes) of test_cataset
create meigneors classifler object node
eons = means(n_cius
‘Train te model using the training sets and check score
ode £20(0)
predictede noel. greater x test)
nbrary(cluster)
Fat < bneans(t, 3)EUs
Python
Code
tngart Lieacy
kssuned you have, X (predictor) and ¥ (target) for
‘trsining data set and x test (predictor) of test
nodel= Randotorestclassifier()
‘rein the node using the tring sets and check score
motel #5806, 9)
recictede model. predict (x test)
typort Uibeary
Library (randonForest)
x <= ebind(x train,y_tratn)
Fitting model
#18 < randonForest(spectes ~ ., xpntree=50)
sunmary #20)
eredict ovtput
predietede predict (#it,x test)2
Es
i
2
=
Python
Code
Create PCA object peox deconpositionPCAa component)
defoutt value of K =aln(n sarge, m Fetures)
for Factor analysis
foe decopasition Factenaaiysis()
‘Reduced the osnension of training dataset using PCL
‘train seduced = pea Fit_transforn(train)
est_reduced » pea.transfor(test)
sport brary
brary(stats)
pee & princonp(train, cor = TRUE)
train reduced < predict(pca, train)
test reduced < predict(pca,test)EI
ks
2
Py
r
s
Fy
Ee
Python
Code
taport Library
‘ron sklear.ensenble Snport GradientoostingClassifier
ftssuned you have, X (predictor) and ¥ (target) for
training data set ard x test(predictor) of test_dataset
create Gradient Boosting Classifier object
GradientBoostingClassifier(n_estinators-100, \
Learning ratert.0, max depthea, random stat
train the noel using the training sets and check score
mocel #100, 9)
roredict Ovtout
ede -precice(x test)
amport Library
brary caret)
2 cbind(x troinyy_train)
sriccing model
ixcontrol © tratnControl( wethed = “repeatedcv",
+ nunber = 4, repeats = 4)
#5 tratngy = 5 date
predietede predict(Fit,x_test, type
pause)
prob") L,2]