Skip to content

Commit 9037721

Browse files
committed
Examples in SSPOC and SensorSelector docstrings
1 parent 9789ac2 commit 9037721

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

pysensors/classification/_sspoc.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,30 @@ class SSPOC(BaseEstimator):
9797
9898
Examples
9999
--------
100-
TODO
100+
>>> from sklearn.metrics import accuracy_score
101+
>>> from sklearn.datasets import make_classification
102+
>>> from pysensors.classification import SSPOC
103+
>>>
104+
>>> x, y = make_classification(n_classes=3, n_informative=3, random_state=10)
105+
>>>
106+
>>> model = SSPOC(n_sensors=10, l1_penalty=0.03)
107+
>>> model.fit(x, y, quiet=True)
108+
SSPOC(basis=Identity(n_basis_modes=100),
109+
classifier=LinearDiscriminantAnalysis(), l1_penalty=0.03, n_sensors=10)
110+
>>> print(model.selected_sensors)
111+
[10 13 6 19 17 16 15 14 12 11]
112+
>>>
113+
>>> acc = accuracy_score(y, model.predict(x[:, model.selected_sensors]))
114+
>>> print("Accuracy:", acc)
115+
Accuracy: 0.66
116+
>>>
117+
>>> model.update_sensors(n_sensors=5, xy=(x, y), quiet=True)
118+
>>> print(model.selected_sensors)
119+
[10 13 6 19 17]
120+
>>>
121+
>>> acc = accuracy_score(y, model.predict(x[:, model.selected_sensors]))
122+
>>> print("Accuracy:", acc)
123+
Accuracy: 0.6
101124
"""
102125

103126
def __init__(
@@ -120,13 +143,7 @@ def __init__(
120143
self.n_basis_modes = None
121144

122145
def fit(
123-
self,
124-
x,
125-
y,
126-
quiet=False,
127-
prefit_basis=False,
128-
refit=True,
129-
**optimizer_kws,
146+
self, x, y, quiet=False, prefit_basis=False, refit=True, **optimizer_kws,
130147
):
131148
"""
132149
Fit the SSPOC model, determining which sensors are relevant.
@@ -327,8 +344,7 @@ def update_sensors(
327344
)
328345
if (
329346
method(
330-
np.abs(self.sensor_coef_[sorted_sensors[-1], :]),
331-
**method_kws,
347+
np.abs(self.sensor_coef_[sorted_sensors[-1], :]), **method_kws,
332348
)
333349
== 0
334350
and warn

pysensors/pysensors.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,26 @@ class SensorSelector(BaseEstimator):
5757
5858
Examples
5959
--------
60-
TODO
60+
>>> import numpy as np
61+
>>> from pysensors import SensorSelector
62+
>>>
63+
>>> x = np.linspace(0, 1, 501)
64+
>>> monomials = np.vander(x, 15).T
65+
>>>
66+
>>> model = SensorSelector(n_sensors=5)
67+
>>> model.fit(monomials)
68+
SensorSelector(basis=Identity(n_basis_modes=15), n_sensors=5, optimizer=QR())
69+
>>> print(model.selected_sensors)
70+
[500 377 0 460 185]
71+
>>> print(x[model.selected_sensors])
72+
[1. 0.754 0. 0.92 0.37 ]
73+
>>> model.set_n_sensors(7)
74+
>>> print(x[model.selected_sensors])
75+
[1. 0.754 0. 0.92 0.37 0.572 0.134]
76+
>>> f = np.sin(3*x)
77+
>>> f_pred = model.predict(f[model.selected_sensors])
78+
>>> print(np.linalg.norm(f - f_pred))
79+
0.022405698005838044
6180
"""
6281

6382
def __init__(self, basis=None, optimizer=None, n_sensors=None):
@@ -386,9 +405,7 @@ def score(self, x, y=None, score_function=None, score_kws={}, solve_kws={}):
386405
)
387406
else:
388407
return score_function(
389-
x,
390-
self.predict(x[:, sensors], **solve_kws),
391-
**score_kws,
408+
x, self.predict(x[:, sensors], **solve_kws), **score_kws,
392409
)
393410

394411
def reconstruction_error(self, x_test, sensor_range=None, score=None, **solve_kws):

0 commit comments

Comments
 (0)