sklearn DummyClassifier 的预测不正确

问题描述 投票:0回答:1

我正在尝试对学校项目的数据集执行虚拟分类。这个想法是为了了解不同政党发表演讲的频率。我的想法是按以下方式编写这段代码:

from sklearn.dummy import DummyClassifier
import pandas as pd
import bz2


with bz2.open("data/ch3/speeches-201718.json.bz2") as source:
    speeches_201718 = pd.read_json(source)

with bz2.open("data/ch3/speeches-201819.json.bz2") as source:
    speeches_201819 = pd.read_json(source)


training_data, test_data = speeches_201718, speeches_201819

train_parties_count = training_data['party'].value_counts()
test_parties_count = test_data['party'].value_counts()
dummy_clf = DummyClassifier(strategy="most_frequent")

X = train_parties_count
y = train_parties_count.index
dummy_clf.fit(X.values, y)
print(X)
print(y)

test_parties_count.index = pd.CategoricalIndex(test_parties_count.index, categories=train_parties_count.index, ordered=True)
X_test = test_parties_count.sort_index()
print(X_test)
pred_mfc = dummy_clf.predict(X_test.values)

print("Urval av prediktioner [0-4]: ", pred_mfc[:5])

我得到以下输出: enter image description here

正如您所看到的,预测应该是 S 时却是 C,什么可能是错误的?

我尝试以多种方式定义训练和测试数据,但没有成功。

python-3.x machine-learning scikit-learn classification prediction
1个回答
0
投票

sklearn
中的虚拟估计器不适用于实际问题(它们用于使用非常简单的规则来获取性能的基线度量)。在您的情况下,虚拟估计器被配置为“始终”输出“C”,无论输入如何。

RandomForestClassifier

通常是一个很好的“现成”估计器。我建议您在完成训练后查看训练分数,以验证模型是否正在学习某些内容。然后您可以评估它在未见过的数据(验证集)上的性能。

为了获得准确度分数,您可以使用 

my_classifier.score(X_data, y_data)

    

© www.soinside.com 2019 - 2024. All rights reserved.