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]
我正在寻找一种更优雅/整洁的方式来创建一个数字的numpy数组[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])
4 个解决方案
#1
3
You could pass a list comprehension to np.array
and then slice off what you don't need.
您可以将列表推导传递给np.array,然后切掉您不需要的内容。
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.
或者如果你想在numpy中完成所有操作,你可以使用矩阵乘法,用ravel展平,然后切掉最后。
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])
#2
3
np.array([x * 10**y for y in range(-1,4) for x in range(1,10)] + [1e4])
#3
2
One could use numpy.fromfunction()
.
可以使用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.
优点是这是纯粹的numpy,因此与任何列表理解技术相比应该是高效的。
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.
另一种纯粹的numpy方法是使用1e-1到1e4的对数阵列的外积和1到9的线性阵列。
a = np.outer(np.logspace(-1, 4,6),np.arange(1, 10)).flatten()[:-8]
#4
1
shorter :
更短:
(10**(np.arange(37)//(37/4.))).cumsum()
#1
3
You could pass a list comprehension to np.array
and then slice off what you don't need.
您可以将列表推导传递给np.array,然后切掉您不需要的内容。
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.
或者如果你想在numpy中完成所有操作,你可以使用矩阵乘法,用ravel展平,然后切掉最后。
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])
#2
3
np.array([x * 10**y for y in range(-1,4) for x in range(1,10)] + [1e4])
#3
2
One could use numpy.fromfunction()
.
可以使用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.
优点是这是纯粹的numpy,因此与任何列表理解技术相比应该是高效的。
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.
另一种纯粹的numpy方法是使用1e-1到1e4的对数阵列的外积和1到9的线性阵列。
a = np.outer(np.logspace(-1, 4,6),np.arange(1, 10)).flatten()[:-8]
#4
1
shorter :
更短:
(10**(np.arange(37)//(37/4.))).cumsum()