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.

enter image description here

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

      

+3


source to share


2 answers


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.

+6


source


vector form on numpy lib is like (3,). when you try to multiply them with the np.dot (a, b) function it gives a dull error. At this point, you should use the np.outer (a, b) function.



0


source







All Articles