[英]Polynomial regression in Python using sklearn, numpy and matplotlib
[英]numpy polynomial linear regression with sklearn
我正在嘗試將多項式的線性系統擬合到數據。 numpy
的polynomial
模塊包含一個擬合函數,該函數可以完美運行。 當我嘗試使用sklearn
線性求解器擬合模型時,擬合非常糟糕! 我不明白怎么了。 我構造了一個矩陣X,其中x_ {ij}對應於第i
個觀察到的輸入和第j
個多項式。 我知道X矩陣還可以,因為當我用numpy
查找系數時,數據就非常合適。 我使用了sklearn的fit
函數(我已經嘗試了幾種線性求解器),但是它所求解的系數( coef_
對象)是錯誤的。 我究竟做錯了什么? 如何使sklearn
線性求解器找到的系數與numpy
找到的系數匹配?
import numpy as np
from sklearn import linear_model
from sklearn.linear_model import OrthogonalMatchingPursuit
import matplotlib.pyplot as plt
# accept x and polynomial order, return basis of that order
def legs(x, c):
s = np.zeros(c + 1)
s[-1] = 1
return np.polynomial.legendre.legval(x, s)
# Generate normalized samples
samples = np.random.uniform(2, 3, 5)
evals = samples ** 2
xnorm = (samples - 2) * 2 / (3 - 2) - 1
# instantiate linear regressor
omp = linear_model.LinearRegression()
#omp = linear_model.Lasso(alpha=0.000001)
#omp = OrthogonalMatchingPursuit(n_nonzero_coefs=2)
# construct X matrix. Each row is an observed value.
# Each column is a different polynomial.
X = np.array([[legs(xnorm[jj], ii) for ii in range(5)] for jj in range(xnorm.size)])
# Perform the fit. Why isn't this working?
omp.fit(X, evals)
# Plot the truth data
plt.scatter(xnorm, evals, label='data', s=15, marker='x')
# Dot the coefficients found with sklearn against X
plt.scatter(xnorm, omp.coef_.dot(X.T), label='linear regression')
# Dot the coefficients found with numpy against X
plt.scatter(xnorm, np.polynomial.legendre.legfit(xnorm, evals, 4).dot(X.T), label='Numpy regression')
# complete the plot
plt.legend(ncol=3, prop={'size':3})
plt.savefig('simpleExample')
plt.clf()
您的omp.coef_.dot(XT)
不包含截距; 手動添加或直接使用omp.predict
。
即:
plt.scatter(xnorm, omp.coef_.dot(X.T) + omp.intercept_, label='linear regression')
plt.scatter(xnorm, evals, label='data', s=15, marker='x')
要么
plt.scatter(xnorm, omp.predict(X), label='linear regression')
plt.scatter(xnorm, evals, label='data', s=15, marker='x')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.