使用sklearn进行多项式回归的最简单方法是什么? [英] Which is the simplest way to make a polynomial regression with sklearn?

查看:180
本文介绍了使用sklearn进行多项式回归的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据不符合线性回归,

I have some data that doesn't fit a linear regression,

实际上应该完全"适合二次函数:

In fact should fit 'exactly' a quadratic function:

P = R*I**2 

我在模仿:

model = sklearn.linear_model.LinearRegression()

model = sklearn.linear_model.LinearRegression()

X = alambres[alambre]['mediciones'][x].reshape(-1, 1)
Y = alambres[alambre]['mediciones'][y].reshape(-1, 1)
model.fit(X,Y)

是否有可能通过执行以下操作来解决此问题:

Is there any chance to solve it by doing something like:

model.fit([X,X**2],Y)?

推荐答案

您可以使用numpy的

You can use numpy's polyfit.

import numpy as np
from matplotlib import pyplot as plt
X = np.linspace(0, 100, 50)
Y = 23.24 + 2.2*X + 0.24*(X**2) + 10*np.random.randn(50) #added some noise
coefs = np.polyfit(X, Y, 2)
print(coefs)
p = np.poly1d(coefs)
plt.plot(X, Y, "bo", markersize= 2)
plt.plot(X, p(X), "r-") #p(X) evaluates the polynomial at X
plt.show()

出局:

[  0.24052058   2.1426103   25.59437789]

这篇关于使用sklearn进行多项式回归的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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