RandomForestRegressor leads to KeyError: 'squared_error'

RandomForestRegressor leads to KeyError: 'squared_error'

我正在尝试使用随机森林预测加利福尼亚住房数据集中的房价。我不明白为什么我在这个简单的代码中得到 KeyError: 'squared_error'

from sklearn.datasets import fetch_california_housing
import sklearn.ensemble

housing = fetch_california_housing()
rfr = sklearn.ensemble.RandomForestRegressor(n_estimators=100,
                       max_depth=int(25),
                       max_features="auto",
                       n_jobs=-1,
                       oob_score = True,
                       min_samples_leaf=20,
                       criterion = 'squared_error')

rfr.fit(housing.data, housing.target)

错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/kratz/anaconda3/lib/python3.8/site-packages/sklearn/ensemble/_forest.py", line 387, in fit
    trees = Parallel(n_jobs=self.n_jobs, verbose=self.verbose,
  File "/home/kratz/anaconda3/lib/python3.8/site-packages/joblib/parallel.py", line 1054, in __call__
    self.retrieve()
  File "/home/kratz/anaconda3/lib/python3.8/site-packages/joblib/parallel.py", line 933, in retrieve
    self._output.extend(job.get(timeout=self.timeout))
  File "/home/kratz/anaconda3/lib/python3.8/multiprocessing/pool.py", line 771, in get
    raise self._value
  File "/home/kratz/anaconda3/lib/python3.8/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/home/kratz/anaconda3/lib/python3.8/site-packages/joblib/_parallel_backends.py", line 595, in __call__
    return self.func(*args, **kwargs)
  File "/home/kratz/anaconda3/lib/python3.8/site-packages/joblib/parallel.py", line 262, in __call__
    return [func(*args, **kwargs)
  File "/home/kratz/anaconda3/lib/python3.8/site-packages/joblib/parallel.py", line 262, in <listcomp>
    return [func(*args, **kwargs)
  File "/home/kratz/anaconda3/lib/python3.8/site-packages/sklearn/utils/fixes.py", line 222, in __call__
    return self.function(*args, **kwargs)
  File "/home/kratz/anaconda3/lib/python3.8/site-packages/sklearn/ensemble/_forest.py", line 169, in _parallel_build_trees
    tree.fit(X, y, sample_weight=curr_sample_weight, check_input=False)
  File "/home/kratz/anaconda3/lib/python3.8/site-packages/sklearn/tree/_classes.py", line 1247, in fit
    super().fit(
  File "/home/kratz/anaconda3/lib/python3.8/site-packages/sklearn/tree/_classes.py", line 350, in fit
    criterion = CRITERIA_REG[self.criterion](self.n_outputs_,
KeyError: 'squared_error'

这可能是由于您的环境中 scikit-learn 的版本。根据 docs for RandomForestRegressor criterion = 'squared_error' 是在 v1.0 中引入的,因此如果您有以前的版本,请改用 criterion='mse'

您可以使用 pip freeze 检查环境中库的版本;对于 scikit-learn,您还可以使用:

import sklearn
sklearn.__version__