RandomForestRegressor leads to KeyError: 'squared_error'

I am trying to predict house prices in the Cali housing data set with a random forest. I do not understand why I get a KeyError: 'squared_error' in this simple code:

from sklearn.datasets import fetch_california_housing
import sklearn.ensemble

housing = fetch_california_housing()
rfr = sklearn.ensemble.RandomForestRegressor(n_estimators=100,
                       oob_score = True,
                       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__
  File "/home/kratz/anaconda3/lib/python3.8/site-packages/joblib/parallel.py", line 933, in retrieve
  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
  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'

1 answer

  • answered 2022-04-30 23:44 Japsz

    It's probably due to the version of scikit-learn in your environment. According to the docs for RandomForestRegressor criterion = 'squared_error' was introduced in v1.0, so if you have a prior version use criterion='mse' instead.

    You can use pip freeze to check for the version of your libraries in your env; for scikit-learn you can also use:

    import sklearn

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum