Python 教程:通过 SQL 机器学习训练线性回归模型

适用于: SQL Server 2017 (14.x) 及更高版本 Azure SQL 托管实例

此系列教程由四个部分组成。在第三部分中,引导你在 Python 中定型线性回归模型。 在本系列的下一部分中,你将在机器学习服务中或 SQL Server 2019 大数据群集上将此模型部署到 SQL Server 数据库中。

此系列教程由四个部分组成。在第三部分中,引导你在 Python 中定型线性回归模型。 在本系列的下一部分中,使用机器学习服务在 SQL Server 数据库中部署此模型。

此系列教程由四个部分组成。在第三部分中,引导你在 Python 中定型线性回归模型。 在本系列的下一部分中,你将使用机器学习服务在 Azure SQL 托管实例数据库中部署此模型。

本文将指导如何进行以下操作:

  • 定型线性回归模型
  • 使用线性回归模型进行预测

第一部分中,你了解了如何还原示例数据库。

第二部分中,你了解了如何将数据从数据库加载到 Python 数据帧中,并在 Python 中准备数据。

第四部分中,你将了解如何将模型存储到数据库中,然后根据你在第二和第三部分中开发的 Python 脚本来创建存储过程。 存储过程将在服务器上运行,以便基于新数据进行预测。

先决条件

  • 本教程的第三部分假设你已完成第一部分及其先决条件。

定型模型

为进行预测,必须找到一个最能描述数据集中变量之间的依赖关系的函数(模型)。 这称为定型模型。 训练数据集是在此系列第二部分中创建的 pandas 数据帧 df 中的整个数据集的一个子集。

使用线性回归算法训练模型 lin_model

# Store the variable we'll be predicting on.
target = "Rentalcount"

# Generate the training set.  Set random_state to be able to replicate results.
train = df.sample(frac=0.8, random_state=1)

# Select anything not in the training set and put it in the testing set.
test = df.loc[~df.index.isin(train.index)]

# Print the shapes of both sets.
print("Training set shape:", train.shape)
print("Testing set shape:", test.shape)

# Initialize the model class.
lin_model = LinearRegression()

# Fit the model to the training data.
lin_model.fit(train[columns], train[target])

可得到类似于下面的结果。

Training set shape: (362, 7)
Testing set shape: (91, 7)

作出预测

使用 PREDICT 函数预测使用模型 lin_model 的租赁计数。

# Generate our predictions for the test set.
lin_predictions = lin_model.predict(test[columns])
print("Predictions:", lin_predictions)

# Compute error between our test predictions and the actual values.
lin_mse = mean_squared_error(lin_predictions, test[target])
print("Computed error:", lin_mse)

可得到类似于下面的结果。

Predictions: [124.41293228 123.8095075  117.67253182 209.39332151 135.46159387
 199.50603805 472.14918499  90.15781602 216.61319499 120.30710327
  89.47591091 127.71290441 207.44065517 125.68466139 201.38119194
 204.29377218 127.4494643  113.42721447 127.37388762  94.66754136
  90.21979191 173.86647615 130.34747586 111.81550069 118.88131715
 124.74028405 211.95038051 202.06309706 123.53053083 167.06313191
 206.24643852 122.64812937 179.98791527 125.1558454  168.00847713
 120.2305587  196.60802649 117.00616326 173.20010759  89.9563518
  92.11048236 120.91052805 175.47818992 129.65196995 120.97443971
 175.95863082 127.24800008 135.05866542 206.49627783  91.63004147
 115.78280925 208.92841718 213.5137192  212.83278197  96.74415948
  95.1324457  199.9089665  206.10791806 126.16510228 120.0281266
 209.08150631 132.88996619 178.84110582 128.85971386 124.67637239
 115.58134503  96.82167192 514.61789505 125.48319717 207.50359894
 121.64080826 201.9381774  113.22575025 202.46505762  90.7002328
  92.31194658 201.25627228 516.97252195  91.36660136 599.27093251
 199.6445585  123.66905128 117.4710676  173.12259514 129.60359486
 209.59478573 206.29481361 210.69322009 205.50255751 210.88011563
 207.65572019]
Computed error: 35003.54030828391

后续步骤

在本系列教程的第三部分中,你已完成以下步骤:

  • 定型线性回归模型
  • 使用线性回归模型进行预测

若要部署已创建的机器学习模型,请按照本系列教程的第四部分进行操作: