1D CNN处理振动数据实现轴承不同故障诊断-python代码

2. 代码详解

2.1 导入相关库

from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Embedding
from keras.layers import Conv1D, GlobalAveragePooling1D, MaxPooling1D
import numpy as np
import keras
import xlrd
import tensorflow as tf
from keras.utils import plot_model
from keras.callbacks import TensorBoard
from keras import regularizers

from time import time
import matplotlib.pyplot as plt
from matplotlib import offsetbox
from sklearn import (manifold, datasets, decomposition, ensemble,
discriminant_analysis, random_projection)


2.2 读取数据

def excel2train(path):
data = xlrd.open_workbook(path)
table = data.sheets()[0]
nrows = table.nrows  # 行数
ncols = table.ncols  # 列数
datamatrix1 = np.zeros((40000,1))
datamatrix = np.zeros((nrows, ncols))
for x in range(ncols): #按列存储
cols = table.col_values(x) #取第x列，返回list型数据
# np.array创建数组，传递的实参一定是数组，不是tuple。例如，np.array((1,2,3)) # wrong, np.array([1,2,3]), # right
cols1 = np.array(cols)  # 把list转换为矩阵进行矩阵操作
datamatrix[:, x] = cols1 # 把数据进行存储
# 取前40000个做训练数据，只取第一列，也就是x方向振动数据进行建模
datamatrix1 = datamatrix[0:40000,0]
return datamatrix1

def excel2test(path):
data = xlrd.open_workbook(path)
table = data.sheets()[0]
nrows = table.nrows  # 行数
ncols = table.ncols  # 列数
datamatrix1 = np.zeros((10000,1))
datamatrix = np.zeros((nrows, ncols))
for x in range(ncols):
cols = table.col_values(x)
cols1 = np.array(cols)  # 把list转换为矩阵进行矩阵操作
datamatrix[:, x] = cols1 # 把数据进行存储
datamatrix1 = datamatrix[40000:50000,0] #取第1列，后10000个点建模
return datamatrix1


path1 = 'E:/xxxxxxxxxx' #数据路径
traindata_rollfault = excel2train(path1 + '/滚动体故障/RollFault1000.xlsx')
traindata_innerfault = excel2train(path1 + '/内圈故障/InnerFault1000.xlsx')
traindata_inner_outer_fault = excel2train(path1 + '/内圈+外圈混合故障/InnerOutterFault1000.xlsx')
traindata_normal = excel2train(path1 + '/正常轴承/Normal1000.xlsx')
traindata = np.hstack([rawdata_rollfault, rawdata_innerfault, rawdata_inner_outer_fault, rawdata_normal])


#产生y标签-one-hot编码
x_train = rawdata.reshape(-1,50,10)
print(x_train.shape)
#每类故障的标签个数是80
y_train1 = keras.utils.to_categorical(np.random.randint(1, size=(80, 1)), num_classes=4)
print(y_train1.shape)
y_train2 = keras.utils.to_categorical(np.random.randint(1, size=(80, 1)), num_classes=3)
y_train2 =  np.insert(y_train2, 0, 0, axis=1)
print(y_train2.shape)
y_train3 = keras.utils.to_categorical(np.random.randint(1, size=(
• 7
点赞
• 75
收藏
觉得还不错? 一键收藏
• 45
评论
04-21 746
02-16 1339
03-26 1441
08-05 6426
03-07 5697

“相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。