python:打印机器学习分类结果的混淆矩阵

作者:CSDN @ _养乐多_

本文记录了使用随机森林分类的结果精度验证方法,混淆矩阵的代码。

from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
from sklearn.metrics import confusion_matrix

# 生成一些示例数据
X, y = make_classification(n_samples=1000, n_features=10, random_state=42)

# 创建随机森林分类器
clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf.fit(X, y)

# 预测类别
y_pred = clf.predict(X)

# 计算混淆矩阵
cm = confusion_matrix(y, y_pred)

# 打印混淆矩阵
print("混淆矩阵:")
print(cm)

猜你喜欢

转载自blog.csdn.net/qq_35591253/article/details/130731876