[英]How would I display a 3rd order polynomial boundary line for linear regression in Python?
假設我有一個mx 2數據集X並對其進行線性回歸以找到權重集W。 還假設我通過三階多項式運算符P((x1,x2))=(1,x1,x2,x1 ^ 2,x1 * x2,x2 ^ 2,x1 ^ 3,x1 ^ 2 * x2,x1來轉換數據* x2 ^ 2,x2 ^ 3) ,並對轉換后的數據進行線性回歸並找到權重集w 。
我知道如何在左側繪制線,但是我不確定如何顯示三階多項式。
我的想法是:
plot_poly(X,labels, weights, initial, final, num):
plt.scatter(X[:, 0][labels=='Blue'], X[:, 1][labels=='Blue'], color='blue', marker = '.')
plt.scatter(X[:, 0][labels=='Red'], X[:, 1][labels=='Red']], color='red', marker = '.')
w = weights
x = np.linspace(initial, final, num)
y = w[0]*1 + w[1]*(x) + w[2]*(x) + w[3]*(x**2) + w[4]*(x**2) + \
w[5]*(x**2) + w[6]*(x**3) + w[7]*(x**3) + w[8]*(x**3) + \
w[9]*(x**3)
plt.plot(x,y)
但是,當我嘗試執行此操作時,它似乎失敗了,特別是垂直軸變得如此之大,以至於數據會收縮並且多項式與數據不相近(下圖)。 是否有更好的方法來繪制此圖?
我認為最簡單的方法是計算線性回歸函數的值,該函數是2個參數X[:, 0]
和X[:, 1]
函數,並使用plt.contour(..., levels=[0.5])
繪制2D函數。 參數levels
告訴我什么是決策邊界,我將其設置在標簽0和1之間的中間位置。然后它僅繪制一條線-決策邊界。
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn import datasets
from sklearn.preprocessing import PolynomialFeatures
def plot_poly(X,labels, weights, initial, final, num):
plt.scatter(X[:, 0][labels==0], X[:, 1][labels==0], color='blue', marker = '.')
plt.scatter(X[:, 0][labels==1], X[:, 1][labels==1], color='red', marker = '.')
w = weights
xx1 = np.linspace(initial[0], final[0], num)
xx2 = np.linspace(initial[1], final[1], num)
z = np.zeros((num, num))
for i_x1, x1 in enumerate(xx1):
for i_x2, x2 in enumerate(xx2):
z[i_x2, i_x1] = \
w[0]*1 + \
w[1]*(x1) + w[2]*(x2) + \
w[3]*(x1**2) + w[4]*(x1*x2) + w[5]*(x2**2) + \
w[6]*(x1**3) + w[7]*(x1**2*x2) + w[8]*(x1*x2**2) + w[9]*(x2**3)
xx1, xx2 = np.meshgrid(xx1, xx2)
plt.contour(xx1, xx2, z, levels=[0.5])
# import some data to play with
iris = datasets.load_iris()
X_raw = iris.data[:, :2] # we only take the first two features.
Y = iris.target
# Use only 2 classes
X_raw = X_raw[(Y <= 1), :]
Y = Y[(Y <= 1)]
# Create poly features
poly = PolynomialFeatures(3)
X = poly.fit_transform(X_raw)
# Fit linear regression
linref = LinearRegression(fit_intercept=False)
linref.fit(X, Y)
# Plot
x_min, x_max = X_raw[:, 0].min() - .5, X_raw[:, 0].max() + .5
y_min, y_max = X_raw[:, 1].min() - .5, X_raw[:, 1].max() + .5
plot_poly(X_raw, Y, weights=linref.coef_, initial=[x_min, y_min], final=[x_max, y_max], num=60)
幾點
plt.pcolormesh
, plt.contourf
, plt.contour
或類似的plt.contour
這是我更改為使用多項式特征的sklearn示例
# Code source: Gaël Varoquaux
# Modified for documentation by Jaques Grobler
# License: BSD 3 clause
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
from sklearn.preprocessing import PolynomialFeatures
# import some data to play with
iris = datasets.load_iris()
X_raw = iris.data[:, :2] # we only take the first two features.
Y = iris.target
poly = PolynomialFeatures(3)
X = poly.fit_transform(X_raw)
logreg = LogisticRegression(C=1e5, solver='lbfgs', multi_class='multinomial')
# we create an instance of Neighbours Classifier and fit the data.
logreg.fit(X, Y)
# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
x_min, x_max = X_raw[:, 0].min() - .5, X_raw[:, 0].max() + .5
y_min, y_max = X_raw[:, 1].min() - .5, X_raw[:, 1].max() + .5
h = .02 # step size in the mesh
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
X_plot_raw = np.c_[xx.ravel(), yy.ravel()]
X_plot = poly.transform(X_plot_raw)
Z = logreg.predict(X_plot)
# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.figure(1, figsize=(4, 3))
plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired)
# Plot also the training points
plt.scatter(X_raw[:, 0], X_raw[:, 1], c=Y, edgecolors='k', cmap=plt.cm.Paired)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.