TypeError: only size-1 arrays can be converted to Python scalars in simple python code
TypeError: only size-1 arrays can be converted to Python scalars in simple python code
这是python代码
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)
这是我遇到的错误
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.
加载的NumPy文件数据
For selected_pixel_data:
shape = (597616, 2)
dtype = int32
For arr_data:
shape = (1064, 590)
dtype = float64
我在互联网上搜索过,但主要是关于 MatLab 和矢量化的
我用过 .flatten()
response=arr_func(arr_data.flatten(),selected_pixel_data.flatten())
错误消失了,但这是正确的方法吗?
你检查过arr_data
和selected_pixel_data
的shape
和dtype
了吗?告诉我们!与其在网络上搜索“类似的错误”,不如专注于理解您的数据。如有必要,构造一个更简单的案例。您的代码“简单”这一事实并不能减少您出错的可能性!
我可以用
重现你的错误信息
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]
是一个号码的槽位。 np.arange(2)
是2个数字,一个“序列”。你看到不匹配了吗?
编辑
使用 i,n,m
标量和
shape = (597616, 2)
shape = (1064, 590)
单值槽:result_arr[m,n]
selected_pixels_list[i] # (2,) shape
arr[_] # (2,590) shape
您不能将二维数组放入单个数字槽中。
您需要重新考虑索引和赋值。
编辑
扁平化有什么作用?
shape = (597616*2,)
shape = (1064*590,)
单值槽:result_arr[m,n]
selected_pixels_list[i] # 1 element
arr[_] # 1 element
我在工作,但对吗? 2 号形状的意义是什么?我怀疑这是错误的
另一种可能性是将 selected_pixels_list
的 2 列解释为 arr
的 i,j
索引:
k, l = selected_pixels_list[i] # 2 numbers
arr[k,l] # 1 element
这是python代码
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)
这是我遇到的错误
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.
加载的NumPy文件数据
For selected_pixel_data:
shape = (597616, 2)
dtype = int32
For arr_data:
shape = (1064, 590)
dtype = float64
我在互联网上搜索过,但主要是关于 MatLab 和矢量化的 我用过 .flatten()
response=arr_func(arr_data.flatten(),selected_pixel_data.flatten())
错误消失了,但这是正确的方法吗?
你检查过arr_data
和selected_pixel_data
的shape
和dtype
了吗?告诉我们!与其在网络上搜索“类似的错误”,不如专注于理解您的数据。如有必要,构造一个更简单的案例。您的代码“简单”这一事实并不能减少您出错的可能性!
我可以用
重现你的错误信息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]
是一个号码的槽位。 np.arange(2)
是2个数字,一个“序列”。你看到不匹配了吗?
编辑
使用 i,n,m
标量和
shape = (597616, 2)
shape = (1064, 590)
单值槽:result_arr[m,n]
selected_pixels_list[i] # (2,) shape
arr[_] # (2,590) shape
您不能将二维数组放入单个数字槽中。
您需要重新考虑索引和赋值。
编辑
扁平化有什么作用?
shape = (597616*2,)
shape = (1064*590,)
单值槽:result_arr[m,n]
selected_pixels_list[i] # 1 element
arr[_] # 1 element
我在工作,但对吗? 2 号形状的意义是什么?我怀疑这是错误的
另一种可能性是将 selected_pixels_list
的 2 列解释为 arr
的 i,j
索引:
k, l = selected_pixels_list[i] # 2 numbers
arr[k,l] # 1 element