[Reprinted] numpy tutorial: matrix and its operation

Reference link: numpy.asmatrix in Python

http://blog.csdn.net/pipisorry/article/details/48791403

Introduction to numpy matrix

There are two different data types (matrix and array) in the NumPy function library, both of which can be used to process digital elements represented by rows and columns. Although they look similar, performing the same mathematical operations on these two data types may get different results, and the matrices in the NumPy library are equivalent to the matrices in MATLAB.

The matrix object in the numpy module is numpy.matrix, which includes matrix data processing, matrix calculation, and basic statistical functions, transposition, invertibility, etc., including the processing of complex numbers, all in the matrix object.

About the choice between matrix and two-dimensional array in numpy

Matrix is ​​a branch of array. Matrix and array are common in many cases, but the official recommendation is that if the two can be used in common, then choose array, because array is more flexible and faster. Many people also translate two-dimensional array into matrix. The advantage of matrix is ​​the relatively simple operation symbols, such as the symbol * for matrix multiplication, but the method .dot() for array multiplication.

Note: array * mat is also matrix multiplication, not dot multiplication.

The advantage of array is that it not only represents two dimensions, but also 3, 4, 5... dimensions, and in most Python programs, array is also more commonly used.

Note: 

1. Two-dimensional arrays in numpy do not support inversion (for gui), but you can use the linalg.inv() function in scripy to invert.

2. lz recommends using a two-dimensional ndarray instead of matrix, combined with the scripy.linalg library to achieve all matrix operations. [Scipy tutorial-linear algebra library linalg]

Pippi Blog

 

 

Matrix objects

Create example

np.matrix

>>> a = np.matrix(’1 2; 3 4’)>>> print a[[1 2][3 4]]

>>> np.matrix([[1, 2], [3, 4]])matrix([[1, 2],[3, 4]])

Note:

1. class numpy.matrix(data,dtype,copy): returns a matrix, where data is an ndarray object or character form; dtype: is the type of data; copy: is the type of bool.

2. The line breaks of the matrix must be separated by semicolons (;), the internal data must be in the form of strings (''), and the elements of the matrix must be separated by spaces.

3. The data in the matrix can be an array object.

e.g. asmatrix

>>> x = np.array([[1, 2], [3, 4]])>>> m = np.asmatrix(x)>>> x[0,0] = 5>>> mmatrix([[5, 2],[3, 4]])

Attribute

 

Matrix object methods

 

 

[numpy-ref-1.8.1 - 1.6.2 Matrix objects p120]

Matrix matrix object method usage example

 

>>> a = np.asmatrix('0 2 7; 3 4 8; 5 0 9')

>>> a.all()

False

>>> a.all(axis=0)

matrix([[False, False,  True]], dtype=bool)

>>> a.all(axis=1)

matrix([[False],

[ True],

[False]], dtype=bool)

 

ü Astype method

>>> a.astype(float)

matrix([[ 12.,   3.,   5.],

[ 32.,  23.,   9.],

[ 10., -14.,  78.]])

 

ü Argsort method

>>> a=np.matrix('12 3 5; 32 23 9; 10 -14 78')

>>> a.argsort()

matrix([[1, 2, 0],

[2, 1, 0],

[1, 0, 2]])

 

ü Clip method

>>> a

matrix([[ 12,   3,   5],

[ 32,  23,   9],

[ 10, -14,  78]])

>>> a.clip(12,32)

matrix([[12, 12, 12],

[32, 23, 12],

[12, 12, 32]])

 

ü Cumprod method

>>> a.cumprod(axis=1)

matrix([[    12,     36,    180],

[    32,    736,   6624],

[    10,   -140, -10920]])

 

ü Cumsum method

>>> a.cumsum(axis=1)

matrix([[12, 15, 20],

[32, 55, 64],

[10, -4, 74]])

 

ü Tolist method

>>> b.tolist()

[[12, 3, 5], [32, 23, 9], [10, -14, 78]]

 

ü Tofile method

>>> b.tofile('d:\\b.txt')

 

ü compress() method

>>> from numpy import *

>>> a = array([10, 20, 30, 40])

>>> condition = (a > 15) & (a < 35)

>>> condition

array([False, True, True, False], dtype=bool)

>>> a.compress(condition)

array([20, 30])

>>> a[condition]                                      # same effect

array([20, 30])

>>> compress(a >= 30, a)                              # this form a

so exists

array([30, 40])

>>> b = array([[10,20,30],[40,50,60]])

>>> b.compress(b.ravel() >= 22)

array([30, 40, 50, 60])

>>> x = array([3,1,2])

>>> y = array([50, 101])

>>> b.compress(x >= 2, axis=1)                       # illustrates 

the use of the axis keyword

array([[10, 30],

[40, 60]])

>>> b.compress(y >= 100, axis=0)

array([[40, 50, 60]])

 

Pippi Blog

 

 

The Matrix class numpy matrix class

Build matrix

 

Note: numpy.mat(data, dtype=None)   Interpret the input as a matrix.Unlike matrix, asmatrix does not make a copy if the input is already a matrix or an ndarray. Equivalent to matrix(data, copy=False).

[numpy-ref-1.8.1 - 3.1.7 The Matrix class p484]

Pippi Blog

 

Matrix library (numpy.matlib)

This module contains all functions in the numpy namespace, with the following replacement functions that return matrices instead of ndarrays.

Functions that are also in the numpy namespace and return matrices

 

Replacement functions in matlib

 

[numpy-ref-1.8.1 - 3.21 Matrix library p940]

from:http://blog.csdn.net/pipisorry/article/details/48791403

Guess you like

Origin blog.csdn.net/u013946150/article/details/113057632