如何衡量随机森林分类器的准确性?

问题描述

因此,我使用随机森林分类器使用以下代码进行预测:

# Import Random Forest
from sklearn.ensemble import RandomForestClassifier

# Create a Gaussian Classifier
clf_two=RandomForestClassifier(n_estimators=3)

# Train the model using the training sets
clf_two.fit(emb_train,ytrain.ravel())

y_pred_two=clf_two.predict(emb_test)

我想知道我的分类器的准确性并尝试这样做:

# Import scikit-learn metrics module for accuracy calculation
from sklearn import metrics

# Model Accuracy
print("Accuracy:",metrics.accuracy_score(ytrain,y_pred_two))

问题在于 y_pred_two 是大小为 (5989,) 的行向量,而 ytrain 是大小为 (16128,1) 的列向量。所以两者之间存在大小不匹配,我收到此错误

ValueError: Found input variables with inconsistent numbers of samples: [16128,5989]

如果 y_pred_twoytrain 的大小不同或者我做错了什么,是否仍然可以测量准确度?但这就是训练和测试数据给我的方式。

非常感谢您的快速帮助!

解决方法

在我看来,问题只是您试图评估通过在具有训练数据集目标标签的测试样本上运行模型而获得的预测值的准确性。

您只需要加载或生成测试集标签(ytest)并运行:

print("Accuracy:",metrics.accuracy_score(ytest,y_pred_two))