<jupyter_start><jupyter_text>Here we already have 98% accuracy, but still we will try to maximise the accuracy as much as possible<jupyter_code>error_rate = []
for i in range(1,30):
    model_final = KNeighborsClassifier(n_neighbors=i)
    model_final.fit(X_train, y_train)
    prediction_final = model_final.predict(X_test)
    error_rate.append(np.mean(prediction_final != y_test))
sns.lineplot(x= range(1,30), y= error_rate, color= 'red')
sns.set_theme(context= 'paper', style = 'darkgrid')
plt.title("K Values V/s Error Rate")
plt.xlabel("K Values")
plt.ylabel("Error Rates")
# We see that for the K values of range 5-17 has the least amount of error rate

knn= KNeighborsClassifier(n_neighbors=9)
knn.fit(X_train,y_train)
knn_prediction = knn.predict(X_test)
knn_prediction[17:25]
y_test[17:25]
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test,knn_prediction))
print(classification_report(y_test,knn_prediction))<jupyter_output>[[19  0  0]
 [ 0 13  0]
 [ 0  0 13]]
              precision    recall  f1-score   support

      setosa       1.00      1.00      1.00        19
  versicolor       1.00      1.00      1.00        13
   virginica       1.00      1.00      1.00        13

    accuracy                           1.00        45
   macro avg       1.00      1.00      1.00        45
weighted avg       1.00      1.00      1.00        45
{'fraction_non_alphanumeric': 0.09513590844062947, 'fraction_numerical': 0.06652360515021459, 'mean_word_length': 2.7306666666666666, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '431870', 'license': 'MIT', 'n_tokens_mistral': 548, 'n_tokens_neox': 496, 'n_words': 123, 'path': 'Machine Learning/KNN Classification/Iris_KNN Classification.ipynb', 'repo_name': 'Mithun162001/Python-Notebooks', 'size': '1398'}