Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing FPS to take numpy array of ints as initialize parameter #225

Merged
merged 14 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The rules for CHANGELOG file:

0.3.0 (XXXX/XX/XX)
------------------
- Updating ``FPS`` to allow a numpy array of ints as an initialize parameter (#145)
- Supported Python versions are now ranging from 3.9 - 3.12.

0.2.0 (2023/08/24)
Expand Down
17 changes: 9 additions & 8 deletions src/skmatter/_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ class _FPS(GreedySelector):
Parameters
----------

initialize: int, list of int, or 'random', default=0
initialize: int, list of int, numpy.ndarray of int, or 'random', default=0
Index of the first selection(s). If 'random', picks a random
value when fit starts. Stored in :py:attr:`self.initialize`.

Expand Down Expand Up @@ -1038,7 +1038,14 @@ def _init_greedy_search(self, X, y, n_to_select):
self.hausdorff_ = np.full(X.shape[self._axis], np.inf)
self.hausdorff_at_select_ = np.full(X.shape[self._axis], np.inf)

if self.initialize == "random":
if isinstance(self.initialize, (np.ndarray, list)):
if all(isinstance(i, numbers.Integral) for i in self.initialize):
for i, val in enumerate(self.initialize):
self.selected_idx_[i] = val
self._update_post_selection(X, y, self.selected_idx_[i])
else:
raise ValueError("Invalid value of the initialize parameter")
elif self.initialize == "random":
random_state = check_random_state(self.random_state)
initialize = random_state.randint(X.shape[self._axis])
self.selected_idx_[0] = initialize
Expand All @@ -1047,12 +1054,6 @@ def _init_greedy_search(self, X, y, n_to_select):
initialize = self.initialize
self.selected_idx_[0] = initialize
self._update_post_selection(X, y, self.selected_idx_[0])
elif isinstance(self.initialize, list) and all(
[isinstance(i, numbers.Integral) for i in self.initialize]
):
for i, val in enumerate(self.initialize):
self.selected_idx_[i] = val
self._update_post_selection(X, y, self.selected_idx_[i])
else:
raise ValueError("Invalid value of the initialize parameter")

Expand Down
2 changes: 1 addition & 1 deletion src/skmatter/feature_selection/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FPS(_FPS):
Parameters
----------

initialize: int, list of int, or 'random', default=0
initialize: int, list of int, numpy.ndarray of int, or 'random', default=0
Index of the first selection(s). If 'random', picks a random
value when fit starts. Stored in :py:attr:`self.initialize`.

Expand Down
2 changes: 1 addition & 1 deletion src/skmatter/sample_selection/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class FPS(_FPS):
Parameters
----------

initialize: int, list of int, or 'random', default=0
initialize: int, list of int, numpy.ndarray of int, or 'random', default=0
Index of the first selection(s). If 'random', picks a random
value when fit starts. Stored in :py:attr:`self.initialize`.

Expand Down
26 changes: 26 additions & 0 deletions tests/test_feature_simple_fps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest

import numpy as np
from sklearn.datasets import load_diabetes as get_dataset
from sklearn.utils.validation import NotFittedError

Expand Down Expand Up @@ -42,6 +43,31 @@ def test_initialize(self):
for i in range(4):
self.assertEqual(selector.selected_idx_[i], self.idx[i])

initialize = np.array(self.idx[:4])
with self.subTest(initialize=initialize):
selector = FPS(n_to_select=len(self.idx) - 1, initialize=initialize)
selector.fit(self.X)
for i in range(4):
self.assertEqual(selector.selected_idx_[i], self.idx[i])

initialize = np.array([1, 5, 3, 0.25])
with self.subTest(initialize=initialize):
with self.assertRaises(ValueError) as cm:
selector = FPS(n_to_select=len(self.idx) - 1, initialize=initialize)
selector.fit(self.X)
self.assertEqual(
str(cm.exception), "Invalid value of the initialize parameter"
)

initialize = np.array([[1, 5, 3], [2, 4, 6]])
with self.subTest(initialize=initialize):
with self.assertRaises(ValueError) as cm:
selector = FPS(n_to_select=len(self.idx) - 1, initialize=initialize)
selector.fit(self.X)
self.assertEqual(
str(cm.exception), "Invalid value of the initialize parameter"
)

with self.assertRaises(ValueError) as cm:
selector = FPS(n_to_select=1, initialize="bad")
selector.fit(self.X)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_sample_simple_fps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest

import numpy as np
from sklearn.datasets import load_diabetes as get_dataset
from sklearn.utils.validation import NotFittedError

Expand Down Expand Up @@ -43,6 +44,31 @@ def test_initialize(self):
for i in range(4):
self.assertEqual(selector.selected_idx_[i], self.idx[i])

initialize = np.array(self.idx[:4])
with self.subTest(initialize=initialize):
selector = FPS(n_to_select=len(self.idx) - 1, initialize=initialize)
selector.fit(self.X)
for i in range(4):
self.assertEqual(selector.selected_idx_[i], self.idx[i])

initialize = np.array([1, 5, 3, 0.25])
with self.subTest(initialize=initialize):
with self.assertRaises(ValueError) as cm:
selector = FPS(n_to_select=len(self.idx) - 1, initialize=initialize)
selector.fit(self.X)
self.assertEqual(
str(cm.exception), "Invalid value of the initialize parameter"
)

initialize = np.array([[1, 5, 3], [2, 4, 6]])
with self.subTest(initialize=initialize):
with self.assertRaises(ValueError) as cm:
selector = FPS(n_to_select=len(self.idx) - 1, initialize=initialize)
selector.fit(self.X)
self.assertEqual(
str(cm.exception), "Invalid value of the initialize parameter"
)

with self.assertRaises(ValueError) as cm:
selector = FPS(n_to_select=1, initialize="bad")
selector.fit(self.X)
Expand Down
Loading