Third, NumPy Base: array element Query, modify

Source: Internet
Author: User
Tags true true

First, the index
    • The order in which the values are taken is from the perimeter to the innermost element position, which is written sequentially.
1.1. Single Value Index
Import NumPy as NPA = Np.arange (+). Reshape (2,2,4) print ("original array: \ n", a) print ("single value index: \ n", a[1][1][2]) >>> original array: [[[0]  1  2  3]  [4  5 6 7] [[8 9] [12 13 14  15]]] Single value index value: 14
1.2. Fancy Index
    • You can index more than one element by using an array, indicating the position of the element in the array.
Import NumPy as Npa=np.arange. Reshape (5,5) #定义一个5 two-dimensional array print ("original array: \ n", a) print ("Fancy indexed multi-value (outer row value): \ n", A[[3, 3, 1, 4)] ) #注意这里一定传入的是数组格式print ("Fancy indexed multi-value (two times value, pre-column): \ n", a[[0,2,2,4],[0,2,4,4]]) #注意这里一定传入的是数组格式 >>> original array: [[0  1  2  3  4] [5  6  7 8 9  ] [10 11 12 13 14] [15 16 17 18 19] [20 21 22 23 24]] fancy indexed multi-value (outer row value): [[5] [7] [6] [9] [8] [+] [+ 20] 21 22 23]  ] Fancy indexed multi-value (outer row value): [24 0 12 14]
1.3. Boolean index
    • You can generate a Boolean array by setting the filter criteria, and the results that match the criteria are reversed by a Boolean array
Import NumPy as Npa=np.random.random ((bis)) #生成4 a two-dimensional array of x4 b=a>0.5# generate a Boolean array with a>0.5 as filter print (b) print (A[b]) # Take the original value out of a Boolean array and turn it into a one-dimensional array >>>[[true true false] [true True True  false] [true  true True]  True] [True False  True false]][0.72159895 0.85017348 0.88332226 0.7494597  0.8514071  0.91133411 0.89253366 0.80979503 0.61827433 0.94660476 0.67418788]
1.4. Indexer Ix_ ()
    • Ix_ () can traverse in axes, rather than single-point traversal in fancy indexes, compared to fancy indexes
Import NumPy as Npa=np.arange. Reshape (4,6) #生成4 x4 Two-dimensional array b=a[[1,3],[3,5]] #花式索引c =a[np.ix_ ([1,3],[3,5]) #索引器d =a[[ 1,3]][:,[3,5]]print ("original array: \ n", a) print ("Fancy index: \ n", b) print ("Indexer index: \ n", c) print ("C equivalent to d:\n", D) >>> original array: [[0  1  2  3  4  5] [6  7  8  9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23]] Fancy Index: [9 23] Indexer index: [[9] [23]]c equivalent to D: [[9 11] [21 23]]

  

Second, slicing
    • NumPy slicing in the same way as Python by default
    • Slice down content share the same memory space as the original array
Import NumPy as NPA = Np.arange (+). Reshape (2,2,4) print ("original array: \ n", a) print ("Take a value: \ n", a[1][1][2]) print ("Slice range value: \ n", a[1 ][1][2:4]) print ("Larger slice traversal:: \ n", A[0][:][0:4]) >>> original array: [[[0  1  2  3]  [4  5  6  7] ] [[8  9]  [12 13 14 15]]] Take a value: 14

[14 15]
A larger range of slice traversal:
[[0 1 2 3] [4 5 6 7]

Third, Element modification
    • Direct assignment of variables by index and slice can be modified

Third, NumPy Base: array element Query, modify

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.