<jupyter_start><jupyter_text><jupyter_code>import pandas as pd                #import necessary packages
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from matplotlib.colors import ListedColormap
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import (KNeighborsClassifier, NeighborhoodComponentsAnalysis)
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
from sklearn.model_selection import KFold
from sklearn import metrics
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.pipeline import Pipeline
from sklearn.datasets import load_iris

iris_data=load_iris()                 #load data
classes={0:'setosa',1:'virginica',2:'versicolour'}      #define classes
iris_class=iris_data.target            #create array of target based on the classes
iris_class
species=[classes[k] for k in iris_class]    #call the species correspond each target value
species
df=pd.DataFrame(iris_data.data, columns=iris_data.feature_names)  #create dataframe if it is needed
df
df.shape            #shape of the data out of data frame
df['iris_class']=iris_class.tolist()    #add class and species columns 
df['species']=species
df
iris_data.data.shape      #again shape of the data
iris_data.target.shape            #shape of the target
fig, ax = plt.subplots()           #scatter plot to preview the data
sepal_length=df['sepal length (cm)'].to_list()
sepal_width=df['sepal width (cm)'].to_list()
species=['setosa','virginica','versicolour']
colors = {'setosa':'red','virginica':'pink','versicolour':'purple'}
scatter=ax.scatter(sepal_length, sepal_width,c=df['species'].map(colors))
plt.xlabel('Sepal Length (cm)')
plt.ylabel('Sepal Width (cm)')
plt.xticks(color='blue',rotation=45)
plt.yticks(color='blue')
plt.show()
fig, ax = plt.subplots()           #scatter plot to preview the data
sepal_length=df['petal length (cm)'].to_list()
sepal_width=df['petal width (cm)'].to_list()
species=['setosa','virginica','versicolour']
colors = {'setosa':'red','virginica':'pink','versicolour':'purple'}
scatter=ax.scatter(sepal_length, sepal_width,c=df['species'].map(colors))
plt.xlabel('Petal Length (cm)')
plt.ylabel('Petal Width (cm)')
plt.xticks(color='blue',rotation=45)
plt.yticks(color='blue')
plt.show()
x=iris_data.data                   #define x & y
y=iris_data.target
#split dataset to train and test dataset
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.20, random_state=4)
knn= KNeighborsClassifier(n_neighbors=3)  #apply KNN model
knn.fit(x_train, y_train)
print('KNN Score: %.3f' % knn.score(x_test,y_test)) #get KNN score
y_pred=knn.predict(x_test)     #define y_pred 
y_pred
knn.predict(x_test)[0:3]            #if the test data is big to show it evaluation recalts, one can even test few of them
y_pred = knn.predict(x_test)     #draw the confusion matrix to show the performance of model
conf_matrix = confusion_matrix(y_true=y_test, y_pred=y_pred)
fig, ax = plt.subplots(figsize=(5, 5))
ax.matshow(conf_matrix, cmap=plt.cm.Oranges, alpha=0.3)
for i in range(conf_matrix.shape[0]):
    for j in range(conf_matrix.shape[1]):
        ax.text(x=j, y=i,s=conf_matrix[i, j], va='center', ha='center', size='xx-large')
 
plt.xlabel('Predictions', fontsize=18)
plt.ylabel('Actuals', fontsize=18)
plt.title('Confusion Matrix', fontsize=18)
plt.show()
print('Accuracy: %.3f' %metrics.accuracy_score(y_test, y_pred))  #find accuracy

print(classification_report(y_test, y_pred))  #to evaluate the algorithm and find all values necessary for evaluation
knn1= KNeighborsClassifier(n_neighbors=20)   #KNN model tested with different k value
knn1.fit(x_train, y_train)
print('KNN Score: %.3f' % knn1.score(x_test,y_test))  #model accuracy for another k valu
knn_accuracies=[]                 #call the range of the k values to find the optimal k value
k_num=range(1,25)
for k in k_num:
 knn= KNeighborsClassifier(n_neighbors=k)
 knn.fit(x_train, y_train)
 y_pred=knn.predict(x_test)
 knn_accuracies.append(metrics.accuracy_score(y_test, y_pred))
print(knn_accuracies)

plt.plot(k_num, knn_accuracies, color='g')       #plot the graph of the k range
plt.title('KNN Model K Value vs. Accuracy',color='b')
plt.xlabel('K Value',color='b')
plt.ylabel('Accuracy',color='b')<jupyter_output><empty_output><jupyter_text>**5 and 10 Fold Cross Validation Scores for Testing KNN**<jupyter_code>#apply cross validation
knn_cross_test_scores=cross_val_score(knn,x_test,y_test,cv=10)
knn_cross_test_scores
#get the overall accuracy of the cross validation
print("%0.2f%% accuracy with a standard deviation of %0.2f" % (knn_cross_test_scores.mean()*100, knn_cross_test_scores.std()))
k_cv_score=[]             #call the same k range in cross validation to find the optimal k value
k_cv_num=range(1,25)
for k in k_cv_num:
 knn= KNeighborsClassifier(n_neighbors=k)
 #knn.fit(x_train, y_train)
 #y_pred=knn.predict(x_test)
 knn_cross_test_scores=cross_val_score(knn,x_test,y_test,cv=10)
 k_cv_score.append(knn_cross_test_scores.mean())
print(k_cv_score)

#draw the graph of accuracy for k range from the cross validation
plt.plot(k_cv_num,k_cv_score,color='g') 
plt.title('KNN Model K Value vs. Cross Validation Accuracy',color='b')
plt.xlabel('K Value',color='b')
plt.ylabel('Cross Validation Accuracy',color='b')
#apply cross validation
knn_cross_test_scores=cross_val_score(knn,x_test,y_test,cv=5)
knn_cross_test_scores
#get the overall accuracy of the cross validation
print("%0.2f%% accuracy with a standard deviation of %0.2f" % (knn_cross_test_scores.mean()*100, knn_cross_test_scores.std()))
k_cv_score=[]             #call the same k range in cross validation to find the optimal k value
k_cv_num=range(1,25)
for k in k_cv_num:
 knn= KNeighborsClassifier(n_neighbors=k)
 #knn.fit(x_train, y_train)
 #y_pred=knn.predict(x_test)
 knn_cross_test_scores=cross_val_score(knn,x_test,y_test,cv=5)
 k_cv_score.append(knn_cross_test_scores.mean())
print(k_cv_score)
#draw the graph of accuracy for k range from the cross validation
plt.plot(k_cv_num,k_cv_score,color='g') 
plt.title('KNN Model K Value vs. Cross Validation Accuracy',color='b')
plt.xlabel('K Value',color='b')
plt.ylabel('Cross Validation Accuracy',color='b')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1181916370930412, 'fraction_numerical': 0.01018361363987039, 'mean_word_length': 6.234375, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '278051', 'license': 'MIT', 'n_tokens_mistral': 2399, 'n_tokens_neox': 2287, 'n_words': 551, 'path': 'INFO_5502_Assign7_KNN_.ipynb', 'repo_name': 'Kate5-7-2021/INFO-5502-SU2021', 'size': '6481'}