user2003965
user2003965

Reputation: 503

How to raise an array in power while broadcasting it?

i have to following problem: I need a 1d-string with equaly distributed numbers on a LOG-scale. To eb precise: 1,2,3,4,5,6,7,8,9,10,20,30,40...,100,200,300,... and so on. This can go up to 10^9, so typing is not an option ;)

my code so far is the following:

ome = np.linspace(1,9,9).reshape(9,1)
pow = np.linspce(0,5,6).reshape(1,6)
logome = ome*(10**pow)

but this not working and i don't know how to proceed... any suggestions?

okay, i figured some way out, if anybody is interested:

ome = np.linspace(1,9,9).reshape(1,9)
pow = np.linspce(0,5,6)
pow = np.power(10,pow).reshape(6,1)
logome = ome*pow
logome.reshape(54)

et voila :)

Upvotes: 3

Views: 346

Answers (1)

DSM
DSM

Reputation: 353059

To get your desired output, I'd probably do something like this:

>>> (np.arange(1, 10) * 10**np.arange(9)[:,None]).flatten()
array([        1,         2,         3,         4,         5,         6,
               7,         8,         9,        10,        20,        30,
              40,        50,        60,        70,        80,        90,
             100,       200,       300,       400,       500,       600,
             700,       800,       900,      1000,      2000,      3000,
            4000,      5000,      6000,      7000,      8000,      9000,
           10000,     20000,     30000,     40000,     50000,     60000,
           70000,     80000,     90000,    100000,    200000,    300000,
          400000,    500000,    600000,    700000,    800000,    900000,
         1000000,   2000000,   3000000,   4000000,   5000000,   6000000,
         7000000,   8000000,   9000000,  10000000,  20000000,  30000000,
        40000000,  50000000,  60000000,  70000000,  80000000,  90000000,
       100000000, 200000000, 300000000, 400000000, 500000000, 600000000,
       700000000, 800000000, 900000000])

where the second term works like this:

>>> np.arange(5)
array([0, 1, 2, 3, 4])
>>> np.arange(5)[:, None]
array([[0],
       [1],
       [2],
       [3],
       [4]])
>>> 10**np.arange(5)[:, None]
array([[    1],
       [   10],
       [  100],
       [ 1000],
       [10000]])

You might also be interested in np.logspace. BTW, note that this array isn't equally distributed on a log-scale.

Upvotes: 3

Related Questions