python - 具有无限求和的 Python 曲线拟合
问题描述
我正在尝试在 python 中进行曲线拟合,其中包括无限求和。
我总是出错。
from mpmath import nsum, exp, inf
import numpy as np
import pylab
from scipy.optimize import curve_fit
# get the data
y=[]
x=[]
for t in range(0,100,10):
x += [t]
y += [round(float((nsum(lambda n: exp(-(0.0002)*(n)*t),[0, inf]))),3)]
print(y)
# curve fitting
def test(t,D):
return [round(float((nsum(lambda n: exp(-(D)*(n)*t),[0, inf]))),3)]
parameters, params_covariance=curve_fit(test, x, y)
print(parameters)
我希望我可以从曲线拟合函数中得到估计的 D 但我总是得到错误:
TypeError: cannot create mpf from array([mpf('0.0'), mpf('0.0'), mpf('0.0'), mpf('0.0'), mpf('0.0'),
mpf('0.0'), mpf('0.0'), mpf('0.0'), mpf('0.0'), mpf('0.0')],
dtype=object)
谢谢!
解决方案
您需要将 test() 的输入和输出列在列表中
def test(x,D):
return [float((nsum(lambda n: exp(-(D)*(n)*t),[0, inf]))) for t in x]
推荐阅读
- javascript - 如何访问数组对象并使用新创建的函数再次调用它们?使用有问题的代码示例
- java - 为什么我在提交 Fragment 后收到错误“getParentActivityIntent:badActivity name”?
- encryption - 尝试恢复密码但卡住 base64 编码?
- tensorflow - Spyder 无法在 conda 环境中加载 tensorflow,尽管 python 可以
- youtube - Windows 10 中的网络使用率永远不会超过 1%?
- ios - 如何从转义闭包中获取数据到 UICollectionview?
- gridview - 如何在 Kartik 的 GridView 中显示 RBAC 用户角色 - Yii2
- encryption - 从公钥获取 RSA 私钥
- python - AttributeError:部分初始化的模块'numpy1'没有属性'array'(很可能是由于循环导入)
- rxjs - 如果其中一个为真,我如何将 2 个布尔可观察对象组合成一个布尔可观察对象?