ValueError: matrices are not aligned
I have the following code:
dotp = np.dot(X[i], w) mult = -Y[i] * dotp lhs = Y[i] * X[i] rhs = logistic(mult) s += lhs * rhs
And it throws me the following error (shortened for brevity):
File "/Users/leonsas/Projects/temp/learners/learners.py", line 26, in log_likelihood_grad
s += lhs * rhs
File "/usr/local/lib/python2.7/site-packages/numpy/matrixlib/defmatrix.py", line 341, in __mul__
return N.dot(self, asmatrix(other))
`ValueError: matrices are not aligned`
I expected lhs to be a column vector and rhs to be a scalar and so the operation should work. For debugging, I printed out the dimensions:
print "lhs", np.shape(lhs)
print "rhs", rhs, np.shape(rhs)
What are the outputs:
lhs (1, 18209)
rhs [[ 0.5]] (1, 1)
So they seem to be compatible for multiplication. Any thoughts on what I am doing wrong?
EDIT: More information on what I'm trying to do.
This code is designed to implement a log-similarity gradient for estimating coefficients.
Where z
is the dot product of the weights with the x values.
My attempt at implementing this:
def log_likelihood_grad(X, Y, w, C=0.1):
K = len(w)
N = len(X)
s = np.zeros(K)
for i in range(N):
dotp = np.dot(X[i], w)
mult = -Y[i] * dotp
lhs = Y[i] * X[i]
rhs = logistic(mult)
s += lhs * rhs
s -= C * w
return s
source to share
You have a lhs
shape (1, 18209)
and rhs
shape matrix (1, 1)
and you are trying to multiply them. Since they are of type matrix
(as it appears from the stack trace), the operator *
translates to dot
. The matrix product is only defined for cases where the number of columns in the first matrix and the number of rows in the second are equal, and in your case they are not ( 18209
and 1
). Hence the error.
How to fix it: Check the math behind the code and correct the formula. Perhaps you forgot to transfer the first matrix or something.
source to share