Python中的多元(多项式)最佳拟合曲线? [英] Multivariate (polynomial) best fit curve in python?

查看:296
本文介绍了Python中的多元(多项式)最佳拟合曲线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在python中计算最佳拟合线,然后将其绘制在matplotlib中的散点图上?

How do you calculate a best fit line in python, and then plot it on a scatterplot in matplotlib?

我当时使用普通最小二乘回归计算线性最佳拟合线,如下所示:

I was I calculate the linear best-fit line using Ordinary Least Squares Regression as follows:

from sklearn import linear_model
clf = linear_model.LinearRegression()
x = [[t.x1,t.x2,t.x3,t.x4,t.x5] for t in self.trainingTexts]
y = [t.human_rating for t in self.trainingTexts]
clf.fit(x,y)
regress_coefs = clf.coef_
regress_intercept = clf.intercept_      

这是多变量的(每种情况有很多x值).因此,X是一个列表列表,而y是一个列表. 例如:

This is multivariate (there are many x-values for each case). So, X is a list of lists, and y is a single list. For example:

x = [[1,2,3,4,5], [2,2,4,4,5], [2,2,4,4,1]] 
y = [1,2,3,4,5]

但是我该如何使用高阶多项式函数呢?例如,不仅是线性的(x等于M = 1的幂),还包括二项式(x等于M = 2的幂),二次方(x等于M = 4的幂)等等.例如,如何从以下项中获得最佳拟合曲线?

But how do I do this with higher order polynomial functions. For example, not just linear (x to the power of M=1), but binomial (x to the power of M=2), quadratics (x to the power of M=4), and so on. For example, how to I get the best fit curves from the following?

摘录自克里斯托弗·毕晓普(Christopher Bishops)的模式识别和机器学习",第7页:

推荐答案

对此的公认答案问题 提供 小型的多边形拟合库 ,它可以完全满足您的需求numpy,然后您可以将结果插入到绘图中,如下所示.

The accepted answer to this question provides a small multi poly fit library which will do exactly what you need using numpy, and you can plug the result into the plotting as I've outlined below.

您只需将x和y点的数组以及所需的拟合度(顺序)传递到multipolyfit中.这将返回系数,然后您可以使用numpy的polyval进行绘制.

You would just pass in your arrays of x and y points and the degree(order) of fit you require into multipolyfit. This returns the coefficients which you can then use for plotting using numpy's polyval.

注意:以下代码已进行修改以进行多变量拟合,但是绘图图像是较早的非多变量答案的一部分.

Note: The code below has been amended to do multivariate fitting, but the plot image was part of the earlier, non-multivariate answer.

import numpy
import matplotlib.pyplot as plt
import multipolyfit as mpf

data = [[1,1],[4,3],[8,3],[11,4],[10,7],[15,11],[16,12]]
x, y = zip(*data)
plt.plot(x, y, 'kx')

stacked_x = numpy.array([x,x+1,x-1])
coeffs = mpf(stacked_x, y, deg) 
x2 = numpy.arange(min(x)-1, max(x)+1, .01) #use more points for a smoother plot
y2 = numpy.polyval(coeffs, x2) #Evaluates the polynomial for each x2 value
plt.plot(x2, y2, label="deg=3")

注意:这是前面答案的一部分,如果您没有多变量数据,它仍然很重要.代替coeffs = mpf(...,使用coeffs = numpy.polyfit(x,y,3)

Note: This was part of the answer earlier on, it is still relevant if you don't have multivariate data. Instead of coeffs = mpf(..., use coeffs = numpy.polyfit(x,y,3)

对于非多元数据集,最简单的方法可能是使用numpy的 polyfit :

For non-multivariate data sets, the easiest way to do this is probably with numpy's polyfit:

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

最小二乘多项式拟合.

将阶数deg的多项式p(x) = p[0] * x**deg + ... + p[deg]拟合到点(x, y).返回系数p的矢量,该矢量使平方误差最小.

Fit a polynomial p(x) = p[0] * x**deg + ... + p[deg] of degree deg to points (x, y). Returns a vector of coefficients p that minimises the squared error.

这篇关于Python中的多元(多项式)最佳拟合曲线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆