【发布时间】:2022-03-08 23:12:23
【问题描述】:
我正在使用 cvxpy 实现软边距 svm。我不断收到错误 ValueError: setting an array element with a sequence。但是我不明白这个错误是从哪里来的。
这段代码
import numpy as np
import matplotlib.pyplot as plt
import cvxpy as cp
from cvxpy import *
np.random.seed(6201)
mean1 = [-1, -1]
mean2 = [1, 1]
cov = [[1, 0.25], [0.25, 1]]
x = np.random.multivariate_normal(mean1, cov, 200)
y = np.random.multivariate_normal(mean2, cov, 200)
#plt.scatter(x[:,0],x[:,1],color='blue')
#plt.scatter(y[:,0],y[:,1],color='red')
print(x.shape)
m = x.shape[0]
n = x.shape[1]
w = cp.Variable((n, 1))
b = cp.Variable()
xi = cp.Variable((m, 1))
print(w.shape)
print(b.shape)
print(y.shape)
objective = cp.Minimize(1/2*cp.square(cp.norm(w))+ C * cp.sum(xi))
constraints=[cp.matmul(y, (np.dot(x,w) + b)) + xi >=1, xi>=0 ]
prob = cp.Problem(objective,constraints)
result = prob.solve()
报错
---> 64 constraints=[cp.matmul(y, (np.dot(x,w) + b)) + xi >=1, xi>=0 ]
ValueError: setting an array element with a sequence.
谁能告诉我如何解决上述代码中的问题,以使其停止抛出此错误消息?
【问题讨论】:
标签: python machine-learning svm cvxpy