【问题标题】:ValueError in Random forest (Python)随机森林中的 ValueError (Python)
【发布时间】:2016-05-15 17:32:22
【问题描述】:

我正在尝试在 Python 中执行随机森林分析。一切似乎都很好,但是当我尝试运行代码时,我收到以下错误消息:

你们中有人得到这个 ValueError 吗?

干杯

数据集:https://www.dropbox.com/s/ehyccl8kubazs8x/test.csv?dl=0&preview=test.csv

代码:

from sklearn.ensemble import RandomForestRegressor as RF
import numpy as np
import pylab as pl


headers = file("test.csv").readline().strip().split('\r')[0].split(',')[1:]

data = np.loadtxt("test.csv", delimiter=',', skiprows=1, usecols = range(1,14))

#yellow==PAR, green==VPD, blue== Tsoil and orange==Tair
PAR  = data[:,headers.index("PAR")]
VPD  = data[:,headers.index("VPD")]
Tsoil= data[:,headers.index("Tsoil")]
Tair = data[:,headers.index("Tair")]

drivers = np.column_stack([PAR,VPD,Tsoil,Tair])

hour = data[:,-1].astype("int")


#performs a random forest hour-wise to explain each NEE, GPP and Reco fluxes
importances = np.zeros([24,2,3,4])

for ff,flux in enumerate(["NEE_f","GPP_f","Reco"]):
    fid = headers.index(flux)
    obs = data[:,fid]

    #store importances: dim are average/std; obs var; expl var


    for hh in range(24):
        mask = hour == hh
        forest = RF(n_estimators=1000)
        forest.fit(drivers[mask],obs[mask]) 


        importances[hh,0,ff] = forest.feature_importances_
        importances[hh,1,ff] = np.std([tree.feature_importances_ for tree in forest.estimators_],axis=0)

fig = pl.figure('importances',figsize=(15,5));fig.clf()
xx=range(24)

colors = ["#F0E442","#009E73","#56B4E9","#E69F00"];labels= ['PAR','VPD','Tsoil','Tair']
for ff,flux in enumerate(["NEE_f","GPP_f","Reco"]):
    ax = fig.add_subplot(1,3,ff+1)
    for vv in range(drivers.shape[1]):
        ax.fill_between(xx,importances[:,0,ff,vv]+importances[:,1,ff,vv],importances[:,0,ff,vv]-importances[:,1,ff,vv],color=colors[vv],alpha=.35,edgecolor="none")
        ax.plot(xx,importances[:,0,ff,vv],color=colors[vv],ls='-',lw=2,label = labels[vv])
        ax.set_title(flux);ax.set_xlim(0,23)
        if ff == 0:
            ax.legend(ncol=2,fontsize='medium',loc='upper center')
fig.show()
fig.savefig('importance-hourly.png')

【问题讨论】:

  • 复制您帖子中的代码。

标签: python csv error-handling random-forest


【解决方案1】:

问题是我选择了存储年数的列,而不是小时数。因此,RF 是在空数组上训练的。

【讨论】:

    猜你喜欢
    • 2018-04-10
    • 2021-05-29
    • 2013-06-26
    • 2015-02-12
    • 2017-01-13
    • 2020-08-08
    • 2017-08-25
    • 2016-07-23
    • 2021-03-30
    相关资源
    最近更新 更多