以下是一个简单的 Python 随机森林代码示例,用于演示如何使用 scikit-learn 库进行分类任务:

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(n_estimators=100, max_depth=2,
                             random_state=0)

# 训练分类器
clf.fit(X, y)

# 预测新数据
print(clf.predict([[0, 0, 0, 0]]))

在这个例子中,我们使用 'make_classification' 函数生成随机数据集,并使用 'RandomForestClassifier' 类来创建随机森林分类器。我们使用 'fit' 方法训练分类器,并使用 'predict' 方法来预测新数据。

Python 随机森林代码示例:分类任务实战

原文地址: https://www.cveoy.top/t/topic/mMTi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录