当前位置:   article > 正文

基于1D-CNN的齿轮故障诊断及TSNE可视化

基于1D-CNN的齿轮故障诊断及TSNE可视化

数据来自kaggle。

  1. #加载相关模块
  2. import pandas as pd
  3. import numpy as np
  4. import os
  5. import matplotlib.pyplot as plt
  6. import seaborn as sns
  7. #数据路径
  8. Directory='Gear Data\BrokenTooth'
  9. for root, dirs, files in os.walk(Directory):
  10. for i in range (len(files)):
  11. print(files[i])
  12. path = os.path.join(root,files[0])
  13. path
  14. df_temp = pd.read_csv(path)
  15. df_temp
  16. #时域波形
  17. plt.plot(df_temp.iloc[:,0])
  18. #设置标签
  19. load_col = [int(files[0][5:-4])/100 for j in range(len(df_temp))]
  20. lab='F'
  21. label_col = [lab for j in range(len(df_temp))]
  22. label_col
  23. df_temp['load']=load_col
  24. df_temp['fault']=label_col
  25. df_temp
  26. #数据集处理
  27. def MakeDataset(Directory,lab):
  28. df=pd.DataFrame(columns=['a1','a2','a3','a4'])
  29. for root, dirs, files in os.walk(Directory):
  30. for i in range (len(files)):
  31. path = os.path.join(root,files[i])
  32. df_temp = pd.read_csv(path)
  33. load_col = [int(files[i][5:-4])/100 for j in range(len(df_temp))]
  34. label_col = [lab for j in range(len(df_temp))]
  35. df_temp['load']=load_col
  36. df_temp['fault']=label_col
  37. df = pd.concat([df,df_temp],axis=0)
  38. print(path)
  39. return df
  40. #故障数据
  41. Directory='Gear Data\BrokenTooth'
  42. df_F = MakeDataset(Directory,lab='F')
  43. df_F
  44. #健康数据
  45. Directory='Gear Data\Healthy'
  46. df_H = MakeDataset(Directory,lab='H')
  47. df_H
  48. #将故障数据和健康数据利用concat函数进行连接并输出,便于后续模型使用
  49. df = pd.concat([df_F,df_H],axis=0)
  50. df.to_csv('Gear_Fault_data.csv',index=False)
  51. #数据归一化操作
  52. df = pd.read_csv('Gear_Fault_data.csv')
  53. from sklearn.preprocessing import StandardScaler
  54. scaler=StandardScaler()
  55. df.iloc[:,:-2]=scaler.fit_transform(df.iloc[:,:-2])
  56. ##为 CNN 创建数据集
  57. from sklearn.preprocessing import LabelEncoder
  58. from tensorflow.keras.utils import to_categorical
  59. win_len=100 #窗口长度
  60. stride=200 #移动步长
  61. X=[]
  62. Y=[]
  63. for k in ['F','H']:
  64. df_temp_1 = df[df['fault']==k]
  65. for j in (np.arange(0,1,0.1)):
  66. df_temp_2=df_temp_1[df_temp_1['load']==j]
  67. for i in np.arange(0,len(df_temp_2)-(win_len),stride):
  68. X.append(df_temp_2.iloc[i:i+win_len,:-1])
  69. Y.append(df_temp_2.iloc[i+win_len,-1])
  70. #训练数据
  71. X=np.array(X)
  72. X=X.reshape((X.shape[0],X.shape[1],X.shape[2],1))
  73. #X = np.repeat(X, 3, axis=3) # To repeat into 3 chanel format
  74. #标签
  75. Y=np.array(Y)
  76. encoder= LabelEncoder()
  77. encoder.fit(Y)
  78. encoded_Y = encoder.transform(Y)
  79. OHE_Y = to_categorical(encoded_Y)
  80. #训练集尺寸
  81. X.shape
  82. ##T-sne可视化
  83. X_pre_cnn = X.reshape(X.shape[0],X.shape[1]*X.shape[2])
  84. from sklearn.manifold import TSNE
  85. X_t_sne = TSNE(n_components=2, learning_rate='auto',verbose=1, perplexity=40, n_iter=300).fit_transform(X_pre_cnn)
  86. tSNEdf = pd.DataFrame(data = X_t_sne, columns = ['t-SNE component 1', 't-SNE component 2'])
  87. tSNEdf['Fault']=Y
  88. #绘制2个主成分
  89. fig, ax = plt.subplots(figsize=(7,7))
  90. sns.scatterplot(x=tSNEdf['t-SNE component 1'],y=tSNEdf['t-SNE component 2'],hue='Fault',
  91. data=tSNEdf,
  92. legend="full",
  93. alpha=0.3)
  94. plt.show()
  95. #训练集和测试集划分
  96. from sklearn.model_selection import train_test_split
  97. X_train,X_test,y_train,y_test = train_test_split(X,OHE_Y,test_size=0.3,shuffle=True)
  98. #构建CNN模型
  99. from tensorflow.keras.models import Sequential,Model
  100. from tensorflow.keras.layers import Input,Dense, Dropout, Flatten
  101. from tensorflow.keras.layers import Conv2D, MaxPooling2D
  102. no_classes = 2 #2个类别
  103. cnn_model = Sequential()
  104. cnn_model.add(Conv2D(32, kernel_size=(20, 3),activation='relu',input_shape=(X.shape[1],X.shape[2],1),padding='same'))
  105. cnn_model.add(MaxPooling2D((20, 2),strides=(5, 5),padding='same'))
  106. cnn_model.add(Conv2D(64, (10, 3), activation='relu',padding='same'))
  107. cnn_model.add(MaxPooling2D(pool_size=(10, 2),strides=(3, 3),padding='same'))
  108. cnn_model.add(Flatten())
  109. cnn_model.add(Dense(128, activation='relu'))
  110. cnn_model.add(Dense(no_classes, activation='softmax'))
  111. cnn_model.summary()
  112. cnn_model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])
  113. #设置训练参数并训练CNN
  114. batch_size = 128
  115. epochs = 5
  116. history = cnn_model.fit(X_train, y_train, batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(X_test,y_test),shuffle=True)
  117. #保存模型
  118. cnn_model.save('CNN_model_gear.h5')
  119. ##模型性能计算
  120. def inv_Transform_result(y_pred):
  121. y_pred = y_pred.argmax(axis=1)
  122. y_pred = encoder.inverse_transform(y_pred)
  123. return y_pred
  124. #预测
  125. y_pred=cnn_model.predict(X_test)
  126. Y_pred=inv_Transform_result(y_pred)
  127. Y_test = inv_Transform_result(y_test)
  128. from sklearn.metrics import confusion_matrix
  129. #混淆矩阵
  130. plt.figure(figsize=(5,5))
  131. cm = confusion_matrix(Y_test, Y_pred)
  132. f = sns.heatmap(cm, annot=True, fmt='d',xticklabels=encoder.classes_,yticklabels=encoder.classes_)
  133. plt.show()
  134. #输出可视化
  135. dummy_cnn = Model(inputs=cnn_model.input,outputs=cnn_model.layers[5].output)
  136. y_viz = dummy_cnn.predict(X_train)
  137. y_viz.shape
  138. from sklearn.manifold import TSNE
  139. X_t_sne = TSNE(n_components=2, learning_rate='auto',verbose=1, perplexity=40, n_iter=300).fit_transform(y_viz)
  140. tSNEdf = pd.DataFrame(data = X_t_sne, columns = ['principal component 1', 'principal component 2'])
  141. tSNEdf['Fault']=inv_Transform_result(y_train)
  142. # 绘制两个主成分分量
  143. fig, ax = plt.subplots(figsize=(10,10))
  144. sns.scatterplot(x=tSNEdf['principal component 1'],y=tSNEdf['principal component 2'],hue='Fault',
  145. data=tSNEdf,
  146. legend="full",
  147. alpha=0.3)
  148. plt.show()
  149. #Flatten层可视化
  150. dummy_cnn = Model(inputs=cnn_model.input,outputs=cnn_model.layers[4].output)
  151. y_viz = dummy_cnn.predict(X_train)
  152. from sklearn.manifold import TSNE
  153. X_t_sne = TSNE(n_components=2, learning_rate='auto',verbose=1, perplexity=40, n_iter=300).fit_transform(y_viz)
  154. tSNEdf = pd.DataFrame(data = X_t_sne, columns = ['t-SNE component 1', 't-SNE component 2'])
  155. tSNEdf['Fault']=inv_Transform_result(y_train)
  156. # 绘制两个主成分
  157. fig, ax = plt.subplots(figsize=(7,7))
  158. sns.scatterplot(x=tSNEdf['t-SNE component 1'],y=tSNEdf['t-SNE component 2'],hue='Fault',
  159. data=tSNEdf,
  160. legend="full",
  161. alpha=0.3)
  162. plt.show()

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