The NumPy package in Python

Source: Internet
Author: User

Through this study you can master NumPy

NumPy Introduction (Get Address)

The main object of NumPy is the homogeneous multidimensional array. That is, in an element (usually a number) table, the elements are of the same type.

The array class of NumPy is made Ndarray. Alias is an array. Numpy.array Unlike the standard Python library class Array.array, the one in the standard library class can handle only one-dimensional arrays and less functionality.

For example, Ndarray is a matrix

About dimension and type operations:
The number of axes (dimensions) of the Ndarray.ndim array.
Ndarray.shape: The dimension of the array.
Ndarray.size: The total number of elements in the array. (m*n)
Ndarray.dtype: An object used to describe the type of an element in an array.

Example:

1 ImportNumPy as NP2 3A = Np.array ([1,3,1])4B = Np.array ([[1],[2],[3]])5C = Np.array ([1,3,1],dtype="float64")6 Print("""7 the dimension of array A is%s8 the dimension of array A is%s9 the total number of elements in array A is%sTen """%(A.ndim, B.shape, a.size)) One Print(""" A type of array a%s - type of array C%s - """% (A.dtype, C.dtype))

Results:

About generating special matrix operations:

The zeros () element is all 0

Ones () elements are all 1

Arange (Start, end, step)

Linspace (start, end, number of elements)

In order to generate a sequence of numbers. NumPy provides a function similar to Arange, which returns a list of parameters (start, end, step)
The function linspace is a better choice because we can specify how many elements need to be created for the function, the parameters are (start, end, number of elements)


Example:

__author__ = "WSX"

Import NumPy as NP

A = Np.zeros ((3,3))
B = Np.ones ((3,3))
C = Np.arange (1.0,5,2.1)
D = Np.linspace (1,5,4)
Print ("" "
The resulting A is: \n%s
The resulting B is: \n%s
The resulting c is: \n%s
The resulting d is: \n%s
"" "% (a,b,c,d))

Results:

About the use of reshape:

A= Np.array ([1,2,3,4,5,6,7,8,9])
b= Np.array ([1,2,3,4,5,6,7,8,9,10,11,12])
Print ("A (3,3) \ n", A.reshape (3,3)) #二维矩阵
Print ("B (1,9) \ n", A.reshape (1,9)) #二维矩阵
Print ("B (2,2,3) \ n", B.reshape (2,2,3)) #三维矩阵

Results:

About the operation of the matrix:

The arithmetic operator on the array is the element-wise one that gets the re-created array, and then writes the result to the new array:

The product operator x in NumPy is element-wise

The product of a matrix can be obtained by using the dot function

(To be Continued ...) )

The NumPy package in Python

Related Article

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.