| """ |
| ============================ |
| GridSearch within a session |
| ============================ |
| |
| This example demonstrates how to make a model selection in pipelines |
| for finding the best model parameter, using grid search. Two models |
| are compared, one "vanilla" model with model tuned via grid search. |
| """ |
|
|
| import os |
|
|
| import matplotlib.pyplot as plt |
| import seaborn as sns |
| from pyriemann.estimation import Covariances |
| from pyriemann.tangentspace import TangentSpace |
| from sklearn.linear_model import LogisticRegression |
| from sklearn.pipeline import Pipeline |
|
|
| from moabb.datasets import BNCI2014_001 |
| from moabb.evaluations import WithinSessionEvaluation |
| from moabb.paradigms import MotorImagery |
|
|
|
|
| |
| fmin = 8 |
| fmax = 35 |
| tmax = None |
|
|
| |
| subjects = [1] |
| |
| dataset = BNCI2014_001() |
|
|
| events = ["right_hand", "left_hand"] |
|
|
| paradigm = MotorImagery( |
| events=events, n_classes=len(events), fmin=fmin, fmax=fmax, tmax=tmax |
| ) |
|
|
| |
| path = os.path.join(str("Results")) |
| os.makedirs(path, exist_ok=True) |
|
|
| |
| |
| |
| |
| |
| |
|
|
| pipelines = {} |
| pipelines["VanillaEN"] = Pipeline( |
| steps=[ |
| ("Covariances", Covariances("cov")), |
| ("Tangent_Space", TangentSpace(metric="riemann")), |
| ( |
| "LogistReg", |
| LogisticRegression( |
| penalty="elasticnet", |
| l1_ratio=0.75, |
| intercept_scaling=1000.0, |
| solver="saga", |
| max_iter=1000, |
| ), |
| ), |
| ] |
| ) |
|
|
| pipelines["GridSearchEN"] = Pipeline( |
| steps=[ |
| ("Covariances", Covariances("cov")), |
| ("Tangent_Space", TangentSpace(metric="riemann")), |
| ( |
| "LogistReg", |
| LogisticRegression( |
| penalty="elasticnet", |
| l1_ratio=0.70, |
| intercept_scaling=1000.0, |
| solver="saga", |
| max_iter=1000, |
| ), |
| ), |
| ] |
| ) |
|
|
| |
| |
| |
|
|
| param_grid = {} |
| param_grid["GridSearchEN"] = {"LogistReg__l1_ratio": [0.15, 0.30, 0.45, 0.60, 0.75]} |
|
|
| |
| |
| |
| |
| |
|
|
| dataset.subject_list = dataset.subject_list[:1] |
| evaluation = WithinSessionEvaluation( |
| paradigm=paradigm, |
| datasets=dataset, |
| overwrite=True, |
| random_state=42, |
| hdf5_path=path, |
| n_jobs=-1, |
| save_model=True, |
| ) |
| result = evaluation.process(pipelines, param_grid) |
|
|
| |
| |
| |
| |
| |
|
|
| fig, axes = plt.subplots(1, 1, figsize=[8, 5], sharey=True) |
|
|
| sns.stripplot( |
| data=result, |
| y="score", |
| x="pipeline", |
| ax=axes, |
| jitter=True, |
| alpha=0.5, |
| zorder=1, |
| palette="Set1", |
| ) |
| sns.pointplot(data=result, y="score", x="pipeline", ax=axes, palette="Set1") |
| axes.set_ylabel("ROC AUC") |
|
|