【发布时间】:2015-09-25 19:28:24
【问题描述】:
我正在尝试使用 matplotlib 绘制一些可视化,并且在我的一个函数中,我检查波是否是对数的。这是我目前的工作版本:
import numpy as np
def is_logarithmic(waves):
def expfunc(x, a, b, c):
return a*np.exp(b*x) + c
wcopy = list(waves)
wcopy.sort()
# If the ratio of x-max : x-min < 10, don't use a logarithmic scale
# (at least in matplotlib)
if (wcopy[-1] / wcopy[0]) < 10:
return False
# Take a guess at whether it is logarithmic by seeing how well the x-scale
# fits an exponential curve
diffs = []
for ii in range(len(wcopy) - 1):
diffs.append(wcopy[ii + 1] - wcopy[ii])
# Fit the diffs to an exponential curve
x = np.arange(len(wcopy)-1)
try:
popt, pcov = curve_fit(expfunc, x, diffs)
except Exception as e:
print e
popt = [0.0, 0.0, 0.0]
pcov = np.inf
# If a > 0.5 and covsum < 1000.0
# use a logarithmic scale.
if type(pcov) == float:
# It's probably np.inf
covsum = pcov
else:
covsum = pcov.diagonal().sum()
res = (covsum < 1000.0) & (popt[0] > 0.5)
return res
我正在尝试寻找 scipy 的 curve_fit() 的替代品,因为我不想安装这么大的库只是为了使用那个功能。有没有其他我可以使用的东西,或者理想情况下仅使用 numpy 和 matplotlib 的其他功能的组合,以获得类似的结果?
【问题讨论】:
-
好吧,
curve_fit使用Levenberg-Marquardt 算法来最小化错误。你总是可以自己实现它。 -
除非您使用嵌入式系统,否则 scipy 的
46 MB(安装在 linux 上)并没有那么多。相比之下,Matplotlib 是72 MB。
标签: python numpy matplotlib scipy