K-Nearest Neighbors in Python? - with practical example
K-Nearest Neighbors (KNN) is a simple and popular machine learning algorithm used for classification and regression tasks. It works by finding the K data points in the training set that are closest to the new data point, and then assigning the new data point the majority label of those K neighbors.
Let's start with an example of using KNN for a classification task:
Example 1:
Suppose we have a dataset of flowers with features like petal length and width, and a target variable specifying the type of flower. We want to predict the type of a new flower based on its petal length and width using KNN.
Step 1: Import necessary libraries and load the dataset.
Step 2: Split the data into training and testing sets.
Step 3: Create a KNN model with a specified value of K.
Step 4: Fit the model on the training data.
Step 5: Make predictions on the test data.
Step 6: Evaluate the model performance using metrics like accuracy.
Now, let's move on to another example using KNN for a regression task: Example 2: Suppose we have a dataset with housing prices and features like the number of bedrooms and square footage. We want to predict the price of a new house based on these features using KNN. Step 1: Import necessary libraries and load the dataset. Step 2: Split the data into training and testing sets. Step 3: Create a KNN model with a specified value of K for regression. Step 4: Fit the model on the training data. Step 5: Make predictions on the test data. Step 6: Evaluate the model performance using regression metrics like mean squared error.
In these examples, we have demonstrated how to use K-Nearest Neighbors (KNN) algorithm for both classification and regression tasks in Python. By following the steps outlined in each example, you can apply KNN to your own datasets and make predictions based on the nearest neighbors.
# Example 1: KNN for classification from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score # Step 1 # Import necessary libraries and load the dataset from sklearn.datasets import load_iris iris = load_iris() X = iris.data y = iris.target # Step 2 # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Step 3 # Create a KNN model with K=3 knn = KNeighborsClassifier(n_neighbors=3) # Step 4 # Fit the model on the training data knn.fit(X_train, y_train) # Step 5 # Make predictions on the test data y_pred = knn.predict(X_test) # Step 6 # Evaluate the model performance accuracy = accuracy_score(y_test, y_pred) print("Accuracy:", accuracy)
Now, let's move on to another example using KNN for a regression task: Example 2: Suppose we have a dataset with housing prices and features like the number of bedrooms and square footage. We want to predict the price of a new house based on these features using KNN. Step 1: Import necessary libraries and load the dataset. Step 2: Split the data into training and testing sets. Step 3: Create a KNN model with a specified value of K for regression. Step 4: Fit the model on the training data. Step 5: Make predictions on the test data. Step 6: Evaluate the model performance using regression metrics like mean squared error.
# Example 2: KNN for regression from sklearn.neighbors import KNeighborsRegressor from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error # Step 1 # Import necessary libraries and load the dataset import pandas as pd from sklearn.datasets import load_boston boston = load_boston() X = boston.data y = boston.target # Step 2 # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Step 3 # Create a KNN model with K=5 for regression knn_reg = KNeighborsRegressor(n_neighbors=5) # Step 4 # Fit the model on the training data knn_reg.fit(X_train, y_train) # Step 5 # Make predictions on the test data y_pred = knn_reg.predict(X_test) # Step 6 # Evaluate the model performance mse = mean_squared_error(y_test, y_pred) print("Mean Squared Error:", mse)
In these examples, we have demonstrated how to use K-Nearest Neighbors (KNN) algorithm for both classification and regression tasks in Python. By following the steps outlined in each example, you can apply KNN to your own datasets and make predictions based on the nearest neighbors.
Comments
Post a Comment