146 percent Russian
146 percent Russian

Reputation: 2096

Numpy matrix multiplication error

I have 2 big matrices:

       Xn = np.matrix(X)
       Xnt = Xn.transpose()

Then Xn is like this:

   >>> Xn
   matrix([['0,208', '0,22', '0,208', ..., '0,194', '0,205', '0,205'],
    ['0,22', '0,208', '0,214', ..., '0,205', '0,205', '0,214'],
    ['0,208', '0,214', '0,22', ..., '0,205', '0,214', '0,211'],
    ..., 
    ['0,214', '0,214', '0,208', ..., '0,199', '0,211', '0,226'],
    ['0,214', '0,208', '0,208', ..., '0,211', '0,226', '0,252'],
    ['0,208', '0,208', '0,211', ..., '0,226', '0,252', '0,24']], 
   dtype='|S5')

But I want to multiply Xn and Xnt. An error occurs.

  >>> print(Xn*Xnt)

    Traceback (most recent call last):
    File "<pyshell#2>", line 1, in <module>
    print(Xn*Xnt)
    File "C:\Python27\lib\site-packages\numpy\matrixlib\defmatrix.py", line 330, in __mul__
    return N.dot(self, asmatrix(other))
    ValueError: data type must provide an itemsize

What's the problem?

Upvotes: 2

Views: 4114

Answers (1)

mtrw
mtrw

Reputation: 35088

Your matrix elements are strings - note dtype='|S5' when you print the matrix. Try converting the elements of X from strings to floats first.

Upvotes: 4

Related Questions