哪个是使用sklearn进行多项式回归的最简单方法?

时间:2016-05-09 08:22:41

标签: python pandas scikit-learn polynomials

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

enter image description here

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

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)
model.fit(X,Y)

是否有机会通过以下方式解决问题:

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

2 个答案:

答案 0 :(得分:4)

您可以使用numpy的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]

enter image description here

答案 1 :(得分:1)

使用PolynomialFeatures。

import numpy as np
from sklearn.preprocessing import PolynomialFeatures

x = np.array([[1,],[2,],[3,]])
X = PolynomialFeatures(degree=2).fit_transform(x)
X

输出:

array([[1., 1., 1.],
       [1., 2., 4.],
       [1., 3., 9.]])