arg*_*dos 6 python arrays indexing numpy
是否可以根据take功能的工作原理分配一个numpy数组?
例如,如果我有一个数组a,一个索引列表inds和一个所需的轴,我可以使用如下:
import numpy as np
a = np.arange(12).reshape((3, -1))
inds = np.array([1, 2])
print(np.take(a, inds, axis=1))
[[ 1 2]
[ 5 6]
[ 9 10]]
Run Code Online (Sandbox Code Playgroud)
当所需的索引/轴在运行时可能发生变化时,这非常有用.
但是,numpy不允许你这样做:
np.take(a, inds, axis=1) = 0
print(a)
Run Code Online (Sandbox Code Playgroud)
看起来这个通道有一些有限的(1-D)支持numpy.put,但我想知道是否有更清洁的方法来做到这一点?
In [222]: a = np.arange(12).reshape((3, -1))
...: inds = np.array([1, 2])
...:
In [223]: np.take(a, inds, axis=1)
Out[223]:
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
In [225]: a[:,inds]
Out[225]:
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
Run Code Online (Sandbox Code Playgroud)
构造一个索引元组
In [226]: idx=[slice(None)]*a.ndim
In [227]: axis=1
In [228]: idx[axis]=inds
In [229]: a[tuple(idx)]
Out[229]:
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
In [230]: a[tuple(idx)] = 0
In [231]: a
Out[231]:
array([[ 0, 0, 0, 3],
[ 4, 0, 0, 7],
[ 8, 0, 0, 11]])
Run Code Online (Sandbox Code Playgroud)
或者a[inds,:]:
In [232]: idx=[slice(None)]*a.ndim
In [233]: idx[0]=inds
In [234]: a[tuple(idx)]
Out[234]:
array([[ 4, 0, 0, 7],
[ 8, 0, 0, 11]])
In [235]: a[tuple(idx)]=1
In [236]: a
Out[236]:
array([[0, 0, 0, 3],
[1, 1, 1, 1],
[1, 1, 1, 1]])
Run Code Online (Sandbox Code Playgroud)
def put_at(inds, axis=-1, slc=(slice(None),)):
return (axis<0)*(Ellipsis,) + axis*slc + (inds,) + (-1-axis)*slc
Run Code Online (Sandbox Code Playgroud)
用作 a[put_at(ind_list,axis=axis)]
我在numpy功能上看过两种风格.这看起来像一个用于extend_dims,我的使用apply_along/over_axis.
在最近的一个take问题中,我/我们发现它相当于arr.flat[ind]一些raveled指数.我得看一下.
有一个np.put相当于赋予flat:
Signature: np.put(a, ind, v, mode='raise')
Docstring:
Replaces specified elements of an array with given values.
The indexing works on the flattened target array. `put` is roughly
equivalent to:
a.flat[ind] = v
Run Code Online (Sandbox Code Playgroud)
它的文档也提及place和putmask(和copyto).
我评论take(没有轴)相当于:
lut.flat[np.ravel_multi_index(arr.T, lut.shape)].T
Run Code Online (Sandbox Code Playgroud)
用ravel:
In [257]: a = np.arange(12).reshape((3, -1))
In [258]: IJ=np.ix_(np.arange(a.shape[0]), inds)
In [259]: np.ravel_multi_index(IJ, a.shape)
Out[259]:
array([[ 1, 2],
[ 5, 6],
[ 9, 10]], dtype=int32)
In [260]: np.take(a,np.ravel_multi_index(IJ, a.shape))
Out[260]:
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
In [261]: a.flat[np.ravel_multi_index(IJ, a.shape)] = 100
In [262]: a
Out[262]:
array([[ 0, 100, 100, 3],
[ 4, 100, 100, 7],
[ 8, 100, 100, 11]])
Run Code Online (Sandbox Code Playgroud)
并使用put:
In [264]: np.put(a, np.ravel_multi_index(IJ, a.shape), np.arange(1,7))
In [265]: a
Out[265]:
array([[ 0, 1, 2, 3],
[ 4, 3, 4, 7],
[ 8, 5, 6, 11]])
Run Code Online (Sandbox Code Playgroud)
ravel在这种情况下使用是不必要的,但在其他情况下可能有用.
| 归档时间: |
|
| 查看次数: |
778 次 |
| 最近记录: |