简体   繁体   English

在RandomForestRegressors列表上调用np.array将返回DecisionTreeRegressors数组

[英]Calling np.array on a list of RandomForestRegressors returns an array of DecisionTreeRegressors

When I try to convert a list of RandomForestRegressors to a numpy array, I get an array of Decision Trees. 当我尝试将RandomForestRegressors列表转换为numpy数组时,得到了决策树数组。 How do I get an array of RandomForestRegressors instead? 我如何获取RandomForestRegressors数组呢?

eg 例如

clf0=RandomForestRegressor()
clf1=RandomForestRegressor()

X = np.random.randn(10,1)
y = np.random.randn(10,1)

clf0.fit(X,y)
clf1.fit(X,y)

m = np.array( [clf0, clf1 ] )

m.shape

Out[1]: (2, 10)

I want m.shape to be (2,), not (2,10). 我希望m.shape是(2,),而不是(2,10)。 And it should contrain RandomForestRegressors, not DecisionTreeRegressors. 并且它应该禁忌RandomForestRegressors,而不是DecisionTreeRegressors。

This was recently answered on the scikit-learn mailing list: a random forest behaves as sequence of decision trees: 最近在scikit-learn邮件列表中回答了这个问题:随机森林表现为决策树序列

>>> len(clf0)
10
>>> clf0[:2]
[DecisionTreeRegressor(compute_importances=None, criterion='mse',
           max_depth=None, max_features='auto', max_leaf_nodes=None,
           min_density=None, min_samples_leaf=1, min_samples_split=2,
           random_state=1087328618, splitter='best'),
 DecisionTreeRegressor(compute_importances=None, criterion='mse',
           max_depth=None, max_features='auto', max_leaf_nodes=None,
           min_density=None, min_samples_leaf=1, min_samples_split=2,
           random_state=357905606, splitter='best')]

NumPy picks this up and converts the RFs themselves to arrays. NumPy接管此过程并将RF本身转换为阵列。 The workaround is easy: 解决方法很简单:

>>> m = np.empty(2, object)
>>> m[:] = clf0, clf1
>>> m
array([ RandomForestRegressor(bootstrap=True, compute_importances=None,
           criterion='mse', max_depth=None, max_features='auto',
           max_leaf_nodes=None, min_density=None, min_samples_leaf=1,
           min_samples_split=2, n_estimators=10, n_jobs=1, oob_score=False,
           random_state=None, verbose=0),
       RandomForestRegressor(bootstrap=True, compute_importances=None,
           criterion='mse', max_depth=None, max_features='auto',
           max_leaf_nodes=None, min_density=None, min_samples_leaf=1,
           min_samples_split=2, n_estimators=10, n_jobs=1, oob_score=False,
           random_state=None, verbose=0)], dtype=object)

(Mind you, your y has the wrong shape. It should be (10,) , not (10, 1) .) (请记住,您的y形状错误。应该是(10,) ,而不是(10, 1) 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM