dimanche 27 septembre 2020

Numpy repeat random choice

Imagine a 2D Boolean field. Suppose it is a a x b = 3x3 field

F= [[0 1 0] [0 1 0] [1 1 1]]

without_zeros(range(9)*F) = (1 4 6 7 8 ) this is the list of idices where F = True.

Now I need N = e.g.1M possibilities to randomly make m (e.g. 3) selections from this number range without repetitions

e.g.

1 7 4
4 6 8
8 6 1
1 6 4
... 

My approach with vectorized functions failed.

My best result so far is with a = 3, b = 3, N = 5E6, m = 3 ~ 84s. on an i3 7250.

My question: Are there better methods with prevent the for-loop and work in parallel / vectorized?

compiled - start timer
Time: 84.47356009483337
finish

working example:

import numpy as np
from numba import jit, prange
import time as t

@jit
def startzellen_zufall(mask, m, N):
    b = mask.reshape(mask.size)
    a = np.arange(1, len(b) + 1, dtype=np.int16)
    c = a * b
    clean = np.array(c[c != 0], dtype=np.int16)

    l = []
    for i in prange(0, N):
        l.append(np.random.choice(clean, m, replace=False))
    return np.stack(l)

##############
N = 5000000
m = 3
mask = np.array([[True, False, False], [False, True, True], [True, False, True]])

startzellen_zufall(mask, m, N)
print("compiled - start timer")
t1 = t.time()
startzellen_zufall(mask, m, N)
t2 = t.time()
print("Time: %s" % (t2 - t1))
print("finish")




Aucun commentaire:

Enregistrer un commentaire