jeg*_*agh 6 python nlp classification scipy
我在客户支持方面工作,并且我正在使用scikit-learn来预测我们门票的标签,给出一套训练门票(训练集中大约40,000张门票).
我正在使用基于此的分类模型.它只是预测"()"作为我的许多测试票集的标签,即使训练集中没有一张票没有标签.
我的标签培训数据是一个列表列表,例如:
tags_train = [['international_solved'], ['from_build_guidelines my_new_idea eligibility'], ['dropbox other submitted_faq submitted_help'], ['my_new_idea_solved'], ['decline macro_backer_paypal macro_prob_errored_pledge_check_credit_card_us loading_problems'], ['dropbox macro__turnaround_time other plq__turnaround_time submitted_help'], ['dropbox macro_creator__logo_style_guide outreach press submitted_help']]
Run Code Online (Sandbox Code Playgroud)
虽然我的故障单描述的培训数据只是一个字符串列表,例如:
descs_train = ['description of ticket one', 'description of ticket two', etc]
Run Code Online (Sandbox Code Playgroud)
这是构建模型的代码的相关部分:
import numpy as np
import scipy
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import LinearSVC
# We have lists called tags_train, descs_train, tags_test, descs_test with the test and train data
X_train = np.array(descs_train)
y_train = tags_train
X_test = np.array(descs_test)
classifier = Pipeline([
('vectorizer', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', OneVsRestClassifier(LinearSVC(class_weight='auto')))])
classifier.fit(X_train, y_train)
predicted = classifier.predict(X_test)
Run Code Online (Sandbox Code Playgroud)
但是,"预测"给出了一个如下所示的列表:
predicted = [(), ('account_solved',), (), ('images_videos_solved',), ('my_new_idea_solved',), (), (), (), (), (), ('images_videos_solved', 'account_solved', 'macro_launched__edit_update other tips'), ('from_guidelines my_new_idea', 'from_guidelines my_new_idea macro__eligibility'), ()]
Run Code Online (Sandbox Code Playgroud)
我不明白为什么当训练集中没有空格时会预测空白().它不应该预测最接近的标签吗?任何人都可以推荐我正在使用的模型的任何改进吗?
非常感谢你的帮助!
问题出在你的tags_train变量上.根据OneVsRestClassifier文档,目标需要是"一系列标签序列",而您的目标是一个元素的列表.
以下是代码的编辑,自包含和可用版本.注意改变tags_train,特别tags_train是它是一个元素元组的事实.
import numpy as np
import scipy
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import LinearSVC
# We have lists called tags_train, descs_train, tags_test, descs_test with the test and train data
tags_train = [('label', ), ('international' ,'solved'), ('international','open')]
descs_train = ['description of ticket one', 'some other ticket two', 'label']
X_train = np.array(descs_train)
y_train = tags_train
X_test = np.array(descs_train)
classifier = Pipeline([
('vectorizer', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', OneVsRestClassifier(LinearSVC(class_weight='auto')))])
classifier = classifier.fit(X_train, y_train)
predicted = classifier.predict(X_test)
print predicted
Run Code Online (Sandbox Code Playgroud)
输出是
[('international',), ('international',), ('international', 'open')]
Run Code Online (Sandbox Code Playgroud)