使用python實現(xiàn)CNN-GRU故障診斷的代碼示例
要實現(xiàn)1DCNN-GRU進(jìn)行故障診斷,您可以使用以下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)練集標(biāo)簽數(shù)據(jù)
test_X = np.load('test_X.npy') # 加載測試集特征數(shù)據(jù)
test_Y = np.load('test_Y.npy') # 加載測試集標(biāo)簽數(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)練過程的準(zhǔ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()在測試集上進(jìn)行預(yù)測并計算準(zhǔn)確率和混淆矩陣:
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)準(zhǔn)備好訓(xùn)練集和測試集的數(shù)據(jù)(train_X.npy、train_Y.npy、test_X.npy和test_Y.npy)。這只是一個簡單示例,您可能需要根據(jù)您的數(shù)據(jù)集的特點進(jìn)行必要的調(diào)整,例如輸入信號的形狀、類別數(shù)量和標(biāo)簽格式等。
希望對您有所幫助!如需更詳細(xì)或個性化的幫助,請?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)文章
詳解Python的Django框架中的模版相關(guān)知識
這篇文章主要介紹了Python的Django框架中的模版相關(guān)知識,模版的存在大大簡化了創(chuàng)作頁面時HTML的相關(guān)工作,需要的朋友可以參考下2015-07-07
python3獲取控制臺輸入的數(shù)據(jù)的具體實例
在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于python3獲取控制臺輸入的數(shù)據(jù)的具體實例內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-08-08
Python Selenium XPath根據(jù)文本內(nèi)容查找元素的方法
這篇文章主要介紹了Python Selenium XPath根據(jù)文本內(nèi)容查找元素的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

