【发布时间】: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
【问题讨论】:
-
能否请您包括引发错误的行?
-
我已经添加了线路信息。
-
我怀疑你不能接受
data的float因为data不是长度为1 的数组,因此它不能转换为python 标量(即@ 987654331@) 使用data.astype('float'),一种 numpy 方法。 -
不行,可能是因为[0,0,0,0]等数据中的符号','不能转换为float类型?
-
扁平化输入特征对您来说是一个有效的选择吗?
标签: python machine-learning scikit-learn svm