使用SVM进行癫痫检测
背景:
这是一片论文的简单复现,只是还原思想,不知道是不是我看的不够仔细,我觉得论文说的也就是这样简单的操作
论文名称:Seizure prediction with spectral power of EEG using cost-sensitive support vector machines
有需要的同学可以好好看一下,论文中不仅仅只是检测,还有预测功能。
过程:
SVM Procces
代码:
一个单纯的SVM例子:
import numpy as np
import matplotlib.pyplot
from sklearn import svm
from numpy.fft import fft
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
#数据预处理
def datapro():
data = pd.read_csv(x_src, error_bad_lines=False,header=None)
label = pd.read_csv(y_src, error_bad_lines=False,header=None)
x = np.array(data.values)
print(x)
y = label.values
return x,y
if __name__ == '__main__':
x_src = 'training_data/C3_P3_CZ_PZ.csv'
y_src = 'training_data/data_y.csv'
x,y = datapro(x_src,y_src)
#train_test_split是交叉验证中常用的函数,功能是从样本中随机的按比例选取train data和testdata
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=1/3.) # 分割训练集和测试集
# 标准化
scaler = StandardScaler()
X_train_std = scaler.fit_transform(X_train)
X_test_std = scaler.transform(X_test)
# 交叉验证,调整参数
param_grid = {'C':[1e1,1e2,1e3, 5e3,1e4,5e4],
'gamma':[0.0001,0.0008,0.0005,0.008,0.005,]}
print(X_train_std.shape)
print(y_train.shape)
#cv 几叉验证
clf = GridSearchCV(svm.SVC(kernel= 'rbf',class_weight='balanced',max_iter=6000,tol=0.01,probability=True),param_grid,cv=10)
clf = clf.fit(X_train_std,y_train)
print(clf.best_estimator_)
print(clf.predict_proba(X_test_std))
#打分
clf.score(X_test_std,y_test)
y_pred = clf.predict(X_test_std)
#测试准确率
print(classification_report(y_test,y_pred))
print(confusion_matrix(y_test,y_pred))
"""
https://blog.csdn.net/akadiao/article/details/78788864
precision: 识别为此类中正确的样本所占比例
recall: 一类样本中被正确识别的概率
"""