使用python生成一个随机森林的程序
时间: 2023-05-25 20:03:22 浏览: 110
基于python实现随机森林Random Forest
以下是使用Python生成随机森林的程序:
首先,我们需要安装scikit-learn库。在命令行窗口中输入以下命令即可:
pip install scikit-learn
接下来,我们需要导入必要的库并加载数据集。本示例中将使用Iris数据集。
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
iris_data = load_iris()
df = pd.DataFrame(data=iris_data.data, columns=iris_data.feature_names)
df['target'] = iris_data.target
X_train, X_test, y_train, y_test = train_test_split(df[iris_data.feature_names], df['target'], test_size=0.2)
然后,我们可以创建随机森林分类器并使用fit方法拟合训练数据。
rfc = RandomForestClassifier(n_estimators=100)
rfc.fit(X_train, y_train)
最后,我们可以使用predict方法对测试数据进行预测并计算分类器的准确性。
predictions = rfc.predict(X_test)
accuracy = sum(predictions == y_test) / len(predictions)
print("Accuracy:", accuracy)
完整的程序如下:
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
iris_data = load_iris()
df = pd.DataFrame(data=iris_data.data, columns=iris_data.feature_names)
df['target'] = iris_data.target
X_train, X_test, y_train, y_test = train_test_split(df[iris_data.feature_names], df['target'], test_size=0.2)
rfc = RandomForestClassifier(n_estimators=100)
rfc.fit(X_train, y_train)
predictions = rfc.predict(X_test)
accuracy = sum(predictions == y_test) / len(predictions)
print("Accuracy:", accuracy)
阅读全文