- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我的代码:
for i in range(-area,area):
stDev1= []
for j in range(-area,area):
stDev0 = stDev[i+i0][j+j0]
stDev1.append(stDev0)
slices[i] = stDev1
fitV = []
xV = []
for l in range(-area,area):
y = np.asarray(slices[l])
x = np.arange(0,2*area,1)
for m in range(-area,area):
fitV.append(slices[m][l])
xV.append(l)
fit = np.polyfit(xV,fitV,4)
yfit = function(fit,area)
x100 = np.arange(0,100,1)
plt.plot(xV,fitV,'.')
plt.savefig("fits1.png")
def function(fit,area):
yfit = []
for x in range(-area,area):
yfit.append(fit[0]+fit[1]*x+fit[2]*x**2+fit[3]*x**3+fit[4]*x**4)
return(yfit)
i0 = 400
j0 = 400
area = 50
stdev = 2d np.array([1300][800]) #just an image of "noise" feel free to add any image // 2d np array you like.
显然这是完全错误的?我想我想念 Polyfit 的概念?从文档中,要求我向它提供两个形状为 x[i] y[i] 的数组?我的值(value)观
xV = [ x_1_-50,x_1_-49,...,x_1_49,x_2_-50,...,x_49_49]
我的ys是:
fitV = [y_1_-50,y_1_-49,...,y_1_49,...y_2_-50,...,y_2_49]
最佳答案
我不完全理解你的程序。将来,如果您将问题提炼为 MCVE 将会很有帮助。 。但这里有一些想法:
在您的数据中,对于给定的 x 值,似乎有多个 y 值。给定 (x,y) 数据,polyfit
返回一个表示多项式函数的元组,但没有函数可以将x的单个值映射到y的多个值。第一步,考虑使用平均值、中位数或众数等将每组 y 值折叠为单个代表值。或者,在您的领域中,可能有一种更自然的方法来做到这一点。
其次,有一种惯用的方式来使用这对函数 np.polyfit
和np.polyval
,并且您没有以标准方式使用它们。当然,存在许多与此模式不同的有用的方式,但首先请确保您了解这两个函数的基本模式。
a.给定您在时间或地点 x_data 进行的测量值 y_data,绘制它们并猜测拟合顺序。也就是说,它看起来像一条线吗?像抛物线一样?假设您认为您的数据是抛物线型的,并且您将使用二阶多项式拟合。
b.确保您的数组按升序排序 x
。有很多方法可以做到这一点,但是 np.argsort
这是一个简单的问题。
c.运行polyfit
:p = polyfit(x_data,y_data,2)
,它返回一个包含 p
中的二阶、一阶和零阶系数的元组, (c2,c1,c0)
.
d.惯用用法是 polyfit
和polyval
,接下来您将生成适合的: polyval(p,x_data)
。或者您可能希望对拟合进行更粗略或更精细的采样,在这种情况下,您可能会采用 x_data
的子集。或在 x_data
中插入更多值.
下面是一个完整的示例。
import numpy as np
from matplotlib import pyplot as plt
# these are your measurements, unsorted
x_data = np.array([18, 6, 9, 12 , 3, 0, 15])
y_data = np.array([583.26347805, 63.16059915, 100.94286909, 183.72581827, 62.24497418,
134.99558191, 368.78421529])
# first, sort both vectors in increasing-x order:
sorted_indices = np.argsort(x_data)
x_data = x_data[sorted_indices]
y_data = y_data[sorted_indices]
# now, plot and observe the parabolic shape:
plt.plot(x_data,y_data,'ks')
plt.show()
# generate the 2nd order fitting polynomial:
p = np.polyfit(x_data,y_data,2)
# make a more finely sampled x_fit vector with, for example
# 1024 equally spaced points between the first and last
# values of x_data
x_fit = np.linspace(x_data[0],x_data[-1],1024)
# now, compute the fit using your polynomial:
y_fit = np.polyval(p,x_fit)
# and plot them together:
plt.plot(x_data,y_data,'ks')
plt.plot(x_fit,y_fit,'b--')
plt.show()
希望有帮助。
关于python - numpy polyfit 产生无意义的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39660663/
这个问题在这里已经有了答案: Relationship between SciPy and NumPy (8 个答案) 关闭 6 年前。 我开始知道 numpy 和 scipy 都有 polyfit
为什么要 numpy.polyfit 和 numpy.polynomial.polynomial.polyfit 在下面的测试中产生不同的图? import numpy as np from nump
有没有办法计算两个变量的多项式模型的参数。它们是独立的,因此: z = a + bx + cx^2 + dy + ey^2 有人告诉我,您可以为此使用 numpy.polyfit,但它只能支持两个变量
有什么区别 https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html 和 https://docs.scipy.o
我使用了 numpy 的 polyfit 并获得了两个数组 x 和 y 的非常好的拟合(使用 7 阶多项式)。我的关系是这样的; y(x) = p[0]* x^7 + p[1]*x^6 + p[2]*
我正在尝试将二次方程拟合到一些实验数据并在 numpy 中使用 polyfit。我希望得到一条凹曲线,因此要确保二次项的系数为负,并且拟合本身也是加权的,因为点上有一些权重。有没有简单的方法来做到这一
我有一个对象以抛物线模式抛出的信息。从开始位置到结束位置以特定间隔总共拍摄了 30 张图像。 现在我已经设法提取了在所有 30 个图像中抛出的对象的 x,y 坐标...... 我认为使用 polyfi
我正在模拟三体问题并绘制 3D 轨迹图。我试图弄清楚如何通过使用 np.polyfit 扩展绘图线来预测这些行星的轨迹。我有使用数据框和 2D 图执行此操作的经验,但没有在 3D 中且不使用任何类型的
我正在尝试使用np.polyfit,但我不断收到错误: TypeError: polyfit() got an unexpected keyword argument 'w' documentatio
我有一个包含 20 列的数据框。我希望创建散点图,每个散点图都有一条最合适的线。 x 列将保持不变,我想使用 for 循环遍历数据框中的每个其他列。结果将是 19 个散点图。 我当前的设置看起来像这样
当用一条直线拟合一组数据并用误差加权时,我期望 polyfit 返回一个 2x2 协方差矩阵,我可以从中对对角线元素求平方根以找到系数中的不确定性,但我没有. 这是一个最小的工作示例: from nu
我找不到调整数据的曲线(列出“chi”和“m”)。我使用 polyfit 生成曲线,但它不足以捕捉点的行为。 前面的代码有一个图表,显示了数据和调整之间的差异。 import matplotlib.p
我想向量化以下函数 def nppolyfit(pnp_array, **kwargs): """ Moving polyfit """ win_size = kwargs['length
我浏览了文档,但无法正确解释 在我的代码中,我想找到一条通过 2 个点 (x1,y1), (x2,y2) 的直线,所以我使用了 np.polyfit((x1,x2),(y1,y2),1)因为它是一个
在用numpy的polyfit创建一条best fit时,可以指定参数full为True。除了系数之外,这会返回 4 个额外的值。这些值是什么意思,它们告诉我函数与我的数据的拟合程度如何? https
我有一些数据,我希望对其进行建模,以便能够在与数据相同的范围内获得相对准确的值。 为此,我使用 polyfit 来拟合 6 阶多项式,由于我的 x 轴值,它建议我将其居中并缩放以获得更准确的拟合。 但
这只是一个基本问题。我正在使用 polyfit 拟合线以散点。在某些情况下,我的散点具有相同的 X 值,而 polyfit 无法为其拟合一条线。必须有一些东西可以处理这种情况。毕竟,它只是一个线条配合
我正在尝试适应这些值: 这是我的代码: for i in range(-area,area): stDev1= [] for j in range(-area,area):
关于此:polynomial equation parameters我得到平方函数的 3 个参数 y = a*x² + b*x + c 现在我只想得到描述我的平方函数的 first 参数函数 y =
Polyfit 是一个很好的工具,可以将一条线拟合到一组点上。然而,我的数据具有不同程度的统计显着性。 例如,对于一个点 (x1,y2) 我可能只有 10 个观测值,而对于另一个点 (x2,y2) 我
我是一名优秀的程序员,十分优秀!