<jupyter_start><jupyter_text>Loading Required Libraries<jupyter_code># Loading Required Libraries
import numpy as np 
import pandas as pd 
import sklearn
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix 
from sklearn.metrics import accuracy_score 
from sklearn.datasets import load_iris
from matplotlib import pyplot as plt
from sklearn import datasets
from sklearn import tree<jupyter_output><empty_output><jupyter_text>Exploring Iris Dataset<jupyter_code># Loading Datasets 
iris_data = load_iris() 
iris = pd.DataFrame(iris_data.data)
iris_targets = pd.DataFrame(iris_data.target)

# Priting Features Name of Iris Data 
print ("Features Name : ", iris_data.feature_names) 

# Priting Targets Name of Iris Data 
print ("Targets Name : ", iris_data.target_names) 

# Shape of Datasets 
print ("Dataset Shape: ", iris.shape) 

# First Five Sample features
print ("Dataset: ",iris.head())  

# First Five Sample Targets
print ("Dataset: ",iris_targets.head())<jupyter_output>Features Name :  ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
Targets Name :  ['setosa' 'versicolor' 'virginica']
Dataset Shape:  (150, 4)
Dataset:       0    1    2    3
0  5.1  3.5  1.4  0.2
1  4.9  3.0  1.4  0.2
2  4.7  3.2  1.3  0.2
3  4.6  3.1  1.5  0.2
4  5.0  3.6  1.4  0.2
Dataset:     0
0  0
1  0
2  0
3  0
4  0<jupyter_text>Splitting Dataset into training and testing sets<jupyter_code># Features and Targets
X = iris_data.data
Y = iris_data.target

# Splitting the Dataset into Training and Testing sets 
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.2, random_state = 42)<jupyter_output><empty_output><jupyter_text>Normalizing the dataset<jupyter_code>from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train)

X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

X_train[0:4,:]<jupyter_output><empty_output><jupyter_text>KNN Classifier<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
KNN = KNeighborsClassifier(n_neighbors = 5)
KNN.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Predicting<jupyter_code>Y_pred = KNN.predict(X_test)<jupyter_output><empty_output><jupyter_text>Accuracy & Confusion Matrix<jupyter_code>from sklearn.metrics import confusion_matrix

#Accuray of the Model 
print("Accuracy:", accuracy_score(y_test, Y_pred)*100, "%")
print(confusion_matrix(y_test, Y_pred))<jupyter_output>Accuracy: 100.0 %
[[10  0  0]
 [ 0  9  0]
 [ 0  0 11]]<jupyter_text>Calculating Error for K Values<jupyter_code>error = []

# Calculating error for K values between 1 and 40
for i in range(1, 40):
    knn = KNeighborsClassifier(n_neighbors=i)
    knn.fit(X_train, y_train)
    pred_i = knn.predict(X_test)
    error.append(np.mean(pred_i != y_test))
    print(np.mean(pred_i != y_test))<jupyter_output>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
0.03333333333333333
0.0
0.03333333333333333
0.03333333333333333
0.03333333333333333
0.03333333333333333
0.03333333333333333
0.03333333333333333
0.03333333333333333
0.03333333333333333
0.03333333333333333
0.06666666666666667
0.03333333333333333
0.03333333333333333<jupyter_text>Plotting Error for K Values<jupyter_code>plt.figure(figsize=(12, 6))
plt.plot(range(1, 40), error, color='red', linestyle='dashed', marker='o',
         markerfacecolor='blue', markersize=10)
plt.title('Error Rate K Value')
plt.xlabel('K Value')
plt.ylabel('Mean Error')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1101457399103139, 'fraction_numerical': 0.10902466367713004, 'mean_word_length': 5.283450704225352, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 13, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '12971', 'license': 'MIT', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1434, 'n_words': 326, 'path': 'Applying KNN Classifier on Iris Dataset.ipynb', 'repo_name': 'wnoyan/Machine-Learning', 'size': '3568'}