0

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?

10
  • 1
    Minor side-note: Instead of laboriously using a while loop to simulate a for loop, you could just do from itertools import count and write for i in range(0, len(selected_pixels_list), 4): with a first line of n = i // 2 (or for likely faster, but uglier code, do n_indices = range(0, len(selected_pixels_list), 2), then loop with for i, n in zip(n_indices[::2], n_indices): and you don't need to directly compute i or n). Also note: Every time you do int(SOME_INT / DIVISOR) you should be doing SOME_INT // DIVISOR instead (to do pure int math with floor division directly). Commented Apr 13, 2022 at 18:22
  • "Can anyone help me with what is wrong with the code?" Well, did you read the error message? Do you understand the error message? For example, see how the error message quotes your code 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 of arr[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 to result_arr[m,n]? Why or why not? Commented Apr 13, 2022 at 18:37
  • Welcome back to Stack Overflow. Please read ericlippert.com/2014/03/05/how-to-debug-small-programs and meta.stackoverflow.com/questions/261592 and meta.stackoverflow.com/questions/284236. "can you send me the full code, please" Also please read How to Ask. We don't work like that here. Commented Apr 13, 2022 at 18:38
  • I know the error come at result_arr[m,n] = arr[selected_pixels_list[i]] I am not good at NumPy and new to this and can you explain if you know @KarlKnechtel Commented Apr 13, 2022 at 19:01
  • "I know the error come at result_arr[m,n] = arr[selected_pixels_list[i]]" Well, why does the error appear there? What do you think 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 by printing the result? Commented Apr 13, 2022 at 19:07

1 Answer 1

1

Did you check the shape and dtype of arr_data and selected_pixel_data? Tell us! Rather than searching the web for "like sounding errors", focus on understanding your data. If necessary, construct a simpler case. The fact that your code is "simple" does not reduce the chance that you'll get an error!.

I can reproduce your error message with

In [14]: res = np.zeros((2,3))
In [15]: res[0,0] = np.arange(2)
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):
  Input In [15] in <cell line: 1>
    res[0,0] = np.arange(2)
ValueError: setting an array element with a sequence.

res[0,0] is the slot for one number. np.arange(2) is 2 numbers, a "sequence". Do you see the mismatch?

edit

With i,n,m scalars and

shape = (597616, 2)
shape = (1064, 590)

single value slot: result_arr[m,n]

 selected_pixels_list[i] # (2,) shape
 arr[_]   # (2,590) shape

You can't put that 2d array in a single number slot.

You need to rethink the indexing and assignment.

edit

What does the flattening do?

shape = (597616*2,)
shape = (1064*590,)

single value slot: result_arr[m,n]

 selected_pixels_list[i] # 1 element
 arr[_]   # 1 element

I works, but is that right? What's the significance of the size 2 shape? I suspect that is wrong

Another possibility is to interpret the 2 columns of selected_pixels_list as i,j indices of arr:

k, l = selected_pixels_list[i]   # 2 numbers
arr[k,l]   # 1 element
Sign up to request clarification or add additional context in comments.

2 Comments

I have updated the shape and dtype can you check once
can i use .flatten() like response = arr_func(arr_data.flatten(), selected_pixel_data.flatten()) Errors are gone but i don't know it is correct or not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.