3 Upvotes

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

df = pd.read_csv('data/processed_data.csv')

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?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
Top Contributors
75