Python 教學課程:使用 SQL 機器學習定型線性迴歸模型

適用於:SQL Server 2017 (14.x) 和更新版本Azure SQL 受控執行個體

在這個教學課程系列的第三部分 (總共四個部分) 中, 您將使用 Python 來定型線性迴歸模型。 在本系列的下一部分中,您將使用機器學習服務在 SQL Server 資料庫中部署此模型,或在 SQL Server 2019 巨量資料叢集上部署。

在這個教學課程系列的第三部分 (總共四個部分) 中, 您將使用 Python 來定型線性迴歸模型。 在本系列的下一部分中,您將使用機器學習服務在 SQL Server 資料庫中部署此模型。

在這個教學課程系列的第三部分 (總共四個部分) 中, 您將使用 Python 來定型線性迴歸模型。 在此系列的下一部份中,您將使用機器學習服務在 Azure SQL 受控執行個體資料庫中部署此模型。

在本文中,您將學會如何:

  • 定型線性迴歸模型
  • 使用線性迴歸模型進行預測

第一部分,您已了解如何還原範例資料庫。

第二部分中,您已了解如何將資料從資料庫載入到 Python 資料框架,並以 Python 準備資料。

第四部分中,您將了解如何將模型儲存在資料庫中,然後從您在第二和第三部分中開發的 Python 指令碼建立預存程序。 預存程序將會在伺服器上執行,以根據新資料進行預測。

Prerequisites

  • 本教學課程的第三部分會假設您已完成第一部分及其必要條件。

將模型定型

為了進行預測,您必須找出最能描述資料集內變數之間相依性的函數 (模型)。 這稱為「定型模型」。 定型的資料集會是您在本系列課程第二部分中所建立 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)

進行預測

使用預測函數以使用 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

後續步驟

在本教學課程系列的第三部分中,您已完成下列步驟:

  • 定型線性迴歸模型
  • 使用線性迴歸模型進行預測

若要部署您已建立的機器學習模型,請遵循本教學課程系列的第四部分: