| |
- abc.ABC(builtins.object)
-
- KNeighborsBase
class KNeighborsBase(abc.ABC) |
|
KNeighborsBase(n_neighbors=5, distance_metric='euclidean', one_hot_encode=False, fp_precision=<class 'numpy.float64'>, numba=False)
Abstract base class for implementing k-nearest neighbors (KNN) algorithms.
Provides common functionality for fitting data, computing distances, and managing configurations.
Attributes:
n_neighbors (int): Number of neighbors to use for the KNN algorithm.
distance_metric (str): Distance metric for calculating distances ('euclidean', 'manhattan', 'minkowski').
one_hot_encode (bool): Whether to apply one-hot encoding to categorical columns.
fp_precision (type): Floating point precision for calculations.
numba (bool): Whether to use numba for performance optimization.
X_train (np.ndarray): Training feature data.
y_train (np.ndarray): Training target data.
Methods:
__init__(n_neighbors=5, distance_metric="euclidean", one_hot_encode=False,
fp_precision=np.float64, numba=False):
Initializes the KNeighborsBase class with specified parameters.
fit(X, y):
Fits the model using the training data and target values.
get_distance_indices(X):
Computes distances and returns indices of the nearest points in the training data.
_data_precision(X, y=None):
Sets the floating point precision for the input data.
_check_data(X, y):
Validates input data to ensure it is numeric and consistent.
_one_hot_encode(X):
Applies one-hot encoding to categorical columns in the input data.
_compute_distances(X):
Computes distances between input data and training data using the specified distance metric.
_compute_distances_euclidean(X):
Computes distances using the Euclidean distance formula.
_compute_distances_manhattan(X):
Computes distances using the Manhattan distance formula.
_compute_distances_minkowski(X, p=3):
Computes distances using the Minkowski distance formula with specified order `p`.
predict(X):
Abstract method to be implemented by subclasses for making predictions based on input data. |
|
- Method resolution order:
- KNeighborsBase
- abc.ABC
- builtins.object
Methods defined here:
- __init__(self, n_neighbors=5, distance_metric='euclidean', one_hot_encode=False, fp_precision=<class 'numpy.float64'>, numba=False)
- Initialize the KNeighborsBase class.
Args:
n_neighbors: int, default=5. The number of neighbors to use for the KNN algorithm.
distance_metric: str, default='euclidean'. The distance metric to use for calculating distances.
one_hot_encode: bool, default=False. Whether to apply one-hot encoding to the categorical columns.
fp_precision: data type, default=np.float64. The floating point precision to use for the calculations.
numba: bool, default=True. Whether to use numba for speeding up the calculations.
- fit(self, X, y)
- Fit the model using the training data.
Args:
X: array-like, shape (n_samples, n_features) - The training data.
y: array-like, shape (n_samples,) - The target values.
- get_distance_indices(self, X)
- Compute the distances and return the indices of the nearest points im the training data.
Args:
X: array-like, shape (n_samples, n_features) - The input data.
Returns:
indices: array, shape (n_samples, n_neighbors) - The indices of the nearest neighbors.
- predict(self, X)
- The @abstractmethod decorator indicates that this method must be implemented by any subclass of KNNBase.
Data descriptors defined here:
- __dict__
- dictionary for instance variables
- __weakref__
- list of weak references to the object
Data and other attributes defined here:
- __abstractmethods__ = frozenset({'predict'})
| |