[英]applying a function to rows of a ndarray && converting itertool object to numpy array
I am trying to create permutations of size 4 from a group of real numbers.我正在尝试从一组实数创建大小为 4 的排列。 After that, I'd like to know the position of the first element in a permutation after I sort it.
之后,我想知道排序后排列中第一个元素的 position。 Here is what I have tried so far.
到目前为止,这是我尝试过的。 What's the best way to do this?
最好的方法是什么?
import numpy as np
from itertools import chain, permutations
N_PLAYERS = 4
N_STATES = 60
np.random.seed(0)
state_space = np.linspace(0.0, 1.0, num=N_STATES, retstep=True)[0].tolist()
perms = permutations(state_space, N_PLAYERS)
perms_arr = np.fromiter(chain(*perms),dtype=np.float16)
def loc(row):
return np.where(np.argsort(row) == 0)[0].tolist()[0]
locs = np.apply_along_axis(loc, 0, perms)
In [153]: N_PLAYERS = 4
...: N_STATES = 60
...: np.random.seed(0)
...: state_space = np.linspace(0.0, 1.0, num=N_STATES, retstep=True)[0].tolist()
...: perms = itertools.permutations(state_space, N_PLAYERS)
In [154]: alist = list(perms)
In [155]: len(alist)
Out[155]: 11703240
Simply making a list from the permuations produces a list of lists, with all sublists of length N_PLAYERS
.简单地从排列中制作一个列表会产生一个列表列表,其中所有子列表的长度为
N_PLAYERS
。
Making an array from that with chain flattens it:用 chain 制作一个数组使其变平:
In [156]: perms = itertools.permutations(state_space, N_PLAYERS)
In [158]: perms_arr = np.fromiter(itertools.chain(*perms),dtype=np.float16)
In [159]: perms_arr.shape
Out[159]: (46812960,)
In [160]: alist[0]
Which could be reshaped to (11703240,4).可以将其重塑为 (11703240,4)。
Using apply
on that 1d array doesn't work (or make sense):在该 1d 数组上使用
apply
不起作用(或有意义):
In [170]: perms_arr.shape
Out[170]: (46812960,)
In [171]: locs = np.apply_along_axis(loc, 0, perms_arr)
In [172]: locs.shape
Out[172]: ()
Reshape to 4 columns:重塑为 4 列:
In [173]: locs = np.apply_along_axis(loc, 0, perms_arr.reshape(-1,4))
In [174]: locs.shape
Out[174]: (4,)
In [175]: locs
Out[175]: array([ 0, 195054, 578037, 769366])
This applies loc
to each column, returning one value for each.这将
loc
应用于每一列,为每一列返回一个值。 But loc
has a row
variable.但是
loc
有一个row
变量。 Is that supposed to be significant?那应该很重要吗?
I could switch the axis;我可以切换轴; this takes much longer, and al
这需要更长的时间,而且
In [176]: locs = np.apply_along_axis(loc, 1, perms_arr.reshape(-1,4))
In [177]: locs.shape
Out[177]: (11703240,)
This iteration does the same thing as your apply_along_axis
, and I expect is faster (though I haven't timed it - it's too slow).这个迭代与你的
apply_along_axis
做同样的事情,我希望它更快(虽然我没有计时 - 它太慢了)。
In [188]: locs1 = np.array([loc(row) for row in perms_arr.reshape(-1,4)])
In [189]: np.allclose(locs, locs1)
Out[189]: True
But argsort
takes an axis, so I can sort all rows at once (instead of iterating):但是
argsort
采用一个轴,所以我可以一次对所有行进行排序(而不是迭代):
In [185]: np.nonzero(np.argsort(perms_arr.reshape(-1,4), axis=1)==0)
Out[185]:
(array([ 0, 1, 2, ..., 11703237, 11703238, 11703239]),
array([0, 0, 0, ..., 3, 3, 3]))
In [186]: np.allclose(_[1],locs)
Out[186]: True
Or going the other direction: - cf with Out[175]或者转向另一个方向:- cf with Out[175]
In [187]: np.nonzero(np.argsort(perms_arr.reshape(-1,4), axis=0)==0)
Out[187]: (array([ 0, 195054, 578037, 769366]), array([0, 1, 2, 3]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.