0
votes

I'm trying to multiply two matrices of dimensions (17,2) by transposing one of the matrices

Here is example p1

    p1 = [[ 0.15520622 -0.92034567]
 [ 0.43294367 -1.05921439]
 [ 0.7569707  -1.15179354]
 [ 1.08099772 -1.15179354]
 [ 1.35873517 -0.96663524]
 [-1.51121847 -0.64260822]
 [-1.32606018 -0.87405609]
 [-1.00203315 -0.96663524]
 [-0.67800613 -0.96663524]
 [-0.3539791  -0.87405609]
 [ 0.89583942  1.02381648]
 [ 0.66439155  1.3478435 ]
 [ 0.3866541   1.48671223]
 [ 0.15520622  1.5330018 ]
 [-0.07624165  1.5330018 ]
 [-0.3539791   1.44042265]
 [-0.58542698  1.20897478]]

here is another example matrix p2

 p2 = [[ 0.20932473 -0.90029958]
 [ 0.53753779 -1.03849455]
 [ 0.88302521 -1.10759204]
 [ 1.24578701 -1.02122018]
 [ 1.47035383 -0.77937898]
 [-1.46628927 -0.69300713]
 [-1.29354556 -0.9521227 ]
 [-0.96533251 -1.03849455]
 [-0.63711946 -1.00394581]
 [-0.3089064  -0.90029958]
 [ 0.86575084  1.06897874]
 [ 0.55481216  1.37991742]
 [ 0.26114785  1.50083802]
 [ 0.03658102  1.51811239]
 [-0.1879858   1.50083802]
 [-0.46437574  1.37991742]
 [-0.74076568  1.08625311]]

I'm trying to multiply them using numpy

import numpy

print(p1.T * p2)

But I'm getting the following error

operands could not be broadcast together with shapes (2,17) (17,2) 

This is the expected matrix multiplication output

[[11.58117944  2.21072324]
 [-0.51754442 22.28728876]]

Where exactly am I going wrong

3
And what would the desired output look like? P.S. reduce example to 3 (as least as possible) rows. - zipa
The result should be a 2X2 something like this - Saikiran
[[ 0.99879867 0.04900222] [-0.04900222 0.99879867]] - Saikiran
Are you sure you need multiplication, I answered with np.dot but your result is not what you get with dot product. - zipa
How did you define p1 and p2 before? With np.matrix? How do you read them from JSON? with np.array? Star multiplication is different for the 2 classes of array. - hpaulj

3 Answers

1
votes

Matrix multiplication is done with np.dot(p1.T,p2), because A * B means matrix elements-wise multiply.

0
votes

So you should use np.dot:

p1.T.dot(p2)
0
votes

Sorry for a vague question. Initially, I was getting p1 and p2 values from numpy matrix. I later stored them in json file as list for optimization by using

.tolist()

method and was reading it back as numpy array using

numpy.array()

method which is apparently wrong..I changed my code to read the numpy array using

numpy.matrix()

method which seems to solve the issue. Hope this helps someone