Skip to content

Commit 7db7d03

Browse files
authored
Merge pull request #15 from arcondello/docs/tuning-example
Add a tuning example to the README
2 parents ee8e283 + 1ba6b42 commit 7db7d03

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ For an introduction to hybrid methods for feature selection, see the [Feature Se
1717

1818
## Examples
1919

20+
### Basic Usage
21+
2022
A minimal example of using the plugin to select 20 of 30 features of an `sklearn` dataset:
2123

2224
```python
@@ -42,6 +44,39 @@ The feature selector can be re-instantiated with a longer time limit.
4244
>>> X_new = SelectFromQuadraticModel(num_features=20, time_limit=200).fit_transform(X, y)
4345
```
4446

47+
### Tuning
48+
49+
You can use `SelectFromQuadraticModel` with scikit-learn's
50+
[hyper-parameter optimizers](https://scikit-learn.org/stable/modules/classes.html#hyper-parameter-optimizers).
51+
52+
For example, the number of features can be tuned using a grid search. **Please note that this will
53+
submit many problems to the hybrid solver.**
54+
55+
```python
56+
>>> import numpy as np
57+
...
58+
>>> from sklearn.datasets import load_breast_cancer
59+
>>> from sklearn.ensemble import RandomForestClassifier
60+
>>> from sklearn.model_selection import GridSearchCV
61+
>>> from sklearn.pipeline import Pipeline
62+
>>> from dwave.plugins.sklearn import SelectFromQuadraticModel
63+
...
64+
>>> X, y = load_breast_cancer(return_X_y=True)
65+
...
66+
>>> num_features = X.shape[1]
67+
>>> searchspace = np.linspace(1, num_features, num=5, dtype=int, endpoint=True)
68+
...
69+
>>> pipe = Pipeline([
70+
>>> ('feature_selection', SelectFromQuadraticModel()),
71+
>>> ('classification', RandomForestClassifier())
72+
>>> ])
73+
...
74+
>>> clf = GridSearchCV(pipe, param_grid=dict(feature_selection__num_features=searchspace))
75+
>>> search = clf.fit(X, y)
76+
>>> print(search.best_params_)
77+
{'feature_selection__num_features': 22}
78+
```
79+
4580
## Installation
4681

4782
To install the core package:

0 commit comments

Comments
 (0)