Matrix and array in numpy, numpymatrixarray

Source: Internet
Author: User

Matrix and array in numpy, numpymatrixarray
Preface

During the implementation of related clustering algorithms, the confusion between array and matrix often occurs when implemented in python. Here is a summary.

Array

In numpy, the most basic (default) type is array. All its operations are performed by elements, that is, they are used for numerical calculation (operations by elements include + ,-,,/,*). Multiplication example:

    from numpy  import *    >>> a=array([1,2])    >>> a    array([1, 2])    >>> b=array([2,3])    >>> b    array([2, 3])    >>> c=a*b    >>> c    array([2, 6])    >>> dot(a,b)    8

The multiplication of two arrays * refers to the multiplication of the corresponding elements; the dot of the two arrays indicates the multiplication of the matrix.

  • If a is an array, a. T indicates transpose.
  • Convert array to matrix using asmatrix ()

  • Most numpy functions return the array type instead of the matrix type.

Matrix

The special type in numpy appears as a subclass of array, so it inherits all the features of array and has its own special places, it is specially used to process linear algebra operations (* indicates the multiplication of matrices, but the Division/of two matrices indicates the division of the corresponding elements .). Multiplication example:

    >>> m=mat([2,3])    >>> m    matrix([[2, 3]])    >>> n=([1,2])    >>> n    [1, 2]    >>> p=m*n    Traceback (most recent call last):      File "<stdin>", line 1, in <module>      File "C:\Python27\lib\site-packages\numpy\matrixlib\defmatrix.py", line 341, i    n __mul__        return N.dot(self, asmatrix(other))    ValueError: objects are not aligned    >>> dot(m,n)    matrix([[8]])    >>> multiply(m,n)    matrix([[2,6]])

* Multiply two matricesThe error is that m columns are not equal to n rows, that is, aligned. If m columns are aligned, a matrix is returned by multiplying the corresponding elements.Represents the multiplication of two matrices. The dot of the two matrices indicates that the matrices are multiplied. The multiply of the two matrices represents the multiplication of the corresponding elements. *

  • In matrix,. H,. A,. I indicates the concatenation, transpose, and inverse matrix.
  • Convert a matrix to an array Using asarray ()
  • Asanyarray () is consistent with your input type.
A hard-to-understand point of array and matrix

The concept of rank is involved here. In linear algebra (math), rank indicates rank, but it must be clear that rank in numpy is not a concept of rank, but a concept of dimension, to understand this, read this article: Explanation of the multi-dimensional arrays data structure:
Multi-dimensional arrays Data Structure

It is understood as rank for the time being. Although this is wrong, it can be said something. (In the actual array and matrix, rank described in English is understood by the rank of linear algebra, but the dimensions is equal to or equal to the number of dimensions in English, it is required that the dimesions of matrix must be 2. Here it actually refers to the rank, and dimensions is the real understanding form in numpy)

The array must be 1 (N * N, etc.) or greater than 2.
Matrix requires that the rank must be 2 (rank must be 2)

  • The following is an understanding of ndim and size:
    >>> a        array([1, 2])        >>> b        array([[1, 2],               [2, 3]])        >>> c        matrix([[1, 2, 3, 4],                [2, 3, 4, 5]])        >>> d        array([[1, 2, 3, 4],               [2, 3, 4, 5]])        >>> e        matrix([[1, 2, 3, 4],                [2, 3, 4, 5],                [3, 4, 5, 6]])        >>> a.size        2        >>> a.ndim        1        >>> b.size        4        >>> b.ndim        2        >>> c.size        8        >>> c.ndim        2        >>> d.size        8        >>> d.ndim        2        >>> e.ndim        2        >>>e.size        12

Here ndim is used to calculate rank, so we will find that all matrix Values are 2, but there will be differences in array values, which need to be calculated. Size returns the number of elements.

  • Understanding about dim, shape, rank, dimension and axis in numpy:

Stackoverflow address

Supplement

How to make

M = matrix ([[1], [2], [3], [4]) how to transform to array ([1, 2, 3, 4])

Elegant method:

>>> x=matrix(arange(12).reshape((3,4)))>>> xmatrix([[ 0,  1,  2,  3],        [ 4,  5,  6,  7],        [ 8,  9, 10, 11]])>>> x.getA1()array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
Summary

It is better to use array in application. The only defect of array is to use dot instead of * to represent the multiplication of matrices *.

For the remaining operation functions of array and matrix in numpy, see the wiki address: http://wiki.scipy.org/NumPy_for_Matlab_Users

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.