使用numpy.polyfit [英] Using numpy.polyfit
问题描述
我已使用numpy.polyfit在某些数据上绘制了一条曲线,并试图找到该曲线与另一条线相交的位置.但是,我似乎误解了该函数的工作方式,就像试图使用生成的系数来查找曲线上的值时一样,我得到了无意义的答案.
I have plotted a curve over some data using numpy.polyfit and am trying to find where the curve intersects a different line. However, I seem to be misunderstanding how the function works as when trying to use the coefficients produced to find values on the curve I am getting non-sensical answers.
2458880.2995 1.595
2458880.3046 1.62
2458880.3566 1.609
2458880.3585 1.599
2458880.7 1.667
2458880.7549 1.571
这里JD是左列,mag是右列
Here JD is the left column and the mag is the right column
x = JD
y = mag
coeffs = numpy.polyfit(x,y,2)
poly = numpy.poly1d(coeffs)
new_x = numpy.linspace(x[0], 2458940)
new_y = poly(new_x)
plt.plot(x,y,'x', new_x,new_y)
a,b,c = coeffs
# y = ax^2 + bx + c
xa = 2458880.2995
ya = a*(xa**2) + b*(xa) + c
print(ya)
当我们期望值接近1.595时,它将输出-2.827387571334839的值
This outputs a value of -2.827387571334839 when we expect a value close to 1.595
因此,曲线正确地拟合了数据,但是当尝试使用生成的系数时,我得到了错误的答案.
So the curve fits correctly over the data but when trying to use the coefficients produced I get incorrect answers.
推荐答案
import matplotlib.pyplot as plt
import numpy
import warnings
warnings.simplefilter('ignore', numpy.RankWarning)
JD = [2458880.2995,2458880.3046,2458880.3566,2458880.3585,2458880.7,2458880.7549]
mag=[1.595,1.62,1.609,1.599,1.667,1.571]
x = JD
y = mag
coeffs = numpy.polyfit(x,y,2)
poly = numpy.poly1d(coeffs)
new_x = numpy.linspace(x[0], 2458940)
new_y = poly(new_x)
plt.plot(x,y,'x', new_x,new_y)
a,b,c = coeffs
# y = ax^2 + bx + c
xa = 2458880.2995
ya = a*(xa**2) + b*(xa) + c
print(ya)
我已添加
import warnings
warnings.simplefilter('ignore', numpy.RankWarning)
由于我收到引起问题的等级警告错误,现在输出为1.6
as i was getting rankwarning error which caused the issue , now the output is 1.6
等级警告意味着系数矩阵在最小二乘拟合中的等级不足.仅当full = False时才发出警告."最后 numpy.ployfit 提到排名警告
"Rank warning means that the rank of the coefficient matrix in the least-squares fit is deficient. The warning is only raised if full = False." numpy.ployfit in the last Rank Warning is mentioned
这篇关于使用numpy.polyfit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!