为2D数组实现numpy in1d? [英] Implementation of numpy in1d for 2D arrays?

查看:92
本文介绍了为2D数组实现numpy in1d?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表示状态空间的二维numpy数组S,具有8000万行(作为状态)和5列(作为状态变量).

我用S初始化K0,并在每次迭代时,对Ki中的所有状态应用状态转移函数f(x),并删除f(x)不在Ki中的状态,得出Ki + 1.直到收敛,即Ki + 1 = Ki.

这样会花费很多时间:

K = S
to_delete = [0]
While to_delete:
    to_delete = []
    for i in xrange(len(K)):
        if not f(i) in K:
        to_delete.append(K(i))
    K = delete(K,to_delete,0)

所以我想做一个向量化的实现:

在列中切片K,应用f,然后再次加入它们,从而以某种方式获得f(K).

现在的问题是,如何获得长度为len(K)的数组,例如Sel,其中每一行Sel [i]确定f(K [i])是否在K中.完全类似于in1d函数起作用. /p>

然后简单地制作

K=K[Sel]]

解决方案

您的问题很难理解,因为它包含无关的信息并包含错别字.如果我理解正确,您只是想要一种有效的方法来对2D数组的行执行设置操作(在本例中为Kf(K)的行的交集).

您可以使用 numpy.in1d 进行此操作如果您创建结构化数组视图.

代码:

如果这是K:

In [50]: k
Out[50]:
array([[6, 6],
       [3, 7],
       [7, 5],
       [7, 3],
       [1, 3],
       [1, 5],
       [7, 6],
       [3, 8],
       [6, 1],
       [6, 0]])

这是f(K)(在本示例中,我从第一个列中减去1,然后在第二个列中添加1):

In [51]: k2
Out[51]:
array([[5, 7],
       [2, 8],
       [6, 6],
       [6, 4],
       [0, 4],
       [0, 6],
       [6, 7],
       [2, 9],
       [5, 2],
       [5, 1]])

然后您可以通过执行以下操作找到K中的所有行,这些行也位于f(K)中:

In [55]: k[np.in1d(k.view(dtype='i,i').reshape(k.shape[0]),k2.view(dtype='i,i').
reshape(k2.shape[0]))]
Out[55]: array([[6, 6]])

viewreshape创建平面结构化视图,以便每一行都显示为in1d的单个元素. in1d创建匹配项的k布尔索引,该索引用于查找索引k并返回过滤后的数组.

I have a 2D numpy array S representing a state space, with 80000000 rows (as states) and 5 columns (as state variables).

I initialize K0 with S, and at each iteration, I apply a state transition function f(x) on all of the states in Ki, and delete states whose f(x) is not in Ki, resulting Ki+1. Until it converges i.e. Ki+1 = Ki.

Going like this would take ages:

K = S
to_delete = [0]
While to_delete:
    to_delete = []
    for i in xrange(len(K)):
        if not f(i) in K:
        to_delete.append(K(i))
    K = delete(K,to_delete,0)

So I wanted to make a vectorized implementation :

slice K in columns, apply f and, join them once again, thus obtaining f(K) somehow.

The question now is how to get an array of length len(K), say Sel, where each row Sel[i] determine whether f(K[i]) is in K. Exactly like the function in1d works.

Then it would be simple to make

K=K[Sel]]

解决方案

Your question is difficult to understand because it contains extraneous information and contains typos. If I understand correctly, you simply want an efficient way to perform a set operation on the rows of a 2D array (in this case the intersection of the rows of K and f(K)).

You can do this with numpy.in1d if you create structured array view.

Code:

if this is K:

In [50]: k
Out[50]:
array([[6, 6],
       [3, 7],
       [7, 5],
       [7, 3],
       [1, 3],
       [1, 5],
       [7, 6],
       [3, 8],
       [6, 1],
       [6, 0]])

and this is f(K) (for this example I subtract 1 from the first col and add 1 to the second):

In [51]: k2
Out[51]:
array([[5, 7],
       [2, 8],
       [6, 6],
       [6, 4],
       [0, 4],
       [0, 6],
       [6, 7],
       [2, 9],
       [5, 2],
       [5, 1]])

then you can find all rows in K also found in f(K) by doing something this:

In [55]: k[np.in1d(k.view(dtype='i,i').reshape(k.shape[0]),k2.view(dtype='i,i').
reshape(k2.shape[0]))]
Out[55]: array([[6, 6]])

view and reshape create flat structured views so that each row appears as a single element to in1d. in1d creates a boolean index of k of matched items which is used to fancy index k and return the filtered array.

这篇关于为2D数组实现numpy in1d?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆