无法在randomforest回归器python sklearn上调用fit函数

时间:2018-07-05 22:35:30

标签: python-3.x scikit-learn random-forest

我无法在RandomForestRegressor上调用fit函数,甚至智能感知也仅显示预测和其他一些参数。下面是我的代码,回溯调用和显示智能感知内容的图像。

import pandas
import numpy as np
from sklearn.model_selection import KFold
from sklearn.ensemble import RandomForestRegressor
def predict():
    Fvector = 'C:/Users/Oussema/Desktop/Cred_Data/VEctors/FinalFeatureVector.csv'
    data = np.genfromtxt(Fvector, dtype=float, delimiter=',', names=True)
    AnnotArr = np.array(data['CredAnnot']) #this is a 1D array containig   the ground truth (50000 rows)
    TempTestArr = np.array([data['GrammarV'],data['TweetSentSc'],data['URLState']]) #this is the features vector the shape is (3,50000) the values range is [0-1]
    FeatureVector = TempTestArr.transpose() #i used the transpose method to get the shape (50000,3)
    RF_model = RandomForestRegressor(n_estimators=20, max_features = 'auto', n_jobs = -1)
    RF_model.fit(FeatureVector,AnnotArr)
    print(RF_model.oob_score_)
predict()

Intelisense内容:     [1]:https://i.stack.imgur.com/XweOo.png

追踪呼叫

Traceback (most recent call last):
File "C:\Users\Oussema\source\repos\Regression_Models\Regression_Models\Random_forest_TCA.py", line 15, in <module>
predict()
File "C:\Users\Oussema\source\repos\Regression_Models\Regression_Models\Random_forest_TCA.py", line 14, in predict
print(RF_model.oob_score_)
AttributeError: 'RandomForestRegressor' object has no attribute 'oob_score_'

1 个答案:

答案 0 :(得分:0)

初始化RandomForestRegressor时,需要将oob_score参数设置为True

根据the documentation

  

oob_score:bool,可选(默认= False)

whether to use out-of-bag samples to estimate the R^2 on unseen data.

因此,只有在执行以下操作时,属性oob_score_才可用:

def predict():
    ....
    ....
    RF_model = RandomForestRegressor(n_estimators=20, 
                                     max_features = 'auto', 
                                     n_jobs = -1, 
                                     oob_score=True)  #<= This is what you want
    ....
    ....
    print(RF_model.oob_score_)