1

I have the following numpy Array

import numpy as np
A = np.array([[0.5, 0.5]])

Now I want to calculate A^t*A, for which I thought of the following

np.dot(A.T,A)

What I want to get is an Array in form of

A_new = np.array([[0.0025, 0.0025], [0.0025,0.0025]])

But what I actually get is just a number

A_new = 0.005

How can I do this kind of array multiplication? Shouldn't 2x1 shape times 1x2 shape result in 2x2 shape?

1
  • I can't reproduce your error. For me, the result of A = np.array([[0.5, 0.5]]) followed by print(np.dot(A.T,A)). Is there a chance that you have A = np.array([0.5, 0.5]) (a one dimensional array) by mistake? If your question does reflect your code accurately, then it might be helpful if you would share the result of np.__version__. Commented May 2, 2022 at 22:48

3 Answers 3

1

Use matmul:

>>> np.matmul(A.transpose(), A)
array([[0.25, 0.25],
       [0.25, 0.25]])
Sign up to request clarification or add additional context in comments.

Comments

0

You can do this as:

result = A*A.T

Comments

0

Your array - changed a bit to make the result a bit more interesting:

In [28]: A = np.array([[0.5, 0.25]])
In [29]: A.shape
Out[29]: (1, 2)
In [30]: A.T.shape
Out[30]: (2, 1)

Matrix multiplication

In [31]: A.T@A
Out[31]: 
array([[0.25  , 0.125 ],
       [0.125 , 0.0625]])

ELement wise, with broadcasting, does the same thing, since the @ summation is on a size 1 dimension:

In [32]: A.T*A
Out[32]: 
array([[0.25  , 0.125 ],
       [0.125 , 0.0625]])

A*A.T is the same thing, but matrix multipication produces a (1,1):

In [33]: [email protected]
Out[33]: array([[0.3125]])

If A was 1d by mistake, you could get a scalar value

In [34]: A1 = A.ravel()
In [35]: A1.shape
Out[35]: (2,)
In [36]: A1.T.shape
Out[36]: (2,)    
In [38]: A1.T@A1
Out[38]: 0.3125

dot does the same thing:

In [39]: np.dot(A.T,A)
Out[39]: 
array([[0.25  , 0.125 ],
       [0.125 , 0.0625]])
In [40]: np.dot(A1.T,A1)
Out[40]: 0.3125

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.