当前位置:   article > 正文

简单的基于深度学习的时间序列异常检测(Python)

简单的基于深度学习的时间序列异常检测(Python)
  1. #importing required libraries
  2. import pandas as pd
  3. import numpy as np
  4. import tensorflow as tf
  5. import matplotlib.pyplot as plt
  6. from keras.layers import Input, Dense
  7. from keras.models import Model
  8. from sklearn.metrics import precision_recall_fscore_support
  9. #Load data and perform required operations to clean data ready for further processing
  10. data= pd.read_csv('ambient_temperature_system_failure.csv')
  11. #Exclude datatime column
  12. data_values= data.drop('timestamp' , axis=1).values
  13. #convert data to float type
  14. data_values= data_values.astype('float32')
  15. #create new dataframe with converted values
  16. data_converted= pd.DataFrame(data_values, columns=data.columns[1:])
  17. #Add back datetime column
  18. data_converted.insert(0, 'timestamp', data['timestamp'])
  19. #Remove NAN values from dataset
  20. data_converted= data_converted.dropna()
  21. # Exclude datetime column again
  22. data_tensor = tf.convert_to_tensor(data_converted.drop('timestamp', axis=1).values, dtype=tf.float32)
  23. # Define the autoencoder model
  24. input_dim = data_converted.shape[1] - 1
  25. encoding_dim = 10
  26. input_layer = Input(shape=(input_dim,))
  27. encoder = Dense(encoding_dim, activation='relu')(input_layer)
  28. decoder = Dense(input_dim, activation='relu')(encoder)
  29. autoencoder = Model(inputs=input_layer, outputs=decoder)
  30. # Compile and fit the model
  31. autoencoder.compile(optimizer='adam', loss='mse')
  32. autoencoder.fit(data_tensor, data_tensor, epochs=100, batch_size=32, shuffle=True)
  33. # Calculate the reconstruction error for each data point
  34. reconstructions = autoencoder.predict(data_tensor)
  35. mse = tf.reduce_mean(tf.square(data_tensor - reconstructions), axis=1)
  36. anomaly_scores = pd.Series(mse.numpy(), name='anomaly_scores')
  37. anomaly_scores.index = data_converted.index
  38. #define anomaly detection threshold & assess the model’s effectiveness using precision
  39. threshold = anomaly_scores.quantile(0.99)
  40. anomalous = anomaly_scores > threshold
  41. binary_labels = anomalous.astype(int)
  42. precision, recall,f1_score, _ = precision_recall_fscore_support(binary_labels, anomalous, average='binary')
  43. test= data_converted['value'].values
  44. predictions = anomaly_scores.values
  45. print("Precision: " , precision)
  46. print("Recall: " , recall)
  47. print("F1 Score: " , f1_score)
  48. #Visualizing Anomaly results
  49. # Plot the data with anomalies marked in red
  50. plt.figure(figsize=(8, 8))
  51. plt.plot(data_converted['timestamp'], data_converted['value'])
  52. plt.plot(data_converted['timestamp'][anomalous], data_converted['value'][anomalous], 'ro')
  53. plt.title('Anomaly Detection')
  54. plt.xlabel('Time')
  55. plt.ylabel('Value')
  56. plt.show()

知乎学术咨询:https://www.zhihu.com/consult/people/792359672131756032?isMe=1

担任《Mechanical System and Signal Processing》等审稿专家,擅长领域:现代信号处理,机器学习,深度学习,数字孪生,时间序列分析,设备缺陷检测、设备异常检测、设备智能故障诊断与健康管理PHM等。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/998479
推荐阅读
相关标签
  

闽ICP备14008679号