Create Naarray (1)

Source: Internet
Author: User

    • Create Ndarray

NumPy the method of creating ndarray is quite sufficient, almost the common method of matrix operation.

Conventions:

Import NumPy as NP

Common functions for creating Ndarray are: Np.array, Np.asarray, Np.arange, Np.ones, Np.ones_like, Np.zeros, Np.zeros_like, Np.empty, np.empty_ Like, Np.eye, np.identity

    1. Create a nadrray, i.e. Np.array, with an array-like data structure, Np.asarray
Print Np.array ([1, 2, 3], Dtype=np.int)

[1 2 3]
[Finished in 0.1s]

As shown in the code above, the main parameters of the function are Array_like object and dtype= two. Object can be list, nested list, tuple, nested tuple, or ndarray;

Dtype= includes commonly used np.int, np.float and so on.

It should be noted that the array function deeply replicates the data of object and Np.asarray directly references the data of object.

Print Np.array ([[1, 2], [3, 4]], dtype=np.int)

[[1 2]
[3 4]]
[Finished in 0.1s]

The code above generates a 2-D ndarray. In this two-dimensional ndarray, the outermost [] is axis 0 (axis=0), and the inside [] is axis 1. If it is a 3-D Ndarray, the axis 0,1,2 from outside to inside.

Ndarray overloads the [] operation, so its access is similar to the normal Python list. Subsequent index chapters are described in detail.

Np.asarray and Np.array are similar, except that the data is not deeply copied.

Create Naarray (1)

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.