NumPy - วนซ้ำอาร์เรย์

แพ็คเกจ NumPy มีอ็อบเจ็กต์ตัววนซ้ำ numpy.nditer. เป็นอ็อบเจ็กต์ตัววนซ้ำหลายมิติที่มีประสิทธิภาพโดยใช้ซึ่งเป็นไปได้ที่จะวนซ้ำบนอาร์เรย์ แต่ละองค์ประกอบของอาร์เรย์ถูกเยี่ยมชมโดยใช้อินเทอร์เฟซ Iterator มาตรฐานของ Python

ให้เราสร้างอาร์เรย์ 3X4 โดยใช้ฟังก์ชัน arange () และทำซ้ำโดยใช้ nditer.

ตัวอย่าง 1

import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)

print 'Original array is:'
print a
print '\n'

print 'Modified array is:'
for x in np.nditer(a):
   print x,

ผลลัพธ์ของโปรแกรมนี้มีดังนี้ -

Original array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

Modified array is:
0 5 10 15 20 25 30 35 40 45 50 55

ตัวอย่าง 2

ลำดับของการวนซ้ำถูกเลือกให้ตรงกับเลย์เอาต์หน่วยความจำของอาร์เรย์โดยไม่ต้องพิจารณาลำดับที่เจาะจง สิ่งนี้สามารถเห็นได้โดยการวนซ้ำบนทรานสโพสของอาร์เรย์ด้านบน

import numpy as np 
a = np.arange(0,60,5) 
a = a.reshape(3,4) 
   
print 'Original array is:'
print a 
print '\n'  
   
print 'Transpose of the original array is:' 
b = a.T 
print b 
print '\n'  
   
print 'Modified array is:' 
for x in np.nditer(b): 
   print x,

ผลลัพธ์ของโปรแกรมข้างต้นมีดังนี้ -

Original array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

Transpose of the original array is:
[[ 0 20 40]
 [ 5 25 45]
 [10 30 50]
 [15 35 55]]

Modified array is:
0 5 10 15 20 25 30 35 40 45 50 55

ลำดับการทำซ้ำ

หากองค์ประกอบเดียวกันถูกจัดเก็บโดยใช้คำสั่งแบบ F ตัววนซ้ำจะเลือกวิธีที่มีประสิทธิภาพมากขึ้นในการทำซ้ำบนอาร์เรย์

ตัวอย่าง 1

import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'

print 'Transpose of the original array is:'
b = a.T
print b
print '\n'

print 'Sorted in C-style order:'
c = b.copy(order = 'C')
print c
for x in np.nditer(c):
   print x,

print '\n'

print 'Sorted in F-style order:'
c = b.copy(order = 'F')
print c
for x in np.nditer(c):
   print x,

ผลลัพธ์จะเป็นดังนี้ -

Original array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

Transpose of the original array is:
[[ 0 20 40]
 [ 5 25 45]
 [10 30 50]
 [15 35 55]]

Sorted in C-style order:
[[ 0 20 40]
 [ 5 25 45]
 [10 30 50]
 [15 35 55]]
0 20 40 5 25 45 10 30 50 15 35 55

Sorted in F-style order:
[[ 0 20 40]
 [ 5 25 45]
 [10 30 50]
 [15 35 55]]
0 5 10 15 20 25 30 35 40 45 50 55

ตัวอย่าง 2

เป็นไปได้ที่จะบังคับ nditer วัตถุที่จะใช้คำสั่งเฉพาะโดยกล่าวถึงอย่างชัดเจน

import numpy as np 
a = np.arange(0,60,5) 
a = a.reshape(3,4) 

print 'Original array is:' 
print a 
print '\n'  

print 'Sorted in C-style order:' 
for x in np.nditer(a, order = 'C'): 
   print x,  
print '\n' 

print 'Sorted in F-style order:' 
for x in np.nditer(a, order = 'F'): 
   print x,

ผลลัพธ์จะเป็น -

Original array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

Sorted in C-style order:
0 5 10 15 20 25 30 35 40 45 50 55

Sorted in F-style order:
0 20 40 5 25 45 10 30 50 15 35 55

การแก้ไขค่าอาร์เรย์

nditer วัตถุมีพารามิเตอร์ทางเลือกอื่นที่เรียกว่า op_flags. ค่าเริ่มต้นคืออ่านอย่างเดียว แต่สามารถตั้งค่าเป็นโหมดอ่านเขียนหรือเขียนอย่างเดียวได้ สิ่งนี้จะเปิดใช้งานการแก้ไของค์ประกอบอาร์เรย์โดยใช้ตัววนซ้ำนี้

ตัวอย่าง

import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print 'Original array is:'
print a
print '\n'

for x in np.nditer(a, op_flags = ['readwrite']):
   x[...] = 2*x
print 'Modified array is:'
print a

ผลลัพธ์มีดังนี้ -

Original array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

Modified array is:
[[ 0 10 20 30]
 [ 40 50 60 70]
 [ 80 90 100 110]]

ห่วงภายนอก

ตัวสร้างคลาส nditer มี ‘flags’ พารามิเตอร์ซึ่งสามารถรับค่าต่อไปนี้ -

ซีเนียร์ พารามิเตอร์และคำอธิบาย
1

c_index

สามารถติดตามดัชนี C_order ได้

2

f_index

มีการติดตามดัชนี Fortran_order

3

multi-index

สามารถติดตามประเภทของดัชนีที่มีหนึ่งรายการต่อการวนซ้ำ

4

external_loop

ทำให้ค่าที่กำหนดให้เป็นอาร์เรย์มิติเดียวที่มีหลายค่าแทนอาร์เรย์มิติศูนย์

ตัวอย่าง

ในตัวอย่างต่อไปนี้อาร์เรย์หนึ่งมิติที่สอดคล้องกับแต่ละคอลัมน์จะถูกส่งผ่านโดยตัววนซ้ำ

import numpy as np 
a = np.arange(0,60,5) 
a = a.reshape(3,4) 

print 'Original array is:' 
print a 
print '\n'  

print 'Modified array is:' 
for x in np.nditer(a, flags = ['external_loop'], order = 'F'):
   print x,

ผลลัพธ์มีดังนี้ -

Original array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

Modified array is:
[ 0 20 40] [ 5 25 45] [10 30 50] [15 35 55]

การออกอากาศซ้ำ

หากมีสองอาร์เรย์ broadcastableรวมกัน nditerวัตถุสามารถทำซ้ำได้พร้อมกัน สมมติว่าอาร์เรย์a มีมิติ 3X4 และมีอาร์เรย์อื่น b ของมิติ 1X4 ใช้ตัววนซ้ำประเภทต่อไปนี้ (อาร์เรย์ b ออกอากาศเป็นขนาด a).

ตัวอย่าง

import numpy as np 
a = np.arange(0,60,5) 
a = a.reshape(3,4) 

print 'First array is:' 
print a 
print '\n'  

print 'Second array is:' 
b = np.array([1, 2, 3, 4], dtype = int) 
print b  
print '\n' 

print 'Modified array is:' 
for x,y in np.nditer([a,b]): 
   print "%d:%d" % (x,y),

ผลลัพธ์จะเป็นดังนี้ -

First array is:
[[ 0 5 10 15]
 [20 25 30 35]
 [40 45 50 55]]

Second array is:
[1 2 3 4]

Modified array is:
0:1 5:2 10:3 15:4 20:1 25:2 30:3 35:4 40:1 45:2 50:3 55:4