Python のチュートリアル:SQL 機械学習で線形回帰モデルをトレーニングする

適用対象: SQL Server 2017 (14.x) 以降 Azure SQL Managed Instance

この 4 部構成のチュートリアル シリーズの第 3 部では、Python で線形回帰モデルをトレーニングします。 このシリーズの次の部では、Machine Learning Services または SQL Server 2019 ビッグ データ クラスターを使用して、このモデルを SQL Server データベースにデプロイします。

この 4 部構成のチュートリアル シリーズの第 3 部では、Python で線形回帰モデルをトレーニングします。 このシリーズの次の部では、Machine Learning Services を使用して、このモデルを SQL Server データベースにデプロイします。

この 4 部構成のチュートリアル シリーズの第 3 部では、Python で線形回帰モデルをトレーニングします。 このシリーズの次の部では、Machine Learning Services を使用して、このモデルを Azure SQL Managed Instance データベースにデプロイします。

この記事では、次の方法について学習します。

  • 線形回帰モデルをトレーニングする
  • 線形回帰モデルを使用して予測を作成する

第 1 部では、サンプル データベースを復元する方法を学習しました。

パート 2 では、データベースから Python データ フレームにデータを読み込み、Python でデータを準備する方法を学習しました。

パート 4 では、モデルをデータベースに格納した後、パート 2 と 3 で開発した Python スクリプトからストアド プロシージャを作成する方法について学習します。 ストアド プロシージャは、新しいデータに基づいて予測を行うためにサーバーで実行されます。

前提条件

  • このチュートリアルの第 3 部は、第 1 部とその前提条件を完了していることを前提としています。

モデルをトレーニングする

予測するには、データセット内の変数間の依存関係を最も適切に説明する関数 (モデル) を見つける必要があります。 これは、モデルのトレーニングと呼ばれます。 トレーニング データセットは、このシリーズの第 2 部で作成した、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 を使用してレンタル カウントを予測するには、predict 関数を使用します。

# 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

次のステップ

このチュートリアル シリーズのパート 3 では、次の手順を完了しました。

  • 線形回帰モデルをトレーニングする
  • 線形回帰モデルを使用して予測を作成する

作成した機械学習モデルをデプロイするには、このチュートリアル シリーズのパート 4 の手順に従います。