The numpy of the Python operation learning------The slice index of the array and the loop traversal, condition and Boolean array,

Source: Internet
Author: User
Tags rand

The slice index of the array:

  The tile index of the array is very similar to the list, and the following code is a simple explanation

1A = Np.random.rand (+). Reshape (4, 4)2 Print("Array a:\n", a)3 Print(A[-1][1:4])4 out[1]: 5 Array A:6[0.04175379 0.43013992 0.5398909 0.40638248]7[0.3305902 0.11958799 0.48680358 0.30755734]8[0.00893887 0.3848716 0.21018253 0.88170218]9[0.80198391 0.4922656 0.67535542 0.64647139]]Ten[0.4922656 0.67535542 0.64647139]

Because it is similar to a list, and to symbolize the characteristics of a multidimensional array, it is not much elaborated here. There is a need to refer to the list of relevant knowledge.

Loop traversal of the array:

1A = Np.random.rand (9). Reshape (3, 3)2 Print("Array a:\n", a)3  forRowinchA:4     Print(ROW)#output of a row of rows5  forIteminchA.flat:6     Print(item)7 #General-purpose cyclic functions8For_test = Np.apply_along_axis (Np.mean, axis=0, Arr=a)#Apply_along_axis (func1d, axis, arr, *args, **kwargs)9 Print("Debug of Np.apply_along_axis: \ n", for_test)#axis=0 to column, Axis=1 to rowTenout[2]:  One Array A: A[[0.97420758 0.20766438 0.52942127] -[0.82673775 0.44288163 0.41729451] -[0.1373707 0.68103565 0.92256133]] the[0.97420758 0.20766438 0.52942127] -[0.82673775 0.44288163 0.41729451] -[0.1373707 0.68103565 0.92256133] -0.9742075804081937 +0.20766438289931244 -0.5294212665874829 +0.8267377457345865 A0.44288163199889663 at0.4172945079908593 -0.13737070280419617 -0.6810356459375222 -0.922561331228303 - Np.apply_along_axis Debug: -[0.64610534 0.44386055 0.62309237]

The Np.apply_along_axis () method is best used when we are doing column-by-row operations on the matrix. Note that the first parameter is a method, and the method can be a function method that operates on each element of the matrix itself.

Conditions and Boolean arrays:

 1  a = Np.random.rand (9). Reshape (3, 3)  2  print  (A < 0.5) #   output boolean array  3  print  (A[a < 0.5]) #   output true for element  4   out[3":   5  [[true False true]  6   [false True]  7   [false True]]  8  [0.19353844 0.03944841 0.38137674 0.3069755]  

Array shape changes:

1A = Np.random.rand (9). Reshape (3, 3)2A = A.ravel ()#A is a new array at this point.3 Print(a)#to tile an array into a one-dimensional array4A.shape = (3, 3)#You can also use reshape5 Print(a)6 out[4]: 7[0.83017305 0.11660585 0.83060752 0.221212 0.35489551 0.7492569680.61087204 0.85969402 0.90966368]9[[0.83017305 0.11660585 0.83060752]Ten[0.221212 0.35489551 0.74925696] One[0.61087204 0.85969402 0.90966368]]

Note the transpose with A.T, or with A.transpose ().

The numpy of the Python operation learning------The slice index of the array and the loop traversal, condition and Boolean array,

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.