VeilEclipse
VeilEclipse

Reputation: 2866

Transpose of matrix

I have a numpy array named class1of dimension 50x4.

I am find the mean of each column of class1. mean1 = np.mean(class1, axis=0)

np.mean returns me mean1 = [ 5.006 3.428 1.462 0.246]

When I try to mean1.T, it still returns me [ 5.006 3.428 1.462 0.246]

What is the correct method to do tranpose?

Basically I want to do mean1.T * mean1 so that I get a 4x4 matrix

Upvotes: 1

Views: 169

Answers (2)

Daniel
Daniel

Reputation: 19547

Likely the simplest and most robust way for many cases is to use np.outer:

>>> mean1 = np.array([ 5.006,  3.428,  1.462,  0.246])
>>> np.outer(mean1, mean1)
array([[ 25.060036,  17.160568,   7.318772,   1.231476],
       [ 17.160568,  11.751184,   5.011736,   0.843288],
       [  7.318772,   5.011736,   2.137444,   0.359652],
       [  1.231476,   0.843288,   0.359652,   0.060516]])

As mean1 is a 1D array transpose does nothing as there is nothing to transpose. This is a well intentioned feature of numpy, that sometimes catches people off guard.

Upvotes: 5

marco
marco

Reputation: 826

what about

>>> mean1 = mean1[np.newaxis]
>>> mean1 * mean1.T

?

Upvotes: 1

Related Questions