Python scientific computing 1: numpy fast data processing (1)

Source: Internet
Author: User

The following are important for recording numpy usage. It is recommended to try it out in person for better results.


Import numpy as np


1, a = np. array ([1, 2, 3, 4]) by. shape knows that a is a column vector, and a = ([[1, 2, 3, 4]) is a horizontal volume;

2, c = array ([1, 2, 3, 4],
[4, 5, 6, 7],
[7, 5, 6, 8]) assigning values to the shape attribute of c can change the shape of the array. C. shape = is to convert the 3*4 array into a 4*3 array. The result is:

Array ([[1, 2, 3], when you are too lazy to calculate the number of rows in each row, you can write: c. shape = 4,-1 is the same. However, an error will be reported when the sharding fails.
[4, 4, 5],
[6, 7, 7],
[5, 6, 8])

3, d = c. reshape: Deformation of data without changing the original data;

4. type (c): view the data type of c. dtype: view the data type in c. Therefore, you can also set the data type when defining it. B = np. array ([1.2, 3.1,], dtype = np.int), the result is array ([1, 2, 2, 3])

5. arange () is equivalent to the range () function. It specifies the start value, end value, and step to create a one-dimensional array. Note that the end value is not included, for example, B = np. arange (0.2, 0.2). The result is: array ([0., 0.4, 0.6, 0.8,]). However, linspace () must include the final value. It is used to create a one-dimensional array by specifying the start value, end value, and number of data. Example: B = np. linspace (, 10) is to divide the range from 1 to 9 into nine parts. The result is: array ([1 ., 1.88888889, 2.77777778, 3.66666667, 4.55555556,
5.44444444, 6.33333333, 7.22222222, 8.11111111, 9.]);

6. zeros (), ones (), and empty () generate the corresponding array according to the specified shape. Example: np. ones (2, 3) array ([1 ., 1 ., 1.], [1 ., 1 ., 1.]) np. zeros (3, 4), dtype = np. float) array ([0 ., 0 ., 0 ., 0.], [0 ., 0 ., 0 ., 0.], [0 ., 0 ., 0 ., 0.]) the only difference is np. empty (266) array ([[2.43631958e-, 0.20.0000e + 000], [0.20.0000e + 000, 0.20.0000e + 000],
[0.00000000e + 000, 0.00000000e + 000], [0.00000000e + 000, 0.00000000e + 000], [0.00000000e + 000, 0.00000000e + 000, instead, it is the space address allocated to the data.

7. Slice a = np. arange (, 1) uses a [] to represent an array consisting of all elements from a [3] to a [6] = array ([3, 4, 5, 6]); you can add step, but do not include the last element. A [:-1], which contains a negative number, indicates starting from the back. If the step size is a negative number, it indicates the reverse order, for example, a [7: 1: -1] The result is array ([7, 6, 5, 4, 3, 2]).

8. Get the elements from the integer list to form a new array: a [[1, 2, 3, 5]. Then retrieve the elements in a to form a new array. The result is array ([1, 2, 2, 2, 3, 5]); subscripts are also negative;

9. The value of np. random. rand (10) is 0 ~ 1 random array;

10. Remember a special example:

A = np. arange (, 10). reshape (-) + np. arange)
>>>
Array ([[0, 1, 2, 3, 4, 5],
[10, 11, 12, 13, 14, 15],
[20, 21, 22, 23, 24, 25],
[30, 31, 32, 33, 34, 35],
[40, 41, 42, 43, 44, 45],
[50, 51, 52, 53, 54, 55]) a [3, 3: 6] indicates columns 4th to 6 of the 4th rows. The result is array ([33, 34, 35]); a [: 2] indicates the first two columns: array ([0, 1, 2, 3, 4, 5], [10, 11, 12, 13, 14, 15]) and

A [:, 2] indicates 3rd columns, array ([2, 12, 22, 32, 42, 52])

A [1:,: 2] indicates
Array ([[10, 12, 14],
[20, 22, 24],
[30, 32, 34],
[40, 42, 44],
[50, 52, 54])

Note:

A [: 2] And a [[0, 1] a [[0, 1],:] can both represent the above results. However, the first slice is sequential, and the second slice is random. For example:

>>> A [[1, 2, 0, 2]
Array ([[10, 11, 12, 13, 14, 15],
[20, 21, 22, 23, 24, 25],
[0, 1, 2, 3, 4, 5],
[20, 21, 22, 23, 24, 25])


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.