score:0

Accepted answer

Three options:

  1. Something similar to list comprehension using NumPy's .item.
  2. itertools.starmap
  3. np.broadcast

Timing is below. Option 3 seems to be by far the fastest route.

from itertools import starmap

np.random.seed(123)
target = np.random.randn(2, 2)
roundto = np.arange(1, 5, dtype=np.int16).reshape((2, 2)) # must be type int

def method1():
    return (np.array([round(target.item(i), roundto.item(j)) 
                    for i, j in zip(range(target.size), 
                                    range(roundto.size))])
                                    .reshape(target.shape))

def method2():
    return np.array(list(starmap(round, zip(target.flatten(), 
                                 roundto.flatten())))).reshape(target.shape)

def method3():
    b = np.broadcast(target, roundto)
    out = np.empty(b.shape)
    out.flat = [round(u,v) for (u,v) in b]
    return out

from timeit import timeit

timeit(method1, number=100)
Out[50]: 0.003252145578553467

timeit(method2, number=100)
Out[51]: 0.002063405777064986

timeit(method3, number=100)
Out[52]: 0.0009481473990007316

print('method 3 is %0.2f x faster than method 2' % 
      (timeit(method2, number=100) / timeit(method3, number=100)))
method 3 is 2.91 x faster than method 2

Related Query