Christina
Christina

Reputation: 935

Error: not aligned matrix multiplication in Python

I want to perform the following least squares minimization problem in python using cvxpy:

import numpy as np
import cvxpy as cp

# Generate the data
m = 20
n = 15
A = np.random.randn(m, n+2)
b = np.random.randn(m)

# Define and solve the CVXPY problem.
x1 = cp.Variable(1) # a single variable
x2 = cp.Variable(1) # a single variable
x3 = cp.Variable(n) # a vector of length n

cost_func = cp.sum_squares(A .dot([x1, x2, x3]) - b)
problem = cp.Problem(cp.Minimize(cost_func))
problem.solve()

I am always getting the error "shapes (20,17) and (3,) not aligned: 17 (dim 1) != 3 (dim 0)". This means that cvx doesn't consider [x1, x2, x3] as a n+2-vector but a 3-vector.

I tried to replace .dot by @ but also didn't work. How can I do the matrix multiplication inside the sum_squares above?

Any help will be very appreciated!

Upvotes: 0

Views: 188

Answers (1)

sascha
sascha

Reputation: 33532

As indicated in the comment:

cost_func = cp.sum_squares(A .dot([x1, x2, x3]) - b)
->
cost_func = cp.sum_squares(A @ cp.hstack([x1, x2, x3]) - b)

See Docs: Vector/matrix functions

Upvotes: 1

Related Questions