Numpy I want to add numbers to my for loops when it reaches row 5

Question:

I am trying to add 1,2,3,4,5 after the fourth role which means I want to add numbers starting from row 5 of my 10×10 arrays. Below Attached with my code.

big_arr = np.zeros((10,10),dtype=np.uint8)
count = 0
for x in range(0,10):
    for y in range(0,10):
        if big_arr[x,y] > 50:
            big_arr[x,y] = count + 1
            count += 1

Im not sure why the value of big_arr[x,y] is 0 and if i switch the operator to < it will then add 1 continuously. Aren’t the value will be adding after the row and columns reach 50? Thank you

Asked By: Anscom

||

Answers:

Hello I just found out the answer.

big_arr = np.zeros((10,10),dtype=np.uint8)
count = 0
for x in range(5,10):
    for y in range(0,10):
        big_arr[x,y] = count + 1
        count += 1

All I did was changing the range in x to 5 and the value starts kicking in. Thank you.

Answered By: Anscom

If you’re working on numpy, and doing for loops, you’re most likely doing something wrong.

The speed of numpy achieved through slicing .

import numpy as np

my_array=np.zeros((10,10))
print(my_array)

#[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

fill_array = np.arange(1,51).reshape(5,10)
print(fill_array)

#[[ 1  2  3  4  5  6  7  8  9 10]
# [11 12 13 14 15 16 17 18 19 20]
# [21 22 23 24 25 26 27 28 29 30]
# [31 32 33 34 35 36 37 38 39 40]
# [41 42 43 44 45 46 47 48 49 50]]

my_array[5:,:] = fill_array
print(my_array)


#[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
# [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
# [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
# [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
# [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
# [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]
# [11. 12. 13. 14. 15. 16. 17. 18. 19. 20.]
# [21. 22. 23. 24. 25. 26. 27. 28. 29. 30.]
# [31. 32. 33. 34. 35. 36. 37. 38. 39. 40.]
# [41. 42. 43. 44. 45. 46. 47. 48. 49. 50.]]
Answered By: xvan