Closed toncho11 closed 2 years ago
Ok, I probably need to update bi2012. We can remove all references to HCBRClassifier, compressed_sensing and serialize and add it as an example. ^^'
It should be possible to add braininvaders2012
to the requirements of pyrieman-qiskit
, and import from braininvaders2012.dataset import BrainInvaders2012
.
So the objective is to use bi2012_quantum_classify.py
created by @gcattan and add QuantumClassifierWithDefaultRiemannianPipeline
#from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
#from sklearn.pipeline import Pipeline
from pyriemann_qiskit.datasets import get_bi2012_dataset
from pyriemann_qiskit.classification import QuantumClassifierWithDefaultRiemannianPipeline
bi2012 = get_bi2012_dataset()
pipe = QuantumClassifierWithDefaultRiemannianPipeline()
for (X, y) in bi2012:
assert X is not None and y is not None
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
pipe.fit(X_train, y_train)
pipe.score(X_test, y_test)
Should use StratifiedKfold instead of split function and use balanced accuracy for scoring.
from sklearn.metrics import balanced_accuracy_score
balanced_accuracy_score(y_true, y_pred)
Here is the first version of the code that uses the dataset bi2012 now part of MOABB. I am having problems testing due to some Python version incompatibility.
"""
====================================================================
ERP EEG decoding with Quantum Classifier.
====================================================================
"""
# Author: Anton Andreev
# Modified from plot_classify_EEG_tangentspace.py of pyRiemann
# License: BSD (3-clause)
from pyriemann.estimation import XdawnCovariances
from pyriemann.tangentspace import TangentSpace
from pyriemann_qiskit.classification import QuanticSVM
from pyriemann_qiskit.utils.filtering import NaiveDimRed
from pyriemann_qiskit.datasets import get_mne_sample
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import (confusion_matrix, ConfusionMatrixDisplay,
balanced_accuracy_score)
from matplotlib import pyplot as plt
import warnings
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from pyriemann.estimation import Xdawn, XdawnCovariances
from pyriemann.tangentspace import TangentSpace
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.pipeline import make_pipeline
import moabb
#from moabb.datasets import BNCI2014009
from moabb.datasets import bi2012, bi2013a, bi2014a, bi2014b, bi2015a, bi2015b
from moabb.evaluations import WithinSessionEvaluation
from moabb.paradigms import P300
from pyriemann_qiskit.classification import QuantumClassifierWithDefaultRiemannianPipeline
##############################################################################
# getting rid of the warnings about the future
warnings.simplefilter(action="ignore", category=FutureWarning)
warnings.simplefilter(action="ignore", category=RuntimeWarning)
moabb.set_log_level("info")
##############################################################################
# This is an auxiliary transformer that allows one to vectorize data
# structures in a pipeline For instance, in the case of an X with dimensions
# Nt x Nc x Ns, one might be interested in a new data structure with
# dimensions Nt x (Nc.Ns)
class Vectorizer(BaseEstimator, TransformerMixin):
def __init__(self):
pass
def fit(self, X, y):
"""fit."""
return self
def transform(self, X):
"""transform. """
return np.reshape(X, (X.shape[0], -1))
##############################################################################
# Create Pipelines
# ----------------
#
# Pipelines must be a dict of sklearn pipeline transformer.
pipelines = {}
##############################################################################
# We have to do this because the classes are called 'Target' and 'NonTarget'
# but the evaluation function uses a LabelEncoder, transforming them
# to 0 and 1
labels_dict = {"Target": 1, "NonTarget": 0}
paradigm = P300(resample=128)
datasets = [bi2012()] #bi2012, bi2013a, bi2014a, bi2014b, bi2015a, bi2015b
overwrite = True # set to True if we want to overwrite cached results
pipelines["Quantum+Riemannian"] = QuantumClassifierWithDefaultRiemannianPipeline()
evaluation = WithinSessionEvaluation(
paradigm=paradigm, datasets=datasets, suffix="examples", overwrite=overwrite
)
results = evaluation.process(pipelines)
Hi, difficult to say. Can you raise a draft PR so we could download and test your example?
[alpha, b, support] = optimize_svm(kernel_matrix, labels)
File C:\Work\python38\lib\site-packages\qiskit\aqua\utils\qp_solver.py:59 in optimize_svm
raise NameError("The CVXPY package is required to use the "
NameError: The CVXPY package is required to use the optimize_svm() function. You can install it with 'pip install qiskit-aqua[cvx]'.
a specifc version of which python library? I can not test because of that.
I am on python 3.8 my scipy version is 1.7.3 and scikit-learn = 1.0.1
Related to #39
This has been completed.
Rework the classification of BrainInvaders2012 or simply add a new example for the BrainInvaders2012 dataset.