我想将两个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个元素。我将如何将其放入支持向量机中,从而得到一个将这两个数字分开的方程式?
我试图将第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.
答案 0 :(得分:1)
我要做的第一件事是正确标记数据点。如果为clf分配变量,您将开始看到问题。
X, y = np3, classified_array
您将看到数据和目标向量不正确。我会尝试从这些行开始问题。
group_data = [i+[0] for i in group1] + [i+[1] for i in group2]
df = pd.DataFrame(data=group_data, columns=['x','y','target'])
这将为组1创建标签0,为组2创建标签1。然后,您可以像这样分配数据,
X,y = df[['x','y']].values, df.target.values
答案 1 :(得分:0)
据我了解,group1和group2属于两个不同的类,因此我们有二进制分类问题。让我们解决它。
from sklearn import svm
import numpy as np
import matplotlib.pyplot as plt
group1 = np.array([[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 = np.array([[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]])
# Choose appropriate C
clf = svm.SVC(kernel='linear', C=100)
clf.fit(X, y)
plt.plot(group1[:,0], group1[:,1], 'ro', group2[:,0], group2[:,1], 'bx')
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()
xx = np.linspace(xlim[0], xlim[1], 20)
yy = np.linspace(ylim[0], ylim[1], 20)
YY, XX = np.meshgrid(yy, xx)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
Z = clf.decision_function(xy).reshape(XX.shape)
ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
linestyles=['--', '-', '--'])
ax.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=100,
linewidth=1, facecolors='none', edgecolors='k')
plt.show()