Logistic Regression モデルの学習
データのラベルと特徴量を作成できたので、いよいよそれを使って学習するモデルを構築します。ただし学習に入る前に、この演習の最後のパートでは、データを学習用とテスト用に分割し、学習データで Logistic Regression モデルを実行し、最後に学習済みモデルの精度を確認します。
作業環境には SparkContext の sc と、samples 変数が用意されていることを思い出してください。
この演習はコースの一部です
PySparkで学ぶBig Data入門
演習の手順
- 結合済みデータを 80:20 の比率で学習用とテスト用に分割します。
- 学習用データセットで Logistic Regression モデルを学習します。
- 学習済みモデルを使ってテストデータセットの予測ラベルを作成します。
zip関数を使って、テストデータセットのラベルと予測データセットのラベルを結合します。- 元のラベルと予測ラベルを用いて学習済みモデルの精度を計算し、出力します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Split the data into training and testing
train_samples,test_samples = samples.____([0.8, 0.2])
# Train the model
model = LogisticRegressionWithLBFGS.train(____)
# Create a prediction label from the test data
predictions = model.____(test_samples.map(lambda x: x.features))
# Combine original labels with the predicted labels
labels_and_preds = test_samples.map(lambda x: x.label).zip(____)
# Check the accuracy of the model on the test data
accuracy = labels_and_preds.filter(lambda x: x[0] == x[____]).count() / float(test_samples.count())
print("Model accuracy : {:.2f}".format(____))