Невозможно вызвать функцию подгонки для случайного леса регрессора python sklearn - PullRequest
0 голосов
/ 06 июля 2018

Я не могу вызвать функцию подгонки для RandomForestRegressor, и даже intellisense показывает только прогноз и некоторые другие параметры. Ниже мой код, вызов traceback и изображение, показывающее содержание intellisense.

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 голосов
/ 06 июля 2018

Вам необходимо установить параметр oob_score на True при инициализации RandomForestRegressor.

Согласно документации :

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_)
...