Project

K-Fold Cross Validation

#Importing the Required Libraries
from math import gamma
from typing import Type
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier

# Loading the Iris Data Set
iris=load_iris()
print(dir(iris))
print(type(iris))
df=pd.DataFrame(iris.data,columns=iris.feature_names)
print(df)
df['target']=iris.target
print(df)
print(df.describe())
print(df[df.target==1])
print(iris.target_names)

# Spliting the Data to test & training sets
from sklearn.model_selection import train_test_split
X=df[["sepal length (cm)","sepal width (cm)","petal length (cm)","petal width (cm)"]]
Y=df.target
print(X)
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.35)

# K-Fold Cross Validation
from sklearn.model_selection import KFold
kf = KFold(n_splits=5)
kf
KFold(n_splits=5, random_state=None, shuffle=False)

for train_index,test_index in kf.split(df):
    print(train_index,test_index)

# comparing the Accuracy using the get_score function
def get_score(model,X_train,X_test,Y_train,Y_test):
    model.fit(X_train,Y_train)
    return model.score(X_test,Y_test)

print("SVC Accuracy = ",get_score(SVC(),X_train,X_test,Y_train,Y_test))
print("KNN Accuracy = ",get_score(KNeighborsClassifier(n_neighbors=4),X_train,X_test,Y_train,Y_test))
print("Random Forest Accuracy",get_score(RandomForestClassifier(n_estimators=80),X_train,X_test,Y_train,Y_test))

# Startified K-Fold
from sklearn.model_selection import cross_val_score
print("SVC Accuracy_Startified K-Fold = ",cross_val_score(SVC(),X,Y))
print("KNN Accuracy_Startified K-Fold = ",cross_val_score(KNeighborsClassifier(n_neighbors=4),X,Y))
print("Random Forest Accuracy_Startified K-Fold = ",cross_val_score(RandomForestClassifier(n_estimators=80),X,Y))
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.