随机森林分类器抛出错误“DataFrame”对象没有属性“_validate_params”

问题描述 投票:0回答:1

This is the dataset im using and some code.

我一直在从事一个机器学习项目,当我尝试使用随机森林分类进行训练时,它抛出了此“DataFrame”对象没有属性“_validate_params”错误。有人能帮我解决这个问题吗?输出错误是here

我的列名称中有空格,之前显示过类似的错误。现在我已经用下划线替换了空格,现在就抛出这个错误了。

rf = RandomForestClassifier
rf.fit(X_train, y_train)

使用上面数据的代码会引发错误。

    AttributeError                            Traceback (most recent call last)
/var/folders/94/3rhbwt5n0s5fbvc_2gk50dwm0000gn/T/ipykernel_62767/1137608719.py in ?()
      3 glaucomaDF.head()
      4 # training the randomforestclassifier model.
      5 
      6 rf = RandomForestClassifier
----> 7 rf.fit(X_train, y_train)

~/Desktop/projects/medical_webapp/medical_env/lib/python3.11/site-packages/sklearn/base.py in ?(estimator, *args, **kwargs)
   1141                 fit_method.__name__ == "partial_fit" and _is_fitted(estimator)
   1142             )
   1143 
   1144             if not global_skip_validation and not partial_fit_and_fitted:
-> 1145                 estimator._validate_params()
   1146 
   1147             with config_context(
   1148                 skip_parameter_validation=(

~/Desktop/projects/medical_webapp/medical_env/lib/python3.11/site-packages/pandas/core/generic.py in ?(self, name)
   6200             and name not in self._accessors
   6201             and self._info_axis._can_hold_identifiers_and_holds_name(name)
   6202         ):
   6203             return self[name]
-> 6204         return object.__getattribute__(self, name)

AttributeError: 'DataFrame' object has no attribute '_validate_params'
python machine-learning random-forest
1个回答
0
投票

您的错误在于如何初始化 RandomForestClassifier:

考虑一下:

from sklearn.ensemble import RandomForestClassifier
import numpy as np
X = np.random.rand(100, 3)
y = np.array([np.random.choice([0, 1]) for x in range(100)])
rf = RandomForestClassifier
rf.fit(X, y)

这基本上会给你错误(除了 numpy 数组):

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/leo/.venvs/base/lib/python3.11/site-packages/sklearn/base.py", line 1145, in wrapper
    estimator._validate_params()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'numpy.ndarray' object has no attribute '_validate_params'

其实应该是

from sklearn.ensemble import RandomForestClassifier
import numpy as np
X = np.random.rand(100, 3)
y = np.array([np.random.choice([0, 1]) for x in range(100)])
rf = RandomForestClassifier()
rf.fit(X, y)
© www.soinside.com 2019 - 2024. All rights reserved.