Numpy-iterating-over-array

提供:Dev Guides
移動先:案内検索

NumPy-配列の繰り返し

NumPyパッケージには、イテレータオブジェクト numpy.nditer が含まれています。 これは効率的な多次元反復子オブジェクトであり、これを使用して配列を反復処理できます。 配列の各要素には、Pythonの標準のIteratorインターフェースを使用してアクセスします。

arange()関数を使用して3X4配列を作成し、 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:
[[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:
[[Transpose of the original array is:
[[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:
[[Transpose of the original array is:
[[Sorted in C-style order:
[[Sorted in F-style order:
[[Example 2

It is possible to force *nditer *object to use a specific order by explicitly mentioning it.

[source,prettyprint,notranslate]

numpyをnp a = np.arange(0,60,5)a = a.reshape(3,4)としてインポート

print '元の配列は:' print '\ n'

np.nditer(a、order = 'C')のxに対して 'Cスタイルの順序で並べ替え:'を出力:xを印刷、 '\ n’を印刷

np.nditer(a、order = 'F')のxに対して 'Fスタイルの順序で並べ替え:'を印刷:xを印刷、

Its output would be −

[source,result,notranslate]

元の配列は次のとおりです。[[Cスタイルの順序で並べ替え:0 5 10 15 20 25 30 35 40 45 50 55

Fスタイルの順序で並べ替え:0 20 40 5 25 45 10 30 50 15 35 55

=== Modifying Array Values

The* nditer *object has another optional parameter called* op_flags*. Its default value is read-only, but can be set to read-write or write-only mode. This will enable modifying array elements using this iterator.

==== Example

[source,prettyprint,notranslate]

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

np.nditer(a、op_flags = ['readwrite'])のxの場合:x […​] = 2 * x print 'Modified array is:' print a

Its output is as follows −

[source,result,notranslate]

元の配列:[[変更された配列:[[外部ループ

nditerクラスのコンストラクターには、次の値を取ることができる ’flags’ パラメーターがあります-

Sr.No. Parameter & Description
1

c_index

C_orderインデックスを追跡できます

2

f_index

Fortran_orderインデックスが追跡されます

3

multi-index

反復ごとに1つのインデックスのタイプを追跡できます

4

external_loop

指定された値を、ゼロ次元配列ではなく複数の値を持つ1次元配列にします

次の例では、各列に対応する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, flags = ['external_loop'], order = 'F'):
   print x,

出力は次のとおりです-

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

放送の反復

2つの配列が broadcastable である場合、結合された nditer オブジェクトはそれらを同時に反復処理できます。 配列 a の次元が3X4であり、次元1X4の別の配列 b があると仮定すると、次の型の反復子が使用されます(配列 ba のサイズにブロードキャストされます)。

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:
[[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