eFF
eFF

Reputation: 277

ValueError in Random forest (Python)

I am trying to perform a Random Forest analysis in Python. Everything seems OK but, when I try to run the code, I get the following error message:

enter image description here

Did any of you get this ValueError?

Cheers

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

Code:

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')

Upvotes: 0

Views: 141

Answers (1)

eFF
eFF

Reputation: 277

The problem was that I selected the column where years are stored, not where hours are. Therefore the RF was trained on empty arrays.

Upvotes: 2

Related Questions