当前位置:网站首页>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]
#%%
边栏推荐
- JVM原理简介
- Reptile exercise 02
- [set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
- Network security textual research recommendation
- FISCO bcos zero knowledge proof Fiat Shamir instance source code
- MediaTek 2023 IC written examination approved in advance (topic)
- Games101 Lesson 9 shading 3 Notes
- Market status and development prospect prediction of global fermented plant protein industry in 2022
- 2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
- Mobile terminal - uniapp development record (public request encapsulation)
猜你喜欢
论文阅读_中文NLP_ELECTRA
Learning practice: comprehensive application of cycle and branch structure (I)
Valentine's day limited withdrawal guide: for one in 200 million of you
On typescript and grammar
2022 P cylinder filling test content and P cylinder filling simulation test questions
2022 new examination questions for the main principals of hazardous chemical business units and examination skills for the main principals of hazardous chemical business units
Php+mysql registration landing page development complete code
Leetcode simple question: the key with the longest key duration
FISCO bcos zero knowledge proof Fiat Shamir instance source code
Library management system based on SSM
随机推荐
Leetcode simple question: check whether the array is sorted and rotated
Employee attendance management system based on SSM
Market status and development prospect prediction of global colorimetric cup cover industry in 2022
Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
Market status and development prospects of the global automatic tea picker industry in 2022
Handling record of electric skateboard detained by traffic police
Analysis of proxy usage of ES6 new feature
2022 chemical automation control instrument examination summary and chemical automation control instrument certificate examination
"Niuke brush Verilog" part II Verilog advanced challenge
Shell script Basics - basic grammar knowledge
【SQL注入点】注入点出现位置、判断
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
Pyqt control part (II)
JS multidimensional array to one-dimensional array
Priv-app permission异常
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation
Golang -- realize file transfer
[PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)