python实现RF,并计算特征重要性


随机森林模型的训练以及特征重要性

import xlrd
import csv
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.interpolate import spline  
#设置路径
path='/Users/kqq/Documents/postgraduate/烟叶原始光谱2017.4.7数字产地.csv'
#读取文件
df = pd.read_csv(path, header = 0)

#df.info()

#训练随机森林模型
from sklearn.cross_validation import train_test_split
from sklearn.ensemble import RandomForestClassifier
x, y = df.iloc[:, 1:].values, df.iloc[:, 0].values
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 0)
feat_labels = df.columns[1:]
forest = RandomForestClassifier(n_estimators=10000, random_state=0, n_jobs=-1)
forest.fit(x_train, y_train)


#打印特征重要性评分
importances = forest.feature_importances_
#indices = np.argsort(importances)[::-1]
imp=[]
for f in range(x_train.shape[1]):
    print(f + 1, feat_labels[f], importances[f])
    


#将打印的重要性评分copy到featureScore.xlsx中;plot特征重要性
#设置路径
path='/Users/kqq/Documents/postgraduate/实验分析图/featureScore.xlsx'
#打开文件
myBook=xlrd.open_workbook(path)
#查询工作表
sheet_1_by_index=myBook.sheet_by_index(0)
data=[]
for i in range(0,sheet_1_by_index.nrows):
    data.append(sheet_1_by_index.row_values(i))   
data=np.array(data)
X=data[:1,].ravel()
y=data[1:,]
plt.figure(1,figsize=(8, 4))
i=0
print(len(y))
while i<len(y):  
    #power_smooth = spline(X,y[i],xnew)    
    #plt.grid(True)
    plt.legend(loc='best')
    plt.plot(X,y[i],linewidth=1)
    plt.ylabel('Log(1/R)')
    plt.xlabel('wavelength(nm)')
    i=i+1
plt.legend(loc='best')
plt.savefig('/Users/kqq/Documents/postgraduate/实验图/featureScore', dpi=200) 
plt.show()

特征重要性的展示:


原始数据:点击打开链接 密码:71nu

特征重要性:点击打开链接  密码:53jl

  • 1
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值