<jupyter_start><jupyter_text>WEEK - 3 ASSIGNMENTS Data Science & Data Analytics Laboratory Name: <NAME> Section: CSE 3H Roll No.: 29 Enrollment No.: 12018009019439  k Nearest Neighbours<jupyter_code>from sklearn.datasets import load_iris
iris = load_iris()
print(iris.DESCR)
import pandas as pd
data = pd.DataFrame(iris.data, columns=iris.feature_names)
data['Species'] = pd.DataFrame(iris.target)
data.head()
X = data.iloc[:, :-1]
Y = data.iloc[:, -1]
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3)
from sklearn.neighbors import KNeighborsClassifier

model = KNeighborsClassifier(n_neighbors=5)
model.fit(X_train, Y_train)

Y_pred = model.predict(X_test)
targetVsPredicted = pd.DataFrame()
targetVsPredicted["Target"] = Y_test
targetVsPredicted["Predicted"] = Y_pred

targetVsPredicted
from sklearn.metrics import accuracy_score

print(f"{accuracy_score(Y_pred, Y_test).round(2)*100}%")
scores = []
k_range = range(1, 31)
for k in k_range:
    model = KNeighborsClassifier(n_neighbors=k)
    model.fit(X_train, Y_train)
    Y_pred = model.predict(X_test)

    scores.append(accuracy_score(Y_pred, Y_test).round(2)*100)
for k in k_range:
    print(f"k={k}: {scores[k-1]}%")
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(16,9))
plt.plot(k_range, scores)
plt.xlabel("Neighbors")
plt.ylabel("Accuracy (%)")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13052486187845305, 'fraction_numerical': 0.026243093922651933, 'mean_word_length': 6.5863874345549736, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '409031', 'license': 'MIT', 'n_tokens_mistral': 605, 'n_tokens_neox': 559, 'n_words': 112, 'path': 'Week-3/Classwork/k-Nearest-Neighbours.ipynb', 'repo_name': 'ShruKin/Data-Science-Data-Analytics-Lab', 'size': '1460'}