Sklearn文本分类:为什么准确率这么低?

好的,我正在按照https://medium.com/@phylypo/text-classification-with-scikit-learn-on-khmer-documents-1a395317d195https://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html 的指导,尝试基于类别对文本进行分类。我的数据框布局如下,并命名为result

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

目标是根据类型对帖子进行分类,而target只是为16种类型中的每一种分配了1到16的编号。为了对文本进行分类,我这样做:

result = result[:1000] #缩短df - 之前是:600# 将数据集分割为训练和验证数据集train_x, valid_x, train_y, valid_y = model_selection.train_test_split(result['post'], result['type'], test_size=0.30, random_state=1)# 对目标变量进行标签编码encoder = preprocessing.LabelEncoder()train_y = encoder.fit_transform(train_y)valid_y = encoder.fit_transform(valid_y)def tokenizersplit(str):    return str.split()tfidf_vect = TfidfVectorizer(tokenizer=tokenizersplit, encoding='utf-8', min_df=2, ngram_range=(1, 2), max_features=25000)tfidf_vect.fit(result['post'])tfidf_vect.transform(result['post'])xtrain_tfidf = tfidf_vect.transform(train_x)xvalid_tfidf = tfidf_vect.transform(valid_x)def train_model(classifier, trains, t_labels, valids, v_labels):    # 使用分类器拟合训练数据集    classifier.fit(trains, t_labels)    # 预测验证数据集的标签    predictions = classifier.predict(valids)    return metrics.accuracy_score(predictions, v_labels)# 朴素贝叶斯accuracy = train_model(naive_bayes.MultinomialNB(), xtrain_tfidf, train_y, xvalid_tfidf, valid_y)print ("NB accuracy: ", accuracy)# 逻辑回归accuracy = train_model(linear_model.LogisticRegression(), xtrain_tfidf, train_y, xvalid_tfidf, valid_y)print ("LR accuracy: ", accuracy)

根据我在开始时对result的缩短程度,所有算法的准确率最高约为0.4。准确率应该是0.8-0.9。

我阅读了scikit在分类器(朴素贝叶斯,决策树分类器)上的准确率非常低,但不知道如何将其应用到我的数据框中。我的数据很简单 – 有类别(type)和文本(post)。

这里出了什么问题?

编辑 – 朴素贝叶斯尝试2:

text_clf = Pipeline([    ('vect', CountVectorizer()),    ('tfidf', TfidfTransformer()),    ('clf', MultinomialNB()),])text_clf.fit(result.post, result.target)docs_test = result.postpredicted = text_clf.predict(docs_test)np.mean(predicted == result.target)print("Naive Bayes: ")print(np.mean(predicted == result.target))

回答:

你在做什么

我认为错误在于这些行:

encoder = preprocessing.LabelEncoder()train_y = encoder.fit_transform(train_y)valid_y = encoder.fit_transform(valid_y)

通过两次拟合,你重置了LabelEncoder的知识。
在一个更简单的例子中:

from sklearn import preprocessingle = preprocessing.LabelEncoder()y_train = le.fit_transform(["class1", "class2", "class3"])y_valid = le.fit_transform(["class2", "class3"])print(y_train)print(y_valid)

输出这些标签编码:

[0 1 2][0 1]

这是错误的,因为编码标签0在训练集中是class1,在验证集中是class2

修复

我会将你的前几行改为:

result = result[:1000] #缩短df - 之前是:600# 在分割之前编码标签encoder = preprocessing.LabelEncoder()y_encoded = encoder.fit_transform(result['type'])# 注意,我将目标从result['type']改为了y_encodedtrain_x, valid_x, train_y, valid_y = model_selection.train_test_split(result['post'], y_encoded, test_size=0.30, random_state=1)def tokenizersplit(str):    return str.split()...

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注