Python sklearn 多标签分类:用户警告:所有训练示例中都存在标签不是 226

Abt*_*Pst 4 python machine-learning scikit-learn logistic-regression multilabel-classification

我正在尝试多标签分类问题。我的数据看起来像这样

DocID   Content             Tags           
1       some text here...   [70]
2       some text here...   [59]
3       some text here...  [183]
4       some text here...  [173]
5       some text here...   [71]
6       some text here...   [98]
7       some text here...  [211]
8       some text here...  [188]
.       .............      .....
.       .............      .....
.       .............      .....
Run Code Online (Sandbox Code Playgroud)

这是我的代码

traindf = pd.read_csv("mul.csv")
print "This is what our training data looks like:"
print traindf

t=TfidfVectorizer()

X=traindf["Content"]

y=traindf["Tags"]

print "Original Content"
print X
X=t.fit_transform(X)
print "Content After transformation"
print X
print "Original Tags"
print y
y=MultiLabelBinarizer().fit_transform(y)
print "Tags After transformation"
print y

print "Features extracted:"
print t.get_feature_names()
print "Scores of features extracted"
idf = t.idf_
print dict(zip(t.get_feature_names(), idf))

print "Splitting into training and validation sets..."
Xtrain, Xvalidate, ytrain, yvalidate = train_test_split(X, y, test_size=.5)

print "Training Set Content and Tags"
print Xtrain
print ytrain
print "Validation Set Content and Tags"
print Xvalidate
print yvalidate

print "Creating classifier"
clf = OneVsRestClassifier(LogisticRegression(penalty='l2', C=0.01))

clf.fit(Xtrain, ytrain)

predictions=clf.predict(Xvalidate)
print "Predicted Tags are:"
print predictions
print "Correct Tags on Validation Set are :"
print yvalidate
print "Accuracy on validation set: %.3f"  % clf.score(Xvalidate,yvalidate)
Run Code Online (Sandbox Code Playgroud)

代码运行良好,但我不断收到这些消息

X:\Anaconda2\lib\site-packages\sklearn\multiclass.py:70: UserWarning: Label not 288 is present in all training examples.
  str(classes[c]))
X:\Anaconda2\lib\site-packages\sklearn\multiclass.py:70: UserWarning: Label not 304 is present in all training examples.
  str(classes[c]))
X:\Anaconda2\lib\site-packages\sklearn\multiclass.py:70: UserWarning: Label not 340 is present in all training examples.
Run Code Online (Sandbox Code Playgroud)

这是什么意思?它是否表明我的数据不够多样化?

小智 5

当某些项目存在于所有或许多记录中时,某些数据挖掘算法会出现问题。例如,这是使用 Apriori 算法进行关联规则挖掘时的一个问题。

是否有问题取决于分类器。我不知道您使用的特定分类器,但这里有一个示例,当拟合具有最大深度的决策树时可能很重要。

假设您正在使用 Hunt 算法和 GINI 指数拟合具有最大深度的决策树以确定最佳分割(参见此处的解释,第 35 页起)。第一次拆分可以是记录是否具有标签 288。如果每个记录都有这个标签,那么 GINI 索引将是这种拆分的最佳选择。这意味着前如此多的拆分将是无用的,因为您实际上并未拆分训练集(您拆分的是一个没有 288 的空集,以及带有 288 的集本身)。所以,树的第一个层次是无用的。如果您随后设置了最大深度,这可能会导致决策树的准确性较低。

在任何情况下,您收到的警告都不是您的代码的问题,充其量是您的数据集的问题。您应该检查您使用的分类器是否对此类事物敏感 - 如果是,则当您过滤掉随处可见的标签时,它可能会给出更好的结果。