4

I have the following snippet.

values = [[0.1, 0.7, 0.5], [0.6, 0.3, 0.2], [0.2, 0.8, 0.77]]
A = np.array(values).reshape(3,3)
print A.shape
print np.mean(A, axis=1)

B = np.mat(np.random.rand(3, 3));
print B.shape
print np.mean(B, axis=1)

Output of print statements:

(3, 3)
[ 0.43333333  0.36666667  0.59      ]

(3, 3)
[[ 0.47252016]
 [ 0.44380355]
 [ 0.51070646]]

I have two same shaped numpy array's with different values as an input, one is generated with the rand function the other is a python list created with the array function and calling reshape on it.

However, the shape of the mean return's a different shape for both, even though the input shapes are the same. Any Ideas what can cause this?

sshashank124
  • 31,495
  • 9
  • 67
  • 76
user1767754
  • 23,311
  • 18
  • 141
  • 164
  • That absolutely does, just figured out going over it... thanks, that explains it! – user1767754 Jan 20 '20 at 06:38
  • 1
    Many of the functions/methods that take an `axis` parameter also take a `keepdims` parameter. If `True`, it can make the array mean behave more like the matrix one. – hpaulj Jan 20 '20 at 08:08

1 Answers1

3

As explained in the documentation for numpy.matrix,

A matrix is a specialized 2-D array that retains its 2-D nature through operations

Additionally,

It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.

sshashank124
  • 31,495
  • 9
  • 67
  • 76