1 2 3 4 5 6 7 8 9 10from mltrain.supervised.KNN import KNN # Initialize the model model = KNN(k=3,distance='euclidean',purpose='classification') # Train the model model.train(X_train, y_train) # Make predictions predictions = model.predict(X_test)
The KNN class implements the k-Nearest Neighbors algorithm for both classification and regression tasks. It supports two distance metrics: Euclidean and Manhattan. The class provides methods to train the model, make predictions, and evaluate performance using accuracy or mean squared error.
k (default=3): The number of nearest neighbors to consider.distance_metric (default='euclidean'): The distance metric to use. Options are 'euclidean' and 'manhattan'.purpose (default='classification'): The purpose of the model. Options are 'classification' and 'regression'.__init__(self, k=3, distance_metric='euclidean', purpose='classification')Initializes the KNN model with specified hyperparameters.
predict_single(self, x)Predicts the label or value for a single input sample.
predict(self, X)Predicts labels or values for multiple input samples.
mean_squared_error(self, y_true, y_pred)Calculates the Mean Squared Error (MSE) between true and predicted values.
accuracy(self, y_true, y_pred)Calculates the accuracy of predictions for classification tasks.
confusion_matrix(self, y_true, y_pred)Computes the confusion matrix for classification tasks.
- The predict_single method calculates distances between the input sample and all training samples, then selects the k nearest neighbors to make a prediction.
- For classification, the most common label among the k nearest neighbors is returned. For regression, the mean value of the k nearest neighbors is returned.
- The mean_squared_error and accuracy methods are only applicable for regression and classification tasks, respectively.