【发布时间】:2019-10-02 02:03:50
【问题描述】:
我想将两个 np 数组提供给 sklearn svm。我有两组数据
group1 = [[0.067, 0.21], [0.092, 0.21],
[0.294, 0.445], [0.227, 0.521], [0.185, 0.597],
[0.185, 0.689], [0.235, 0.748], [0.319, 0.773],
[0.387, 0.739], [0.437, 0.672], [0.496, 0.739],
[0.571, 0.773], [0.639, 0.765], [0.765, 0.924],
[0.807, 0.933], [0.849, 0.941]]
group2 = [[0.118, 0.143], [0.118, 0.176],
[0.345, 0.378], [0.395, 0.319], [0.437, 0.261],
[0.496, 0.328], [0.546, 0.395], [0.605, 0.462],
[0.655, 0.529], [0.697, 0.597], [0.706, 0.664],
[0.681, 0.723], [0.849, 0.798], [0.857, 0.849],
[0.866, 0.899]]
组的长度分别为 15 和 16 个元素。我将如何将它放入一个 SVM 中,它会给我一个分离这两个数字的方程?
我尝试将第 2 组的平均值归入末尾,然后将它们连接成一个 64 个样本的一维数组。但我无法让 clf.fit 中的 y 向量与我制作的数组一起使用。
我目前拥有的东西
from sklearn import svm
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
group1 = [[0.067, 0.21], [0.092, 0.21],
[0.294, 0.445], [0.227, 0.521], [0.185, 0.597],
[0.185, 0.689], [0.235, 0.748], [0.319, 0.773],
[0.387, 0.739], [0.437, 0.672], [0.496, 0.739],
[0.571, 0.773], [0.639, 0.765], [0.765, 0.924],
[0.807, 0.933], [0.849, 0.941]]
group2 = [[0.118, 0.143], [0.118, 0.176],
[0.345, 0.378], [0.395, 0.319], [0.437, 0.261],
[0.496, 0.328], [0.546, 0.395], [0.605, 0.462],
[0.655, 0.529], [0.697, 0.597], [0.706, 0.664],
[0.681, 0.723], [0.849, 0.798], [0.857, 0.849],
[0.866, 0.899]]
def print_group(group):
for i in group:
for j in i:
print j
#print_group(group1)
len_group1=str(len(group1))
len_group2=str(len(group2))
#c = " ".join([a, b])
print "length of group 1" + " " + len_group1 + " " + "units in group 1"
print "length of group 2" + " " + len_group2 + " " + "units in group 2"
np_1= np.array(group1)
np_2= np.array(group2)
mean1, std1 = np.mean(np_1), np.std(np_1)
mean2, std2 = np.mean(np_2), np.std(np_2)
mean2_a=np.array(mean2)
np22=np.append(mean2_a, np_2)
np22=np.append(mean2_a, np22)
np1=np_1.ravel().reshape(32,1)
np2=np22.ravel().reshape(32,1)
#
#print(mean1, std1)
#print(mean2, std2)
#
np3=np.concatenate((np1,np2))
#plt.scatter(np_1, np_2)
#plt.show()
#
#np11=np1.reshape(1,-1)
#np22=np2.reshape(1,-1)
#np.append(np2,mean2)
#np.append(np2,mean2)
clf = svm.SVC(gamma='scale')
classified_array= np.zeros((64, 0))
clf.fit([np3],[classified_array])
File "<ipython-input-39-50dcfc391d51>", line 1, in <module>
runfile('C:/Users/AliDesktop/Desktop/Magic Briefcase/Jobs/StarMind.py', wdir='C:/Users/AliDesktop/Desktop/Magic Briefcase/Jobs')
File "C:\Users\AliDesktop\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Users\AliDesktop\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/AliDesktop/Desktop/Magic Briefcase/Jobs/StarMind.py", line 76, in <module>
clf.fit([np3],[classified_array])
File "C:\Users\AliDesktop\Anaconda2\lib\site-packages\sklearn\svm\base.py", line 151, in fit
X, y = check_X_y(X, y, dtype=np.float64, order='C', accept_sparse='csr')
File "C:\Users\AliDesktop\Anaconda2\lib\site-packages\sklearn\utils\validation.py", line 521, in check_X_y
ensure_min_features, warn_on_dtype, estimator)
File "C:\Users\AliDesktop\Anaconda2\lib\site-packages\sklearn\utils\validation.py", line 405, in check_array
% (array.ndim, estimator_name))
ValueError: Found array with dim 3. Estimator expected <= 2.
【问题讨论】: