First, NumPy base: Create an array

Source: Internet
Author: User

Tag:table    using    one   lis    array elements      floating point     floating point    int    fill    

NumPy is a scientific computing library of Python that provides the functions of matrix operations, which are generally used in conjunction with SCIPY and Matplotlib. In fact, the list already provides a matrix-like representation, but NumPy provides us with more functions. If contact with Matlab, Scilab, then numpy very good start.

One, module reference
Import NumPy as NP
Second, the creation of the array

Here first look at the format of the multi-dimensional group, the following example is a three-dimensional array, the building of the way to understand the most intuitive.

[#数组一层
[1, 3, 4, 6], #一层1单元
[2, 5, 6, 7] #一层2单元
],
[#数组二层
[1, 3, 4, 6], #二层1单元
[2, 5, 6, 7] #二层2单元
]

Note that the array element must be placed between [] or ()

2.1 Normal creation of one-dimensional array creation
Np.array ([1,2,3,4]) >>>array ([1, 2, 3, 4])
Multidimensional array creation
C=np.array ([[[[[            1,2],[3,4] #3维层 (inner element is thought)        ], #2维层        [            [[5,6],[7,8]]    ], #1维层    [ [[            [9,10],[11,12]        ],        [[            13,14],[15,16] #3维层 (inner element is thinking)        ] #2维层    ] #1维层]) print (c) >>>[[[[1  2]   [3  4]]  [[5 6  ]   [7 8]] [[[9] [one]]  [[13 14]   [15 16]]?
2.1.1 Built-in method creation (Arange creation)

The Arange method is the same as the python built-in method range, which is not mentioned here, see the program

Import NumPy as Npa=np.arange b=np.arange (10,20) c=np.arange (10,20,2) d=np.arange (20,10,-1) print ("Normal play:", a) print ("Gameplay with starting point:", b) print ("Play with Stride:", c) print ("Playing backwards:", d) >>> common gameplay: [0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19] Play with starting point: [10 11 12 13 14 15 16 17 18 19] Play with step: [10 12 14 16 18] Play backwards: [ 20 19 18 17 16 15 14 13 12 11]
2.1.2 Built-in method creation (random creation)

Unlike the random module, this random is a numpy built-in method that is identical in usage, and also looks at the program

Import NumPy as Npa=np.random.random ((2,2)) a random number of #0 to-B=NP.RANDOM.RANDN (2,2) #符合正态分布的随机数c =np.random.randint (2,10, 2,2 ) #从2 a random integer print ("Random number of 0~1:", a) print ("Random number of normal distribution:", b) Print ("Random integer from 2~10:", c) >>>
Random number of 0~1: [[0.93614432 0.56444734] [0.29267612 0.38625066]] random number of normal distribution: [[ -1.26606533 0.68466118] [0.34377799 0.1318619]] random integers from 2~10: [[4 8] [9 6]]

The following table shows all the ways to create Numpy.random:

Method name Analytical
Rand (D0, D1, ..., DN) Random values.
Randn (D0, D1, ..., DN) Returns a sample with a standard normal distribution.
Randint (low[, high, size]) Returns a random integer that is located in the half open interval [low, high].
Random_integers (low[, high, size]) Returns a random integer in the closed interval [low, high].
Random_sample ([size]) Returns the random floating-point number in the half-open interval [0.0, 1.0].
Choice (a[, size, replace, p]) Generates a random sample from a given one-dimensional array.

2.1.3 Built-in method creation (equal to bad ratio creation)

Np.linspace (start value, end value, total number of elements): Creates a one-dimensional equal margin group.
Np.logspace (start value, terminating value, total number of elements): Creates a one-dimensional equal-ratio array.

Import NumPy as Npa=np.linspace (0,10,5) b=np.logspace (0,2,5) print ("Equal margin Group:", a) print ("Equal-ratio array:", b)
>>> equal differential Group: [0. 2.5 5. 7.5 10. ] Equal than array: [ 1. 3.16227766 . 31.6227766 . ]
2.1.4 built-in method creation (special constructs)

Np.zeros (Shape, dtype=float, order= ' C '): Create a full 0 array, shape to be passed in in tuple format.
Np.ones (Shape, Dtype=none, order= ' C '): Create a full 1 array, shape to be passed in in tuple format.
Np.empty (Shape, Dtype=none, order= ' C '): Creates an array with an approaching 0 value, and the shape is passed in in a tuple format.
Np.eye (n, M=none, k=0, dtype=float, order= ' C '): Creates a diagonal matrix, N represents a row, and M represents a column. One of the parameters K is the default value of 0, which represents the offset, positive 1 o'clock to the main diagonal to the right of 1-bit, minus 1 to the main diagonal left down 1-bit.
Np.full (Shape, Fill_value, Dtype=none, order= ' C '): Create an array populated with Fill_value, fill_value as the value you want to populate

Import NumPy as Npa=np.zeros ((2,2)) B=np.ones ((2,2)) C=np.empty ((2,2)) D=np.eye (2, 2, 0) e=np.full ((2,2), +) print (" All 0 Arrays: ", a" print ("All 1 arrays:", b) print ("Approaching 0 arrays:", c) print ("Diagonal matrix:", D) print ("All 17 arrays:", e) >>> all 0 arrays: [[0]. 0.] [0. 0.]] All 1 arrays: [[1]. 1.] [1. 1.]] approaching 0 arrays: [[9.90263869e+067 8.01304531e+262] [2.60799828e-310 0.00000000e+000]] diagonal Matrix: [[1]. 0.] [0. 1.]] All 17 arrays: [[17 17] [17 17]]

  

  

First, NumPy base: Create an array

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.