Error: not aligned matrix multiplication in Python
Asked Answered
A

1

0

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!

Arakawa answered 12/2, 2021 at 1:45 Comment(2)
To join variables use vstack, hstack or something of this kind.Neils
@MichalAdamaszek Thank you. I tried it and doesn't work. If you have any intuition please provide me a detailed answer about how can I solve this problem.Arakawa
R
1

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

Rico answered 12/2, 2021 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.