lanery
lanery

Reputation: 5364

More elegant way of defining numpy array

I'm looking for a more elegant/neat way to create a numpy array of the numbers [1e-1, 2e-1, 3e-1, ..., 1e0, 2e0, 3e0, ..., 1e3, 2e3, 3e3, ..., 8e3, 9e3, 1e4]

The best I could come up with was

a = np.arange(9)+1
b = np.array([a*10**-1, a*10**0, a*10**1, a*10**2, a*10**3]).flatten()
b = np.append(b, 10000)

In [84]: b
Out[84]: 
array([  1.00000000e-01,   2.00000000e-01,   3.00000000e-01,
         4.00000000e-01,   5.00000000e-01,   6.00000000e-01,
         7.00000000e-01,   8.00000000e-01,   9.00000000e-01,
         1.00000000e+00,   2.00000000e+00,   3.00000000e+00,
         4.00000000e+00,   5.00000000e+00,   6.00000000e+00,
         7.00000000e+00,   8.00000000e+00,   9.00000000e+00,
         1.00000000e+01,   2.00000000e+01,   3.00000000e+01,
         4.00000000e+01,   5.00000000e+01,   6.00000000e+01,
         7.00000000e+01,   8.00000000e+01,   9.00000000e+01,
         1.00000000e+02,   2.00000000e+02,   3.00000000e+02,
         4.00000000e+02,   5.00000000e+02,   6.00000000e+02,
         7.00000000e+02,   8.00000000e+02,   9.00000000e+02,
         1.00000000e+03,   2.00000000e+03,   3.00000000e+03,
         4.00000000e+03,   5.00000000e+03,   6.00000000e+03,
         7.00000000e+03,   8.00000000e+03,   9.00000000e+03,
         1.00000000e+04])

Upvotes: 0

Views: 537

Answers (4)

B. M.
B. M.

Reputation: 18648

shorter :

(10**(np.arange(37)//(37/4.))).cumsum()

Upvotes: 1

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339350

One could use numpy.fromfunction().

import numpy as np
a = np.fromfunction(lambda i, j: (i+1)*10**(j-1), (9, 6)).T.flatten()[:-8]

which prints

[  1.00000000e-01   2.00000000e-01   3.00000000e-01   4.00000000e-01
   5.00000000e-01   6.00000000e-01   7.00000000e-01   8.00000000e-01
   9.00000000e-01   1.00000000e+00   2.00000000e+00   3.00000000e+00
   4.00000000e+00   5.00000000e+00   6.00000000e+00   7.00000000e+00
   8.00000000e+00   9.00000000e+00   1.00000000e+01   2.00000000e+01
   3.00000000e+01   4.00000000e+01   5.00000000e+01   6.00000000e+01
   7.00000000e+01   8.00000000e+01   9.00000000e+01   1.00000000e+02
   2.00000000e+02   3.00000000e+02   4.00000000e+02   5.00000000e+02
   6.00000000e+02   7.00000000e+02   8.00000000e+02   9.00000000e+02
   1.00000000e+03   2.00000000e+03   3.00000000e+03   4.00000000e+03
   5.00000000e+03   6.00000000e+03   7.00000000e+03   8.00000000e+03
   9.00000000e+03   1.00000000e+04]

The advantage is that this is pure numpy and should therefore be highly efficient compared to any list comprehension techniques.

Another pure numpy method would be to use the outer product of the the logarithmic array from 1e-1 to 1e4 and the linear array from 1 to 9.

a = np.outer(np.logspace(-1, 4,6),np.arange(1, 10)).flatten()[:-8]

Upvotes: 2

James
James

Reputation: 36658

You could pass a list comprehension to np.array and then slice off what you don't need.

np.array([x*(10**y) for y in range(-1,5)  for x in range(1,10)])[:-8]

array([  1.00000000e-01,   2.00000000e-01,   3.00000000e-01,
         4.00000000e-01,   5.00000000e-01,   6.00000000e-01,
         7.00000000e-01,   8.00000000e-01,   9.00000000e-01,
         1.00000000e+00,   2.00000000e+00,   3.00000000e+00,
         4.00000000e+00,   5.00000000e+00,   6.00000000e+00,
         7.00000000e+00,   8.00000000e+00,   9.00000000e+00,
         1.00000000e+01,   2.00000000e+01,   3.00000000e+01,
         4.00000000e+01,   5.00000000e+01,   6.00000000e+01,
         7.00000000e+01,   8.00000000e+01,   9.00000000e+01,
         1.00000000e+02,   2.00000000e+02,   3.00000000e+02,
         4.00000000e+02,   5.00000000e+02,   6.00000000e+02,
         7.00000000e+02,   8.00000000e+02,   9.00000000e+02,
         1.00000000e+03,   2.00000000e+03,   3.00000000e+03,
         4.00000000e+03,   5.00000000e+03,   6.00000000e+03,
         7.00000000e+03,   8.00000000e+03,   9.00000000e+03,
         1.00000000e+04])

Or if you want to do it all in numpy, you can use matrix multiplication, flatten with ravel, and then slice off the end.

np.ravel(np.power(10.0, np.arange(-1,5))[:,np.newaxis]*np.arange(1,10))[:-8]

array([  1.00000000e-01,   2.00000000e-01,   3.00000000e-01,
         4.00000000e-01,   5.00000000e-01,   6.00000000e-01,
         7.00000000e-01,   8.00000000e-01,   9.00000000e-01,
         1.00000000e+00,   2.00000000e+00,   3.00000000e+00,
         4.00000000e+00,   5.00000000e+00,   6.00000000e+00,
         7.00000000e+00,   8.00000000e+00,   9.00000000e+00,
         1.00000000e+01,   2.00000000e+01,   3.00000000e+01,
         4.00000000e+01,   5.00000000e+01,   6.00000000e+01,
         7.00000000e+01,   8.00000000e+01,   9.00000000e+01,
         1.00000000e+02,   2.00000000e+02,   3.00000000e+02,
         4.00000000e+02,   5.00000000e+02,   6.00000000e+02,
         7.00000000e+02,   8.00000000e+02,   9.00000000e+02,
         1.00000000e+03,   2.00000000e+03,   3.00000000e+03,
         4.00000000e+03,   5.00000000e+03,   6.00000000e+03,
         7.00000000e+03,   8.00000000e+03,   9.00000000e+03,
         1.00000000e+04])

Upvotes: 3

BallpointBen
BallpointBen

Reputation: 13780

np.array([x * 10**y for y in range(-1,4) for x in range(1,10)] + [1e4])

Upvotes: 3

Related Questions