Python Scientific Computing-1 numpy Library

Source: Internet
Author: User

NumPy itself does not provide much advanced data analysis capabilities, understanding Numpuy arrays and array-oriented computations will help you more efficiently use tools such as Pandas

Pyhton saves a set of values in a list, which can be used as an array. In addition, Python has an array modulo fast, but he does not support multidimensional arrays, whether it is a list or array module has no scientific operation function, not suitable for scientific calculations such as matrices. Therefore, NumPy does not use the Python array mechanism, but instead provides the Ndarray array object, which is constantly accessible to the array, and has a rich array of calculation functions, such as the addition, subtraction, multiplication of vectors.

Using the Ndarray array, you first need to import the Numpy function library, or you can import the library directly:

from numpy import *

Or specify an alias for the import library (this method is recommended when multiple libraries are introduced).

Import numpy as np

1. Create an array:

A = np. array([[1,2,4.0],[3, 6,9]])

# View number of rows
A. Ndim

#2

# View the dimensions of the array, return (N,M), where n is the number of rows and M is the number of columns.
A. shape

# (2,3)

# View the types of elements, such as Numpy.int32, Numpy.float64
A. Dtype

#dtype (' float64 ')

2, NumPy's special array mainly has the following kinds:

    • Zeros array: Full zero group, all elements are 0;
    • Ones array: All 1 arrays, elements are all 1;
    • Empty array: A null array with an element full approximation of 0;

NP. Zeros((2,3))

Array ([[0., 0., 0.],
[0., 0., 0.]])

< Span class= "p" >np ones ((3 ,< Span class= "Mi" >4 )

Array ([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.])

1.3 Sequence Array

Arange function: He is similar to the Python range function, but he belongs to the NumPy library, whose parameters are: 开始值、结束值、步长 .

NP. arange(1,5)
Array ([1, 6, 11, 16])

We can also use the Linspace function to create an array of linear sequences whose parameters are:开始值、结束值、元素数量

NP. linspace(0,2,9)
Array ([0.  , 0.25, 0.5, 0.75, 1.  , 1.25, 1.5, 1.75, 2. ])

1.4 Array Index?

Each element of the Numpy array, each line element, and each column element can be accessed with an index, but note: The index starts at 0.

Its operation is basically the same as the list.

A = np. array([[1,2,4.0],[3, 6,9]])

# take the first line element of a
A[0]

Array ([1., 2., 4.])

# Take the second column element of a
A[:,1]

Array ([2., 6.])

# take the third element of the first line of a
A[0,2]

4.0

Python Scientific Computing-1 numpy Library

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.