• Home
  • Docs
  • About






  • KNN Model

    Example Usage

    1from mltrain.supervised.KNN import KNN 2 3# Initialize the model 4model = KNN(k=3,distance='euclidean',purpose='classification') 5 6# Train the model 7model.train(X_train, y_train) 8 9# Make predictions 10predictions = model.predict(X_test) 11

    Overview

    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.


    Hyperparameters

    • 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'.

    Methods

    __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.


    Additional Notes

    - 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.