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
12 changes: 10 additions & 2 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):
Copy link
Collaborator

@PicoCentauri PicoCentauri May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these operattions also be performed for a List and not only for a numpy array? Something like

if isinstance(self.initialize, np.ndarray) or isinstance(self.initialize, list):

Even though there is probably a shorter way to check of an instance belongs to either one of two classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Let me know what you think about the new changes I made.

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 @@ -1053,6 +1060,7 @@ def _init_greedy_search(self, X, y, n_to_select):
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