使用 sklearn 进行多项式回归的最简单方法? [英] Simplest way to make a polynomial regression with sklearn?

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

问题描述

我有一些不适合线性回归的数据:

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

P = R*I**2

我正在做这个:

model = sklearn.linear_model.LinearRegression()X = alambres[alambre]['mediciones'][x].reshape(-1, 1)Y = alambres[alambre]['mediciones'][y].reshape(-1, 1)模型拟合(X,Y)

是否有机会通过执行以下操作来解决它:

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

解决方案

你可以使用 numpy 的

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

In fact should fit a quadratic function 'exactly':

P = R*I**2 

I'm making this:

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)

解决方案

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()

Out:

[  0.24052058   2.1426103   25.59437789]

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

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