import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import train_test_split
from tensorflow.keras import layers, losses
from tensorflow.keras.models import Model
dataframe = pd.read_csv('data\ecg.csv',header=None)
dataframe.head()
data = dataframe.values
data.shape
(4998, 141)
# The last column contains the labels
labels = data[:, -1]
features = data[:, 0:-1]
xtrain, xtest, ytrain, ytest = train_test_split(
features, labels, test_size=0.4, random_state=55
)
xval,xtest,yval,ytest=train_test_split(
xtest, ytest, test_size=0.5, random_state=56)
ytest.shape
(1000,)
Normalizing data using min max scalar
min_vals = tf.reduce_min(xtrain)
max_vals = tf.reduce_max(xtrain)
xtrain = (xtrain - min_vals) / (max_vals - min_vals)
xval = (xval - min_vals) / (max_vals - min_vals)
xtest = (xtest - min_vals) / (max_vals - min_vals)
xtrain = tf.cast(xtrain, tf.float32)
xtest = tf.cast(xtest, tf.float32)
xval=tf.cast(xval, tf.float32)
ytrain.shape
(2998,)
ytrain = ytrain.astype(bool)
ytest = ytest.astype(bool)
yval = yval.astype(bool)
normal_xtrain = xtrain[ytrain]
normal_xval = xval[yval]
anomalous_xtrain = xtrain[~ytrain]
anomalous_xval = xval[~yval]
plt.grid()
plt.plot(np.arange(140), normal_xtrain[0])
plt.title("Normal ECG")
plt.show()
plt.grid()
plt.plot(np.arange(140), anomalous_xtrain[0])
plt.title("Anomalous ECG")
plt.show()
class AnomalyDetector(Model):
def __init__(self):
super(AnomalyDetector, self).__init__()
self.encoder = tf.keras.Sequential([
layers.Dense(32, activation="relu"),
layers.Dense(8, activation="relu")])
self.decoder = tf.keras.Sequential([
layers.Dense(32, activation="relu"),
layers.Dense(140, activation="sigmoid")])
def call(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
autoencoder = AnomalyDetector()
autoencoder.compile(optimizer='nadam', loss='mae')
history = autoencoder.fit(normal_xtrain, normal_xtrain,
epochs=100,
batch_size=512,
validation_data=(xval, xval),
shuffle=True)
plt.plot(history.history["loss"], label="Training Loss")
plt.plot(history.history["val_loss"], label="Validation Loss")
plt.legend()
encoded_imgs = autoencoder.encoder(normal_xval).numpy()
decoded_imgs = autoencoder.decoder(encoded_imgs).numpy()
plt.plot(normal_xval[0],'b')
plt.plot(decoded_imgs[0],'r')
plt.fill_between(np.arange(140), decoded_imgs[0], normal_xval[0], color='yellow' )
plt.legend(labels=["Input", "Reconstruction", "Error"])
plt.show()
encoded_imgs = autoencoder.encoder(anomalous_xval).numpy()
decoded_imgs = autoencoder.decoder(encoded_imgs).numpy()
plt.plot(anomalous_xval[0],'b')
plt.plot(decoded_imgs[0],'r')
plt.fill_between(np.arange(140), decoded_imgs[0], anomalous_xval[0], color='yellow' )
plt.legend(labels=["Input", "Reconstruction", "Error"])
plt.show()
reconstructions = autoencoder.predict(normal_xtrain)
train_loss = tf.keras.losses.mae(reconstructions, normal_xtrain)
plt.hist(train_loss, bins=50)
plt.xlabel("Train loss")
plt.ylabel("No of examples")
plt.show()
reconstructions = autoencoder.predict(anomalous_xtrain)
test_loss = tf.keras.losses.mae(reconstructions, anomalous_xtrain)
plt.hist(test_loss, bins=50)
plt.xlabel("anamoly loss")
plt.ylabel("No of examples")
plt.show()
#From the above train loss and anomaly loss plots thresold(hyper parameter) is taken.
threshold = 0.03
reconstructions = autoencoder(xval)
loss = tf.keras.losses.mae(reconstructions, xval)
preds = tf.math.less(loss, threshold)
print("Accuracy = {}".format(accuracy_score(yval, preds)))
print("Precision = {}".format(precision_score(yval, preds)))
print("Recall = {}".format(recall_score(yval, preds)))
Accuracy = 0.967
Precision = 0.9878472222222222
Recall = 0.9563025210084034reconstructions = autoencoder(xtest)
loss = tf.keras.losses.mae(reconstructions, xtest)
preds = tf.math.less(loss, threshold)
print("Accuracy = {}".format(accuracy_score(ytest, preds)))
print("Precision = {}".format(precision_score(ytest, preds)))
print("Recall = {}".format(recall_score(ytest, preds)))
Accuracy = 0.963
Precision = 0.9857142857142858
Recall = 0.9500860585197934
工学博士,担任《Mechanical System and Signal Processing》《中国电机工程学报》《控制与决策》等期刊审稿专家,擅长领域:现代信号处理,机器学习,深度学习,数字孪生,时间序列分析,设备缺陷检测、设备异常检测、设备智能故障诊断与健康管理PHM等。