【问题标题】:Is there any way to pass multiple label inputs to predict one label using svm有没有办法通过多个标签输入来使用 svm 预测一个标签
【发布时间】:2018-12-22 11:25:25
【问题描述】:

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

我需要传递一个记录(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() 方法中传递两个标签(名称、描述)来预测位置作为结果或任何其他可用的解决方案(如果适用)意味着请发布解决方案。

编辑:

我按照cmets修改了代码:

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

【问题讨论】:

  • 试试:svm.fit(data_1[['name','description']], data_1['location'])
  • 你试过了吗?成功了吗?
  • 它正在生成ValueError:添加svm.fit(data_1[['name','description']],data_1['location']后发现样本数不一致的输入变量[2,100] ) 而不是 svm.fit(data_1['name'], data_1['description'])。有没有其他解决方案
  • 在上面的问题中发布您的代码。它应该被排序。
  • 您可以从数据变量中添加示例记录吗?

标签: python machine-learning svm


【解决方案1】:

看起来错误在下面一行

svm.predict(data_2['location']) 

你需要给data_2的输入数据,像这个!

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

【讨论】:

    【解决方案2】:

    以下代码不正确:

    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']) 
    

    只有在您也发布数据样本时才能找到确切的错误。

    【讨论】:

    • 我在上面发布了示例数据。您能告诉我如何预测位置字段吗。您还有其他解决方案吗。
    • 是数据框还是numpy数组?在上面的问题中放置一个示例。
    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    相关资源
    最近更新 更多