 ANALYSEUP

Training a Model Using Stratified K-Fold

Python
Supervised Learning

Training a logistic regression model using stratified 5-fold cross validation. For a full tutorial on this method then visit www.analyseup.com

import pandas as pd
from sklearn.model_selection import StratifiedKFold

skf = StratifiedKFold(n_splits=5)
target = df.loc[:,'Returned_Units']

from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

model = LogisticRegression()
def train_model(train, test, fold_no):
X = ['Retail_Price','Discount']
y = ['Returned_Units']
X_train = train[X]
y_train = train[y]
X_test = test[X]
y_test = test[y]
model.fit(X_train,y_train)
predictions = model.predict(X_test)
print('Fold',str(fold_no),'Accuracy:',accuracy_score(y_test,predictions))

fold_no = 1
for train_index, test_index in skf.split(df, target):
train = df.loc[train_index,:]
test = df.loc[test_index,:]
train_model(train,test,fold_no)
fold_no += 1

By analyseup - Last Updated Jan. 10, 2022, 11:30 p.m. Did you find this snippet useful?

RELATED SNIPPETS
4
Train Catboost Classifier with GridSearch
Python
Supervised Learning
3
LightGBM Custom Loss Function
Python
Supervised Learning
2
Scoring Models with Sklearn MSE and MAE
Python
Supervised Learning
1
Logistic Regression with Sklearn
Python
Supervised Learning
1
Linear Regression with Sklearn
Python
Supervised Learning
1
Tuning XGBoost with Grid Search
Python
Supervised Learning
1
Top Contributors 82 75 36
Find Snippets by Language
Find Snippets by Use