【发布时间】:2020-08-27 16:36:05
【问题描述】:
好的,尝试在此处获取 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'))
【问题讨论】:
-
“实际准确度”是什么意思?您正在打印 4 个建模管道的平均准确度分数。
-
@thomaskolasa 查看我的编辑
-
你有多少行?你的编辑没有做 10 倍的 CV,所以它有 10 倍的例子可供学习。
-
@thomaskolasa 我有 2000 个。老实说,我对这一切都很陌生 - 我应该用这里的折叠数改变什么?
-
对不起,我上面的评论不正确。 10 倍 CV 的每个分区对 90% 的数据进行训练。
标签: python machine-learning scikit-learn cross-validation