Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn.model_selection import train_test_split
- from collections import Counter
- from sklearn.metrics import accuracy_score
- dataset_path = 'data/diabetes.csv'
- df = pd.read_csv(dataset_path)
- # Show the first few rows of the dataset
- df.head()
- X = df.drop('Outcome', axis=1).values
- y = df['Outcome'].values
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
- X_train.shape, X_test.shape, y_train.shape, y_test.shape
- class KNN:
- def __init__(self, k=3):
- self.k = k
- def fit(self, X, y):
- self.X_train = X
- self.y_train = y
- def predict(self, X):
- y_pred = [self._predict(x) for x in X]
- return np.array(y_pred)
- def _predict(self, x):
- # Compute distances between x and all examples in the training set
- distances = [np.linalg.norm(x - x_train) for x_train in self.X_train]
- # Sort by distance and return indices of the first k neighbors
- k_indices = np.argsort(distances)[:self.k]
- # Extract the labels of the k nearest neighbor training samples
- k_nearest_labels = [self.y_train[i] for i in k_indices]
- # Return the most common class label among k nearest neighbors
- most_common = Counter(k_nearest_labels).most_common(1)
- return most_common[0][0]
- # Initialize KNN classifier with k=3
- knn = KNN(k=3)
- # Fit the model with the training data
- knn.fit(X_train, y_train)
- # Make predictions on the test data
- y_pred = knn.predict(X_test)
- # Display some of the predictions
- y_pred[:10]
- accuracy = accuracy_score(y_test, y_pred)
- accuracy
Advertisement
Add Comment
Please, Sign In to add comment