【发布时间】:2019-05-14 13:11:39
【问题描述】:
我正在处理短文本 sn-ps(1-3 个句子)的分类。就主题而言,该数据集包含有关植物、动物和不相关事物的文本。首先,我使用分类器(线性 SVM)来标记每个 sn-p 的类别(svm_gen:0 - 不相关,1 - 植物,2 - 动物)。工作没有问题。
现在每个类别都有子类别,我还想为每个类别使用自己的算法进行分类。比如说,对于植物(svm_plant:0 - 其他,1 - 母猪,2 - 生长,3 - 收获)和动物(svm_animal:0 - 其他,1 - 饲料,2 - 宠物,3 - 玩耍)。如何在不拆分然后再次附加数据集的情况下将各个管道选择性地应用于预分类变量?
如前所述,我可以根据预分类拆分数据集,在新的 pandas Dataframes 上应用第二轮分类器,然后将它们重新附加在一起。有没有更好的办法?
svm_gen = Pipeline([( ... )])
svm_gen.fit()
df_complete['Topic'] = svm_gen.predict(df_complete['Text'])
# write all texts concerning animals into a new df to apply the respective SVM
df_plant = df_complete[df_complete.Topic == 1]
# same for animals
# categories both individually for the sub-categories
# glue them back together
df_final = df_plant.append(df_animals, ingnore_index = True)
基本上,我想要一个包含“文本”列、“主题”列和“子主题”列的最终数据集(熊猫数据框)。后者是根据第一轮分类中文本属于哪个类别进行选择性分类的。有点像:
df_complete.loc[df_complete['Topic'] == 1, 'sub_Topic'] = svm_plant.predict['Text']
df_complete.loc[df_complete['Topic'] == 2, 'sub_Topic'] = svm_animals.predict['Text']
df_complete.loc[df_complete['Topic'] == 0, 'sub_Topic'] = 'n/a'
【问题讨论】: