在毕设中学习04——mat数据导入与预处理,ICA处理


import scipy.io as scio
import numpy as np
import mne
import matplotlib.pyplot as plt


#导入mat类型的原始数据,分别读取到训练集train和测试集tesr
path = '/Users/sgfile/Downloads/BaiduNetdisk/20220528毕业论文(半完成)/BCI大赛第二届第三组数据-运动想象EEG/dataset_BCIcomp1.mat'
path2 ='/Users/sgfile/Downloads/BaiduNetdisk/20220528毕业论文(半完成)/BCI大赛第二届第三组数据-运动想象EEG/labels_data_set_iii.mat'
raw_data_1 = scio.loadmat(path)
raw_data_2 = scio.loadmat(path2)

# 获取【训练集数据,训练集标签,测试集数据】
x_train = raw_data_1['x_train'].T   # 原本数据是1152,3,140转置后位140,3,1152
y_train = raw_data_1['y_train']
x_test = raw_data_1['x_test'].T  
# 获取【测试集标签】
y_test = raw_data_2['y_test']

#查看数据集可以 print(y_train, y_train.shape)
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)


# 获得events:140*3
events = np.zeros([140, 3], dtype=int)
print('打印events的类型:', type(events))
print('打印出训练集标签的第一个元素:', y_train[0][0])

for i in range(0, 140, 1):
    events[i, 0] = i
    events[i, 2] = label1[i][0]

# 事件标签,左1右2
events_id = dict(right_hand=1, left_hand=2)

# 创建epochs对象
sfreq1 = 128
ch_names1 = ['C3', 'CZ', 'C4']
ch_types1 = ['eeg', 'eeg', 'eeg']
info = mne.create_info(ch_names=ch_names1, ch_types=ch_types1, sfreq=sfreq1)

custom_epochs = mne.EpochsArray(raw_data, info, events, tmin=0,event_id=events_id)
# 打印epochs信息
print(custom_epochs)

# ICA处理
ori_custom_epochs=custom_epochs.copy()

ica = mne.preprocessing.ICA(n_components=3, random_state=97, method='fastica')
ica.fit(ori_custom_epochs)

# 绘制
_ = custom_epochs['right_hand'].average().plot(time_unit='s')
_ = ori_custom_epochs['right_hand'].average().plot(time_unit='s')
plt.show()

你可能感兴趣的:(在本科毕设中学习,python,机器学习,深度学习)