gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:48:38 25 4
gpt4 key购买 nike

如果我有这个输入:

"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

最佳答案

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

关于python - 如何在SVM中操作多维特征或者使用多维特征来训练模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52055072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com