The k-nearest neighbors (KNN) algorithm is a supervised learning method. The method can be used for solving regression or classification problems. The k-nearest neighbors regressor is used for solving regression problems, and the k-nearest neighbors classifier is used to solve classification problems.
The algorithm works by determining the distance between different data points. Most often, the Euclidean distance is used to measure the distance between data points. In the case of the KNN classifier, the output is calculated as the class with the highest probability based on the votes from the k-nearest neighbors.
How to solve classification problems using the k-nearest neighbors classifier in sklearn?
Let’s read the iris dataset. The dataset contains the petal length, petal width, sepal length, and sepal width of flowers. And based on these features, the type of flower can be determined. In this example, we will use the same dataset to determine the type of flower based on these four features. We can use the following Python code for that purpose:
import seaborn from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score from sklearn.preprocessing import LabelEncoder df = seaborn.load_dataset("iris") print(df.info()) label_encoder = LabelEncoder() df["species"] = label_encoder.fit_transform(df["species"]) print(df.head()) df_features = df.drop(labels=["species"], axis=1) df_target = df.filter(items=["species"]) X_train, X_test, y_train, y_test = train_test_split(df_features, df_target["species"], shuffle=True, random_state=1) knn_classifier = KNeighborsClassifier(n_neighbors=5) knn_classifier.fit(X_train, y_train) y_test_pred = knn_classifier.predict(X_test) accuracy = accuracy_score(y_test, y_test_pred) print("Accuracy Score: ", accuracy)
Firstly, we are reading the iris dataset using the seaborn library. Please note that machine learning models understand only numbers. So, we need to encode the output labels. We are using a label encoder to encode the species column. …
0 Comments