ml_grid.model_classes.knn_wrapper_class

Classes

KNNWrapper

Initializes the KNNWrapper.

Module Contents

class ml_grid.model_classes.knn_wrapper_class.KNNWrapper(n_neighbors: int = 5, weights: str = 'uniform', algorithm: str = 'auto', leaf_size: int = 30, p: int = 2, metric: str = 'minkowski', metric_params: Dict[str, Any] | None = None, device: str | None = None)[source]

Initializes the KNNWrapper.

Parameters:
  • n_neighbors (int) – Number of neighbors to use.

  • weights (str) – Weight function used in prediction.

  • algorithm (str) – Algorithm used to compute the nearest neighbors.

  • leaf_size (int) – Leaf size passed to BallTree or KDTree.

  • p (int) – Power parameter for the Minkowski metric.

  • metric (str) – The distance metric to use for the tree.

  • metric_params (Optional[Dict[str, Any]]) – Additional keyword arguments for the metric function. Defaults to None.

  • device (Optional[str]) – The device to use (‘gpu’ or ‘cpu’). If None, it auto-detects GPU availability. Defaults to None.

n_neighbors = 5[source]
weights = 'uniform'[source]
algorithm = 'auto'[source]
leaf_size = 30[source]
p = 2[source]
metric = 'minkowski'[source]
metric_params = None[source]
device = 'cpu'[source]
model: simbsig.neighbors.KNeighborsClassifier | None = None[source]
fit(X: pandas.DataFrame | numpy.ndarray, y: pandas.Series | numpy.ndarray) KNNWrapper[source]

Fits the KNN model.

Initializes and fits the simbsig.neighbors.KNeighborsClassifier.

Parameters:
  • X (Union[pd.DataFrame, np.ndarray]) – The training input samples.

  • y (Union[pd.Series, np.ndarray]) – The target values.

Returns:

The fitted estimator.

Return type:

KNNWrapper

get_params(deep: bool = False) Dict[str, Any][source]

Gets parameters for this estimator.

Parameters:

deep (bool) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

Parameter names mapped to their values.

Return type:

Dict[str, Any]

predict(X: pandas.DataFrame | numpy.ndarray) numpy.ndarray[source]

Predicts class labels for samples in X.

Parameters:

X (Union[pd.DataFrame, np.ndarray]) – The input samples to predict.

Returns:

The predicted class labels.

Return type:

np.ndarray

predict_proba(X: pandas.DataFrame | numpy.ndarray) numpy.ndarray[source]

Predicts class probabilities for samples in X.

Parameters:

X (Union[pd.DataFrame, np.ndarray]) – The input samples.

Returns:

The class probabilities of the input samples.

Return type:

np.ndarray

score(X: pandas.DataFrame | numpy.ndarray, y: pandas.Series | numpy.ndarray) float[source]

Returns the mean accuracy on the given test data and labels.

Parameters:
  • X (Union[pd.DataFrame, np.ndarray]) – Test samples.

  • y (Union[pd.Series, np.ndarray]) – True labels for X.

Returns:

Mean accuracy of self.predict(X) wrt. y.

Return type:

float

set_params(**parameters: Any) KNNWrapper[source]

Sets the parameters of this estimator.

Parameters:

**parameters (Any) – Estimator parameters.

Returns:

The instance with updated parameters.

Return type:

KNNWrapper