Sparkを使用してボストンの住宅価格予測モデルを作成
3988 ワード
インストール&セッション設定 !pip install pyspark==3.0.1 py4j==0.10.9
!pip install pyspark==3.0.1 py4j==0.10.9
from pyspark.sql import SparkSession
spark = SparkSession \
.builder \
.appName("Boston Housing Linear Regression example") \
.getOrCreate()
spark

ボストン住宅価格予測モデル
データの読み込み
!wget https://s3-geospatial.s3-us-west-2.amazonaws.com/boston_housing.csv
!wget https://s3-geospatial.s3-us-west-2.amazonaws.com/boston_housing.csv
!ls -tl

data = spark.read.csv('./boston_housing.csv', header=True, inferSchema=True)
Sparkマウントdata.printSchema()

フィーチャーベクトルの作成
from pyspark.ml.feature import VectorAssembler
feature_columns = data.columns[:-1]
assembler = VectorAssembler(inputCols=feature_columns, outputCol="features")
data_2 = assembler.transform(data)

トレーニングおよびテスト用のデータテンプレートとリニア回帰モデルの作成
train, test = data_2.randomSplit([0.7, 0.3])
from pyspark.ml.regression import LinearRegression
algo = LinearRegression(featuresCol="features", labelCol="medv")
model = algo.fit(train)
Sparkからモデルのパフォーマンスの測定
evaluation_summary = model.evaluate(test)
evaluation_summary.meanAbsoluteError
3.497902588473653
evaluation_summary.rootMeanSquaredError
5.183455868492738
evaluation_summary.r2
0.6745318757463364
モデル予測の表示
predictions = model.transform(test)
predictions.show()

学習モデルの保存
model.save("boston_housing_model")
!ls boston_housing_model
data metadata
!ls -tl boston_housing_model

from google.colab import drive
drive.mount('/content/gdrive')
model_save_name = "boston_housing_model"
path = F"/content/gdrive/My Drive/boston_housing_model2"
model.save(path)
保存したモデルの使用
from pyspark.ml.regression import LinearRegressionModel
loaded_model = LinearRegressionModel.load(path) # "boston_housing_model")
ストレージReference
この問題について(Sparkを使用してボストンの住宅価格予測モデルを作成), 我々は、より多くの情報をここで見つけました https://velog.io/@peterpictor/Spark-Spark를-이용한-보스턴-주택-가격-예측-모델-제작テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol