In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the relevant knowledge of "what is the use of Python Numpy index and slice". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "what is the use of Python Numpy index and slice" can help you solve the problem.
1. Index and slicing
Elements in an array can be accessed or modified by indexing and slicing, just like slicing a list.
The following is directly implemented in code, and the specific operation mode and meaning are subject to the code comments:
(1) Index slicing through subscript and built-in function
"Author:XiaoMadate:2021/12/30" import numpy as np a = np.arange (10) # create an one-dimensional array print (a) I = slice (2,7,2) # from 0 to 9, indexed every other element from 2 to 7, that is, start for 1 stop, 7 for 2print (a [I])
The resulting output is as follows:
(2) use colon-separated parameters for slice indexing.
"Author:XiaoMadate:2021/12/30" import numpy as np a = np.arange (10) # create an one-dimensional array print (a [2: 7:2]) from 0 to 9 # split the start, end, and step size with colons to represent the index slice
The resulting output is as follows:
(3) Index and slice some elements
a. Cut the middle part.
"Author:XiaoMadate:2021/12/30" import numpy as np a = np.arange (10) # create an one-dimensional array print (a [2:5]) # from 0-9 and slice from 2-5
The output is as follows:
b. All after cutting an element
"" Author:XiaoMadate:2021/12/30 "" import numpy as np a = np.arange (10) # create an one-dimensional array print (a [3:]) # from 0 to 9 to print from 3 until the entire array is printed
The output is as follows:
(4) Index and slice the multi-dimensional array.
"" Author:XiaoMadate:2021/12/30 "import numpy as np b = np.array ([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print ('start slicing from array index a [2:]:') print (b [2:]) print ('second column of the array:') print (b [, 2]) print ('second row of the array:') print (b [2) ) print ('slice from the second column:') print (b [, 1:])
The output is as follows:
two。 Advanced index
(1) Integer array index
"" Author:XiaoMadate:2021/12/30 "import numpy as np b = np.array ([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) c = b [[1, 0], [1, 0]] print ('index slices of elements at array (1) and (0):') print (c)
The output is as follows:
(2) Boolean index
The target array is indexed by a Boolean array, and the Boolean array can obtain qualified array elements through Boolean operations.
As follows, we can extract no less than 5 from an array:
"Author:XiaoMadate:2021/12/30" import numpy as np a = np.array ([[1, 2, 3, 5], [6, 9, 2, 0], [9, 3, 2, 7]]) print ('the number of not less than 5 in the array is as follows:') print (a [a > = 5]) "
The output is as follows:
(3) fancy index
Fancy indexing refers to indexing using an array of integers. The fancy index takes the value based on the value of the index array as the subscript of an axis of the target array. For using an one-dimensional integer array as an index, if the target is an one-dimensional array, the result of the index is the row of the corresponding subscript, and if the target is a two-dimensional array, it is the element of the corresponding position. Unlike slicing, fancy indexes always copy data to a new array.
"" Author:XiaoMadate:2021/12/30 "import numpy as np x = np.arange (32). Reshape ((8,4)) print ('generated array is:') print (x) print ('incoming sequential index array:') print (x [4,2,1,7]]) print ('incoming reverse index array:') print (x [[- 4,-2,-1) -1]]) print ('pass in multiple index arrays:') print (x [np.ix _ ([1, 5, 7, 2], [0, 3, 1, 2])) # outputs the value of the combination of two vectors in different dimensions
The output is as follows:
This is the end of the introduction on "what is the use of Python Numpy indexes and slices". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.