<jupyter_start><jupyter_text>**K Nearest Neighbour Classification**  **Step 1: Importing the Required Libraries and Datasets**<jupyter_code>import numpy as np
import pandas as pd
dataset = pd.read_csv('Iris.csv')
dataset.head(5)
dataset.describe()
dataset.groupby('Species').size()<jupyter_output><empty_output><jupyter_text>**Step 2: Dividing the Data and Encoding the Labels**<jupyter_code>feature_columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm','PetalWidthCm']
X = dataset[feature_columns].values
y = dataset['Species'].values
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y = le.fit_transform(y)<jupyter_output><empty_output><jupyter_text>**Step 3: Visualising the Dataset**<jupyter_code>import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
from pandas.plotting import parallel_coordinates

plt.figure(figsize=(15,10))
parallel_coordinates(dataset.drop("Id", axis=1), "Species")

plt.title('Parallel Coordinates Plot', fontsize=20, fontweight='bold')
plt.xlabel('Features', fontsize=15)
plt.ylabel('Features values', fontsize=15)
plt.legend(loc=1, prop={'size': 15}, frameon=True,shadow=True, facecolor="white", edgecolor="black")
plt.show()
plt.figure()
sns.pairplot(dataset.drop("Id", axis=1), hue = "Species", size=3, markers=["o", "s", "D"])
plt.show()
plt.figure()
dataset.drop("Id", axis=1).boxplot(by="Species", figsize=(15, 10))
plt.show()<jupyter_output><empty_output><jupyter_text>**Step 4: Splitting the Dataset and Fitting the Model**<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.model_selection import cross_val_score

classifier = KNeighborsClassifier(n_neighbors=3)
classifier.fit(X_train, y_train)

y_pred = classifier.predict(X_test)<jupyter_output><empty_output><jupyter_text>**Step 5: Evaluating the Predictions and Cross Validating**<jupyter_code>cm = confusion_matrix(y_test, y_pred)
cm
accuracy = accuracy_score(y_test, y_pred)*100
print('Accuracy of our model is equal ' + str(round(accuracy, 2)) + ' %.')
k_list = list(range(1,50,2))
cv_scores = []

for k in k_list:
    knn = KNeighborsClassifier(n_neighbors=k)
    scores = cross_val_score(knn, X_train, y_train, cv=10, scoring='accuracy')
    cv_scores.append(scores.mean())
MSE = [1 - x for x in cv_scores]

plt.figure()
plt.figure(figsize=(15,10))
plt.title('The optimal number of neighbors', fontsize=20, fontweight='bold')
plt.xlabel('Number of Neighbors K', fontsize=15)
plt.ylabel('Misclassification Error', fontsize=15)
sns.set_style("whitegrid")
plt.plot(k_list, MSE)

plt.show()
best_k = k_list[MSE.index(min(MSE))]
print("The optimal number of neighbors is %d." % best_k)<jupyter_output>The optimal number of neighbors is 9.
{'fraction_non_alphanumeric': 0.1393188854489164, 'fraction_numerical': 0.01823185414516684, 'mean_word_length': 8.115987460815047, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '661901', 'license': 'MIT', 'n_tokens_mistral': 1107, 'n_tokens_neox': 1041, 'n_words': 222, 'path': 'Classification/KNearestNeighbourClassification.ipynb', 'repo_name': 'ashwinraj-in/MediumArticles', 'size': '2907'}