使用python實現(xiàn)CNN-GRU故障診斷的代碼示例
要實現(xiàn)1DCNN-GRU進行故障診斷,您可以使用以下Python代碼作為參考:
首先,導(dǎo)入所需的庫:
import numpy as np import tensorflow as tf from tensorflow.keras.layers import Conv1D, MaxPooling1D, GlobalAveragePooling1D, GRU, Dense from tensorflow.keras.models import Sequential from sklearn.metrics import classification_report, confusion_matrix import matplotlib.pyplot as plt import seaborn as sns
加載訓(xùn)練集和測試集的數(shù)據(jù):
train_X = np.load('train_X.npy') # 加載訓(xùn)練集特征數(shù)據(jù)
train_Y = np.load('train_Y.npy') # 加載訓(xùn)練集標簽數(shù)據(jù)
test_X = np.load('test_X.npy') # 加載測試集特征數(shù)據(jù)
test_Y = np.load('test_Y.npy') # 加載測試集標簽數(shù)據(jù)定義模型結(jié)構(gòu):
model = Sequential() model.add(Conv1D(64, 3, activation='relu', input_shape=train_X.shape[1:])) model.add(MaxPooling1D(2)) model.add(Conv1D(128, 3, activation='relu')) model.add(MaxPooling1D(2)) model.add(GRU(64, dropout=0.2, recurrent_dropout=0.2)) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
訓(xùn)練模型:
繪制訓(xùn)練過程的準確率和損失曲線:
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper right')
plt.show()在測試集上進行預(yù)測并計算準確率和混淆矩陣:
pred_Y = model.predict(test_X)
pred_Y = np.round(pred_Y).flatten()
accuracy = np.mean(pred_Y == test_Y)
print("Test Accuracy: {:.2f}%".format(accuracy * 100))
cm = confusion_matrix(test_Y, pred_Y)
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", xticklabels=['Normal', 'Fault'], yticklabels=['Normal', 'Fault'])
plt.title("Confusion Matrix")
plt.xlabel("Predicted Labels")
plt.ylabel("True Labels")
plt.show()請確保您已經(jīng)準備好訓(xùn)練集和測試集的數(shù)據(jù)(train_X.npy、train_Y.npy、test_X.npy和test_Y.npy)。這只是一個簡單示例,您可能需要根據(jù)您的數(shù)據(jù)集的特點進行必要的調(diào)整,例如輸入信號的形狀、類別數(shù)量和標簽格式等。
希望對您有所幫助!如需更詳細或個性化的幫助,請?zhí)峁└嘞嚓P(guān)代碼和數(shù)據(jù)。
到此這篇關(guān)于使用python實現(xiàn)CNN-GRU故障診斷的文章就介紹到這了,更多相關(guān)python CNN-GRU故障診斷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Pandas的Series創(chuàng)建方式和常用屬性
這篇文章主要介紹了關(guān)于Pandas的Series創(chuàng)建方式和常用屬性,Series 數(shù)據(jù)結(jié)構(gòu)是用于儲存一個序列的一維數(shù)組,DataFrame 數(shù)據(jù)結(jié)構(gòu)是用于存儲復(fù)雜數(shù)據(jù)的二維數(shù)據(jù)結(jié)構(gòu),本文來詳細說明一下2023-07-07
詳解python如何正確使用時間戳,日期,時間,時區(qū)
這篇文章主要為大家介紹了如何在python中正確使用時間戳,日期,時間,時區(qū),文中通過簡單的示例進行了詳細介紹,希望對大家有一定的幫助2024-11-11
Python讀取大型數(shù)據(jù)文件的6種方式匯總
在 Python 中,我們可以使用多種方法讀取大型數(shù)據(jù)文件,本文主要為大家介紹6個常用的Python讀取大型數(shù)據(jù)文件的方法,希望對大家有所幫助2023-05-05
python性能檢測工具函數(shù)運行內(nèi)存及運行時間
這篇文章主要介紹了python性能檢測工具函數(shù)運行內(nèi)存及運行時間,python雖然是一門慢語言,但是也有著比較多的性能檢測工具來幫助我們優(yōu)化程序的運行效率,下文小編給大家分享五個性能檢測工具,需要的朋友可以參考一下2022-05-05

