简体   繁体   English

Python:从元组列表中创建图像的快速方法

[英]Python: Fast way to create image from a list of tuples

I am doing the following. 我正在做以下事情。

import numpy as np
import pylab

.....

x = np.zeros([250,200])
for tup in tups:
    x[tup[1],tup[0]] = x[tup[1],tup[0]] + 1
pylab.imshow(x)

Where 哪里

tups = [(x1,y1),(x2,y2),....]

and xi , yi are integers xiyi是整数

This is fine for tup with a low number of points. 对于积分较少的tup这很好。 For a large number of points ~10^6 it is taking hours. 对于大量的点~10 ^ 6,需要数小时。

Can you think of a faster way of doing this? 你能想到更快的方法吗?

One small improvement i can easily see, instead of the next: 我可以很容易地看到一个小的改进,而不是下一个:

for tup in tups:
    x[tup[1],tup[0]] = x[tup[1],tup[0]] + 1

try doing 试着做

for tup in tups:
    x[tup[1],tup[0]] += 1

Since this overwrites the same memory-adress, instead of creating a new memory-spot to put 'old value + 1' (note: this will probably only result in a marginal speedup in this case, but if you do this same trick A+=B instead of C = A + B, in the case where A and B are numpy ndarrays of a Gb each or so, it actually is a massive speedup) 由于这会覆盖相同的内存地址,而不是创建一个新的内存点来放置'旧值+ 1'(注意:在这种情况下,这可能只会导致边际加速,但如果你这样做同样的技巧A + = B而不是C = A + B,在A和B分别是Gb的numpy ndarrays的情况下,它实际上是一个巨大的加速)

Why do you read in something as tuples? 你为什么读作元组的东西? shouldnt you try to read it in as a numpy ndarray in the first place, instead of reading it in as a list of tuples and than change to a numpy array? 难道你不应该首先尝试将其作为一个numpy ndarray读取,而不是将其作为元组列表读取而不是更改为numpy数组? Where do you create that big list of tuples? 你在哪里创建那个庞大的元组列表? If that can be avoided, it will be much better, to just avoid the list of tuples, instead of creating it and than later swapping to a numpy solution? 如果可以避免这种情况,那么只是避免元组列表而不是创建元组而不是后来交换到一个numpy解决方案会好得多?

Edit: so i just wanted to tell of this speedup that you can get by the +=, and at the same time ask why you have a big list of tuples, but thats too long to put both things in a comment 编辑:所以我只是想告诉你这个加速你可以得到+ =,同时问为什么你有一个大的元组列表,但这太长了,不能把这两件事放在评论中

Another question: am i right in assuming your tuples can have multiple repeats? 另一个问题:我是否正确假设您的元组可以有多个重复? like 喜欢

tups = [(1,0), (2,4), (1,0), (1,2), ..., (999, 999), (992, 999)]

so that in your endresult, other values than 0 and 1 will exist? 那么在你的结果中,还会存在除0和1之外的其他值吗? or is your resulting array something in which only ones and zeros exist? 或者是你的结果数组中只有1和0的存在?

Using numpy you could convert your pairs of indices into a flat index and bincount it: 使用numpy,您可以将您的索引对转换为平面索引并对其进行bincount:

import numpy as np
import random

rows, cols = 250, 200
n = 1000

tups = [(random.randint(0, rows-1),
         random.randint(0, cols-1)) for _ in range(n)]

x = np.zeros((rows, cols))
for tup in tups:
    x[tup[0],tup[1]] += 1

flat_idx = np.ravel_multi_index(zip(*tups), (rows, cols))
y = np.bincount(flat_idx, minlength=rows*cols).reshape(rows, cols)

np.testing.assert_equal(x, y)

It will be much faster than any looping solution. 它将比任何循环解决方案快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM