Pipeline

Machine Learning với PySpark

Andrew Collier

Data Scientist, Fathom Data

Rò rỉ?

Phương thức fit()

Chỉ dùng cho dữ liệu huấn luyện.

Phương thức transform()

Dùng cho dữ liệu kiểm tra và huấn luyện.

Machine Learning với PySpark

Mô hình bị rò rỉ

Mô hình dùng dữ liệu kiểm tra để huấn luyện

Machine Learning với PySpark

Mô hình kín

Mô hình chỉ dùng dữ liệu huấn luyện để huấn luyện

Machine Learning với PySpark

Pipeline

Một pipeline gồm chuỗi thao tác.

Pipeline với nhiều giai đoạn

Bạn có thể áp dụng từng thao tác riêng lẻ... hoặc áp dụng cả pipeline!

Machine Learning với PySpark

Mô hình xe: Các bước

indexer = StringIndexer(inputCol='type', outputCol='type_idx')

onehot = OneHotEncoder(inputCols=['type_idx'], outputCols=['type_dummy'])
assemble = VectorAssembler( inputCols=['mass', 'cyl', 'type_dummy'], outputCol='features' )
regression = LinearRegression(labelCol='consumption')
Machine Learning với PySpark

Mô hình xe: Áp dụng các bước

Dữ liệu huấn luyện

indexer = indexer.fit(cars_train)
cars_train = indexer.transform(cars_train)
onehot = onehot.fit(cars_train)
cars_train = onehot.transform(cars_train)
cars_train = assemble.transform(cars_train)
# Huấn luyện mô hình trên dữ liệu huấn luyện
regression = regression.fit(cars_train)

Dữ liệu kiểm tra

cars_test  = indexer.transform(cars_test)
cars_test  = onehot.transform(cars_test)
cars_test  = assemble.transform(cars_test)
# Dự báo trên dữ liệu kiểm tra
predictions = regression.transform(cars_test)
Machine Learning với PySpark

Mô hình xe: Pipeline

Kết hợp các bước vào một pipeline.

from pyspark.ml import Pipeline

pipeline = Pipeline(stages=[indexer, onehot, assemble, regression])

Dữ liệu huấn luyện

pipeline = pipeline.fit(cars_train)

Dữ liệu kiểm tra

predictions = pipeline.transform(cars_test)
Machine Learning với PySpark

Mô hình xe: Các giai đoạn

Truy cập từng giai đoạn qua thuộc tính .stages.

# Đối tượng LinearRegression (giai đoạn 4 -> chỉ số 3)
pipeline.stages[3]

print(pipeline.stages[3].intercept)
4.19433571782916
print(pipeline.stages[3].coefficients)
DenseVector([0.0028, 0.2705, -1.1813, -1.3696, -1.1751, -1.1553, -1.8894])
Machine Learning với PySpark

Pipeline tối ưu quy trình!

Machine Learning với PySpark

Preparing Video For Download...