【问题标题】:How to operate multidimensional features in SVM or use multidimensional features to train model?如何在 SVM 中操作多维特征或使用多维特征训练模型?
【发布时间】:2019-02-02 22:07:55
【问题描述】:

如果我有这个输入:

"a1,b1,c1,d1;A1,B1,C1,D1;α1,β1,γ1,θ1;Label1"  
"... ... "  
"an,bn,cn,dn;An,Bn,Cn,Dn;αn,βn,γn,θn;Labelx"

数组表达式:

[
 [[a1,b1,c1,d1],[A1,B1,C1,D1],[α1,β1,γ1,θ1],[Label1]], 
 ... ... ... ... 
 [[an,bn,cn,dn],[An,Bn,Cn,Dn],[αn,βn,γn,θn],[Labelx]]
                                                     ]

实例:

[... ... ... ...
 [[58.32,453.65,980.50,540.23],[774.40,428.79,1101.96,719.79],[503.70,624.76,1128.00,1064.26],[1]], 
 [[0,0,0,0],[871.05,478.17,1109.37,698.36],[868.63,647.56,1189.92,1040.80],[1]],
 [[169.34,43.41,324.46,187.96],[50.24,37.84,342.39,515.21],[0,0,0,0],[0]]]

像这样:
有3个矩形,标签表示相交、包含或其他。
我想使用 3 或 N 个特征来通过 SVM 训练模型。
而我刚刚学习了“python Iris SVM”代码。我该怎么办?

意见:
这是我的尝试:

from sklearn import svm
import numpy as np
mport matplotlib as mpl
from sklearn.model_selection import train_test_split

def label_type(s):
    it = {b'Situation_1': 0, b'Situation_2': 1, b'Unknown': 2}
    return it[s]


path = 'C:/Users/SEARECLUSE/Desktop/MNIST_DATASET/temp_test.data' 
data = np.loadtxt(path, dtype=list, delimiter=';', converters={3: 
label_type})

x, y = np.split((data), (3,), axis=1)
x = x[:, :3]
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=1, 
train_size=0.6)

clf = svm.SVC(C=0.8, kernel='rbf', gamma=20, decision_function_shape='ovr')
clf.fit(x_train, y_train.ravel())

报告错误:

Line: clf.fit(x_train, y_train.ravel())

ValueError: could not convert string to float: 

如果我尝试转换数据:

x, y = np.split(float(data), (3,), axis=1)

报告错误:

Line: x, y = np.split(float(data), (3,), axis=1)

TypeError: only length-1 arrays can be converted to Python scalars

【问题讨论】:

  • 能否请您包括引发错误的行?
  • 我已经添加了线路信息。
  • 我怀疑你不能接受datafloat 因为data 不是长度为1 的数组,因此它不能转换为python 标量(即@ 987654331@) 使用data.astype('float'),一种 numpy 方法。
  • 不行,可能是因为[0,0,0,0]等数据中的符号','不能转换为float类型?
  • 扁平化输入特征对您来说是一个有效的选择吗?

标签: python machine-learning scikit-learn svm


【解决方案1】:

在我回答之前我有几个问题:

Q1。您使用什么样的数据来训练 SVM 模型。是图像数据吗?如果是图像数据,那么它是 RGB 数据吗?您解释数据的方式似乎您打算使用 SVM 进行图像分类。如果我错了,请纠正我。

假设 假设您有图像数据。然后请转换为灰度。然后您尝试将整个数据转换为 numpy 数组。检查numpy 模块以了解如何做到这一点。

一旦你的数据变成numpy 数组,那么你就可以应用你的模型了。

如果有帮助,请告诉我。

【讨论】:

  • A1:我想找一个强特征来解释我的标签。但是现在,我只有3个弱特征,我想找到一个关系,用SVM做3比1。跨度>
  • 如果我理解正确,[a1,b1,c1,d1] 是您想要找到的一项功能,如果它正确解释了标签?和[A1,B1,C1,D1] 等等...
  • 我觉得用一个特征做姿势分类效率低下,所以我尝试做一些弱特征。我尝试做图形交叉识别作为我的简单步骤。
  • 意思是[ [Feature 1-1],[Feature 1-2],[Feature 1-3] ,[label 1]] ,因为很难得到[[Feature 1],[标签 1] ]
  • 这个特征是不是像张量一样属于三个方向的运动?
【解决方案2】:

SVM 最初并不是为处理多维数据而设计的。我建议你扁平化你的输入特征:

x, y = np.split((data), (3,), axis=1)
x = x[:, :3]

# flatten the features
x = np.reshape(x,(len(x),-1))

x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=1, 
train_size=0.6)

clf = svm.SVC(C=0.8, kernel='rbf', gamma=20, decision_function_shape='ovr')
clf.fit(x_train, y_train.ravel())

【讨论】:

  • 非常感谢,我会改变我的想法来完成我的项目。
猜你喜欢
  • 2018-07-23
  • 1970-01-01
  • 2016-06-16
  • 2021-07-09
  • 2016-12-02
  • 2018-02-25
  • 1970-01-01
  • 2021-03-09
  • 2020-12-29
相关资源
最近更新 更多