当前位置:网站首页>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]
#%%
边栏推荐
- Basic use of Metasploit penetration testing framework
- Learning record of arouter principle
- Learning practice: comprehensive application of cycle and branch structure (I)
- Two drawing interfaces - 1 Matlab style interface
- Do you know UVs in modeling?
- Market status and development prospect prediction of the global autonomous hybrid underwater glider industry in 2022
- 【SQL注入】联合查询(最简单的注入方法)
- 【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过
- Games101 Lesson 9 shading 3 Notes
- document. The problem of missing parameters of referer is solved
猜你喜欢

2022 Shandong Province safety officer C certificate examination content and Shandong Province safety officer C certificate examination questions and analysis

并发操作-内存交互操作

C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement

I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional

2022 registration examination for safety production management personnel of hazardous chemical production units and examination skills for safety production management personnel of hazardous chemical

Apache MPM model and ab stress test

Network security textual research recommendation

Uipath practice (08) - selector
![[luatos sensor] 1 light sensing bh1750](/img/70/07f29e072c0b8630f92ec837fc12d5.jpg)
[luatos sensor] 1 light sensing bh1750

Concurrent operation memory interaction
随机推荐
MySQL winter vacation self-study 2022 12 (3)
Shell script Basics - basic grammar knowledge
C Primer Plus Chapter 10, question 14 3 × 5 array
Thesis reading_ Chinese NLP_ ELECTRA
【SQL注入】联合查询(最简单的注入方法)
Current market situation and development prospect forecast of the global fire boots industry in 2022
[XSS bypass - protection strategy] understand the protection strategy and better bypass
[set theory] binary relation (example of binary relation on a | binary relation on a)
Thesis reading_ Tsinghua Ernie
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
STM32 reverse entry
Market status and development prospect forecast of global heat curing adhesive industry in 2022
文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
Career planning of counter attacking College Students
Sdl2 + OpenGL glsl practice (Continued)
Market status and development prospect forecast of global button dropper industry in 2022
消息队列(MQ)介绍
SSM based campus part-time platform for College Students
Day 51 - tree problem
Market status and development prospects of the global autonomous marine glider industry in 2022