Neighbors
KNearestNeighborsClassifier
- class DLL.MachineLearning.SupervisedLearning.Neighbors.KNNClassifier(k=3, metric='euclidian')[source]
Bases:
object
The k-nearest neighbors classifier model. Looks for the k closest samples with respect to a distance metric and calculates the most common label.
- Parameters:
k (int, optional) – The number of closest samples considered for the predictions. Must be a positive integer. Defaults to 3.
metric (str, optional) – A distance metric for the closest points. Must be one of “euclidian” or “manhattan”. Defaults to “euclidian”.
- fit(X, y)[source]
Fits the KNNClassifier model to the input data by storing the input and label matricies.
- Parameters:
X (torch.Tensor of shape (n_samples, n_features)) – The input data, where each row is a sample and each column is a feature. There must be atleast k samples.
y (torch.Tensor of shape (n_samples,)) – The labels corresponding to each sample. Every element must be in [0, …, n_classes - 1].
- Returns:
None
- Raises:
TypeError – If the input matrix or the label matrix is not a PyTorch tensor.
ValueError – If the input matrix or the label matrix is not the correct shape.
- predict(X)[source]
Applies the fitted KNNClassifier model to the input data, predicting the labels.
- Parameters:
X (torch.Tensor of shape (n_samples, n_features)) – The input data to be classified.
- Returns:
The predicted target values corresponding to each sample.
- Return type:
labels (torch.Tensor of shape (n_samples,))
- Raises:
NotFittedError – If the KNNClassifier model has not been fitted before predicting.
TypeError – If the input matrix is not a PyTorch tensor.
ValueError – If the input matrix is not the correct shape.
- predict_proba(X)[source]
Applies the fitted KNNClassifier model to the input data, predicting the probabilities of each class.
- Parameters:
X (torch.Tensor of shape (n_samples, n_features)) – The input data to be classified.
- Returns:
The predicted target probabilities corresponding to each sample.
- Return type:
labels (torch.Tensor of shape (n_samples,) for binary and (n_samples, n_classes) for multiclass classification)
- Raises:
NotFittedError – If the KNNClassifier model has not been fitted before predicting.
TypeError – If the input matrix is not a PyTorch tensor.
ValueError – If the input matrix is not the correct shape.
KNearestNeighborsRegressor
- class DLL.MachineLearning.SupervisedLearning.Neighbors.KNNRegressor(k=3, metric='euclidian', weight='gaussian')[source]
Bases:
object
The k-nearest neighbors regressor model. Looks for the k closest samples with respect to a distance metric and calculates the average according to some weight function.
- Parameters:
k (int, optional) – The number of closest samples considered for the predictions. Must be a positive integer. Defaults to 3.
metric (str, optional) – A distance metric for the closest points. Must be one of “euclidian” or “manhattan”. Defaults to “euclidian”.
weight (str, optional) – A weight function that decides how important are the nearest k samples. Must be in [“uniform”, “distance”, “gaussian”]. Defaults to “gaussian”.
- fit(X, y)[source]
Fits the KNNRegressor model to the input data by storing the input and target matricies.
- Parameters:
X (torch.Tensor of shape (n_samples, n_features)) – The input data, where each row is a sample and each column is a feature.
y (torch.Tensor of shape (n_samples,)) – The target values corresponding to each sample.
- Returns:
None
- Raises:
TypeError – If the input matrix or the target matrix is not a PyTorch tensor.
ValueError – If the input matrix or the target matrix is not the correct shape.
- predict(X)[source]
Applies the fitted KNNRegressor model to the input data, predicting the target values.
- Parameters:
X (torch.Tensor of shape (n_samples, n_features)) – The input data to be regressed.
- Returns:
The predicted target values corresponding to each sample.
- Return type:
targets (torch.Tensor of shape (n_samples,))
- Raises:
NotFittedError – If the KNNRegressor model has not been fitted before predicting.
TypeError – If the input matrix is not a PyTorch tensor.
ValueError – If the input matrix is not the correct shape.