Python深度学习:脑电图癫痫发作检测

学习脑电癫痫分类的时候在网上看到一个用python做深度学习的,拿过来学习一下。
网址http://dy.163.com/v2/article/detail/EEC68EH5054281P3.html

在运行的时候出现一个问题:
only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

思考后修改程序:

#加载机器学习数据集
data = "data.csv"
import pandas as pd 
df = pd.read_csv(data, header=0, index_col=0)

#探索机器学习数据集
df.head()
df.info()

#将目标变量转换为癫痫(y列编码为1)与非癫痫(2-5)
df["seizure"] = 0 
for i in range(11500): 
    if df["y"][i] == 1: 
        df["seizure"][i] = 1 
    else:
        df["seizure"][i] = 0
        
#绘制并观察一些脑电波
import matplotlib.pyplot as plt 
# plotting an epileptic wave form 
plt.plot(range(178), df.iloc[11496,0:178]) 
plt.show()

#将把数据准备成神经网络可以接受的形式。首先解析数据,然后标准化值,最后创建目标数组
# create df1 which only contains the waveform data points 
df1 = df.drop(["seizure", "y"], axis=1) #删除两列
# 1. parse the data 
import numpy as np 
wave = np.zeros((11500, 178)) 

z=0
for index, row in df1.iterrows():
    #row = pd.DataFrame(row,dtype=np.float64)
    wave[z,:] = row
    z +=1

# print the wave.shape to make sure we parsed the data correctly 
print(wave.shape) 
# 2. standardize the data such that it has mean of 0 and standard deviation of 1 
mean = wave.mean(axis=0) 
wave -= mean 
std = wave.std(axis=0) 
wave /= std 
# 3. create the target numpy array 
target = df["seizure"].values

#使用Keras构建了一个具有正则化和dropout的dense 网络,以减少过度拟合
from keras.models import Sequential 
from keras import layers 
from keras import regularizers 
model = Sequential() 
model.add(layers.Dense(64, activation="relu", kernel_regularizer=regularizers.l1(0.001), input_shape = (178,))) 
model.add(layers.Dropout(0.5))
model.add(layers.Dense(64, activation="relu", kernel_regularizer=regularizers.l1(0.001))) 
model.add(layers.Dropout(0.5)) 
model.add(layers.Dense(1, activation="sigmoid")) 
model.summary()

#sklearn的train_test_split函数帮助我们创建训练和测试集。
from sklearn.model_selection import train_test_split 
x_train, x_test, y_train, y_test = train_test_split(wave, target, test_size=0.2, random_state=42)

#编译机器学习模型并训练它100个epochs。
model.compile(optimizer="rmsprop", loss="binary_crossentropy", metrics=["acc"]) 
history = model.fit(x_train, y_train, epochs=100, batch_size=128, validation_split=0.2, verbose=2)

#将模型释放到测试集中,绘制ROC曲线,计算AUC。
from sklearn.metrics import roc_curve, auc 
y_pred = model.predict(x_test).ravel() 
fpr_keras, tpr_keras, thresholds_keras = roc_curve(y_test, y_pred) 
AUC = auc(fpr_keras, tpr_keras) 
plt.plot(fpr_keras, tpr_keras, label='Keras Model(area = {:.3f})'.format(AUC)) 
plt.xlabel('False positive Rate') 
plt.ylabel('True positive Rate') 
plt.title('ROC curve') 
plt.legend(loc='best') 
plt.show()

运行成功

你可能感兴趣的:(Python深度学习:脑电图癫痫发作检测)