随机林中的ValueError(Python)

2024-05-16 02:35:39 发布

您现在位置:Python中文网/ 问答频道 /正文

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

enter image description here

你们有谁得到这个错误吗?你知道吗

干杯

数据集: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')

Tags: infordataindexnpaxheadersff
1条回答
网友
1楼 · 发布于 2024-05-16 02:35:39

问题是我选择了存储年份的列,而不是存储小时的列。因此,射频被训练在空阵列上。你知道吗

相关问题 更多 >