无法在随机森林回归器 python sklearn 上调用 fit 函数



我无法在 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()

智能感知内容: [1]: https://i.stack.imgur.com/XweOo.png

回溯调用

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

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

根据文档:

oob_score:布尔值,可选(默认值=假(

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

相关内容

最新更新