Skip to content Skip to sidebar Skip to footer

Difference Between Numpy.matrix.a1 And Ravel

To my knowledge, I haven't seen any posts regrading this, if any, please note me. According to SciPy website, numpy.matrix.A1 is equivalent to np.asarray(x).ravel(). One example w

Solution 1:

NumPy matrices (np.matrix) are always 2D. (Mathematically, the strict definition of a matrix, rather than a matrix or vector.)

From np.matrix.ravel:

Return the matrix flattened to shape (1, N) where N is the number of elements in the original matrix.

Some motivation for NumPy matrices is for Matlab users. See here for some of the finer points on NumPy matrix versus array.

In brief, a NumPy array (the result of asarray(x) here) can be a 1-dimensional structure. Matrices can be a minimum of 2d. type(np.asarray(x)) is, not shockingly, an array. (Not to be confused with np.asanyarray(), for which your result would be a matrix because it's an array subclass.

Lastly, you noted:

it passes the equality test (x == np.asarray(x))

I see how this could be a bit confusing. Technically, you want to use np.array_equal(x, np.asarray(x)), although that still evaluates to True. However, NumPy logic testing is generally meant to be data-structure agnostic, in general:

np.array_equal([1, 2, 3], np.array([1, 2, 3]))
# True

(Here is the source for array_equal()--both are cast to arrays.)

The bottom line is that their "minimum dimensionalities" are different, and one is a subclass of the other.

issubclass(np.matrix, np.ndarray)
# True

Post a Comment for "Difference Between Numpy.matrix.a1 And Ravel"