Is there a more efficient way to cut a multidimensional array

I noticed that indexing a multidimensional array takes longer than indexing a one-dimensional array

a1 = np.arange(1000000)
a2 = np.arange(1000000).reshape(1000, 1000)
a3 = np.arange(1000000).reshape(100, 100, 100)

When i index a1

%%timeit
a1[500000]

The slowest run took 39.17 times longer than the fastest. This may mean that the intermediate result is cached. 10,000,000, best of 3: 84.6 ns per loop

%%timeit
a2[500, 0]

The slowest run took 31.85 times longer than the fastest. This may mean that the intermediate result is cached. 10,000,000, best 3: 102 ns per loop

%%timeit
a3[50, 0, 0]

The slowest run took 46.72 times longer than the fastest. This may mean that the intermediate result is cached. 10,000,000, best 3: 119 ns per loop


? , ?

+4
1

(n, m) , , .

a = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
2- , 3- a[1, 2] 5
, 1 * a.shape[1] + 2 , a order='C'
, a.ravel()[1 * a.shape[1] + 2]

? , .

, ?


2-D

from timeit import timeit

n, m = 10000, 10000
a = np.random.rand(n, m)
r = pd.DataFrame(index=np.power(10, np.arange(7)), columns=['Multi', 'Flat'])

for k in r.index:
    b = np.random.randint(n, size=k)
    c = np.random.randint(m, size=k)
    kw = dict(setup='from __main__ import a, b, c', number=100)
    r.loc[k, 'Multi'] = timeit('a[b, c]', **kw)
    r.loc[k, 'Flat'] = timeit('a.ravel()[b * a.shape[1] + c]', **kw)

r.div(r.sum(1), 0).plot.bar()

enter image description here

, 100 000 .


3-D

from timeit import timeit

l, n, m = 1000, 1000, 1000
a = np.random.rand(l, n, m)
r = pd.DataFrame(index=np.power(10, np.arange(7)), columns=['Multi', 'Flat'])

for k in r.index:
    b = np.random.randint(l, size=k)
    c = np.random.randint(m, size=k)
    d = np.random.randint(n, size=k)

    kw = dict(setup='from __main__ import a, b, c, d', number=100)
    r.loc[k, 'Multi'] = timeit('a[b, c, d]', **kw)
    r.loc[k, 'Flat'] = timeit('a.ravel()[b * a.shape[1] * a.shape[2] + c * a.shape[1] + d]', **kw)

r.div(r.sum(1), 0).plot.bar()

enter image description here

, , .


2- , 100 000 .

3 , .



- ? - ?

+5

Source: https://habr.com/ru/post/1664392/


All Articles