-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added KNeighborsTransformer, without docs for the moment.
- Loading branch information
Showing
8 changed files
with
156 additions
and
16 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
skfda/preprocessing/dim_reduction/_neighbor_transforms.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any, Literal, TypeVar, Union, overload | ||
|
||
from scipy.sparse import csr_matrix | ||
from sklearn.neighbors import KNeighborsTransformer as _KNeighborsTransformer | ||
|
||
from skfda._utils._sklearn_adapter import InductiveTransformerMixin | ||
|
||
from ..._utils._neighbors_base import AlgorithmType, KNeighborsMixin | ||
from ...misc.metrics import l2_distance | ||
from ...representation import FData | ||
from ...typing._metric import Metric | ||
from ...typing._numpy import NDArrayFloat | ||
|
||
InputBound = Union[NDArrayFloat, FData] | ||
Input = TypeVar("Input", contravariant=True, bound=InputBound) | ||
|
||
|
||
class KNeighborsTransformer( | ||
KNeighborsMixin[Input, Any], | ||
InductiveTransformerMixin[Input, NDArrayFloat, Any], | ||
): | ||
|
||
@overload | ||
def __init__( | ||
self: KNeighborsTransformer[NDArrayFloat], | ||
*, | ||
mode: Literal["connectivity", "distance"] = "distance", | ||
n_neighbors: int = 5, | ||
algorithm: AlgorithmType = 'auto', | ||
leaf_size: int = 30, | ||
metric: Literal["precomputed"], | ||
n_jobs: int | None = None, | ||
) -> None: | ||
pass | ||
|
||
@overload | ||
def __init__( | ||
self: KNeighborsTransformer[InputBound], | ||
*, | ||
mode: Literal["connectivity", "distance"] = "distance", | ||
n_neighbors: int = 5, | ||
algorithm: AlgorithmType = 'auto', | ||
leaf_size: int = 30, | ||
n_jobs: int | None = None, | ||
) -> None: | ||
pass | ||
|
||
@overload | ||
def __init__( | ||
self, | ||
*, | ||
mode: Literal["connectivity", "distance"] = "distance", | ||
n_neighbors: int = 5, | ||
algorithm: AlgorithmType = 'auto', | ||
leaf_size: int = 30, | ||
metric: Metric[Input] = l2_distance, | ||
n_jobs: int | None = None, | ||
) -> None: | ||
pass | ||
|
||
# Not useless, it restricts parameters | ||
def __init__( # noqa: WPS612 | ||
self, | ||
*, | ||
mode: Literal["connectivity", "distance"] = "distance", | ||
n_neighbors: int = 5, | ||
algorithm: AlgorithmType = 'auto', | ||
leaf_size: int = 30, | ||
metric: Literal["precomputed"] | Metric[Input] = l2_distance, | ||
n_jobs: int | None = None, | ||
) -> None: | ||
self.mode = mode | ||
super().__init__( | ||
n_neighbors=n_neighbors, | ||
algorithm=algorithm, | ||
leaf_size=leaf_size, | ||
metric=metric, | ||
n_jobs=n_jobs, | ||
) | ||
|
||
def _init_estimator(self) -> _KNeighborsTransformer: | ||
|
||
return _KNeighborsTransformer( | ||
mode=self.mode, | ||
n_neighbors=self.n_neighbors, | ||
algorithm=self.algorithm, | ||
leaf_size=self.leaf_size, | ||
metric="precomputed", | ||
n_jobs=self.n_jobs, | ||
) | ||
|
||
def transform( | ||
self, | ||
X: Input, | ||
) -> csr_matrix: | ||
self._check_is_fitted() | ||
add_one = self.mode == "distance" | ||
return self.kneighbors_graph( | ||
X, | ||
mode=self.mode, | ||
n_neighbors=self.n_neighbors + add_one, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters