# -*- 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()
基于随机森林的轴承寿命预测和故障诊断
猜你喜欢
转载自www.cnblogs.com/-lcy-/p/random_forest.html
今日推荐
周排行