python - python numpy exponentially spaced samples between a start and end value
问题描述
numpy.linspace
generates evenly spaced
float samples between a start and end value.
print (numpy.linspace(0.0, 1.0, num=9))
# [0 0.125 0.25 0.375 0.5 0.625 0.75 0.875 1]
print (numpy.linspace(9000.0, 1000.0, num=9))
# [9000. 8000. 7000. 6000. 5000. 4000. 3000. 2000. 1000.]
How can I generate exponentially spaced samples between a start and end value ?
For example, to the power of 2:
[0. 0.016 0.0625 0.141 0.25 0.391 0.562 0.766 1.]
Thanks for any suggestions.
解决方案
For the case of going between 0 and 1, you can just take the square of the array:
print (numpy.linspace(0.0, 1.0, num=9)**2 )
# [0. 0.016 0.0625 0.141 0.25 0.391 0.562 0.766 1.]
or
print (numpy.power(numpy.linspace(0.0, 1.0, num=9), 2) )
# [0. 0.016 0.0625 0.141 0.25 0.391 0.562 0.766 1.]
Edit:
A more generalized approach could be to 1) take the inverse power of the start and stop numbers, 2) get the linear spacing between those values, 3) return the array raised to the power.
import numpy as np
def powspace(start, stop, power, num):
start = np.power(start, 1/float(power))
stop = np.power(stop, 1/float(power))
return np.power( np.linspace(start, stop, num=num), power)
print( powspace(0, 1, 2, 9) )
# [0. 0.016 0.0625 0.141 0.25 0.391 0.562 0.766 1.]
Then you can go between any positive values. For example, going from 9000 to 1000 with values spaced to the power of 3:
print( powspace(9000, 1000, 3, 9) )
# [9000. 7358.8 5930.4 4699.9 3652.6 2773.7 2048.5 1462.2 1000.]
推荐阅读
- java - 静态成员的方法是否被认为是静态的?
- kubernetes - 使用 Kubernetes 注册 GOCD 代理
- ios - 检索 RLMArray 中存在值的 RLMObject
在 iOS 中 - c# - 如何在c#或jquery中将双引号替换为单引号?
- java - Hibernate - 自定义查询未通过 ManyToOne 单向关系的 child 参数找到实体
- java - 如何在jsp页面中将字符串显示为xml
- hadoop - 使用 ElasticSearch 作为永久存储
- ios - 在 IOS 中使用 AVFoundation 捕获相机图像和实时过滤器
- angularjs - ng-repeat 在 angularjs 中具有数组的对象上
- ios - 实现从 iOS7 到 iOS 11 的推送通知。* 我必须处理所有三种情况吗?