Skip to main content

PyPI CircleCI

D-Wave scikit-learn Plugin

This package provides a scikit-learn transformer for feature selection using a quantum-classical hybrid solver.

This plugin makes use of a Leap™ quantum-classical hybrid solver. Developers can get started by signing up for the Leap quantum cloud service for free. Those seeking a more collaborative approach and assistance with building a production application can reach out to D-Wave directly and also explore the feature selection offering in AWS Marketplace.

The package's main class, SelectFromQuadraticModel, can be used in any existing sklearn pipeline. For an introduction to hybrid methods for feature selection, see the Feature Selection for CQM.

Examples

Basic Usage

A minimal example of using the plugin to select 20 of 30 features of an sklearn dataset:

>>> from sklearn.datasets import load_breast_cancer
>>> from dwave.plugins.sklearn import SelectFromQuadraticModel
... 
>>> X, y = load_breast_cancer(return_X_y=True)
>>> X.shape
(569, 30)
>>> # solver can also be equal to "cqm"
>>> X_new = SelectFromQuadraticModel(num_features=20, solver="nl").fit_transform(X, y)
>>> X_new.shape
(569, 20)

For large problems, the default runtime may be insufficient. You can use the CQM solver's time_limit or Nonlinear (NL) solver's time_limit method to find the minimum accepted runtime for your problem; alternatively, simply submit as above and check the returned error message for the required runtime.

The feature selector can be re-instantiated with a longer time limit.

>>> # solver can also be equal to "nl"
>>> X_new = SelectFromQuadraticModel(num_features=20, time_limit=200, solver="cqm").fit_transform(X, y)

Tuning

You can use SelectFromQuadraticModel with scikit-learn's hyper-parameter optimizers.

For example, the number of features can be tuned using a grid search. Please note that this will submit many problems to the hybrid solver.

>>> import numpy as np
...
>>> from sklearn.datasets import load_breast_cancer
>>> from sklearn.ensemble import RandomForestClassifier
>>> from sklearn.model_selection import GridSearchCV
>>> from sklearn.pipeline import Pipeline
>>> from dwave.plugins.sklearn import SelectFromQuadraticModel
...
>>> X, y = load_breast_cancer(return_X_y=True)
...
>>> num_features = X.shape[1]
>>> searchspace = np.linspace(1, num_features, num=5, dtype=int, endpoint=True)
...
>>> # solver can also be equal to "cqm"
>>> pipe = Pipeline([
>>>   ('feature_selection', SelectFromQuadraticModel(solver="nl")),
>>>   ('classification', RandomForestClassifier())
>>> ])
...
>>> clf = GridSearchCV(pipe, param_grid=dict(feature_selection__num_features=searchspace))
>>> search = clf.fit(X, y)
>>> print(search.best_params_)
{'feature_selection__num_features': 22}

Installation

To install the core package:

pip install dwave-scikit-learn-plugin

License

Released under the Apache License 2.0

Contributing

Ocean's contributing guide has guidelines for contributing to Ocean packages.

Release Notes

dwave-scikit-learn-plugin makes use of reno to manage its release notes.

When making a contribution to dwave-scikit-learn-plugin that will affect users, create a new release note file by running

reno new your-short-descriptor-here

You can then edit the file created under releasenotes/notes/. Remove any sections not relevant to your changes. Commit the file along with your changes.

Metadata

Release files for dwave-scikit-learn-plugin 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dwave-scikit-learn-plugin 0.2.0
File Size Uploaded
dwave_scikit_learn_plugin-0.2.0.tar.gz 20.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dwave-scikit-learn-plugin 0.2.0
File Interpreter ABI Platform
dwave_scikit_learn_plugin-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 37.2 kB

Release files / dwave_scikit_learn_plugin-0.2.0.tar.gz

Download URL dwave_scikit_learn_plugin-0.2.0.tar.gz
Size 20.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1976acb97c740df46e50d2452ed1a04ded892c8bec77ad18901b69ec6117d869
BLAKE2b-256 checksum
How to use checksums
04084eb091c429b4ecdf92b7b435df209938a70c9aecda1d86e0bc216422d3b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release files / dwave_scikit_learn_plugin-0.2.0-py3-none-any.whl

Download URL dwave_scikit_learn_plugin-0.2.0-py3-none-any.whl
Size 16.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5b63e2c0aa8e1940c9ce56bbaf259dd2b1fa46b8b82fd5448175a0942ade9832
BLAKE2b-256 checksum
How to use checksums
910f7518d9973ebcf1c50fe56ba2ece1b1937566a39f2ee9ff5b1a0b99b4e08f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page