Use a matrix in numpy

Source: Internet
Author: User

Http://blog.csdn.net/pipisorry/article/details/39088003

Numpy is a matrix computing package in Python, which is similar to MATLAB matrix computing.

For more information, see http://www.numpy.org /. The numpy package and its dependent package are included when pythonxy is installed.


(1) Definition Matrix

>>> From numpy import *

>>> A = array ([[1, 2.2, 3], [, 6])

>>> A. Ndim

2

>>> A. Shape

(2, 3)

>>> A. Size

6

>>> Type ()

<Type numpy. ndarray>

>>> A. dtype

Dtype ('float64 ')

 

(2) matrix size conversion (reshape)

>>>. Reshape (6, 1) -- convert the 3x2 matrix into a column vector (6x1). Note that, unlike MATLAB, Matlab transformation is based on column vectors, while numpy is based on row vectors.

So the numpy running result is: 1 4 2.2 5 3 6 (column vector)

The running result of MATLAB is: 1 2.2 3 4 5 6 (column vector)

Note: Many of the corresponding MATLAB vectors are column vectors by default, and row vectors by default in numpy.

 

(3)Basic matrix operations

+ ,-,* (Element multiplication), Dot (matrix multiplication), * =, + =,-=, ** (element multiplication), <,>,... sin, exp ,...

Note: Unlike MATLAB, * Is matrix multiplication and * is element multiplication;


(4)Special Functions

Ones ([...]): a matrix of all elements. For example, ones ([3, 4]) generates a 3x4 matrix of all elements;

Zeros ([...]): All 0 matrices;

Linspace (START, end, num): for example, the result of linspace (0.1, 11) is [0, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1];

Arange (n): generates a vector from 0 to n-1. For example, arange (4) Returns [,].

Random. Random ([...]): generates a random matrix, such as random. Random ([2, 3]) to generate a random number of 2x3 dimensions.

 

(5)Matrix Member

A. Max (): returns the maximum value of the matrix.

A. Min (): returns the minimum value of the matrix.

A. sum (): Sum

A. Mean (): calculate the mean value.

A. Max (axis): 0-evaluate by column, return a row, 1-evaluate by row, return a column

A. Transpose (): transpose

 

(6) matrix access:

For a one-dimensional matrix, a [0] represents the 1st elements of a vector;

For a two-dimensional matrix, a [0, 0] indicates the 1st elements of a, a [0,:] indicates the first row vector of A, and a [:, 0] indicates the first column vector.



Matrix or vector operations should adoptnumpyAndscipyThe method is:

123
import mathimport numpy as npimport scipy as sp

Then you can perform operations, such as addition and division by a number:

123
npdata=np.array(data)data_sum=np.add(npdata,npdata)data_frac=np.true_divide(npdata,2)

You can also press the columnMerge Matrix(The number of rows in the two matrices must be the same ):

new_matrix=np.hstack([mat1,mat2])

Or merge the matrix by row (the columns of the two matrices must be the same ):

new_matrix=np.vstack([mat1,mat2])

The merge matrix command can also be used to merge vectors. However, when merging vectors, it sometimes prompts that the number of rows and columns is incorrect, which may be because a dimension is(N)And the other dimension is(N columns, 1 row)In this casereshapeTo convert:

12
array2=array2.reshape(n)new_array=np.hstack([array1,array2])

To view a matrix or vector dimension, follow these steps:

xxx.shape

For a matrix, if the entire matrix is sorted by rows in the order of the size of the first column of elements, execute:

mat1=mat1[mat1[:,0].argsort()]

Output an array on the screen:

print mat1

Save a matrix to a file:

NP. savetxt (output file name, matrix name)



From:

Http://blog.csdn.net/pipisorry/article/details/39088003

Ref:

Theano learning 2 ---- numpy

Python study Note 1

Use a matrix in numpy

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.