Is it possible to convert an array of indices to an array of ones and zeros, given the range? i.e. [2,3] -> [0, 0, 1, 1, 0], in range of 5
在给定范围的情况下,是否可以将索引数组转换为1和0的数组?即[2,3] - > [0,0,1,1,0],范围为5
I'm trying to automate something like this:
我正在尝试自动化这样的事情:
>>> index_array = np.arange(200,300)
array([200, 201, ... , 299])
>>> mask_array = ??? # some function of index_array and 500
array([0, 0, 0, ..., 1, 1, 1, ... , 0, 0, 0])
>>> train(data[mask_array]) # trains with 200~299
>>> predict(data[~mask_array]) # predicts with 0~199, 300~499
4 个解决方案
#1
16
Here's one way:
这是一种方式:
In [1]: index_array = np.array([3, 4, 7, 9])
In [2]: n = 15
In [3]: mask_array = np.zeros(n, dtype=int)
In [4]: mask_array[index_array] = 1
In [5]: mask_array
Out[5]: array([0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0])
If the mask is always a range, you can eliminate index_array
, and assign 1
to a slice:
如果掩码始终是范围,则可以消除index_array,并将1分配给切片:
In [6]: mask_array = np.zeros(n, dtype=int)
In [7]: mask_array[5:10] = 1
In [8]: mask_array
Out[8]: array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
If you want an array of boolean values instead of integers, change the dtype
of mask_array
when it is created:
如果需要布尔值数组而不是整数,请在创建时更改mask_array的dtype:
In [11]: mask_array = np.zeros(n, dtype=bool)
In [12]: mask_array
Out[12]:
array([False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False], dtype=bool)
In [13]: mask_array[5:10] = True
In [14]: mask_array
Out[14]:
array([False, False, False, False, False, True, True, True, True,
True, False, False, False, False, False], dtype=bool)
#2
4
For a single dimension, try:
对于单个维度,请尝试:
n = (15,)
index_array = [2, 5, 7]
mask_array = numpy.zeros(n)
mask_array[index_array] = 1
For more than one dimension, convert your n-dimensional indices into one-dimensional ones, then use ravel:
对于多个维度,将n维索引转换为一维索引,然后使用ravel:
n = (15, 15)
index_array = [[1, 4, 6], [10, 11, 2]] # you may need to transpose your indices!
mask_array = numpy.zeros(n)
flat_index_array = np.ravel_multi_index(
index_array,
mask_array.shape)
numpy.ravel(mask_array)[flat_index_array] = 1
#3
0
As requested, here it is in an answer. The code:
根据要求,这里是一个答案。代码:
[x in index_array for x in range(500)]
will give you a mask like you asked for, but it will use Bools instead of 0's and 1's.
会给你一个像你要求的面具,但它会使用Bools而不是0和1。
#4
0
There's a nice trick to do this as a one-liner, too - use the numpy.in1d
and numpy.arange
functions like this (the final line is the key part):
有一个很好的技巧可以做一个单线程 - 使用像这样的numpy.in1d和numpy.arange函数(最后一行是关键部分):
>>> x = np.linspace(-2, 2, 10)
>>> y = x**2 - 1
>>> idxs = np.where(y<0)
>>> np.in1d(np.arange(len(x)), idxs)
array([False, False, False, True, True, True, True, False, False, False], dtype=bool)
The downside of this approach is that it's ~10-100x slower than the appropch Warren Weckesser gave... but it's a one-liner, which may or may not be what you're looking for.
这种方法的缺点是它比适当的Warren Weckesser给出的速度慢了10-100倍......但它是一个单线,可能是也可能不是你想要的。
#1
16
Here's one way:
这是一种方式:
In [1]: index_array = np.array([3, 4, 7, 9])
In [2]: n = 15
In [3]: mask_array = np.zeros(n, dtype=int)
In [4]: mask_array[index_array] = 1
In [5]: mask_array
Out[5]: array([0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0])
If the mask is always a range, you can eliminate index_array
, and assign 1
to a slice:
如果掩码始终是范围,则可以消除index_array,并将1分配给切片:
In [6]: mask_array = np.zeros(n, dtype=int)
In [7]: mask_array[5:10] = 1
In [8]: mask_array
Out[8]: array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
If you want an array of boolean values instead of integers, change the dtype
of mask_array
when it is created:
如果需要布尔值数组而不是整数,请在创建时更改mask_array的dtype:
In [11]: mask_array = np.zeros(n, dtype=bool)
In [12]: mask_array
Out[12]:
array([False, False, False, False, False, False, False, False, False,
False, False, False, False, False, False], dtype=bool)
In [13]: mask_array[5:10] = True
In [14]: mask_array
Out[14]:
array([False, False, False, False, False, True, True, True, True,
True, False, False, False, False, False], dtype=bool)
#2
4
For a single dimension, try:
对于单个维度,请尝试:
n = (15,)
index_array = [2, 5, 7]
mask_array = numpy.zeros(n)
mask_array[index_array] = 1
For more than one dimension, convert your n-dimensional indices into one-dimensional ones, then use ravel:
对于多个维度,将n维索引转换为一维索引,然后使用ravel:
n = (15, 15)
index_array = [[1, 4, 6], [10, 11, 2]] # you may need to transpose your indices!
mask_array = numpy.zeros(n)
flat_index_array = np.ravel_multi_index(
index_array,
mask_array.shape)
numpy.ravel(mask_array)[flat_index_array] = 1
#3
0
As requested, here it is in an answer. The code:
根据要求,这里是一个答案。代码:
[x in index_array for x in range(500)]
will give you a mask like you asked for, but it will use Bools instead of 0's and 1's.
会给你一个像你要求的面具,但它会使用Bools而不是0和1。
#4
0
There's a nice trick to do this as a one-liner, too - use the numpy.in1d
and numpy.arange
functions like this (the final line is the key part):
有一个很好的技巧可以做一个单线程 - 使用像这样的numpy.in1d和numpy.arange函数(最后一行是关键部分):
>>> x = np.linspace(-2, 2, 10)
>>> y = x**2 - 1
>>> idxs = np.where(y<0)
>>> np.in1d(np.arange(len(x)), idxs)
array([False, False, False, True, True, True, True, False, False, False], dtype=bool)
The downside of this approach is that it's ~10-100x slower than the appropch Warren Weckesser gave... but it's a one-liner, which may or may not be what you're looking for.
这种方法的缺点是它比适当的Warren Weckesser给出的速度慢了10-100倍......但它是一个单线,可能是也可能不是你想要的。