为什么sklearn cross_val_score的分数这么低?

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

[好,在这里尝试通过4种不同的算法获得cross_val_score。我的数据框看起来像这样:

target   type    post
1      intj    "hello world shdjd"
2      entp    "hello world fddf"
16     estj   "hello world dsd"
4      esfp    "hello world sfs"
1      intj    "hello world ddfd"

type重复的地方。我正在像这样计算cross_val分数:

encoder = preprocessing.LabelEncoder()
y_encoded = encoder.fit_transform(result['type'])

train_x, valid_x, train_y, valid_y = model_selection.train_test_split(result['post'], y_encoded, test_size=0.30, random_state=1)

models = {'lr':LogisticRegression(multi_class = 'multinomial', solver = 'newton-cg'),
          'nb':MultinomialNB(alpha = 0.0001),
          'sgd':SGDClassifier(loss='hinge', penalty='l2',alpha=1e-3, random_state=42,
                      max_iter=5, tol=None),
          'rf':RandomForestClassifier(n_estimators = 10)}

for name,clf in models.items():
    pipe = Pipeline([('vect', CountVectorizer()),
                     ('tfidf', TfidfTransformer()),
                     ('clf', clf)])

    res = cross_val_score(pipe,result.post,result.target,cv=10, n_jobs=8)
    print(name,res.mean(),res.std())

这有效,但是平均值都在0.3左右。所有人的实际准确度约为0.98,逻辑回归的准确度为0.7。

这里怎么了?

编辑-这是我如何知道每种算法的平均准确度都高于0.3(我对每种算法都这样做):

text_clf3 = Pipeline([
    ('vect', CountVectorizer()),
    ('tfidf', TfidfTransformer()),
    ('clf', LogisticRegression(multi_class = 'multinomial', solver = 'newton-cg')),
])

text_clf3.fit(result.post, result.target)

predicted3 = text_clf3.predict(docs_test)
print("Logistics Regression: ")
print(np.mean(predicted3 == result.target))
print(metrics.classification_report(result.target, predicted3))

print(confusion_matrix(result.target, predicted3))
print("LR Precision:",precision_score(result.target, predicted3, average='weighted'))
print("LR Recall:",recall_score(result.target, predicted3, average='weighted'))
python machine-learning scikit-learn cross-validation
1个回答
0
投票

for循环中的模型中,您测量模型在交叉验证分区上的性能。在手动编辑中,您可以测量在docs_test上的表现。通常,您希望自己的CV分数与在样本外测试集上的表现相似。如果您在测试集上的表现明显更好,则可能不是随机创建的docs_test。您可能有目标泄漏。也许该模型恰好可以很好地为该测试集做出预测。

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