2011-05-17 26 views
18

bir dizi halinde bir araya getirme numol dizileri listesini birleştirmek için en hızlı yol nedir? hepsi için? Pythonic way to create a numpy array from a list of numpy arrays denBir diziye (hızlı)

  • merged_array = array(list_of_arrays) ve

  • vstack

A sen vstack hızlıdır görebilirsiniz, ama nedense ilk çalıştırma üç alır:

Ben iki yaklaşımı çalıştı saniyeden daha uzun süreler. Bunun ( preallocation) neden olduğunu farz ediyorum. Peki, vstack için bir diziyi nasıl önceden preallocate edebilirim? Yoksa daha hızlı bir yöntem biliyor musun?

Teşekkürler! Ben (25280, 320) istiyorum

[GÜNCELLEME]

değil (80, 320, 320), benim için merged_array = array(list_of_arrays) alışkanlık çalışmalarını anlamına gelir. Bunu işaret için teşekkürler Joris!

Çıktı:

0.547468900681 s merged_array = array(first_list_of_arrays) 
0.547191858292 s merged_array = array(second_list_of_arrays) 
0.656183958054 s vstack first 
0.236850976944 s vstack second 

Kodu:

import numpy 
import time 
width = 320 
height = 320 
n_matrices=80 

secondmatrices = list() 
for i in range(n_matrices): 
    temp = numpy.random.rand(height, width).astype(numpy.float32) 
    secondmatrices.append(numpy.round(temp*9)) 

firstmatrices = list() 
for i in range(n_matrices): 
    temp = numpy.random.rand(height, width).astype(numpy.float32) 
    firstmatrices.append(numpy.round(temp*9)) 


t1 = time.time() 
first1=numpy.array(firstmatrices) 
print time.time() - t1, "s merged_array = array(first_list_of_arrays)" 

t1 = time.time() 
second1=numpy.array(secondmatrices) 
print time.time() - t1, "s merged_array = array(second_list_of_arrays)" 

t1 = time.time() 
first2 = firstmatrices.pop() 
for i in range(len(firstmatrices)): 
    first2 = numpy.vstack((firstmatrices.pop(),first2)) 
print time.time() - t1, "s vstack first" 

t1 = time.time() 
second2 = secondmatrices.pop() 
for i in range(len(secondmatrices)): 
    second2 = numpy.vstack((secondmatrices.pop(),second2)) 

print time.time() - t1, "s vstack second" 
+2

Python'da basit performans testleri yapmak için ['timeit'] (http://docs.python.org/library/timeit.html) kullanın. Daha doğru sonuçlar üretir. –

+2

Birleştirilmiş dizinin sahip olmasını istediğiniz boyutlar nelerdir? Çünkü '' first1'' '' (80, 320, 320) '' ve '' first2'' '' (25280, 320) '' – joris

+0

@ joris, bunu işaretlediğiniz için teşekkürler. İkincisini istiyorum, bu benim ilk yaklaşımımdı. Soruyu değiştireceğim. – Framester

cevap

18

You have 80 diziler 320x320? Yani muhtemelen dstack kullanmak istiyorum:

timeit numpy.vstack(firstmatrices) 
100 loops, best of 3: 18.2 ms per loop 
: Eğer vstack kullanmak istiyorsanız

timeit numpy.dstack(firstmatrices) 
10 loops, best of 3: 47.1 ms per loop 


timeit numpy.array(firstmatrices) 
1 loops, best of 3: 750 ms per loop 

, bir 25600x320 dizi döndürür:

first3 = numpy.dstack(firstmatrices) 

Bu numpy.array(firstmatrices) yaptığı gibi bir 80x320x320 dizisini döndürür

+0

Merhaba eurmiro, üzgünüm sorumu belirsizdi. Aslında ihtiyacım var (25280, 320) ve değil (80, 320, 320). Soruma ait güncellemeye bakın. – Framester

+0

@Framester - tamam, sonra güncellememi basit 'vstack' ile gör. – eumiro