随机森林模型评估步骤
时间: 2023-11-26 07:48:48 浏览: 37
随机森林模型评估步骤如下:
```python
# 导入必要的库和数据集
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 生成数据集
X, y = make_classification(n_samples=1000, n_features=4, n_informative=2, n_redundant=0, random_state=0, shuffle=False)
# 将数据集分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
# 训练随机森林模型
clf = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0)
clf.fit(X_train, y_train)
# 预测测试集
y_pred = clf.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
以上是随机森林模型评估的步骤,具体包括以下几个步骤:
1. 导入必要的库和数据集。
2. 生成数据集。
3. 将数据集分为训练集和测试集。
4. 训练随机森林模型。
5. 预测测试集。
6. 计算准确率。