首页 > 其他 > 详细

基于随机森林的轴承寿命预测和故障诊断

时间:2020-03-21 03:27:00      阅读:287      评论:0      收藏:0      [点我收藏+]
# -*- coding: utf-8 -*-
"""
Created on Mon Mar  9 13:24:07 2020

@author: Administrator
"""
from scipy.io import loadmat
from sklearn.ensemble import RandomForestRegressor
from sklearn.neural_network import MLPRegressor
from sklearn.svm import SVR
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from sklearn.metrics import r2_score
from sklearn import metrics

mpl.rcParams[font.sans-serif] = [KaiTi]
mpl.rcParams[font.serif] = [KaiTi]         #解决matplotlib无法显示中文的问题
plt.rcParams[axes.unicode_minus]=False       #解决负数坐标显示问题
# In[2] 原始29特征 +随机森林
data=loadmat(original_data.mat)
train_data=data[train_data]
train_label=data[train_label].reshape(-1,1)# 数据集变一列 (-1,n)变n列
test_data=data[test_data]
test_label=data[test_label].reshape(-1,1)
# reshape(1,-1) 数据集变一行 ,(n,-1):变n行
rf1 = RandomForestRegressor(max_depth=2, random_state=0)

rf1.fit (train_data,train_label.ravel())
test_pred1=rf1.predict(test_data).reshape(-1,1)
plt.figure()
plt.plot(test_label,-*,label=true)
plt.plot(test_pred1,-p,label=preict)
plt.title(29特征+随机森林)
plt.xlabel(测试样本)
plt.ylabel(剩余寿命/h)
plt.legend()
plt.show()

# 计算各种指标
print(原始29特征+随机森林)
# mape
test_mape=np.mean(np.abs((test_pred1-test_label)/test_label))
# rmse
test_rmse=np.sqrt(np.mean(np.square(test_pred1-test_label)))
# mae
test_mae=np.mean(np.abs(test_pred1-test_label))
# R2
test_r2=r2_score(test_label,test_pred1)
print(原始29特征+随机森林测试集的mape:,test_mape, rmse:,test_rmse, mae:,test_mae, R2:,test_r2)


# In[3] KPCA+随机森林回归

data=loadmat(kpca_data.mat)
train_data=data[train_data]
train_label=data[train_label].reshape(-1,1)
test_data=data[test_data]
test_label=data[test_label].reshape(-1,1)

rf2 = RandomForestRegressor(max_depth=10, random_state=0)

rf2.fit(train_data,train_label.ravel()) # ravel 多维变一维
test_pred2=rf2.predict(test_data).reshape(-1,1)
plt.figure()
plt.plot(test_label,-*,label=true)
plt.plot(test_pred2,-p,label=preict)
plt.title(KPCA+随机森林)
plt.xlabel(测试样本)
plt.ylabel(剩余寿命/h)
plt.legend()
plt.show()

print(kpca+随机森林)

# mape
test_mape=np.mean(np.abs((test_pred2-test_label)/test_label))
# rmse
# test_rmse=np.sqrt(np.mean(np.square(test_pred2-test_label)))
test_rmse=np.sqrt(metrics.mean_squared_error(test_label,test_pred2))
# mae
test_mae=np.mean(np.abs(test_pred2-test_label))
# R2
test_r2=r2_score(test_label,test_pred2)
print(KPCA+随机森林测试集的mape:,test_mape, rmse:,test_rmse, mae:,test_mae, R2:,test_r2)

# In[4] kpca+SVR回归
rf3 = SVR(C=100,gamma=1)
rf3.fit (train_data,train_label.ravel())
test_pred3=rf3.predict(test_data).reshape(-1,1)
plt.figure()
plt.plot(test_label,-*,label=true)
plt.plot(test_pred3,-p,label=preict)
plt.title(KPCA+SVR)
plt.xlabel(测试样本)
plt.ylabel(剩余寿命/h)
plt.legend()
plt.show()

print(kpca+SVR)
# mape
test_mape=np.mean(np.abs((test_pred3-test_label)/test_label))
# rmse
test_rmse=np.sqrt(np.mean(np.square(test_pred3-test_label)))
# mae
test_mae=np.mean(np.abs(test_pred3-test_label))
# R2
test_r2=r2_score(test_label,test_pred3)
print(KPCA+SVR测试集的mape:,test_mape, rmse:,test_rmse, mae:,test_mae, R2:,test_r2)



# In[] 画在一起
plt.figure()
plt.plot(test_label,-*,label=true)
plt.plot(test_pred1,-p,label=随机森林)
plt.plot(test_pred2,-s,label=KPCA+随机森林)
plt.plot(test_pred3,-o,label=KPCA+SVR)
plt.title(各种对比)
plt.xlabel(测试样本)
plt.ylabel(剩余寿命/h)
plt.legend()
plt.show()

 

基于随机森林的轴承寿命预测和故障诊断

原文:https://www.cnblogs.com/-lcy-/p/random_forest.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!