Differences between a numpy array (ndarray) and a matrix in Python

    numpy array (ndarray)
    • ndarray objects can be multi-dimensional
    • ndarray class is a superclass of matrix class
    • * operator on two ndarray objects results in element-by-element multiplication
    • ** operator results in elementwise exponentiation
    • ndarray class will be present in future versions
    matrix
    • matrix objects are strictly 2-dimensional
    • matrix class is a subclass of ndarray class
    • * operator on two matrix objects results in dot (matrix) product
    • ** operator results in matrix multiplication
    • matrix class will be removed in the future versions
    numpy arrays (ndarrays) are N-dimensional. Numpy matrices are strictly 2-dimensional.
    Matrix objects are a subclass of ndarray, so they inherit all the attributes and methods of ndarrays.
    If a and b are numpy arrays, then a*b is the array formed by multiplying the components element-wise. If m and n are matrices, then m*n is the dot (matrix) product
    If a is an ndarray, a**2 returns an ndarray with each component squared element-wise. If m is a matrix, m**2 returns the matrix product m*m.
    ndarray class is commonly used rather than matrix class, so ndarray is perferred way to represent both arrays and matrices. As per the official documents, it's not advisable to use matrix class since it will be removed in the future versions.


difference_between_a_numpy_array_and_a_matrix
gk