<jupyter_start><jupyter_text>![title](notebook_images/knn_gif.gif)<jupyter_code>iris = load_iris()<jupyter_output><empty_output><jupyter_text>Iris data: classes<jupyter_code>print("classes", iris.target_names)<jupyter_output>classes ['setosa' 'versicolor' 'virginica']<jupyter_text>Iris data: features![features](notebook_images/iris.png)<jupyter_code>iris.feature_names<jupyter_output><empty_output><jupyter_text>considering only two features since it is easy to visualize in 2D<jupyter_code>X = iris.data[:, :2]
X
y = iris.target
y<jupyter_output><empty_output><jupyter_text>Class distribution<jupyter_code>Counter(y)
x_train, x_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=222)
print(x_train.shape, y_train.shape)
print(x_val.shape, y_val.shape)
train_accuracy = []
val_accuracy = []

for i in range(1, 30):
    
    knn = KNeighborsClassifier(n_neighbors=i)
    knn.fit(x_train, y_train)
    
    y_preds = knn.predict(x_val)
    y_preds_train = knn.predict(x_train)
    
    val_accuracy.append(accuracy_score(y_val, y_preds))
    train_accuracy.append(accuracy_score(y_train, y_preds_train))
plt.plot(range(1, 30), val_accuracy)
plt.xlabel("Value of K")
plt.ylabel("Val accuracy")
#step size in the mesh
h = .02  

#Create color maps
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])

x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1

xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))

def plot_decision_boundary(k):
    
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(x_train, y_train)
    Z = knn.predict(np.c_[xx.ravel(), yy.ravel()])
    
    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    plt.figure()
    plt.pcolormesh(xx, yy, Z, cmap=cmap_light)

    # Plot also the training points
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold)
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.title("Iris data classification with k as :"+str(k))
    plt.show()<jupyter_output><empty_output><jupyter_text>Decision boundary for different K in KNN Overfitting and Underfitting<jupyter_code>for i in range(1, 30, 5):
    plot_decision_boundary(i)
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(x_train, y_train)
y_val_preds = knn.predict(x_val)

print(classification_report(y_val, y_val_preds))<jupyter_output>precision    recall  f1-score   support

           0       1.00      0.90      0.95        10
           1       0.46      0.75      0.57         8
           2       0.75      0.50      0.60        12

    accuracy                           0.70        30
   macro avg       0.74      0.72      0.71        30
weighted avg       0.76      0.70      0.71        30
{'fraction_non_alphanumeric': 0.13677918424753868, 'fraction_numerical': 0.03691983122362869, 'mean_word_length': 3.45226917057903, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '67088', 'license': 'MIT', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1136, 'n_words': 211, 'path': 'K - Nearest Neighbors.ipynb', 'repo_name': 'rajesh-bhat/ai4e', 'size': '2844'}