使用 PySpark 进行机器学习
Andrew Collier
Data Scientist, Fathom Data






cars.select('mass', 'cyl', 'consumption').show(5)
+------+---+-----------+
| mass|cyl|consumption|
+------+---+-----------+
|1451.0| 6| 9.05|
|1129.0| 4| 6.53|
|1399.0| 4| 7.84|
|1147.0| 4| 7.84|
|1111.0| 4| 9.05|
+------+---+-----------+
用于构建模型的对象,可为流水线。
regression = LinearRegression(labelCol='consumption')
用于评估模型性能的对象。
evaluator = RegressionEvaluator(labelCol='consumption')
from pyspark.ml.tuning import CrossValidator, ParamGridBuilder
参数网格(暂为空)。
params = ParamGridBuilder().build()
交叉验证对象。
cv = CrossValidator(estimator=regression,
estimatorParamMaps=params,
evaluator=evaluator,
numFolds=10, seed=13)
将交叉验证应用于训练集。
cv = cv.fit(cars_train)
各折的平均 RMSE 是多少?
cv.avgMetrics
[0.800663722151572]
在原始测试集上预测。
evaluator.evaluate(cv.transform(cars_test))
# 测试集 RMSE
0.745974203928479
远小于交叉验证的 RMSE。
# 交叉验证 RMSE
0.800663722151572
简单的训练-测试划分会对模型性能过于乐观。
使用 PySpark 进行机器学习