有没有办法通过svm传递多个标签输入来预测一个标签

时间:2018-12-22 11:25:26

标签: python machine-learning svm

样本数据:
名称说明位置
克什米尔玫瑰花,乌黑
洋葱种类的古吉拉特邦蔬菜

我需要传递一个记录(string1,string2)。这两个字符串是 数据集包含

name    description

但是我尝试在python中使用svm传递一个标签并预测另一个标签

#Python CODE
data=pandas.csv(data.csv)
data_1=data[0:800]
data_2=data[800:1000]
svm.fit(data_1['name'], data_1['description'])
svm.predict(data_2['name'])
print("enter the name")
i=str(input())
predicted=svm.predict(i)
print("predicted description is")
print(predicted)  #here the description will be predicted

但是在上面的代码中,我仅传递标题作为输入并预测描述。

我通过添加另一列来扩展数据集,例如位置

所以数据集中将有三列,如

name,description,location

所以我需要现在输入名称和描述作为输入,并且需要预测位置作为结果

我不知道如何在predict()方法中传递两个标签(名称,描述)来预测结果的位置,或者其他任何可用的解决方案(如果适用)都请发布解决方案。

编辑:

我根据注释更改了代码:

#Python CODE  
data=pandas.csv(data.csv)  
data_1=data[0:100]  
data_2=data[50:100]  
svm.fit(data_1[['name','description']], data_1['location'])   
svm.predict(data_2['location'])  
print("enter the name")  
i=str(input())  
print("enter the description")  
j=str(input())      
predicted=svm.predict(i)  
print("predicted location is")  
print(predicted)    #here the location will be predicted  

运行此代码后出现以下错误:

  

ValueError:在svm_fit(data_1 [['name','description']],data_1 ['location'])中找到样本数量不一致的输入变量[2,100]

2 个答案:

答案 0 :(得分:0)

错误似乎在以下行

svm.predict(data_2['location']) 

您需要像这样输入data_2的输入数据!

svm.predict(data_2[['name','description']])

答案 1 :(得分:0)

以下代码不正确:

print("enter the name")  
i=str(input())  
print("enter the description")  
i=str(input())      
predicted=svm.predict(i)  

您应该使用:

print("enter the name")  
i=str(input())  
print("enter the description")  
j=str(input())  # using i here will lose name string.
predicted=svm.predict([i,j])  # send both name and description now;

但是错误似乎来自以下语句:

svm.fit(data_1[['name','description']], data_1['location']) 

仅当您还发布数据样本时,才能发现确切的错误。