numpy:要移除2x2阵列中相邻的重复子阵列吗? [英] Numpy: Remove neighboring repeated subarrays in a 2x2 array?

查看:98
本文介绍了numpy:要移除2x2阵列中相邻的重复子阵列吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我是Numpy的新手,但我无法弄清楚这一点,因此将其交给专家.我有一个2x2的表数组,如下所示,我想顺序地统一"数组.顺序很重要,因此,如果多个相邻的同一行数组彼此相邻,则它们是多余的,因此应排除在外(行数组的顺序也很重要,因此[111,222]被认为是不同的[222,111]).以另一种方式构造,我只想保留其左或右邻居(或下图所示的顶部/底部)与自身不同(在下面的示例中用*标记)的行数组.

Alright, Im new to Numpy but I cant figure this one out so turning it over to the experts. I have a 2x2 table-array like the one below, and I want to "sequentially uniqify" the array. Sequence matters so if there is more than one of the same row arrays next to each other then they are reduntant and should be excluded (the sequence of the row-arrays also matters so [111,222] are to be considered different [222,111]). Framed in another way, I only want to keep the row-arrays whose left or right neighbor (or top/bottom as it looks like below) is different from itself (marked by * in the example below).

[[[492 105]
  [492 105]
  [492 105]*
  [492 106]*
  [492 106]
  [492 106]
  [491 106]*
  [491 106]
  [491 105]*
  [491 105]
  [491 105]
  [492 105]*
  [492 105]
  [492 105]]]

我尝试了numpy.uniquify函数,但这并不关心我有一个2x2数组,而是在我不想要的平面列表中返回了每个子数组中的每个唯一数字,并且对它进行了排序和更改了顺序我也不想的原始数组.

I tried the numpy.uniquify function but that didnt care about the fact that I had a 2x2 array and instead returned each unique number inside each sub-array in a flat list which I dont want, and it sorted and changed the order of my original arrays which I also dont want.

使用一个简单的for循环,我可以很容易地写出逻辑,但是我需要以Numpy的速度对其进行优化. Ive得到的最接近的结果是返回一个真理数组,该真理数组标记出左邻居不同的点,这似乎在起作用:

With a simple for-loop I could have easily written out the logic of this, but I need this to be optimized at Numpy speed. The closest Ive gotten is to return a trutharray marking the spots where the left-neighbour is different, which seems to be working:

MYARRAY = numpy.matrix(  my2x2array  )
indexes = numpy.arange(len(MYARRAY))
trutharray = numpy.any(MYARRAY[indexes]!=MYARRAY[indexes-1], 1)

但是,我不确定如何进行以及如何处理TruthArray.试图将真理数组提供给numpy.extract函数,但这只会返回每个子数组的平面列表,甚至不返回它应该包含的所有元素;在我的示例中,它返回了"[105 492 492 106]".

However, Im not sure how to proceed and what to do with the trutharray. Tried serving the trutharray to the numpy.extract function but this only returns a flat list of each subarray and doesnt even return all the elements that it should; in the case of my example it returned "[105 492 492 106]".

有帮助吗?如何继续我的示例并得到唯一的顺序子数组?还是有解决此问题的更快方法? Numpy在这个阶段对我很困惑:p

Any help? How can I proceed with my example and end up with unique sequential subarrays? Or are there any faster solutions for this problem? Numpy is very confusing to me at this stage :p

推荐答案

我猜是这样的:

>>> a=array( [[492, 105],
...   [492, 105],
...   [492, 105],
...   [492, 106],
...   [492, 106],
...   [492, 106],
...   [491, 106],
...   [491, 106],
...   [491, 105],
...   [491, 105],
...   [491, 105],
...   [492, 105],
...   [492, 105],
...   [492, 105]]
... )
>>> g_idx=any(a[1:]!=a[:-1], axis=1)
>>> vstack((a[:-1][g_idx][0], a[1:][g_idx]))
array([[492, 105],
       [492, 106],
       [491, 106],
       [491, 105],
       [492, 105]])

a[:-1][g_idx][0]是必需的,否则第一个元素将丢失.

That a[:-1][g_idx][0] is necessary or otherwise the first element will be missing.

这篇关于numpy:要移除2x2阵列中相邻的重复子阵列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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