我正在尝试一个多标签分类问题。我的数据看起来像这样
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 traindft=TfidfVectorizer()X=traindf["Content"]y=traindf["Tags"]print "Original Content"print XX=t.fit_transform(X)print "Content After transformation"print Xprint "Original Tags"print yy=MultiLabelBinarizer().fit_transform(y)print "Tags After transformation"print yprint "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 Xtrainprint ytrainprint "Validation Set Content and Tags"print Xvalidateprint yvalidateprint "Creating classifier"clf = OneVsRestClassifier(LogisticRegression(penalty='l2', C=0.01))clf.fit(Xtrain, ytrain)predictions=clf.predict(Xvalidate)print "Predicted Tags are:"print predictionsprint "Correct Tags on Validation Set are :"print yvalidateprint "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.
这意味着什么?这是否表明我的数据不够多样化?
回答:
一些数据挖掘算法在某些项目在所有或许多记录中存在时会出现问题。例如,使用Apriori算法进行关联规则挖掘时就是一个问题。
这是否是一个问题取决于分类器。我不知道你使用的特定分类器,但这里有一个例子,当使用最大深度拟合决策树时可能会有影响。
假设你使用Hunt的算法和GINI指数来确定最佳分割来拟合一个具有最大深度的决策树(参见这里的解释,从第35页开始)。第一次分割可能是基于记录是否具有标签288。如果每个记录都有这个标签,GINI指数将对这样的分割最优。这意味着前几个分割将是无用的,因为你实际上并没有分割训练集(你是在一个没有288的空集和一个有288的集合本身之间分割)。因此,树的前几个层次是无用的。如果你然后设置一个最大深度,这可能会导致一个低精度的决策树。
无论如何,你收到的警告不是你的代码的问题,最多是与你的数据集有关。你应该检查你使用的分类器是否对这种情况敏感——如果是的话,过滤掉那些在所有地方出现的标签可能会得到更好的结果。