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

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

在本教學課程系列的第四部分 (總共四個部分) 中,您將使用機器學習服務或巨量資料叢集,將以 Python 開發的線性迴歸模型部署到 SQL Server 資料庫。

在本教學課程系列的第四部分 (總共四個部分) 中,您將使用機器學習服務,將以 Python 開發的線性迴歸模型部署到 SQL Server 資料庫。

在此四部分教學課程的第四部份中,您會使用機器學習服務,將以 Python 開發的線性迴歸模型部署到 Azure SQL 受控執行個體資料庫。

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

  • 建立會產生機器學習模型的預存程序
  • 將模型儲存在資料庫資料表中
  • 建立一個使用此模型進行預測的預存程序
  • 以新資料執行模型

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

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

第三部分中,您已了解如何在 Python 中定型線性迴歸機器學習模型。

Prerequisites

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

建立一個會產生模型的預存程序

現在,使用您所開發的 Python 指令碼建立預存程序 generate_rental_py_model,以使用 scikit-learn 的 LinearRegression 來定型及產生線性迴歸模型。

在 Azure Data Studio 中執行下列 T-SQL 陳述式,以建立用來定型模型的預存程序。

-- Stored procedure that trains and generates a Python model using the rental_data and a linear regression algorithm
DROP PROCEDURE IF EXISTS generate_rental_py_model;
go
CREATE PROCEDURE generate_rental_py_model (@trained_model varbinary(max) OUTPUT)
AS
BEGIN
    EXECUTE sp_execute_external_script
      @language = N'Python'
    , @script = N'
from sklearn.linear_model import LinearRegression
import pickle

df = rental_train_data

# Get all the columns from the dataframe.
columns = df.columns.tolist()

# Store the variable well be predicting on.
target = "RentalCount"

# Initialize the model class.
lin_model = LinearRegression()

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

# Before saving the model to the DB table, convert it to a binary object
trained_model = pickle.dumps(lin_model)'

, @input_data_1 = N'select "RentalCount", "Year", "Month", "Day", "WeekDay", "Snow", "Holiday" from dbo.rental_data where Year < 2015'
, @input_data_1_name = N'rental_train_data'
, @params = N'@trained_model varbinary(max) OUTPUT'
, @trained_model = @trained_model OUTPUT;
END;
GO

將模型儲存在資料庫資料表中

在 TutorialDB 資料庫中建立資料表,然後將模型儲存至資料表。

  1. 在 Azure Data Studio 中執行下列 T-SQL 陳述式,以建立名為 dbo.rental_py_models 的資料表以用於儲存模型。

    USE TutorialDB;
    DROP TABLE IF EXISTS dbo.rental_py_models;
    GO
    CREATE TABLE dbo.rental_py_models (
        model_name VARCHAR(30) NOT NULL DEFAULT('default model') PRIMARY KEY,
        model VARBINARY(MAX) NOT NULL
    );
    GO
    
  2. 將模型以二進位物件形式儲存至資料表,並將模型命名為 linear_model

    DECLARE @model VARBINARY(MAX);
    EXECUTE generate_rental_py_model @model OUTPUT;
    
    INSERT INTO rental_py_models (model_name, model) VALUES('linear_model', @model);
    

建立一個進行預測的預存程序

  1. 建立預存程序 py_predict_rentalcount,以使用定型的模型和一組新資料進行預測。 在 Azure Data Studio 中執行下方的 T-SQL。

    DROP PROCEDURE IF EXISTS py_predict_rentalcount;
    GO
    CREATE PROCEDURE py_predict_rentalcount (@model varchar(100))
    AS
    BEGIN
        DECLARE @py_model varbinary(max) = (select model from rental_py_models where model_name = @model);
    
        EXECUTE sp_execute_external_script
                    @language = N'Python',
                    @script = N'
    
    # Import the scikit-learn function to compute error.
    from sklearn.metrics import mean_squared_error
    import pickle
    import pandas
    
    rental_model = pickle.loads(py_model)
    
    df = rental_score_data
    
    # Get all the columns from the dataframe.
    columns = df.columns.tolist()
    
    # Variable you will be predicting on.
    target = "RentalCount"
    
    # Generate the predictions for the test set.
    lin_predictions = rental_model.predict(df[columns])
    print(lin_predictions)
    
    # Compute error between the test predictions and the actual values.
    lin_mse = mean_squared_error(lin_predictions, df[target])
    #print(lin_mse)
    
    predictions_df = pandas.DataFrame(lin_predictions)
    
    OutputDataSet = pandas.concat([predictions_df, df["RentalCount"], df["Month"], df["Day"], df["WeekDay"], df["Snow"], df["Holiday"], df["Year"]], axis=1)
    '
    , @input_data_1 = N'Select "RentalCount", "Year" ,"Month", "Day", "WeekDay", "Snow", "Holiday"  from rental_data where Year = 2015'
    , @input_data_1_name = N'rental_score_data'
    , @params = N'@py_model varbinary(max)'
    , @py_model = @py_model
    with result sets (("RentalCount_Predicted" float, "RentalCount" float, "Month" float,"Day" float,"WeekDay" float,"Snow" float,"Holiday" float, "Year" float));
    
    END;
    GO
    
  2. 建立用以儲存預測的資料表。

    DROP TABLE IF EXISTS [dbo].[py_rental_predictions];
    GO
    
    CREATE TABLE [dbo].[py_rental_predictions](
     [RentalCount_Predicted] [int] NULL,
     [RentalCount_Actual] [int] NULL,
     [Month] [int] NULL,
     [Day] [int] NULL,
     [WeekDay] [int] NULL,
     [Snow] [int] NULL,
     [Holiday] [int] NULL,
     [Year] [int] NULL
    ) ON [PRIMARY]
    GO
    
  3. 執行預存程序以預測租用次數

    --Insert the results of the predictions for test set into a table
    INSERT INTO py_rental_predictions
    EXEC py_predict_rentalcount 'linear_model';
    
    -- Select contents of the table
    SELECT * FROM py_rental_predictions;
    

    您應該會看見如下所示的結果。

    Prediction results from stored procedure

您已成功建立、定型和部署模型。 接著您在預存程序中使用該模型根據新資料來預測值。

後續步驟

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

  • 建立會產生機器學習模型的預存程序
  • 將模型儲存在資料庫資料表中
  • 建立一個使用此模型進行預測的預存程序
  • 以新資料執行模型

若要深入了解如何搭配 SQL 機器學習使用 Python,請參閱: