python - How to use logspace() in numpy array?
问题描述
I'm trying to generate the value between given range i.e. values from 10^-1 up to 10^−14 and I'm getting a wrong output
I have implemented the logic to understand this logspace, but couldn't figure it out.
import numpy as np
delta = np.logspace(1, 14, dtype=np.float64)
print(delta)
Expected output is :
>>> print(delta)
[ 1.00000000e-01 1.00000000e-02 1.00000000e-03 1.00000000e-04
1.00000000e-05 1.00000000e-06 1.00000000e-07 1.00000000e-08
1.00000000e-09 1.00000000e-10 1.00000000e-11 1.00000000e-12
1.00000000e-13 1.00000000e-14]
解决方案
From the documentation:
In linear space, the sequence starts at base ** start (base to the power of start) and ends with base ** stop.
So np.logspace(-1, -14, 14)
gives:
array([1.e-01, 1.e-02, 1.e-03, 1.e-04, 1.e-05, 1.e-06, 1.e-07, 1.e-08,
1.e-09, 1.e-10, 1.e-11, 1.e-12, 1.e-13, 1.e-14])
Or with more "precision" (as asked in the comment):
np.set_printoptions(formatter=dict(float='{:10.8e}'.format))
np.logspace(-1, -14, 14)
Gives:
[1.00000000e-01 1.00000000e-02 1.00000000e-03 1.00000000e-04
1.00000000e-05 1.00000000e-06 1.00000000e-07 1.00000000e-08
1.00000000e-09 1.00000000e-10 1.00000000e-11 1.00000000e-12
1.00000000e-13 1.00000000e-14]
Note: this changes every numpy array with floats that you print afterwards..!
推荐阅读
- c - Execlp 函数找不到二进制文件
- python - Numpy 和 PyTorch 的相同随机种子
- python - TensorFlow 在两个模型之间共享层
- android - Firebase 邀请短信不可点击
- mysql - 详细信息和分组数据给出不同的结果mysql
- bing-maps - Azure Maps 路由在持续时间内不准确
- unity3d - Unity 为头部模型上的切口创建动画(脑外科手术)
- git - Nextcloud 版本控制
- python - 在 pipenv 中获取虚拟环境的路径
- java - 如何在 Mockito 单元测试中摆脱 NullPointerException