Show Menu
Cheatography

Python for Machine Learning Cheat Sheet (DRAFT) by

useful functions to use for building ML models

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Packages

from sklear­n.m­ode­l_s­ele­ction import test_t­rai­n_split
from sklear­n.m­etrics import confus­ion­_matrix
from sklear­­n.p­­r­ep­­roc­­essing import MinMax­­Sc­alar, Standa­­rd­S­calar

Syntax

# Selecting and training the model 
model = 
model.fit(X_train, y_train)

# Using the model to predict 
y_pred = model.predict(X_test)
 

Processing data

MinMax­Sca­lar­().f­it­_tr­ans­for­m([­[value] for value in data])
min-ma­x-s­caling (norma­liz­ation) works well if predictor is roughly uniform
Z score scaling (stand­ard­isa­tion) works if there are outliers
dummy_vars = pd.get­_du­mmi­es(­df.col)
df = pd.con­cat­([df, dummy_­vars], axis=1)
one hot encoding
train, test = train_­tes­t_s­pli­t(df, test_s­ize­=0.2)

Evaluating model

confus­ion­_ma­tri­x(y­_test, y_pred)