交叉点np.数组和s

2024-07-02 14:12:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我想和一个np.数组不需要转换np.数组先到一个列表(使程序减速到无法运行的水平)。在

这是我当前的代码:(请注意,我是从b、g、r rawCapture获取这些数据的,selection_数据只是一个预先设置的集合。)

def GreenCalculations(data):
    data.reshape(1,-1,3)
    data={tuple(item) for item in data[0]}
    ColourCount=selection_data & set(data)
    Return ColourCount

我现在的问题是,由于数据[0],我只比较了图片顶部的第一部分。是否可以遍历所有行?在

注意:tolist()需要很多时间。在


Tags: 数据代码程序列表datadefnp水平
2条回答

首先是一个示例data;我猜这是一个nxnx3数组,其数据类型为uint8

In [791]: data=np.random.randint(0,256,(8,8,3),dtype=np.uint8)

{t>在一个新的数组中返回一个新的方法:

^{pr2}$

data.shape=(1,-1,3)也能做到。但是为什么首字母1?在

取而代之的是:

^{3}$

在我的例子中,有一组64个独特的项目-这并不奇怪,因为我是如何生成这些值的

你什么都不做data.reshape行和{tuple(item) for item in data[0]}解释了为什么它似乎只在图片的第一行工作。在

我猜selection_data是类似的3个项目元组,例如:

In [801]: selection_data = {tuple(data[1,3,:]), (1,2,3), tuple(data[5,5,:])}
In [802]: selection_data
Out[802]: {(1, 2, 3), (49, 132, 26), (76, 131, 16)}
In [803]: selection_data&aset
Out[803]: {(49, 132, 26), (76, 131, 16)}

您没有说明您试图在何处使用tolist,但我猜是在生成元组集时。在

但奇怪的是,tolist加速了转换:

In [808]: timeit {tuple(item) for item in data.reshape(-1,3).tolist()}
10000 loops, best of 3: 57.7 µs per loop
In [809]: timeit {tuple(item) for item in data.reshape(-1,3)}
1000 loops, best of 3: 239 µs per loop
In [815]: timeit data.reshape(-1,3).tolist()
100000 loops, best of 3: 19.8 µs per loop
In [817]: timeit {tuple(item.tolist()) for item in data.reshape(-1,3)}
10000 loops, best of 3: 100 µs per loop

因此,对于这种列表和集合操作,我们最好马上跳转到列表格式。在

numpy有一些集合函数,例如np.in1d。这只对1d数组进行操作,但是正如在一些unique row问题中所演示的那样,我们可以通过将2d数组看作一个结构化数组来解决这个问题。我得摆弄才能走到这一步:

In [880]: dt=np.dtype('uint8,uint8,uint8')
In [881]: data1=data.reshape(-1,3).view(dt).ravel()
In [882]: data1
Out[882]: 
array([(41, 145, 254), (138, 144, 7), (192, 241, 203), (42, 177, 215),
       (78, 132, 87), (221, 176, 87), (107, 171, 147), (231, 13, 53),
       ... 
      dtype=[('f0', 'u1'), ('f1', 'u1'), ('f2', 'u1')])

构造具有相同结构化数组性质的选择:

In [883]: selection=[data[1,3,:],[1,2,3],data[5,5,:]]
In [885]: selection=np.array(selection,np.uint8).view(dt)
In [886]: selection
Out[886]: 
array([[(49, 132, 26)],
       [(1, 2, 3)],
       [(76, 131, 16)]], 
      dtype=[('f0', 'u1'), ('f1', 'u1'), ('f2', 'u1')])

因此selection中的项目也可以在data1中找到:

In [888]: np.in1d(selection,data1)
Out[888]: array([ True, False,  True], dtype=bool)

data1中选择的项目包括:

In [890]: np.where(np.in1d(data1,selection))
Out[890]: (array([11, 45], dtype=int32),)

或是在未展开的形状

In [891]: np.where(np.in1d(data1,selection).reshape(8,8))
Out[891]: (array([1, 5], dtype=int32), array([3, 5], dtype=int32))

我用来生成selection的相同的(1,3)和(5,5)项。在

in1d时间安排很有竞争力:

In [892]: %%timeit
     ...: data1=data.reshape(-1,3).view(dt).ravel()
     ...: np.in1d(data1,selection)
     ...: 
10000 loops, best of 3: 65.7 µs per loop

In [894]: timeit selection_data&{tuple(item) for item in data.reshape(-1,3).tolist()}
10000 loops, best of 3: 91.5 µs per loop

如果我正确地理解了您的问题(我不是百分之百确定我理解;但是使用与hpaulj相同的假设),您的问题就可以通过使用numpy_indexed软件包来解决:

import numpy_indexed as npi
ColourCount = npi.intersection(data.reshape(-1, 3), np.asarray(selection_data))

也就是说,它将重塑后的数组和集合都视为长度为3ndarray的序列,并以矢量化的方式找到其中的交集。在

相关问题 更多 >