随机森林 python
时间: 2023-10-12 22:05:00 浏览: 41
随机森林是一种集成学习方法,它基于决策树算法,通过随机化方法生成多个决策树,并将它们组合起来进行预测。在随机森林中,每个决策树都是基于不同的随机样本和随机特征构建的,这样可以降低模型的方差,提高模型的泛化能力。
在Python中,可以使用sklearn库中的RandomForestClassifier和RandomForestRegressor类来实现随机森林模型。以下是一个简单的随机森林分类模型的示例代码:
```python
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
# 生成随机数据集
X, y = make_classification(n_samples=1000, n_features=4,
n_informative=2, n_redundant=0,
random_state=0, shuffle=False)
# 构建随机森林模型
clf = RandomForestClassifier(max_depth=2, random_state=0)
clf.fit(X, y)
# 预测新数据
print(clf.predict([[0, 0, 0, 0]]))
```
在这个例子中,我们使用make_classification函数生成了一个包含1000个样本和4个特征的随机数据集,然后使用RandomForestClassifier类构建了一个最大深度为2的随机森林模型,并对其进行了拟合。最后,我们使用predict方法对新数据进行预测,并输出了预测结果。