- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我从文件中读取一些 x 和 y 数据,转换为 float 并放入单独的数组中,然后从 scipy
调用曲线拟合函数。
它根据我使用的方程式(在定义的函数中)给我不同的错误消息。我在我想使用的方程式之后对代码进行了注释,它是最上面的、未注释的方程式(第 9 行)。
我能理解为什么它可能需要一个 float 而不是一个字符串,但我在类型转换方面的尝试似乎没有奏效。我最常见的错误是 TypeError: a float is required
如果我尝试传递值而不是通过读取我的文件,而是使用 np.linspace
作为我在 scipy 网站上找到的示例,它会给我一个不同的错误。
我已经对代码中的错误进行了注释,希望您发现它没有歧义。我还粘贴了我正在使用的输入文本文件。
import sys
import numpy as np
import math as m
from scipy.optimize import curve_fit
def func( x, a, b ):
return a*m.pow( x, 2 )*np.exp( -b*x ); #the function I want!: line 9 in funcTypeError: a float is required
#return a*m.exp(-b*x) #line 10 in func TypeError: a float is required
#return a*np.exp(-b*x) #Example equation. line 444 in _general_function
#ValueError:operands could not be broadcast together with shapes
#return a*b*m.pow( x, 2 ); #line 10 in func TypeError: a float is required
#end def
file = sys.argv[1];
f = open( file );
y_array = [];
x_array = [];
for line in f:
words = line.split();
x = words[0].rstrip('\r\n');
y = words[1].rstrip('\r\n');
x_array.append( float( x ) );
y_array.append( float( y ) );
#end for
#data = f.read();
popt, pcov = curve_fit( func, x_array, y_array );
或者我从他们在 scipy 网站上给出的例子中尝试这个,用我上面的,未注释的,期望的等式
x = np.linspace(0,4,50)
y = func(x, 2.5, 1.3 )
yn = y + 0.2*np.random.normal(size=len(x))
popt, pcov = curve_fit(func, x, yn)
#TypeError: only length-1 arrays can be converted to Python scalars.
输入文件(只有几行,还有更多)。两列数字
352 28
423 30
494 32
565 3
636 0
707 0
最佳答案
您的 x
是一个列表,您正在对其调用 math.pow
。 math.pow
只知道如何提高 float 或可转换为 float 的东西。因此,TypeError:需要一个 float
。这就是我们拥有 numpy
的原因之一。 :^)
我们可以通过在整个过程中使用 numpy
来简化这件事。然后我们可以简单地使用 **
来获取整个数组的权力。
def func( x, a, b ):
return a * x**2 * np.exp( -b*x )
file = sys.argv[1]
x,y = np.loadtxt(file, unpack=True)
popt, pcov = curve_fit( func, x, y)
给我
>>> popt
array([ 1., 1.])
>>> pcov
inf
使用您的数据,该函数不太适合。该示例效果更好:
>>> x = np.linspace(0,4,50)
>>> y = func(x, 2.5, 1.3 )
>>> yn = y + 0.2*np.random.normal(size=len(x))
>>> popt, pcov = curve_fit(func, x, yn)
>>> popt
array([ 3.15537828, 1.43218611])
>>> pcov
array([[ 0.08045745, 0.01257863],
[ 0.01257863, 0.00232191]])
关于python - 使用 scipy 进行曲线拟合 - 我收到类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20149877/
我在使用 cx_freeze 和 scipy 时无法编译 exe。特别是,我的脚本使用 from scipy.interpolate import griddata 构建过程似乎成功完成,但是当我尝试
是否可以通过函数在 scipy 中定义一个稀疏矩阵,而不是列出所有可能的值?在文档中,我看到可以通过以下方式创建稀疏矩阵 There are seven available sparse matrix
SciPy为非线性最小二乘问题提供了两种功能: optimize.leastsq()仅使用Levenberg-Marquardt算法。 optimize.least_squares()允许我们选择Le
SciPy 中的求解器能否处理复数值(即 x=x'+i*x")?我对使用 Nelder-Mead 类型的最小化函数特别感兴趣。我通常是 Matlab 用户,我知道 Matlab 没有复杂的求解器。如果
我有看起来像这样的数据集: position number_of_tag_at_this_position 3 4 8 6 13 25 23 12 我想对这个数据集应用三次样条插值来插值标签密度;为此
所以,我正在处理维基百科转储,以计算大约 5,700,000 个页面的页面排名。这些文件经过预处理,因此不是 XML 格式。 它们取自 http://haselgrove.id.au/wikipedi
Scipy 和 Numpy 返回归一化的特征向量。我正在尝试将这些向量用于物理应用程序,我需要它们不被标准化。 例如a = np.matrix('-3, 2; -1, 0') W,V = spl.ei
基于此处提供的解释 1 ,我正在尝试使用相同的想法来加速以下积分: import scipy.integrate as si from scipy.optimize import root, fsol
这很容易重新创建。 如果我的脚本 foo.py 是: import scipy 然后运行: python pyinstaller.py --onefile foo.py 当我启动 foo.exe 时,
我想在我的代码中使用 scipy.spatial.distance.cosine。如果我执行类似 import scipy.spatial 或 from scipy import spatial 的操
Numpy 有一个基本的 pxd,声明它的 c 接口(interface)到 cython。是否有用于 scipy 组件(尤其是 scipy.integrate.quadpack)的 pxd? 或者,
有人可以帮我处理 scipy.stats.chisquare 吗?我没有统计/数学背景,我正在使用来自 https://en.wikipedia.org/wiki/Chi-squared_test 的
我正在使用 scipy.odr 拟合数据与权重,但我不知道如何获得拟合优度或 R 平方的度量。有没有人对如何使用函数存储的输出获得此度量有建议? 最佳答案 res_var Output 的属性是所谓的
我刚刚下载了新的 python 3.8,我正在尝试使用以下方法安装 scipy 包: pip3.8 install scipy 但是构建失败并出现以下错误: **Failed to build sci
我有 my own triangulation algorithm它基于 Delaunay 条件和梯度创建三角剖分,使三角形与梯度对齐。 这是一个示例输出: 以上描述与问题无关,但对于上下文是必要的。
这是一个非常基本的问题,但我似乎找不到好的答案。 scipy 到底计算什么内容 scipy.stats.norm(50,10).pdf(45) 据我了解,平均值为 50、标准差为 10 的高斯中像 4
我正在使用 curve_fit 来拟合一阶动态系统的阶跃响应,以估计增益和时间常数。我使用两种方法。第一种方法是在时域中拟合从函数生成的曲线。 # define the first order dyn
让我们假设 x ~ Poisson(2.5);我想计算类似 E(x | x > 2) 的东西。 我认为这可以通过 .dist.expect 运算符来完成,即: D = stats.poisson(2.
我正在通过 OpenMDAO 使用 SLSQP 来解决优化问题。优化工作充分;最后的 SLSQP 输出如下: Optimization terminated successfully. (Exi
log( VA ) = gamma - (1/eta)log[alpha L ^(-eta) + 测试版 K ^(-eta)] 我试图用非线性最小二乘法估计上述函数。我为此使用了 3 个不同的包(Sc
我是一名优秀的程序员,十分优秀!