Use k-nearest neighbors to classify a dataset and choose the best value for 'k' for optimal accuracy¶
In [1]:
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier
In [2]:
data = load_iris()
x = data.data
y = data.target
In [3]:
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2)
In [4]:
k_values = range(1, 21)
accuracy = []
In [5]:
for k in k_values:
knn = KNeighborsClassifier(n_neighbors = k)
knn.fit(x_train, y_train)
y_pred = knn.predict(x_test)
accuracy.append(accuracy_score(y_test, y_pred))
In [6]:
optimal_k = k_values[accuracy.index(max(accuracy))]
print(f"The optimal value of k is: {optimal_k}")
print(f"Accuracy at optimal k {optimal_k}: {max(accuracy)}")
The optimal value of k is: 1 Accuracy at optimal k 1: 1.0