model.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import numpy as np
  2. from sklearn.linear_model import LogisticRegression
  3. from sklearn.pipeline import make_pipeline
  4. from sklearn.base import BaseEstimator, TransformerMixin
  5. from sklearn.preprocessing import StandardScaler
  6. from mne.decoding import Vectorizer
  7. class ChannelScaler(BaseEstimator, TransformerMixin):
  8. def __init__(self, norm_axis=(0, 2)):
  9. self.channel_mean_ = None
  10. self.channel_std_ = None
  11. self.norm_axis=norm_axis
  12. def fit(self, X, y=None):
  13. '''
  14. :param X: 3d array with shape (n_epochs, n_channels, n_times)
  15. :param y:
  16. :return:
  17. '''
  18. self.channel_mean_ = np.mean(X, axis=self.norm_axis, keepdims=True)
  19. self.channel_std_ = np.std(X, axis=self.norm_axis, keepdims=True)
  20. return self
  21. def transform(self, X, y=None):
  22. X = X.copy()
  23. X -= self.channel_mean_
  24. X /= self.channel_std_
  25. return X
  26. def baseline_model(C=1.):
  27. return make_pipeline(
  28. Vectorizer(),
  29. StandardScaler(),
  30. LogisticRegression(C=C)
  31. )