import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier

names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class']

df = pd.read_csv('iris.data.csv', header=None, names=names)
X = np.array(df.iloc[:, 0:4]) 	
y = np.array(df['class'])

# split the data into the train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)

knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)
pred = knn.predict(X_test)

print ('\nModel accuracy score: ', accuracy_score(y_test, pred))
print("\nFrom the test data")
print('Index\tPredicted\t\tActual')
for i in range(len(pred)):
  if pred[i] != y_test[i]:
    print(i, '\t', pred[i], '\t', y_test[i], ' ****')
    
DataToPredict = np.array([[5.2,3.5,1.4,0.2],[5.7,2.9,3.6,1.3],[5.8,3.0,5.1,1.8]])
pred = knn.predict(DataToPredict)

print("\nPredicted Results")
for i in range(len(pred)):
    print('\t', DataToPredict[i], '\t', pred[i])
