MNE example《Motor imagery decoding from EEG data using the Common Spatial Pattern (CSP)》

MNE example《Motor imagery decoding from EEG data using the Common Spatial Pattern (CSP)》(个人理解,example地址)

根据源地址所示代码,本人的一些理解和疑惑,欢迎一起讨论,同时希望大佬指点迷津 😃

Anaconda上代码——
import numpy as np
import matplotlib.pyplot as plt

from sklearn.pipeline import Pipeline
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.model_selection import ShuffleSplit, cross_val_score

from mne import Epochs, pick_types, events_from_annotations
from mne.channels import make_standard_montage
from mne.io import concatenate_raws, read_raw_edf
from mne.datasets import eegbci
from mne.decoding import CSP

print(__doc__)

# #############################################################################
# # Set parameters and read data

# avoid classification of evoked responses by using epochs that start 1s after
# cue onset.
tmin, tmax = -1., 4.
event_id = dict(hands=2, feet=3)
subject = 1
runs = [6, 10, 14]  # motor imagery: hands vs feet

raw_fnames = eegbci.load_data(subject, runs)
raw = concatenate_raws([read_raw_edf(f, preload=True) for f in raw_fnames])

print('>>>>>>>>>>>>>', raw.ch_names)

eegbci.standardize(raw)  # set channel names
montage = make_standard_montage('standard_1005')
raw.set_montage(montage)

# strip channel names of "." characters
print('***********', raw.ch_names)
raw.rename_channels(lambda x: x.strip('.'))

# Apply band-pass filter
raw.filter(7., 30., fir_design='firwin', skip_by_annotation='edge')

events, _ = events_from_annotations(raw, event_id=dict(T1=2, T2=3))
# print('event shape: ', len(events))  # >>>45: 15*3 (3 is default)

picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False,
                   exclude='bads')

# Read epochs (train will be done only between 1 and 2s)
# Testing will be done with a running classifier
epochs = Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks,
                baseline=None, preload=True)
epochs_train = epochs.copy().crop(tmin=1., tmax=2.)  # 划分训练集
labels = epochs.events[:, -1] - 2  # 将标签归一化为0或1,原标签为3或2



# Define a monte-carlo cross-validation generator (reduce variance):'蒙特卡罗交叉验证生成器'
scores = []
epochs_data = epochs.get_data()
epochs_data_train = epochs_train.get_data()  # train data
cv = ShuffleSplit(10, test_size=0.2, random_state=42)  # 划分测试集,rate=0.2即20%,随机种子42
cv_split = cv.split(epochs_data_train)  # 划分train set data 

# Assemble a classifier
lda = LinearDiscriminantAnalysis()  # an instantiaction of the LinearDiscriminantAnalysis
csp = CSP(n_components=4, reg=None, log=
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值