pulse2percept.model_selection

Model selection

optimizers BaseOptimizer, GridSearchOptimizer, FunctionMinimizer, NotFittedError, ParticleSwarmOptimizer
class pulse2percept.model_selection.BaseOptimizer(estimator, search_params, **params)[source]

Abstract base class for all optimizers.

New in version 0.7.

Parameters:
  • estimator (sklearn.base.estimator) – A scikit-learn estimator, such as a classifier or regressor, that contains fit and predict methods.
  • serch_params (dict) – Initial values of all search parameters.
  • has_loss_function ({False | True}) – If True, the estimator’s scoring function is really a loss function (where smaller values indicate better performance) rather than a true scoring function.
  • verbose ({False | True}) – If True, will print debug information.
fit(X, y=None, fit_params=None)[source]

Optimizes the values of the search parameters

Runs the optimizer to determine the optimal values of the search parameters. After optimization, two new attributes are available:

  • best_score: contains the best score achieved with optimal parameter values
  • best_params: a dict containing the optimal parameter values
Parameters:
  • X (array-like of shape (n_samples, n_features)) – Sample data.
  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.
  • fit_params (dict, optional) – Additional parameters that should be passed to the estimator’s fit method.
get_default_params()[source]

Return a dict of user-settable model parameters

is_fitted

A flag indicating whether the model has been fitted

predict(X)[source]

Predicts the labels of X

Uses the estimator’s predict method to predict the labels of X.

Parameters:X (array-like of shape (n_samples, n_features)) – Sample data.
Returns:y_pred (array-like of shape (n_samples,) or (n_samples, n_outputs)) – Predicted labels for X.
score(X, y, sample_weight=None)[source]

Return the score of the model on the data X

Uses the estimator’s score method to determine the model score.

Parameters:
  • X (array-like of shape (n_samples, n_features)) –
  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.
Returns:

score (float)

class pulse2percept.model_selection.FunctionMinimizer(estimator, search_params, **params)[source]

Loss function minimization

Function minimization using SciPy’s minimize to find the search_params that optimize an estimator’s score.

New in version 0.7.

Parameters:
  • estimator (sklearn.base.estimator) – A scikit-learn estimator, such as a classifier or regressor, that contains fit and predict methods.
  • serch_params (dict of tupels (lower bound, upper bound)) – Dictionary of tupels containing the lower and upper bound of values for each search parameter.
  • search_params_init (dict of floats, optional) – Initial values of all search parameters. If None, initialize to midpoint between lower and upper bounds
  • method (str, optional) – Solving method to use (e.g., ‘Nelder-Mead’, ‘Powell’, ‘L-BFGS-B’)
  • tol (float, optional) – Tolerance for termination. For detailed control, use solver-specific options.
  • options (dict, optional) – A dictionary of solver-specific options.
  • has_loss_function ({False | True}) – If True, the estimator’s scoring function is really a loss function (where smaller values indicate better performance) rather than a true scoring function.
  • verbose ({False | True}) – If True, will print debug information.
fit(X, y=None, fit_params=None)[source]

Optimizes the values of the search parameters

Runs the optimizer to determine the optimal values of the search parameters. After optimization, two new attributes are available:

  • best_score: contains the best score achieved with optimal parameter values
  • best_params: a dict containing the optimal parameter values
Parameters:
  • X (array-like of shape (n_samples, n_features)) – Sample data.
  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.
  • fit_params (dict, optional) – Additional parameters that should be passed to the estimator’s fit method.
get_default_params()[source]

Return a dict of user-settable model parameters

is_fitted

A flag indicating whether the model has been fitted

predict(X)[source]

Predicts the labels of X

Uses the estimator’s predict method to predict the labels of X.

Parameters:X (array-like of shape (n_samples, n_features)) – Sample data.
Returns:y_pred (array-like of shape (n_samples,) or (n_samples, n_outputs)) – Predicted labels for X.
score(X, y, sample_weight=None)[source]

Return the score of the model on the data X

Uses the estimator’s score method to determine the model score.

Parameters:
  • X (array-like of shape (n_samples, n_features)) –
  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.
Returns:

score (float)

class pulse2percept.model_selection.GridSearchOptimizer(estimator, search_params, **params)[source]

Performs a grid search

Exhaustive search over specified parameter values for an estimator.

New in version 0.7.

Parameters:
  • estimator (sklearn.base.estimator) – A scikit-learn estimator, such as a classifier or regressor, that contains fit and predict methods.
  • serch_params (dict) – Dictionary of search parameters with a discrete number of values for each.
  • has_loss_function ({False | True}) – If True, the estimator’s scoring function is really a loss function (where smaller values indicate better performance) rather than a true scoring function.
  • verbose ({False | True}) – If True, will print debug information.
fit(X, y=None, fit_params=None)[source]

Optimizes the values of the search parameters

Runs the optimizer to determine the optimal values of the search parameters. After optimization, two new attributes are available:

  • best_score: contains the best score achieved with optimal parameter values
  • best_params: a dict containing the optimal parameter values
Parameters:
  • X (array-like of shape (n_samples, n_features)) – Sample data.
  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.
  • fit_params (dict, optional) – Additional parameters that should be passed to the estimator’s fit method.
get_default_params()[source]

Return a dict of user-settable model parameters

is_fitted

A flag indicating whether the model has been fitted

predict(X)[source]

Predicts the labels of X

Uses the estimator’s predict method to predict the labels of X.

Parameters:X (array-like of shape (n_samples, n_features)) – Sample data.
Returns:y_pred (array-like of shape (n_samples,) or (n_samples, n_outputs)) – Predicted labels for X.
score(X, y, sample_weight=None)[source]

Return the score of the model on the data X

Uses the estimator’s score method to determine the model score.

Parameters:
  • X (array-like of shape (n_samples, n_features)) –
  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.
Returns:

score (float)

exception pulse2percept.model_selection.NotFittedError[source]

Exception class used to raise if optimizer is used before fitting

This class inherits from both ValueError and AttributeError to help with exception handling and backward compatibility.

name

attribute name

obj

object

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

class pulse2percept.model_selection.ParticleSwarmOptimizer(estimator, search_params, **params)[source]

Performs particle swarm optimization

Optimizes the search parameter values using a particle swarm.

New in version 0.7.

Parameters:
  • estimator (sklearn.base.estimator) – A scikit-learn estimator, such as a classifier or regressor, that contains fit and predict methods.
  • serch_params (dict of tupels (lower bound, upper bound)) – Dictionary of tupels containing the lower and upper bound of values for each search parameter.
  • swarm_size (int, optional, default: 10 * number of search params) – The number of particles in the swarm.
  • max_iter (int, optional, default: 100) – Maximum number of iterations for the swarm to search.
  • min_func (float, optional, default: 0.01) – The minimum change of swarm’s best objective value before the search terminates.
  • min_step (float, optional, default: 0.01) – The minimum step size of swarm’s best objective value before the search terminates.
  • has_loss_function ({False | True}) – If True, the estimator’s scoring function is really a loss function (where smaller values indicate better performance) rather than a true scoring function.
  • verbose (bool, optional, default: True) – Flag whether to print more stuff
fit(X, y=None, fit_params=None)[source]

Optimizes the values of the search parameters

Runs the optimizer to determine the optimal values of the search parameters. After optimization, two new attributes are available:

  • best_score: contains the best score achieved with optimal parameter values
  • best_params: a dict containing the optimal parameter values
Parameters:
  • X (array-like of shape (n_samples, n_features)) – Sample data.
  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.
  • fit_params (dict, optional) – Additional parameters that should be passed to the estimator’s fit method.
get_default_params()[source]

Return a dict of user-settable model parameters

is_fitted

A flag indicating whether the model has been fitted

predict(X)[source]

Predicts the labels of X

Uses the estimator’s predict method to predict the labels of X.

Parameters:X (array-like of shape (n_samples, n_features)) – Sample data.
Returns:y_pred (array-like of shape (n_samples,) or (n_samples, n_outputs)) – Predicted labels for X.
score(X, y, sample_weight=None)[source]

Return the score of the model on the data X

Uses the estimator’s score method to determine the model score.

Parameters:
  • X (array-like of shape (n_samples, n_features)) –
  • y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.
Returns:

score (float)