简体   繁体   English

如何在SVM中操作多维特征或使用多维特征训练模型?

[英]How to operate multidimensional features in SVM or use multidimensional features to train model?

If I have this input: 如果我有此输入:

"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"

Array expression: 数组表达式:

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

Instance: 例如:

[... ... ... ...
 [[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]]]

Like this: 像这样:
There are 3 rectangles,and the label means intersect,contain or some other. 有3个矩形,标号表示相交,包含或其他。
I want to use 3 or N features to train a model by SVM. 我想使用3个或N个功能通过SVM训练模型。
And I just learn the " python Iris SVM " code.What should I do? 我只是学习“ python Iris SVM ”代码。我该怎么办?

The Opinion: 意见:
this is my try: 这是我的尝试:

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())

Report Error: 报告错误:

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

ValueError: could not convert string to float: 

If I try to convert the data: 如果我尝试转换数据:

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

Report Error: 报告错误:

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

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

I have few questions before I go for an answer: 在寻求答案之前,我有几个问题:

Q1. Q1。 What kind of data you are using to train SVM model. 您正在使用哪种数据来训练SVM模型。 Is it image data? 是图像数据吗? If image data then, is it RGB data? 如果是图像数据,是RGB数据吗? The way you explained you data it seems you are intended to do image classification using SVM. 您解释数据的方式似乎打算使用SVM进行图像分类。 Correct me if I am wrong. 如果我错了,请纠正我。

Assumption Let say you have image data. 假设假设您有图像数据。 Then please convert to gray scale. 然后请转换为灰度。 Then you try to convert entire data into numpy array. 然后,您尝试将整个数据转换为numpy数组。 check numpy module to find how to do that. 检查numpy模块以查找操作方法。

Once you data become numpy array then you can apply your model. 一旦数据变为numpy数组,就可以应用模型。

Let me know if that helps. 让我知道是否有帮助。

SVMs were not initially designed to handle multidimensional data. SVM最初并非设计为处理多维数据。 I suggest you flatten your input features: 我建议您拼合输入功能:

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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM