ValueError: setting an array element with a sequence.

528 views
Skip to first unread message

Mohamed Daoudi

unread,
May 4, 2020, 4:31:22 AM5/4/20
to cvxpy

Dea all,

I am trying to run the following code, however, I got ValueError: setting an array element with a sequence.

Thank you for your help,
Med


"print(Z2.shape)
print(Z1.shape)
Y= np.dot(np.dot(Z2.T,X),Z1)
print(Y.shape)

X = cp.Variable((66, 66), PSD = True)

objectivefn = cp.norm(Y,"nuc")

problem = cp.Problem(cp.Minimize(objectivefn))

problem.solve(solver=cp.SCS)"

(66, 2)
(66, 2)
(2, 2)




Aldo Raine

unread,
May 4, 2020, 8:45:40 AM5/4/20
to cvxpy
Adding the first 3 lines to enable code execution...

Z1 = np.random.rand(66, 2)
Z2
= np.random.rand(66, 2)
X
= np.random.rand(66, 66)

print(Z2.shape)
print(Z1.shape)

Y
= np.dot(np.dot(Z2.T, X), Z1)

print(Y.shape)

X
= cp.Variable((66, 66), PSD=True)
objectivefn
= cp.norm(Y, "nuc")
problem
= cp.Problem(cp.Minimize(objectivefn))

problem
.solve()

print(X.value)

This runs on Python 3.7.6, cvxpy version 1.0.31   without throwing the error you mention.

Mohamed Daoudi

unread,
May 4, 2020, 9:10:43 AM5/4/20
to cvxpy
Thank you for your help.

This is error is not clear when I define X  as  X = np.random.rand(66, 66), I don't have this error. 

however when I define X as PSD matrix = cp.Variable((66, 66), PSD=True), 

I got the error ValueError: setting an array element with a sequence.

Mohamed Daoudi

unread,
May 4, 2020, 10:37:22 AM5/4/20
to cvxpy
As you can this version is running correctly 


Z1 = np.random.rand(66, 2)
Z2 = np.random.rand(66, 2)
X = np.random.rand(66, 66)
#X = cp.Variable(shape=(66, 66), PSD = True)
Y = np.dot(np.dot(Z2.T,X),Z1)
#X = np.array(X1, dtype=object)

print(Z2.shape)
print(Z1.shape)
print(Y.shape)

objective = cp.Minimize(cp.norm(np.dot(np.dot(Z2.T,X),Z1),"nuc"))

BUT  with this version, 

Z1 = np.random.rand(66, 2)
Z2 = np.random.rand(66, 2)
#X = np.random.rand(66, 66)
X = cp.Variable(shape=(66, 66), PSD = True)
Y = np.dot(np.dot(Z2.T,X),Z1)

print(Z2.shape)
print(Z1.shape)
print(Y.shape)

objective = cp.Minimize(cp.norm(np.dot(np.dot(Z2.T,X),Z1),"nuc"))

I got this error ! 

%%%%%

ValueError                                Traceback (most recent call last)
<ipython-input-401-6290055a0757> in <module>()
     19 print(Y.shape)
     20 
---> 21 objective = cp.Minimize(cp.norm(np.dot(np.dot(Z2.T,X),Z1),"nuc"))
     22 
     23 #problem = cp.Problem(objective)

/Users/mohameddaoudi/opt/anaconda2/lib/python2.7/site-packages/cvxpy/atoms/norm.pyc in norm(x, p, axis)
     45         return normNuc(x)
     46     elif p == "fro":
---> 47         return pnorm(x, 2, axis)
     48     elif p == 2:
     49         if axis is None and x.is_matrix():

/Users/mohameddaoudi/opt/anaconda2/lib/python2.7/site-packages/cvxpy/expressions/expression.pyc in cast_to_const(expr)

/Users/mohameddaoudi/opt/anaconda2/lib/python2.7/site-packages/cvxpy/expressions/constants/constant.pyc in __init__(self, value)
     42         self._size = intf.size(self.value)
     43         self._is_pos, self._is_neg = intf.sign(self.value)
---> 44         super(Constant, self).__init__()
     45 
     46     def name(self):

/Users/mohameddaoudi/opt/anaconda2/lib/python2.7/site-packages/cvxpy/interface/numpy_interface/ndarray_interface.pyc in const_to_matrix(self, value, convert_scalars)
     48     # Return an identity matrix.
     49     def identity(self, size):
---> 50         return numpy.eye(size)
     51 
     52     # Return the dimensions of the matrix.

Mohamed Daoudi

unread,
May 4, 2020, 10:55:10 AM5/4/20
to cvxpy
I am trying to constrain X to be PSD by another way, constr1=(X >> 0), 

Z1 = np.random.rand(66, 2)
Z2 = np.random.rand(66, 2)
X = np.random.rand(66, 66)
#X = cp.Variable(shape=(66, 66), PSD = True)
Y = np.dot(np.dot(Z2.T,X),Z1)
#X = np.array(X1, dtype=object)

print(Z2.shape)
print(Z1.shape)
print(Y.shape)

constr1=(X >> 0)

objective = cp.Minimize(cp.norm(np.dot(np.dot(Z2.T,X),Z1),"nuc"))

But I have a new error !

TypeError                                 Traceback (most recent call last)
<ipython-input-405-3636b0d9a2a6> in <module>()
     19 print(Y.shape)
     20 
---> 21 constr1=(X >> 0)
     22 
     23 objective = cp.Minimize(cp.norm(np.dot(np.dot(Z2.T,X),Z1),"nuc"))

TypeError: ufunc 'right_shift' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''


Steven Diamond

unread,
Jun 28, 2020, 2:34:40 PM6/28/20
to cvxpy
You cannot use numpy functions such as dot on cvxpy objects.
Reply all
Reply to author
Forward
0 new messages