随机林中的ValueError(Python)

随机林中的ValueError(Python),python,csv,error-handling,random-forest,Python,Csv,Error Handling,Random Forest,我正在尝试用Python执行随机林分析。一切似乎都正常,但当我尝试运行代码时,会收到以下错误消息: 你们中有人有这个错误吗 干杯 数据集: 代码: 问题是我选择了存储年份的列,而不是存储小时的列。因此RF是在空阵列上训练的。问题是我选择了存储年份的列,而不是存储小时的列。因此,RF是在空阵列上训练的。复制帖子中的代码。复制帖子中的代码。 from sklearn.ensemble import RandomForestRegressor as RF import numpy as np imp

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

你们中有人有这个错误吗

干杯

数据集:

代码:


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

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

复制帖子中的代码。复制帖子中的代码。
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')