当前位置:网站首页>Notes | numpy-08 Advanced index
Notes | numpy-08 Advanced index
2022-07-03 04:50:00 【CY3761】
#%%
# NumPy More than average Python Sequences provide more indexing methods . In addition to the indexes with integers and slices seen before , Arrays can be indexed by integer arrays 、 Boolean index and fancy index
#%% md
## Integer array index
#%%
import numpy as np
x = np.array([
[1, 2],
[3, 4],
[5, 6]
])
y = x[[0, 1, 2], [0, 1, 0]] # Take... Separately x Of x[0,0] = 1 x[1,1] = 4 x[2,0] = 5
y
#%%
# The following example obtained 4X3 Elements of four corners in an array . The row index is [0,0] and [3,3], And the column index is [0,2] and [0,2]
x = np.array([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11]
])
x
#%%
rows = np.array([[0, 0], [3, 3]]) # r Row index 0=r0, 2=r0, 9=r3, 11=r3
cols = np.array([[0, 2], [0, 2]]) # r Row index 0=c0, 2=c2, 9=c0, 11=c2
y = x[rows, cols]
y
#%%
# You can slice : or … Combined with index array
import numpy as np
a = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
a
#%%
a[1:3, 1:3] # Row index 1,2 Column index 1,2 [[5,6],[8,9]]
#%%
a[1:3, [1,2]] # Row index 1,2 Column index 1,2 ditto
#%%
a[...,1:] # All right , Column index 1 Start to finish [[2,3], [5,6], [8,9]]
#%% md
## Boolean index
We can index the target array through a Boolean array
The Boolean index uses Boolean operations ( Such as : Comparison operator ) To get an array of elements that meet the specified criteria
#%%
# Get greater than 5 The elements of
x = np.array([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11]
])
x
#%%
b = x > 5 # nr1c
b
#%%
x[b]
#%%
# Used ~ ( Complement operator ) To filter NaN
import numpy as np
a = np.array([np.nan, 1, 2, np.nan, 3, 4, 5])
a
#%%
b = ~np.isnan(a)
b
#%%
a[b]
#%%
# Filtering out non complex elements from an array | Keep the plural
a = np.array([1, 2+6j, 5, 3.5+5j])
a
#%%
b = np.iscomplex(a)
b
#%%
a[b]
#%% md
## Fancy index
Fancy index refers to using an array of integers to index
The fancy index takes the value of the index array as the index of a certain axis of the target array .
For using a one-dimensional integer array as an index , If the target is a one-dimensional array , Then the result of the index is the row corresponding to the subscript , If the target is a two-dimensional array , Then it is the element corresponding to the position
Fancy indexing is not the same as slicing , It always copies the data into a new array
#%%
# Pass in the ordinal index array
import numpy as np
x = np.arange(32) # 0~32( Not included 32)
x = x.reshape((8, 4)) # 8r4c
x
#%%
x[[4,2,1,7]] # Take... Separately Index row 4,2,1,7 Follow the order given by the index
#%%
# Pass in the inverted index array
x[[-4,-2,-1,-7]] # From bottom to top -1 It's the last line | length + Indexes = Real index | 4, 6, 7, 1
#%%
b = np.ix_([1, 5, 7, 2], [0, 3, 1, 2])
b[0]
#%%
b[1]
#%%
# Pass in multiple index arrays ( To use np.ix_)
""" Index row 1 Of Index columns 0,3,1,2 Index row 5 Of Index columns 0,3,1,2 Index row 7 Of Index columns 0,3,1,2 Index row 2 Of Index columns 0,3,1,2 """
x[b]
#%%
x[[1, 5, 7, 2], [0, 3, 1, 2]] # Don't use np.ix_ Will get 4 Elements successively x[1,0], x[5,3], x[7,1] x[2,2]
#%%
边栏推荐
- Number of uniform strings of leetcode simple problem
- Integration of Android high-frequency interview questions (including reference answers)
- Small sample target detection network with attention RPN and multi relationship detector (provide source code, data and download)
- LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)
- Do you know UVs in modeling?
- 2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
- Two drawing interfaces - 1 Matlab style interface
- 2022 P cylinder filling test content and P cylinder filling simulation test questions
- Leetcode simple question: check whether the array is sorted and rotated
- General undergraduate college life pit avoidance Guide
猜你喜欢
[XSS bypass - protection strategy] understand the protection strategy and better bypass
The least operation of leetcode simple problem makes the array increment
Analysis of proxy usage of ES6 new feature
Internationalization and localization, dark mode and dark mode in compose
Integration of Android high-frequency interview questions (including reference answers)
Number of 1 in binary (simple difficulty)
4 years of experience to interview test development, 10 minutes to end, ask too
《牛客刷verilog》Part II Verilog进阶挑战
Flutter monitors volume to realize waveform visualization of audio
2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
随机推荐
2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical
Golang -- realize file transfer
2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
Mount NFS in kubesphere
论文阅读_中文NLP_ELECTRA
Symbol of array element product of leetcode simple problem
Flutter monitors volume to realize waveform visualization of audio
Analysis of proxy usage of ES6 new feature
Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
AWS VPC
I stepped on a foundation pit today
Uipath practice (08) - selector
Thesis reading_ ICD code_ MSMN
Crazy scientist
AWS VPC
Reptile exercise 03
Leetcode simple question: the key with the longest key duration
ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)
Thesis reading_ Chinese NLP_ ELECTRA
普通本科大学生活避坑指南