当前位置:网站首页>Notes | numpy-10 Iterative array
Notes | numpy-10 Iterative array
2022-07-03 04:49:00 【CY3761】
#%% md
NumPy Iterator object numpy.nditer Provides a flexible way to access one or more array elements .
The basic task of iterator is to access array elements .
So let's use arange() Function to create a 2X3 Array , And use nditer Iterate over it b
#%%
import numpy as np
a = np.arange(6).reshape(2, 3) # 2r3c
a
#%%
# Output through iteration
# The above example is not a standard C perhaps Fortran The order , The order of selection is consistent with the array memory layout , This is done to improve the efficiency of access , The default is row order first (row-major order, Or rather, C-order)
for _ in np.nditer(a): # First floor for You can output the entire array | Output line by line ( level )
print(_, end=', ')
#%%
a.T # reverse
#%%
# It reflects that by default, you only need to access each element , Regardless of their specific order . We can see this by iterating the transpose of the above array , And with C Sequential access array transposed copy Compare the ways
# a and a.T The traversal order is the same , That is, they are stored in the same order in memory
for _ in np.nditer(a.T): # First floor for You can output the entire array | have no feelings The order of output is the same | Using or a This iterates
print(_, end=', ')
#%%
# however a.T.copy(order = 'C') The traversal results are different , That's because it's different from the first two storage methods , The default is to access by line
for _ in np.nditer(a.T.copy(order='C')): # First floor for You can output the entire array | The order of output is different | This is output line by line ( level ) This is using replication ( Copy sort changes )
print(_, end=', ')
#%%
# Control traversal order
# for x in np.nditer(a, order='F'):Fortran order, That is, sequence first
# for x in np.nditer(a.T, order='C'):C order, That is, line order takes precedence
#%%
import numpy as np
a = np.arange(0, 60, 5) # 0~60( Not included 60) step 5
a
#%%
a = a.reshape(3, 4) # 3r4c
a
#%%
b = a.T
b # 4r3c
#%%
for _ in np.nditer(b):
print(_, end=', ') # vertical ( Column )
#%%
c = b.copy(order='C')
c
#%%
for _ in np.nditer(c):
print(_, end=', ') # level ( That's ok )
#%%
c = b.copy(order='F')
c
#%%
for _ in np.nditer(c):
print(_, end=', ') # vertical ( Column )
#%%
# You can explicitly set , Mandatory nditer Objects use some sort of order
for _ in np.nditer(a, order='C'):
print(_, end=', ') # level ( That's ok )
#%%
for _ in np.nditer(a, order='F'):
print(_, end=', ') # vertical ( Column )
#%% md
## Change the value of the element in the array
nditer Object has another optional parameter op_flags. By default ,nditer The array to be iterated is considered as a read-only object (read-only), In order to traverse the array at the same time , The implementation of array elements is worth modifying , Must specify read-write perhaps write-only The pattern of
#%%
import numpy as np
for _ in np.nditer(a, op_flags=['readwrite']):
_[...] = 2 * _ # All elements multiply 2 operation
a
#%% md
## Use an external loop
#%%
# nditer Class constructor has flags Parameters , It can accept the following values
""" Parameters describe c_index Can track C Index of order | That's ok f_index Can track Fortran Index of order | Column multi_index Each iteration can track one index type external_loop The given value is a one-dimensional array with multiple values , Instead of a zero dimensional array """
#%%
for _ in np.nditer(a, flags=['external_loop'], order='F'):
print(_, end=', ')
#%% md
## Broadcast iteration
#%%
# If two arrays are broadcast ,nditer Composite objects can iterate over them at the same time . Hypothetical array a The dimensions are 3X4, Array b The dimensions are 1X4 , Then use the following iterators ( Array b Broadcast to a Size )
import numpy as np
a = np.arange(0, 60, 5).reshape(3, 4)
a
#%%
b = np.array([1, 2, 3, 4], dtype=int)
b
#%%
# To iterate This makes it easy to check the calculation method
for x,y in np.nditer([a, b]):
print('x = %d, y = %d' % (x, y))
#%%
边栏推荐
- Symbol of array element product of leetcode simple problem
- Thesis reading_ Tsinghua Ernie
- [luatos sensor] 1 light sensing bh1750
- Market status and development prospect forecast of global heat curing adhesive industry in 2022
- I've seen a piece of code in the past. I don't know what I'm doing. I can review it when I have time
- 2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
- Why should programmers learn microservice architecture if they want to enter a large factory?
- Thesis reading_ ICD code_ MSMN
- MPM model and ab pressure test
- Market status and development prospect prediction of global neutral silicone sealant industry in 2022
猜你喜欢
Leetcode simple question: check whether the array is sorted and rotated
Shuttle + Alluxio 加速内存Shuffle起飞
Prepare for 2022 and welcome the "golden three silver four". The "summary of Android intermediate and advanced interview questions in 2022" is fresh, so that your big factory interview can go smoothly
M1 Pro install redis
Games101 Lesson 9 shading 3 Notes
Two drawing interfaces - 1 Matlab style interface
Human resource management system based on JSP
The programmer went to bed at 12 o'clock in the middle of the night, and the leader angrily scolded: go to bed so early, you are very good at keeping fit
4 years of experience to interview test development, 10 minutes to end, ask too
Basic use of Metasploit penetration testing framework
随机推荐
Day 51 - tree problem
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
雇佣收银员(差分约束)
2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
Shell script -- condition judgment
【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过
Market status and development prospect prediction of the global fire extinguisher industry in 2022
第十九届浙江省 I. Barbecue
The reason why the entity class in the database is changed into hump naming
[set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
Market status and development prospect prediction of global neutral silicone sealant industry in 2022
Reptile exercise 02
Prepare for 2022 and welcome the "golden three silver four". The "summary of Android intermediate and advanced interview questions in 2022" is fresh, so that your big factory interview can go smoothly
Priv-app permission异常
Learning record of arouter principle
【SQL注入点】注入点出现位置、判断
论文阅读_清华ERNIE
Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
Contents of welder (primary) examination and welder (primary) examination in 2022