Created
October 26, 2019 06:32
-
-
Save anuraagdjain/d9253f78887cf94c405eee2d978c7323 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 5-fold cross validation | |
import numpy as np | |
import pandas as pd | |
from sklearn.model_selection import KFold | |
fold5 = KFold(n_splits=5,shuffle=False,random_state=1) | |
scores = [] | |
df = pd.read_csv('iris.csv',header=None) | |
X = df.drop(columns=[4]) | |
Y = df[4] | |
knn = KNeighborsClassifier(n_neighbors = 9) | |
for train_index,test_index in fold5.split(X): | |
X_train,X_test,Y_train,Y_test = X.iloc[train_index],X.iloc[test_index],Y.iloc[train_index],Y.iloc[test_index] | |
knn.fit(X_train,Y_train) | |
pred = knn.predict(X_test) | |
print(metrics.accuracy_score(Y_test,pred) * 100) | |
scores.append(round(knn.score(X_test,Y_test)*100,2)) | |
print(scores) | |
print(np.mean(scores)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment