Numpy array admits a list of indices, for example
例如,Numpy数组允许索引列表
a = np.arange(1000)
l = list([1,44,66,33,90,345])
a[l] = 22
But this method don't work if we want to use a multiple slice indexing or indices plus a slice, for example.
但是,如果我们想要使用多切片索引或索引加切片,则此方法不起作用。
a = np.arange(1000)
l = list([1,44,66,33,90, slice(200,300) , slice(500,600) ])
a[l] = 22
This code returns an error message:
此代码返回一条错误消息:
IndexError: too many indices
My question is very simple: do you know if in numpy or scipy there exist an efficient method for using this kind of indexing?
我的问题很简单:你知道在numpy还是scipy中是否存在使用这种索引的有效方法?
Or what's a good and efficient way for using an indexing method like this?
或者使用像这样的索引方法有什么好方法?
Don't forget that the usage of slices produce a very fast code; and my problem is to have as faster as possible code.
不要忘记切片的使用会产生非常快的代码;我的问题是拥有尽可能快的代码。
2 个解决方案
#1
4
What comes to my mind:
我想到了什么:
a = np.arange(1000)
l = np.hstack(([1, 44, 66, 33, 90], np.arange(200, 300), np.arange(500, 600)))
a[l] = 22
I'm not sure if it's the simplest way, but it works.
我不确定这是否是最简单的方法,但它确实有效。
Edit: you're right that this is slower than using slices; but you cannot create a slice object with arbitrary values. Maybe you should just do several assignments then:
编辑:你说这比使用切片慢;但是您无法使用任意值创建切片对象。也许你应该做几个任务然后:
%timeit a[np.hstack(([1, 44, 66, 33, 90], np.arange(200, 300), np.arange(500, 600)))] = 22
10000 loops, best of 3: 39.5 us per loop
%timeit a[[1, 44, 66, 33, 90]] = 22; a[200:300] = 22; a[500:600] = 22
100000 loops, best of 3: 18.4 us per loop
#2
0
You can use fancy indexing to build an index list.
您可以使用花式索引来构建索引列表。
l = numpy.array([1,44,66,33,90]+range(200,300)+range(500,600))
a[l] = 22
But as @Lev pointed out, this may not be any faster (though it almost certainly will be if you can precompute the index list).
但正如@Lev指出的那样,这可能不会更快(尽管如果你可以预先计算索引列表,几乎可以肯定)。
However, fancy indexing applies per-axis. So you can fancy index on one axis, and slice the others, if that helps at all:
但是,花式索引适用于每个轴。所以你可以在一个轴上看上一个索引,然后对其他轴进行切片,如果这有帮助的话:
a = numpy.random.randn(4, 5, 6)
l = numpy.array([1, 2])
a[l, slice(None), slice(2, 4)] = 10
#1
4
What comes to my mind:
我想到了什么:
a = np.arange(1000)
l = np.hstack(([1, 44, 66, 33, 90], np.arange(200, 300), np.arange(500, 600)))
a[l] = 22
I'm not sure if it's the simplest way, but it works.
我不确定这是否是最简单的方法,但它确实有效。
Edit: you're right that this is slower than using slices; but you cannot create a slice object with arbitrary values. Maybe you should just do several assignments then:
编辑:你说这比使用切片慢;但是您无法使用任意值创建切片对象。也许你应该做几个任务然后:
%timeit a[np.hstack(([1, 44, 66, 33, 90], np.arange(200, 300), np.arange(500, 600)))] = 22
10000 loops, best of 3: 39.5 us per loop
%timeit a[[1, 44, 66, 33, 90]] = 22; a[200:300] = 22; a[500:600] = 22
100000 loops, best of 3: 18.4 us per loop
#2
0
You can use fancy indexing to build an index list.
您可以使用花式索引来构建索引列表。
l = numpy.array([1,44,66,33,90]+range(200,300)+range(500,600))
a[l] = 22
But as @Lev pointed out, this may not be any faster (though it almost certainly will be if you can precompute the index list).
但正如@Lev指出的那样,这可能不会更快(尽管如果你可以预先计算索引列表,几乎可以肯定)。
However, fancy indexing applies per-axis. So you can fancy index on one axis, and slice the others, if that helps at all:
但是,花式索引适用于每个轴。所以你可以在一个轴上看上一个索引,然后对其他轴进行切片,如果这有帮助的话:
a = numpy.random.randn(4, 5, 6)
l = numpy.array([1, 2])
a[l, slice(None), slice(2, 4)] = 10