我希望使用如下所示的ndarray在tuple中索引boolean mask
import numpy as np
n_max = 5
list_no = np.arange ( 0, n_max )
lateral = np.tril_indices ( n_max, -1 )
mask= np.diff ( lateral [0].astype ( int ) )
mask [-1] = 1
Expected=lateral[mask!= 0]但是,在执行行Expected=lateral[mask!= 0]时,编译器将返回一个错误
TypeError:只有整数标量数组才能转换为标量索引
Expected=
0 = {ndarray: (4,)} [1 2 3 4]
1 = {ndarray: (4,)} [0 1 2 3]我能知道我哪里做错了吗?
发布于 2021-01-20 11:12:09
看来面具的大小和侧面是不一样的。由于掩码是数组中每个元素之间的差异,所以当横向大小为n时,它的大小为n-1。您可能需要将其附加到掩码数组中。另外,由于侧向是元组,所以在应用掩码之前,需要对元组进行索引。
你可能需要这样的东西:
import numpy as np
n_max = 5
list_no = np.arange(0, n_max)
lateral = np.tril_indices(n_max, -1)
mask = np.diff(lateral[0].astype(int))
mask = np.append(mask, 1)
expected_0 = lateral[0][mask != 0]
print(expected_0)
expected_1 = lateral[1][mask != 0]
print(expected_1)https://stackoverflow.com/questions/65803355
复制相似问题