Cross-Validation

Machine Learning with PySpark

Andrew Collier

Data Scientist, Fathom Data

ชุดข้อมูลทั้งหมด

Machine Learning with PySpark

ชุดข้อมูลแบ่งเป็นชุดฝึกและชุดทดสอบ

Machine Learning with PySpark

ข้อมูลฝึกแบ่งออกเป็นหลาย fold

Machine Learning with PySpark

fold แล้ว fold - fold แรก

fold แรก

Machine Learning with PySpark

fold แล้ว fold - fold ที่สอง

fold ที่สอง

Machine Learning with PySpark

fold แล้ว fold - fold อื่น ๆ

fold ที่เหลือ

Machine Learning with PySpark

กลับมาที่ข้อมูลรถยนต์

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|
+------+---+-----------+
Machine Learning with PySpark

Estimator และ evaluator

ออบเจกต์สำหรับสร้างโมเดล ซึ่งอาจเป็น pipeline ก็ได้

regression = LinearRegression(labelCol='consumption')

ออบเจกต์สำหรับประเมินประสิทธิภาพโมเดล

evaluator = RegressionEvaluator(labelCol='consumption')
Machine Learning with PySpark

กริดและ cross-validator

from pyspark.ml.tuning import CrossValidator, ParamGridBuilder

กริดของค่าพารามิเตอร์ (ว่างไว้ก่อน)

params = ParamGridBuilder().build()

ออบเจกต์ cross-validation

cv = CrossValidator(estimator=regression,
                    estimatorParamMaps=params,
                    evaluator=evaluator,
                    numFolds=10, seed=13)
Machine Learning with PySpark

Cross-validator ก็ต้องฝึกเช่นกัน

นำ cross-validation ไปใช้กับข้อมูลฝึก

cv = cv.fit(cars_train)

ค่าเฉลี่ย RMSE ใน fold ต่าง ๆ เป็นเท่าไร?

cv.avgMetrics
[0.800663722151572]
Machine Learning with PySpark

Cross-validator ใช้งานเหมือนโมเดล

ทำนายผลบนข้อมูลทดสอบชุดเดิม

evaluator.evaluate(cv.transform(cars_test))
# RMSE on testing data
0.745974203928479

น้อยกว่าค่า RMSE จาก cross-validation มาก

# RMSE from cross-validation
0.800663722151572

การแบ่ง train-test แบบธรรมดาอาจทำให้ประเมินประสิทธิภาพโมเดลดีเกินจริง

Machine Learning with PySpark

Cross-validate ทุกโมเดลเลย!

Machine Learning with PySpark

Preparing Video For Download...