"""
# IRIS SPECIES PREDICTION USING K-NEAREST NEIGHBOR METHOD (SUPERVISED)
"""
"""
## We are going to classify the test data into the 3 Iris species 
"""
"""
## 1. IMPORT ALL THE REQUIRED LIBRARIES
"""
import numpy as np   #Linear algera Library
import pandas as pd
import matplotlib.pyplot as plt  #to plot graphs
import seaborn as sns  #to plot graphs
sns.set()  #setting seaborn as default 

import warnings
warnings.filterwarnings('ignore')

"""
## 2. READ INPUT DATA 
"""
data=pd.read_csv("../input/iris/Iris.csv")  
data.tail()


data['Species'].unique  ##target has 3 groups 
"""
## 3. PLOTTING THE THREE TARGET GROUPS
"""
plt.figure(figsize=(10,5))
sns.countplot(x='Species',data=data)
"""
## 4.Check for Null values
"""
data.isnull().sum()  #no null values
data.columns
"""
## 5. ASSIGN THE REQUIRED COLUMNS TO x AND y
"""
x=data[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
y=data[['Species']]
"""
## 6. SPLIT THE DATA INTO TRAINING AND TEST DATA
"""
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=42)
"""
## 7. USE ELBOW METHOD TO GET THE K VALUE
"""
from sklearn.neighbors import KNeighborsClassifier
error_rate =[]
for i in range(1,40):
    knn=KNeighborsClassifier(n_neighbors=i)
    knn.fit(x_train,y_train)
    pred_i=knn.predict(x_test)
    pred_i=pred_i.reshape(-1,1)   #i added cause of an error
    error_rate.append(np.mean(pred_i != y_test))
plt.figure(figsize=(10,5))
plt.plot(range(1,40),error_rate)
"""
## 8.RUN THE MODEL WITH TRAINING DATA
"""
knn=KNeighborsClassifier(n_neighbors=12)
knn.fit(x_train,y_train)
"""
## 9. PREDICT USING TEST DATA
"""
predictions=knn.predict(x_test)
predictions
"""
## 10. PRINT THE CONFUSION MATRIX AND CLASSIFICATION REPORT
"""
from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,predictions))
print(classification_report(y_test,predictions))