# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Name:- <NAME> , Roll no 100150

# # Step 1 :- read the data from given csv file.

from numpy import genfromtxt
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
csv_data = genfromtxt('Iris data.csv', delimiter=',')
observations_All = csv_data[1:,1:-1];
print("All observations from input CSV file")
print(observations_All)

# # Step 2 :- Set Global variables used in classification

feature_names = ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
target = [0, 0, 0 ,0 ,0 ,0 ,0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
 2, 2]
target_names = ['setosa', 'versicolor', 'virginica']
X = observations_All
y = target

# # Step 3 :- Divide the dataset into training and testing datasets, Total 120 datasets for training and 30 datasets for testing (Train = 40 * 3 , Test = 10 * 3).

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False)
print (X_train.shape)
print (X_test.shape)

# # Step 4a :- Define the KNeighborsClassifier and predict using K = 3 and find the accuracy of the predication

# +
# taking value of k as 3
knn = KNeighborsClassifier(n_neighbors = 3)

knn.fit(X_train, y_train)

y_pred = knn.predict(X_test)

print (metrics.accuracy_score(y_test, y_pred))

# -

# # Step 4b :- Define the KNeighborsClassifier and predict using K = 3 and find the accuracy of the predication

# +
# taking value of k as 5
knn = KNeighborsClassifier(n_neighbors = 5)

knn.fit(X_train, y_train)

y_pred = knn.predict(X_test)

print (metrics.accuracy_score(y_test, y_pred))
# -

# # Step5:- Using different values for 'K' in KNN
# for the case k = 3 we got , accuracy = 96.67%, trying k values from 1 to 25, we get

# +
k_range = range(1, 26)
scores = []
for k in k_range:
    knn = KNeighborsClassifier(n_neighbors = k)
    knn.fit(X_train, y_train)
    y_pred = knn.predict(X_test)
    scores.append(metrics.accuracy_score(y_test, y_pred))

import matplotlib.pyplot as plt
# %matplotlib inline
plt.plot(k_range, scores)
plt.xlabel('Values of K')
plt.ylabel('Accuracy')
# -



StarcoderdataJupyterScriptsDedupFiltered