当它们不是均匀分布时,我可以同时填充 numpy 数组中的非连续列吗?
Can I simultaneously populate non-consecutive columns in a numpy array when they are not evenly spaced?
假设我有以下 numpy 数组
A = np.zeros(7,10)
问题 我可以同时使用给定值或给定值集填充非连续列吗?例如,如果我想用整数 1 替换 A[:,3:6] 和 A[:,8],我可以这样做。
A[:,3:6] = 1
A[:,8] = 1
但我只是想知道是否有办法同时为多个非连续列完成此操作?这些不连续的列不一定均匀分布,所以我不能在索引中使用 start:stop:step 方法。
如果你知道非连续行的索引,你可以这样做:
A = np.zeros((7,10))
rows = [1,2,5]
A[rows, 3:6] = 1
A[rows, 8] = 1
print(A)
#output:
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
更新:针对您更新的问题和评论,如果您想修改某些非连续列的所有行并使用切片表示法的便利,您可以执行以下操作:制作一个元组列表,每个元组表示列的一部分的元组。即使是单个列也可以用切片表示。然后您将遍历列切片:
A = np.zeros((7,10))
col_slices = [(3,6),(8,9)]
for s in col_slices:
A[:, s[0]:s[1]] = 1
print(A)
#output:
[[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]]
最后,您可以结合以上两种技术同时更改所需的行和列:
A = np.zeros((7,10))
rows = [1,2,5]
col_slices = [(3,6),(8,9)]
for s in col_slices:
A[rows, s[0]:s[1]] = 1
print(A)
#output:
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
更新 2:如果您想以矢量化方式使用非连续切片,您可以首先定义一个将切片字典转换为索引列表的函数,然后以矢量化方式使用该函数的输出方法。以下任一函数都可以完成此操作:
#function 1
def slice_to_index(dic):
"""
:param dic: dictionary where key is the start and value is the end of each slice
:return: list of indexes
"""
l = []
for k,v in dic.items():
l.extend(list(range(k,v)))
return l
#function 2
def slice_to_index(dic):
return list(np.concatenate([list(range(k,v)) for k,v in dic.items()]).flat)
A = np.zeros((7,10))
A[:,slice_to_index({3:6, 8:9})] = 1
print(A)
#output:
[[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]]
假设我有以下 numpy 数组
A = np.zeros(7,10)
问题 我可以同时使用给定值或给定值集填充非连续列吗?例如,如果我想用整数 1 替换 A[:,3:6] 和 A[:,8],我可以这样做。
A[:,3:6] = 1
A[:,8] = 1
但我只是想知道是否有办法同时为多个非连续列完成此操作?这些不连续的列不一定均匀分布,所以我不能在索引中使用 start:stop:step 方法。
如果你知道非连续行的索引,你可以这样做:
A = np.zeros((7,10))
rows = [1,2,5]
A[rows, 3:6] = 1
A[rows, 8] = 1
print(A)
#output:
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
更新:针对您更新的问题和评论,如果您想修改某些非连续列的所有行并使用切片表示法的便利,您可以执行以下操作:制作一个元组列表,每个元组表示列的一部分的元组。即使是单个列也可以用切片表示。然后您将遍历列切片:
A = np.zeros((7,10))
col_slices = [(3,6),(8,9)]
for s in col_slices:
A[:, s[0]:s[1]] = 1
print(A)
#output:
[[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]]
最后,您可以结合以上两种技术同时更改所需的行和列:
A = np.zeros((7,10))
rows = [1,2,5]
col_slices = [(3,6),(8,9)]
for s in col_slices:
A[rows, s[0]:s[1]] = 1
print(A)
#output:
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
更新 2:如果您想以矢量化方式使用非连续切片,您可以首先定义一个将切片字典转换为索引列表的函数,然后以矢量化方式使用该函数的输出方法。以下任一函数都可以完成此操作:
#function 1
def slice_to_index(dic):
"""
:param dic: dictionary where key is the start and value is the end of each slice
:return: list of indexes
"""
l = []
for k,v in dic.items():
l.extend(list(range(k,v)))
return l
#function 2
def slice_to_index(dic):
return list(np.concatenate([list(range(k,v)) for k,v in dic.items()]).flat)
A = np.zeros((7,10))
A[:,slice_to_index({3:6, 8:9})] = 1
print(A)
#output:
[[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]
[0. 0. 0. 1. 1. 1. 0. 0. 1. 0.]]