import numpy as np
Single Dimension List
single_dimension_list= [180, 215, 210, 210, 188, 176, 209, 200]
single_dimension_list
[180, 215, 210, 210, 188, 176, 209, 200]
Convert Single Dimension List to Array
numpy_single_dimension_array = np.array(single_dimension_list)
numpy_single_dimension_array
array([180, 215, 210, 210, 188, 176, 209, 200])
Loop Through Single Dimension NumPy Array with nditer
for x in np.nditer(numpy_single_dimension_array):
print(str(x))
180 215 210 210 188 176 209 200
Loop Through Single Dimension Numpy Array without nditer
for A in numpy_single_dimension_array:
print(A)
180 215 210 210 188 176 209 200
Loop Through Single Dimension List Using ndtiter
for x in np.nditer(single_dimension_list):
print(str(x))
(array(180), array(215), array(210), array(210), array(188), array(176), array(209), array(200))
2 Dimension List
double_dimension_list =[[1, 2, 3], [4, 5, 6]]
double_dimension_list
[[1, 2, 3], [4, 5, 6]]
Convert 2 Dimension List to 2 dimension Numpy Array
numpy_double_dimension_array=np.array(double_dimension_list)
numpy_double_dimension_array
array([[1, 2, 3], [4, 5, 6]])
Loop Through numpy 2 dimensional Array with nditer
for x in np.nditer(numpy_double_dimension_array):
print(str(x))
1 2 3 4 5 6
Loop through numpy 2 dimensional Array without nditer
for B in numpy_double_dimension_array:
print(B)
[1 2 3] [4 5 6]
Loop through 2 dimensional list with nditer
for x in np.nditer(double_dimension_list):
print(str(x))
(array(1), array(4)) (array(2), array(5)) (array(3), array(6))
Tranpose 2 Dimensional Numpy Array
Trans_two_dim_np_array=numpy_double_dimension_array.T
Trans_two_dim_np_array
array([[1, 4], [2, 5], [3, 6]])
Three Dimension List
three_dimension_list =[[[1,2],[3,4]], [[5,6],[7,8]]]
Convert Three Dimension List to 3 Dimension Numpy Array
numpy_three_dimension_array=np.array(three_dimension_list)
Loop through numpy three dimension array with nditer
for x in np.nditer(numpy_three_dimension_array):
print(str(x))
1 2 3 4 5 6 7 8
Loop Through 3 Dimensional Numpy Array List without nditer
for C in numpy_three_dimension_array:
print(C)
[[1 2] [3 4]] [[5 6] [7 8]]
Loop Through 3 dimension list with nditer
for x in np.nditer(three_dimension_list):
print(str(x))
(array(1), array(5)) (array(2), array(6)) (array(3), array(7)) (array(4), array(8))
Loop Through 2 dimension array with nditer order by type C
for x in np.nditer(numpy_double_dimension_array, order = 'C'):
print(x)
1 2 3 4 5 6
Loop Through 2 dimension array with nditer order by type F
for x in np.nditer(numpy_double_dimension_array, order = 'F'):
print(x)
1 4 2 5 3 6
Loop Through 3 dimension array with nditer order by type A
for x in np.nditer(numpy_double_dimension_array, order = 'A'):
print(x)
1 2 3 4 5 6
Loop Through 3 dimension array with nditer order by type K
for x in np.nditer(numpy_double_dimension_array, order = 'K'):
print(x)
1 2 3 4 5 6
Modify Numpy 2 Dimensional Array Value with nditer multiply it by 6
for x in np.nditer(numpy_double_dimension_array,op_flags = ['readwrite']):
x[...] = 6*x
print('Modified array is:')
print(numpy_double_dimension_array)
Modified array is: [[ 6 12 18] [24 30 36]]
Tranpose Numpy 2 Dimensional Array Value with nditer using external loop flag
print("Original array is :")
print(numpy_double_dimension_array)
print("Modified array is:")
for x in np.nditer(numpy_double_dimension_array, flags = ['external_loop'],order='F'):
print(x)
Original array is : [[ 6 12 18] [24 30 36]] Modified array is: [ 6 24] [12 30] [18 36]
numpy_double_dimension_array
array([[ 6, 12, 18], [24, 30, 36]])
Tracking Iteration Index using c Order
it = np.nditer(numpy_double_dimension_array, flags=['c_index'])
for x in it:
print("%d <%d>" % (x, it.index), end=' ')
6 <0> 12 <1> 18 <2> 24 <3> 30 <4> 36 <5>
Tracking Iteration Index using f Order
at = np.nditer(numpy_double_dimension_array, flags=['f_index'])
for x in at:
print("%d <%d>" % (x, at.index), end=' ')
6 <0> 12 <2> 18 <4> 24 <1> 30 <3> 36 <5>
numpy_double_dimension_array
array([[ 6, 12, 18], [24, 30, 36]])
Tracking iteration 2 dimensional index using c order
ot = np.nditer(numpy_double_dimension_array, flags=['multi_index'])
for x in ot:
print("%d <%s>" % (x, ot.multi_index), end=' ')
6 <(0, 0)> 12 <(0, 1)> 18 <(0, 2)> 24 <(1, 0)> 30 <(1, 1)> 36 <(1, 2)>
bt = np.nditer(numpy_double_dimension_array, flags=['c_index'])
while not bt.finished:
print("%d <%d>" % (bt[0], bt.index), end=' ')
is_not_finished = bt.iternext()
6 <0> 12 <1> 18 <2> 24 <3> 30 <4> 36 <5>
Buffering an Element
for x in np.nditer(numpy_double_dimension_array, flags=['external_loop'], order='c'):
print(x, end=' ')
[ 6 12 18 24 30 36]
numpy_double_dimension_array
array([[ 6, 12, 18], [24, 30, 36]])
Create another two dimension List
two_dimension_list =[[7, 8, 9], [10, 11, 12]]
two_dimension_list
[[7, 8, 9], [10, 11, 12]]
Convert 2 Dimension list to 2 dimensional numpy array
numpy_two_dimension_array = np.array(two_dimension_list)
numpy_two_dimension_array
array([[ 7, 8, 9], [10, 11, 12]])
Loop two Array Simultaneously
BroadCast Array Iteration -> Whenever functions take multiple operands which combine element-wise. This is called broadcasting
for x, y in np.nditer([numpy_double_dimension_array,numpy_two_dimension_array]):
print("%d:%d" % (x,y), end=' ')
6:7 12:8 18:9 24:10 30:11 36:12
for a,r in np.nditer([numpy_double_dimension_array,numpy_two_dimension_array]):
print(a, r)
6 7 12 8 18 9 24 10 30 11 36 12
Using nditer Buffered Flag
gt = np.nditer(numpy_two_dimension_array, flags=['multi_index','buffered'])
while not gt.finished:
print(gt.multi_index)
gt.iternext()
(0, 0) (0, 0) (0, 0) (0, 0) (0, 0) (0, 0)
for x in np.nditer(numpy_two_dimension_array):
print(x)
7 8 9 10 11 12
numpy_two_dimension_array
array([[ 7, 8, 9], [10, 11, 12]])
External Loop with Buffered
for x in np.nditer(numpy_two_dimension_array, flags=['external_loop','buffered'], order='C'):
print(x)
[ 7 8 9 10 11 12]
Flags and op_flags assignment example
Using op_flags ='readonly','writeonly','allocate'
allocate-> causes the array to be allocated if it is None in the op parameter. readonly-> indicates the operand will only be read from. writeonly-> indicates the operand will only be written to. buffered -> enables buffering
def square(a, out=None):
it = np.nditer([a, out],
op_flags = [
['readonly'],['writeonly','allocate']])
for x, y in it:
y[...] = x*x
return it.operands[1]
square([1,2,3,5,7])
array([ 1, 4, 9, 25, 49])
Add 2 Numpy Array
demo_1= [1, 2, 3, 4, 5]
np_demo_1 = np.array(demo_1)
demo_2= [1, 2, 3, 4,5]
np_demo_2 = np.array(demo_2)
for x, y in np.nditer([np_demo_1, np_demo_2], flags=['reduce_ok', 'external_loop'],
op_flags=[['readonly'], ['readwrite']]):
y[...] += x
print(x,y)
[1 2 3 4 5] [ 2 4 6 8 10]