python-最小二乘法

最小平方法是十九世纪统计学的主题曲。 从许多方面来看, 它之于统计学就相当于十八世纪的微积分之于数学。
—-乔治·斯蒂格勒的《The History of Statistics》

numpy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)

"""
x:要拟合点的横坐标
y:要拟合点的纵坐标
deg:自由度.例如:自由度为2,那么拟合出来的曲线就是二次函数,自由度是3,拟合出来的曲线就是3次函数
"""
def fit_raw_data(x, y):
"""
使用最小二乘法 拟合函数,生成 360*10 个数据,每 1 度 10 个数据点。
:return:

"""
# 求出参数
parameter = np.polyfit(x,y,6)
# 拼接处方程
p = np.poly1d(parameter)
# 上打印拟合多项式
print(p)
# 生成 3600 个数据
gen_x_data = np.linspace(0, 360, 360)
gen_y_data = p(gen_x_data)
return gen_x_data,gen_y_data

问题

RankWarning ‘Polyfit may be poorly conditioned’

polyfit issues a RankWarning when the least-squares fit is badly conditioned. This implies that the best fit is not well-defined due to numerical error. The results may be improved by lowering the polynomial degree or by replacing x by x - x.mean(). The rcond parameter can also be set to a value smaller than its default, but the resulting fit may be spurious: including contributions from the small singular values can add numerical noise to the result.
Note that fitting polynomial coefficients is inherently badly conditioned when the degree of the polynomial is large or the interval of sample points is badly centered. The quality of the fit should always be checked in these cases. When polynomial fits are not satisfactory, splines may be a good alternative.