I have a numpy
array like:
我有一个像numpy数组:
a = np.arange(30)
I know that I can replace the values located at positions indices=[2,3,4]
using for instance fancy indexing:
我知道我可以使用例如花式索引替换位置indices = [2,3,4]的值:
a[indices] = 999
But how to replace the values at the positions that are not in indices
? Would be something like below?
但是如何替换不在指数中的位置的值?会是这样的吗?
a[ not in indices ] = 888
Thank you!
谢谢!
4 个解决方案
#1
26
I don't know of a clean way to do something like this:
我不知道干净的方法做这样的事情:
mask = np.ones(a.shape,dtype=bool) #np.ones_like(a,dtype=bool)
mask[indices] = False
a[~mask] = 999
a[mask] = 888
Of course, if you prefer to use the numpy data-type, you could use dtype=np.bool_
-- There won't be any difference in the output. it's just a matter of preference really.
当然,如果您更喜欢使用numpy数据类型,则可以使用dtype = np.bool_ - 输出中没有任何差异。这只是一个偏好的问题。
#2
5
Only works for 1d arrays:
仅适用于1d数组:
a = np.arange(30)
indices = [2, 3, 4]
ia = np.indices(a.shape)
not_indices = np.setxor1d(ia, indices)
a[not_indices] = 888
#3
3
Obviously there is no general not
operator for sets. Your choices are:
显然,集合没有一般的非运算符。你的选择是:
- Subtracting your
indices
set from a universal set of indices (depends on the shape ofa
), but that will be a bit difficult to implement and read. - 从一组通用索引中减去索引集(取决于a的形状),但这有点难以实现和阅读。
- Some kind of iteration (probably the
for
-loop is your best bet since you definitely want to use the fact that your indices are sorted). - 某种迭代(可能for循环是你最好的选择,因为你肯定想要使用你的索引被排序的事实)。
-
Creating a new array filled with new value, and selectively copying indices from the old one.
创建一个填充了新值的新数组,并有选择地从旧数组中复制索引。
b = np.repeat(888, a.shape) b[indices] = a[indices]
#4
3
Just overcome similar situation, solved this way:
刚刚克服类似情况,解决了这个问题:
a = np.arange(30)
indices=[2,3,4]
a[indices] = 999
not_in_indices = [x for x in range(len(a)) if x not in indices]
a[not_in_indices] = 888
#1
26
I don't know of a clean way to do something like this:
我不知道干净的方法做这样的事情:
mask = np.ones(a.shape,dtype=bool) #np.ones_like(a,dtype=bool)
mask[indices] = False
a[~mask] = 999
a[mask] = 888
Of course, if you prefer to use the numpy data-type, you could use dtype=np.bool_
-- There won't be any difference in the output. it's just a matter of preference really.
当然,如果您更喜欢使用numpy数据类型,则可以使用dtype = np.bool_ - 输出中没有任何差异。这只是一个偏好的问题。
#2
5
Only works for 1d arrays:
仅适用于1d数组:
a = np.arange(30)
indices = [2, 3, 4]
ia = np.indices(a.shape)
not_indices = np.setxor1d(ia, indices)
a[not_indices] = 888
#3
3
Obviously there is no general not
operator for sets. Your choices are:
显然,集合没有一般的非运算符。你的选择是:
- Subtracting your
indices
set from a universal set of indices (depends on the shape ofa
), but that will be a bit difficult to implement and read. - 从一组通用索引中减去索引集(取决于a的形状),但这有点难以实现和阅读。
- Some kind of iteration (probably the
for
-loop is your best bet since you definitely want to use the fact that your indices are sorted). - 某种迭代(可能for循环是你最好的选择,因为你肯定想要使用你的索引被排序的事实)。
-
Creating a new array filled with new value, and selectively copying indices from the old one.
创建一个填充了新值的新数组,并有选择地从旧数组中复制索引。
b = np.repeat(888, a.shape) b[indices] = a[indices]
#4
3
Just overcome similar situation, solved this way:
刚刚克服类似情况,解决了这个问题:
a = np.arange(30)
indices=[2,3,4]
a[indices] = 999
not_in_indices = [x for x in range(len(a)) if x not in indices]
a[not_in_indices] = 888