You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NumPy数组中元素自动从NumPy整数转为Python整数引发索引错误的问题求助

NumPy数组中元素自动从NumPy整数转为Python整数引发索引错误的问题求助

我现在在写代码,把曲线上每个点周围的点分组到同心区域里,然后计算每个区域的几何中位数来判断它的“形状”。

我用np.where语句确定每个区域的点索引,然后把所有np.where的结果放到一个NumPy数组里,这样方便索引,而且能处理区域内没有点的情况。这个主数组的dtype设为object,因为每个区域里的点数不一样。

但是遇到一种罕见情况:当每个区域的点数都相同时,数组结构看起来没变化,但里面的整数却从NumPy整数变成了Python整数,这就导致我用这些值做索引时,出现了索引错误:

IndexError: arrays used as indices must be of integer (or boolean) type

我的代码和尝试:

z = np.array([0,0.04,0.8,1.2,1.6,2])
    
for j in range(1,n1-1): #1 to n1-2, ignoring the initial and final parts 
    print(j) 
    
    r_a = j_r[j-1,0] 
    r_b = j_r[j-1,1]

    dist_a = s_dist[j][:j]
    dist_b = s_dist[j][j+1:]

    dist_pt_a = np.array([np.where((dist_a <= r_a*z[1]) & (dist_a >= r_a*z[0]))[0], #a: r1
                          np.where((dist_a <= r_a*z[2]) & (dist_a >= r_a*z[1]))[0], #a: r2
                          np.where((dist_a <= r_a*z[3]) & (dist_a >= r_a*z[2]))[0], #a: r3
                          np.where((dist_a <= r_a*z[4]) & (dist_a >= r_a*z[3]))[0], #a: r4
                          np.where((dist_a <= r_a*z[5]) & (dist_a >= r_a*z[4]))[0]], dtype=object) #a: r5

    dist_pt_b = np.array([np.where((dist_b <= r_b*z[1]) & (dist_b >= r_b*z[0]))[0] + (j+1), #b: r1
                          np.where((dist_b <= r_b*z[2]) & (dist_b >= r_b*z[1]))[0] + (j+1), #b: r2
                          np.where((dist_b <= r_b*z[3]) & (dist_b >= r_b*z[2]))[0] + (j+1), #b: r3
                          np.where((dist_b <= r_b*z[4]) & (dist_b >= r_b*z[3]))[0] + (j+1), #b: r4
                          np.where((dist_b <= r_b*z[5]) & (dist_b >= r_b*z[4]))[0] + (j+1)], dtype=object) #b: r5

现象对比:

正常情况(各区域点数不同):

#the array with 5 regions, each of differing length
dist_pt_b: [array([1081, 1082, 1083], dtype=int64)
array([1084, 1085, 1086], dtype=int64) 
array([1087, 1088], dtype=int64)
array([1089, 1090, 1091, 1092], dtype=int64)
array([1093, 1094, 1095], dtype=int64)]
#the first region
r (0): [1081 1082 1083], <class 'numpy.ndarray'>
#the first index in the first region
r (0): 1081, <class 'numpy.int64'>
#is indexable to that cartesian point
r (0): [[ 0.96494    23.29605851][ 0.97084    23.29796217][ 0.98314    23.30293551]]

异常情况(各区域点数相同):

#the array with 5 regions, each of same length
dist_pt_b: [[1082 1083 1084][1085 1086 1087][1088 1089 1090][1091 1092 1093][1094 1095 1096]]
#the first region
r (0): [1082 1083 1084], <class 'numpy.ndarray'>
#the first index in the first region
r (0): 1082, <class 'int'>

报错信息:

127     print(f'r ({r}): {dist_pt_b[r]}, {type(dist_pt_b[r])}')
128     print(f'r ({r}): {dist_pt_b[r][0]}, {type(dist_pt_b[r][0])}')
--> 129     print(f'r ({r}): {s_n[dist_pt_b[r]]}')
130 dist_pt_b[r] = np.mean(s_n[dist_pt_b[r]], axis = 0)
131 theta_s_b[2r], theta_s_b[2r+1] = dist_pt_b[r]
IndexError: arrays used as indices must be of integer (or boolean) type

备注:内容来源于stack exchange,提问作者lilbumblingbee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 13:44:51