如何在Python中显示线性回归的三阶多项式边界线?

时间:2018-11-07 04:02:45

标签: python linear-regression

假设我有一个 m x 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

我的目标是复制此类情节。 enter image description here

我知道如何在左侧绘制线,但是我不确定如何显示三阶多项式。

我的想法是:

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)

但是,当我尝试执行此操作时,它似乎失败了,特别是垂直轴变得如此之大,以至于收缩了数据并且多项式与数据不很接近(下图)。有没有更好的方法来绘制此图?

enter image description here

2 个答案:

答案 0 :(得分:2)

我认为最简单的方法是计算线性回归函数的值,该函数是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)

enter image description here

答案 1 :(得分:1)

点对

  • 您似乎想进行分类,我将使用逻辑回归而不是线性回归
  • 您想绘制2D函数-您可以使用plt.pcolormeshplt.contourfplt.contour或类似的

这里是sklearn example,我已更改为使用多项式特征

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

enter image description here