3

我在尝试多标签分类问题。我的数据是这样的Python sklearn多标签分类:UserWarning:标签不是226在所有训练样例中都存在

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] 
.  .............  ..... 
.  .............  ..... 
.  .............  ..... 

这里是我的代码

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) 

代码运行正常,但我不断收到这些消息

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. 

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

回答

4

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

无论是否有问题都取决于分类器。我不知道你正在使用的特定分类器,但这里有一个例子,它适用于具有最大深度的决策树。

假设您正在使用Hunt算法和GINI索引来确定最佳分割(请参阅here以获得解释,请参见第35张幻灯片),以最大深度拟合决策树。第一次拆分可以是记录是否具有标签288.如果每个记录都具有该标签,则GINI索引对于这样的拆分将是最佳的。这意味着第一个这么多的分割将是无用的,因为你实际上并没有分割训练集(你在分割一个空集,没有288,而集本身,288)。所以,树的第一个如此多的级别是没用的。如果您设置了最大深度,则可能会导致低精度决策树。

在任何情况下,您所得到的警告对您的代码来说都不是问题,至多在您的数据集中。你应该检查你使用的分类器是否对这种事情敏感,如果是这样的话,当你过滤出所有发生的标签时,它可能会给出更好的结果。

相关问题