This is the python code
def arr_func(arr,selected_pixels_list):
rows = 2
m = 0
n = 0
i =0
#Calculate the number of pixels selected
length_of_the_list = len(selected_pixels_list)
length_of_the_list = int(length_of_the_list/4)*4
cols = int(length_of_the_list/2)
result_arr = np.zeros((rows,cols))
while(i<length_of_the_list):
result_arr[m,n] = arr[selected_pixels_list[i]]
result_arr[m,n+1] = arr[selected_pixels_list[i+1]]
result_arr[m+1,n] = arr[selected_pixels_list[i+2]]
result_arr[m+1,n+1] = arr[selected_pixels_list[i+3]]
i = i+4
m = 0
n = n+2
return result_arr
import numpy as np
selected_pixel_data = np.load("coordinates.npy")
arr_data = np.load("arr.npy")
response = arr_func(arr_data, selected_pixel_data)
print(response)
This is the error I am getting
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "d:/work/sat/test_data.py", line 34, in <module>
response = arr_func(arr_data, selected_pixel_data)
File "d:/work/sat/test_data.py", line 16, in arr_func
result_arr[m,n] = arr[selected_pixels_list[i]]
ValueError: setting an array element with a sequence.
The data of loaded NumPy files
For selected_pixel_data:
shape = (597616, 2)
dtype = int32
For arr_data:
shape = (1064, 590)
dtype = float64
I have searched on the internet but it was mostly about MatLab and talking about vectorized I have used .flatten()
response=arr_func(arr_data.flatten(),selected_pixel_data.flatten())
The errors are gone but is this the correct way?
whileloop to simulate aforloop, you could just dofrom itertools import countand writefor i in range(0, len(selected_pixels_list), 4):with a first line ofn = i // 2(or for likely faster, but uglier code, don_indices = range(0, len(selected_pixels_list), 2), then loop withfor i, n in zip(n_indices[::2], n_indices):and you don't need to directly computeiorn). Also note: Every time you doint(SOME_INT / DIVISOR)you should be doingSOME_INT // DIVISORinstead (to do pureintmath with floor division directly).result_arr[m,n] = arr[selected_pixels_list[i]]? Did you understand that this is where the problem occurs? If you did: what do you think might be going wrong here? What do you think the result ofarr[selected_pixels_list[i]]should look like? Did you check that? Does it work like you expect? Is that something that you think should be assignable toresult_arr[m,n]? Why or why not?arr[selected_pixels_list[i]]will do? For example, do you think the result should be an integer, or an array, or just what? Now, what does it actually do? Did you try to figure that out, for example byprinting the result?