机器学习:Python实现多项式线性回归

在统计学中,多项式回归是回归分析的一种形式,其中独立变量x和因变量y之间的关系被建模为x中的n次多项式。多项式回归拟合x值与y的相应条件均值之间的非线性关系,被表示为E(y |x),并已被用于描述诸如组织生长速率、湖泊沉积物中碳同位素分布的非线性现象。虽然多项式回归适合于数据的非线性模型,但作为一个统计估计问题,它是线性的,因为回归函数E(y | x)在从数据估计的未知参数中是线性的。因此,多项式回归被认为是多元线性回归的一个特例。

由“基线”变量的多项式展开得到的解释性(独立)变量称为高次项。这些变量也用于分类设置。

请注意PR与MLR(多元线性回归)非常相似,但同时考虑同一个X1变量而不是不同的变量,但用不同的幂;基本上我们用一个变量来表示同一个原始变量的不同幂。

PR由下式给出:

机器学习:Python实现多项式线性回归

当SL直线不适合我们的obervations并且我们想要获得抛物线效果时,我们使用PR:

机器学习:Python实现多项式线性回归

PR有自己独特的案例,当我们遇到问题时,我们可能先尝试SLR和MLR,看看会发生什么。有了PR,我们有时会获得更好的结果。例如,PR用于观察流行病如何在人口中传播,以及类似的用例,因此它是我们想要预测的问题,当然,在我们的库中拥有更多工具总是好的。但此时您可能会问自己为什么PR仍被视为线性模型?这里的诀窍是,当我们谈论线性和非线性模型时,我们并没有考虑变量,而是考虑系数。所以问题是函数是否可以表示为这些系数的线性组合,最终未知是找到系数值的目标,这就是线性和非线性指系数的原因。所以PR是MLR的一个特例,而不是标准的新型回归

为了实现这个Python,我们首先要从sklearn.linearmodel创建一个LR模型并调用我们的Object lin_reg:

# Data Preprocessing

# Importing the Library

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

# Importing the dataset

dataset= pd.read_csv('Data.csv')

X = dataset.iloc[: , 1:2].values

Y = dataset.iloc[: , 2].values

# Fitting Simple Linear Regression model to the data set

from sklearn.linear_model import LinearRegression

regressor = LinearRegression()

regressor.fit(X, y)

下一步是通过sklearn.preprocessing库中的一个名为POLYNOMIAL FEATURE的类来构建PR模型。在这个对象中,我们将调用poly_reg作为我们的转换工具,它将在一个新的多项式矩阵中转换我们的矩阵X,我们可以在其中指定我们想要运行机器学习模型的程度,Python代码如下:

# Data Preprocessing

# Importing the Library

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

# Importing the dataset

dataset= pd.read_csv('Data.csv')

X = dataset.iloc[: , 1:2].values

Y = dataset.iloc[: , 2].values

# Fitting Simple Linear Regression model to the data set

from sklearn.linear_model import LinearRegression

regressor = LinearRegression()

regressor.fit(X, y)

# Fitting Polynomial Regression to the Dataset

poly_reg = PolunomialFeatures(degree = 2)

X_poly = poly_reg.fit_transform(X)

poly_reg.fit(X_poly, y)

lin_reg_2 = LinearRegression()

lin_reg_2.fit(X_poly, y)

下一步是通过SCATTER和PLOT功能可视化我们PR的结果,Python实现如下:

# Data Preprocessing

# Importing the Library

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

# Importing the dataset

dataset= pd.read_csv('Data.csv')

X = dataset.iloc[: , 1:2].values

Y = dataset.iloc[: , 2].values

# Fitting Simple Linear Regression model to the data set

from sklearn.linear_model import LinearRegression

regressor = LinearRegression()

regressor.fit(X, y)

# Fitting Polynomial Regression to the Dataset

poly_reg = PolynomialFeatures(degree = 2)

X_poly = poly_reg.fit_transform(X)

poly_reg.fit(X_poly, y)

lin_reg_2 = LinearRegression()

lin_reg_2.fit(X_poly, y)

# Visualising the Linear Regression results

plt.scatter(X, y, color = 'red')

plt.plot(X, lin_reg.predict(X), color = 'blue')

plt.title('Truth or Bluff (Linear Regression)')

plt.xlabel('Position level')

plt.ylabel('Salary')

plt.show()

# Visualising the Polynomial Regression results

plt.scatter(X, y, color = 'red')

plt.plot(X, lin_reg_2.predict(X), color = 'blue')

plt.title('Truth or Bluff (Linear Regression)')

plt.xlabel('Position level')

plt.ylabel('Salary')

plt.show()

注意:我们可以通过RRANGE函数创建一个新的X_grid来获得更高的分辨率,我们在其中指定下限和上限

# Data Preprocessing

# Importing the Library

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

# Importing the dataset

dataset= pd.read_csv('Data.csv')

X = dataset.iloc[: , 1:2].values

Y = dataset.iloc[: , 2].values

# Fitting Simple Linear Regression model to the data set

from sklearn.linear_model import LinearRegression

regressor = LinearRegression()

regressor.fit(X, y)

# Fitting Polynomial Regression to the Dataset

poly_reg = PolynomialFeatures(degree = 2)

X_poly = poly_reg.fit_transform(X)

poly_reg.fit(X_poly, y)

lin_reg_2 = LinearRegression()

lin_reg_2.fit(X_poly, y)

# Visualising the Linear Regression results

plt.scatter(X, y, color = 'red')

plt.plot(X, lin_reg.predict(X), color = 'blue')

plt.title('Truth or Bluff (Linear Regression)')

plt.xlabel('Position level')

plt.ylabel('Salary')

plt.show()

# Visualising the Polynomial Regression results

X_grid = np.arrange(min(X), max(X), 0.1)

X_grid = X_grid.reshape()

plt.scatter(X, y, color = 'red')

plt.plot(X, lin_reg_2.predict(X), color = 'blue')

plt.title('Truth or Bluff (Linear Regression)')

plt.xlabel('Position level')

plt.ylabel('Salary')

plt.show()

# Predicting a new result with Linear Regression

lin_reg.predict(6.5)

# Predicting a new result with Polynomial Regression

lin_reg_2.predict(poly_reg.fit_transform(6.5)

最后一步是通过PREDICT函数预测我们的LR和PR的结果。

相关推荐