0% found this document useful (0 votes)
4 views3 pages

Coding Guide - Models

Uploaded by

stileless
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Coding Guide - Models

Uploaded by

stileless
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Model Linear Regression Polynomial Regression Logistic Regression KNN Decision Trees K Means Clustering (Unsupervised)

Imports import numpy as np (… the same but…) import numpy as np import numpy as np import numpy as np import numpy as np
import pandas as pd import pandas as pd import pandas as pd import pandas as pd import pandas as pd
from sklearn.model_selection import from [Link] import from sklearn.model_selection import from sklearn.model_selection import from sklearn.model_selection import
train_test_split PolynomialFeatures train_test_split train_test_split train_test_split from [Link] import KMeans
from sklearn.linear_model import from sklearn.linear_model import from [Link] import from sklearn import tree
LinearRegression …and for cross validation techniques: LogisticRegression KNeighborsClassifier from [Link] import
from sklearn import metrics from sklearn.feature_selection import from sklearn import metrics from sklearn import metrics DecisionTreeClassifier
from [Link] import RFE (Recursive Feature Elimination). from [Link] import from [Link] import from sklearn.model_selection import
mean_squared_error, r2_score from sklearn.model_selection import classification_report, accuracy_score, classification_report, accuracy_score, GridSearchCV
cross_val_score confusion_matrix, f1_score, confusion_matrix, from sklearn import metrics
from sklearn.model_selection import ConfusionMatrixDisplay, precision_recall_fscore_support, from [Link] import
KFold precision_recall_fscore_support, precision_score, recall_score classification_report, accuracy_score,
from sklearn.model_selection import precision_score, recall_score f1_score, confusion_matrix,
GridSearchCV precision_recall_fscore_support,
from [Link] import precision_score, recall_score
Normalizer
(Optional import [Link] as plt (… the same but…) import [Link] as plt import [Link] as plt import [Link] as plt
imports) import seaborn as sns import seaborn as sns import seaborn as sns
from [Link] import from sklearn.model_selection import from [Link] import
OneHotEncoder GridSearchCV OneHotEncoder, LabelEncoder
from [Link] import (if multi-class classification)
ColumnTransformer from [Link] import
from [Link] import StandardScaler, MinMaxScaler
StandardScaler, MinMaxScaler from sklearn.model_selection import
from [Link] import Pipeline learning_curve
Establish data df = pd.read_excel('file_name.xlsx')
df = pd.read_csv('file_name.csv')
df = [‘feature_1’, ‘feature_2’, ‘…’ ]
etc.
Define X and y X_raw = df[[‘feature_1’, ‘feature_2’, X_raw = [Link][:, :-1] X = [Link][:, :-1] X = [Link][:, :-1]
‘…’ ]] y_raw = [Link][:, -1:] [whole y = [Link][:, -1:] y = [Link][:, -1:]
y_ = df[‘target’] row, cols]
Encoding (OH) one_hot = (Also, can be helpful but a little
ColumnTransformer(transformers = different)
[("one_hot", OneHotEncoder(),
categorical_columns) ],remainder="p
assthrough")
X_raw=one_hot.fit_transform(X_raw)
type(X_raw)
column_names =
one_hot.get_feature_names_out()
names
df=[Link](data = X_raw,
columns = column_names)
Scale/ scaler = MinMaxScaler() (or Min/Max gives no distortion (scale as needed, then encode labels
Standardise StandardScaler()) Standard used if distribution is known if needing multi-class model)
X = scaler.fit_transform(X_raw) to be normal
print(f"The range of feature inputs RobustScaler() is an option if outliers label_encoder = LabelEncoder()
are within {[Link]()} to {[Link]()}") are present y = label_encoder.fit_transform
(y_raw.[Link]())
[Link](y, return_counts=True)
Train/Test Split X_train, X_test, y_train, y_test =
train_test_split(X, y, test_size=0.2,
stratify=y, random_state = 42)
Transform poly_features = (for L2 (Ridge) penalty and
PolynomialFeatures(degree =2, multinomial)
include_bias=False) penalty= 'l2'
X_train_poly = multi_class = 'multinomial'
poly_features.fit_transform(X_train) solver = 'lbfgs'
X_test_poly = max_iter = 1000
poly_feataures.transform(X_test)
Train Model lm = LinearRegression() lm = LinearRegression() l2_model = knn_model = model = DecisionTreeClassifier km = KMeans(n_clusters=5,
LogisticRegression(random_state=42 KNeighborsClassifier(n_neighbors=2) (random_state=42) random_state=42)
[Link](X_train,y_train) [Link](X_train_poly, y_train) , penalty=penalty,
multi_class=multi_class, knn_model.fit(X_train, [Link](X_train, [Link](X)
solver=solver, max_iter=max_iter) y_train.[Link]()) y_train.[Link]())

l2_model.fit(X_train, y_train)
Residuals results = model(lm).fit()
residuals = [Link]
Predict w/ y_pred = [Link](X_test) y_pred = [Link](X_train_poly) y_pred = l2_model.predict(X_test) y_pred = knn_model.predict(X_test) y_pred = [Link](X_test)
Model
Evaluate Model [Link](X_test,y_test) print("R^2 on training data:", (See below: evaluation function) (See below: evaluation function) (See below: evaluation function) (See below - evaluation is best done
mse = mean_squared_error(y_test, [Link](X_train_poly, y_train)) visually)
y_pred) print("R^2 on testing data:",
r2_score(y_test, y_pred) [Link](X_test_poly,y_test))
Extras Can use pipeline objects Can use pipeline objects and This can be adapted for image
Regularisation also available GridSearch segmentation.
Regularisation also available

Model (lin (pol Because we may need to evaluate the model multiple def evaluate_metrics(yt, yp): def evaluate_metrics(yt, yp):
Optimisation reg reg times with different model hyper parameters, here we results_pos = {} results_pos = {}
Techniques ignore ignore define a utility method to take the ground truths y_test results_pos['accuracy'] = accuracy_score(yt, yp) results_pos['accuracy'] = accuracy_score(yt, yp)
) ) and the predictions preds, and return a Python dict with precision, recall, f_beta, _ = precision, recall, f_beta, _ =
accuracy, recall, precision, and f1score. precision_recall_fscore_support(yt, yp, average='binary') precision_recall_fscore_support(yt, yp, average='binary')
results_pos['recall'] = recall results_pos['recall'] = recall
def evaluate_metrics(yt, yp): results_pos['precision'] = precision results_pos['precision'] = precision
results_pos = {} results_pos['f1score'] = f_beta results_pos['f1score'] = f_beta
results_pos['accuracy'] = accuracy_score(yt, yp) return results_pos return results_pos
precision, recall, f_beta, _ =
precision_recall_fscore_support(yt, yp) evaluate_metrics(y_test, y_pred) evaluate_metrics(y_test, y_pred)
results_pos['recall'] = recall
results_pos['precision'] = precision
results_pos['f1score'] = f_beta
return results_pos

evaluate_metrics(y_test, y_pred)
Feature Using L1 (Lasso) instead of L2 (remember EN is option Trying different values of k We will be using the tree.plot_tree() method provided by for label in [Link](km.labels_):
Engineering #3) sklearn to quickly plot any decision tree model. X_ = X[label == km.labels_]
Options # Try K from 1 to 50 [Link](X_['Annual Income
or # L1 penalty to shrink coefficients without removing any max_k = 50 def plot_decision_tree(model, feature_names): (k$)'], X_['Spending Score (1-100)'],
Visualisation of features from the model # Create an empty list to store f1score for each k [Link](figsize=(25, 20)) label=label)
Model penalty= 'l1' f1_scores = [] tree.plot_tree(model, [Link](xlabel)
# Our classification problem is multinomial feature_names=feature_names, [Link](ylabel)
multi_class = 'multinomial' for k in range(1, max_k + 1): filled=True) [Link]()
# Use saga for L1 penalty and multinomial classes # Create a KNN classifier [Link]()
solver = 'saga' knn = KNeighborsClassifier(n_neighbors=k)
# Max iteration = 1000 # Train the classifier feature_names = [Link] EXTRA: (not related to K-Means)
max_iter = 1000 knn = [Link](X_train, y_train.[Link]()) Cross Validation:
preds = [Link](X_test) plot_decision_tree(model, feature_names)
l1_model = LogisticRegression(random_state=rs, # Evaluate the classifier with f1score
from sklearn.model_selection
penalty=penalty, multi_class=multi_class, solver=solver, f1 = f1_score(preds, y_test) The DecisionTreeClassifier has many arguments (model
import cross_val_score
max_iter = 1000) f1_scores.append((k, round(f1_score(y_test, preds), 4))) hyperparameters) that can be customized and eventually
Rcross =
# Convert the f1score list to a dataframe tune the generated decision tree classifiers. Among these
cross_val_score( lre,x_data[['attribu
l1_model.fit(X_train, y_train) f1_results = [Link](f1_scores, columns=['K', 'F1 arguments, there are three commonly tuned arguments
te_1']],y_data,cv =n)
Score']) as follows:
# n indicates number of folds, for
l1_preds = l1_model.predict(X_test) f1_results.set_index('K')  criterion: gini or entropy, which specifies which which the cross validation is to be
criteria to be used when splitting a tree node.
done
# Plot F1 results  max_depth: a numeric value to specify the max Mean = [Link]()
ax = f1_results.plot(figsize=(12, 12)) depth of the tree. Larger tree depth normally
Std_dev = [Link]()
[Link](xlabel='Num of Neighbors', ylabel='F1 Score') means larger model complexity.
ax.set_xticks(range(1, max_k, 2));  min_samples_leaf: The minimal number of
[Link]((0.85, 1)) samples in leaf nodes. Larger samples in leaf yhat = cross_val_predict
[Link]('KNN F1 Score') nodes will tend to generate simpler trees. (lre,x_data[[‘attribute_1’]],
y_data,cv=4)
To find the optimized hyperparameters, which can
produce the highest F1 score, via GridSearch cross- Grid Search:
validation.
from sklearn.model_selection
We define a params_grid dict object to contain the import GridSearchCV
parameter candidates: from sklearn.linear_model import
Ridge
params_grid = { parameters= [{'alpha':
'criterion': ['gini', 'entropy'], [0.001,0.1,1, 10, 100, 1000, 10000,
'max_depth': [5, 10, 15, 20], ...]}]
'min_samples_leaf': [1, 2, 5] RR=Ridge()
} Grid1 = GridSearchCV(RR,
parameters1,cv=4)
model = DecisionTreeClassifier(random_state=42) [Link](x_data[[‘attribute_1’,
‘attribute_2’, ...]], y_data)
grid_search = GridSearchCV(estimator = model, BestRR=Grid1.best_estimator_
param_grid = params_grid, [Link](x_test[[‘attribute_1’,
scoring='f1', ‘attribute_2’, ...]], y_test
cv = 5, verbose = 1)
grid_search.fit(X_train, y_train.[Link]()) Precision = ability to predict actual
best_params = grid_search.best_params_ pos.
tp/(tp + fp)
best_params Recall: ability to find all pos.
tp/(tp+fn)
F1: harmonic weighted combo
(2 x p x r )/(p + r)

You might also like