import tensorflow as tf
import numpy as np
import wfdb
import pandas as pd
import ast
from matplotlib import pyplot as plt
import keras.backend as K
def load_raw_data(df, sampling_rate, path):
if sampling_rate == 100:
data = [wfdb.rdsamp(path+f) for f in df.filename_lr]
else:
data = [wfdb.rdsamp(path+f) for f in df.filename_hr]
data = np.array([signal for signal, meta in data])
return data
path = ''
sampling_rate = 100
# load and convert annotation data
Y = pd.read_csv('ptbxl_database.csv', index_col='ecg_id')
Y.scp_codes = Y.scp_codes.apply(lambda x: ast.literal_eval(x))
# Load raw signal data
X = load_raw_data(Y, sampling_rate, path)
# Load scp_statements.csv for diagnostic aggregation
agg_df = pd.read_csv('scp_statements.csv', index_col=0)
agg_df = agg_df[agg_df.diagnostic == 1]
def aggregate_diagnostic(y_dic):
tmp = []
for key in y_dic.keys():
if key in agg_df.index:
tmp.append(agg_df.loc[key].diagnostic_class)
return list(set(tmp))
# Apply diagnostic superclass
Y['diagnostic_superclass'] = Y.scp_codes.apply(aggregate_diagnostic)
# Split data into train and test
test_fold = 10
# Train
X_train = X[np.where(Y.strat_fold != test_fold)]
y_train = Y[(Y.strat_fold != test_fold)].diagnostic_superclass
# Test
X_test = X[np.where(Y.strat_fold == test_fold)]
y_test = Y[Y.strat_fold == test_fold].diagnostic_superclass
# tenserflow 训练
print(X_test , y_test)
print(type(y_test))
print(type(X_test))
# <class 'pandas.core.series.Series'>
# <class 'numpy.ndarray'>
#该数据类型
y_train = y_train.astype(int[2])
y_test = y_test.astype(int[2])
X_train = tf.convert_to_tensor(X_train)
X_test = tf.convert_to_tensor(X_test)
# artificial intelligence and the training
model = tf.keras.models.Sequential([
# 卷积层
tf.keras.layers.Conv1D(filters = 96,
kernel_size = 3,
padding = "same"),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.Activation("relu"),
tf.keras.layers.AveragePooling1D(pool_size = 4,
padding = "valid"),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128,activation = "sigmoid",
kernel_regularizer = tf.keras.regularizers.l1()),
tf.keras.layers.Dense(5, activation = "sigmoid",
kernel_regularizer = tf.keras.regularizers.l2())
])
model.compile(optimizer="nadam",
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics = ["sparse_categorical_accuracy"])
model.fit(X_train,y_train,
batch_size = 64,
epochs = 10000,
validation_data = (X_test, y_test),
validation_freq = 1)
history = model.fit(X_train,y_train,
batch_size = 64,
epochs = 10000,
validation_data = (X_test, y_test))
model.summary()
# 可视化
acc=history.history['sparse_categorical_accuaracy']
val_acc = history.history['val_sparse_categorical_accuracy']
loss=history.history['loss']
val_loss = history.history['val_loss']
plt.subplot(1,2,1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.title('Training and Validation Accurcy')
plt.legend()
plt.subplot(1,2,1)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()
这段代码的输出是
[[[-0.145 -0.097 0.049 ... -0.452 -0.269 -0.032]
[-0.115 -0.083 0.032 ... -0.339 -0.246 -0.041]
[-0.064 -0.06 0.005 ... -0.188 -0.199 -0.04 ]
...
[ 0.23 0.292 0.063 ... 0.443 0.362 0.222]
[ 0.251 0.319 0.067 ... 0.498 0.405 0.261]
[ 0.338 0.361 0.023 ... 0.515 0.447 0.299]]
[[ 0.182 0.196 0.013 ... 0.413 0.279 0.284]
[ 0.23 0.309 0.079 ... 0.524 0.4 0.402]
[ 0.125 -0.092 -0.217 ... -0.002 0.022 0.181]
...
[-0.076 -0.085 -0.009 ... -0.02 -0.01 0.032]
[-0.048 -0.067 -0.019 ... 0.007 0.002 0.008]
[-0.04 -0.053 -0.013 ... 0.032 0.022 0.015]]
[[ 0.036 0.025 -0.011 ... 0.082 0.067 -0.014]
[ 0.054 0.044 -0.01 ... 0.11 0.091 0.003]
[ 0.095 0.09 -0.005 ... 0.143 0.123 0.035]
...
[-0.261 -0.018 0.242 ... -0.039 -0.066 -0.082]
[-0.049 -0.039 0.01 ... -0.043 -0.064 -0.2 ]
[ 0.071 -0.047 -0.118 ... -0.049 -0.067 -0.082]]
...
[[-0.045 -0.055 -0.01 ... -0.145 -0.165 -0.124]
[-0.045 -0.055 -0.01 ... -0.142 -0.161 -0.119]
[-0.042 -0.056 -0.013 ... -0.136 -0.154 -0.111]
...
[ 0.03 0.147 0.117 ... -0.038 -0.101 -0.116]
[ 0.059 0.158 0.099 ... -0.001 -0.09 -0.107]
[ 0.076 0.158 0.082 ... 0.017 -0.077 -0.098]]
[[-0.059 -0.023 0.036 ... 0.096 0.031 -0.014]
[-0.033 0.007 0.04 ... 0.127 0.056 0.007]
[-0.004 0.036 0.04 ... 0.184 0.108 0.041]
...
[-0.029 -0.029 0. ... -0.072 -0.123 -0.173]
[-0.07 -0.063 0.007 ... -0.115 -0.119 -0.224]
[-0.015 -0.048 -0.033 ... -0.121 -0.121 -0.209]]
[[-0.031 -0.001 0.03 ... 0.048 0.024 -0.001]
[-0.015 0.015 0.03 ... 0.051 0.024 0. ]
[-0.007 0.018 0.025 ... 0.064 0.021 -0.002]
...
[-0.109 -0.054 0.055 ... -0.057 -0.082 -0.09 ]
[-0.091 -0.067 0.024 ... 0.003 -0.047 -0.085]
[ 0.013 -0.237 -0.249 ... 0.336 0.267 0.112]]] ecg_id
9 [NORM]
38 [NORM]
40 [NORM]
57 [NORM]
59 [NORM]
...
21809 [NORM]
21812 [CD]
21818 [NORM]
21819 [CD]
21826 [MI]
Name: diagnostic_superclass, Length: 2198, dtype: object
<class 'pandas.core.series.Series'>
<class 'numpy.ndarray'>
但后面报ValueError: setting an array element with a sequence.该怎么处理啊