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

Unable to call the fit function on randomforest regressor python sklearn

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

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:\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_'

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

根据the documentation

oob_score : bool, optional (default=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_)