Pipeline

Uczenie maszynowe z PySpark

Andrew Collier

Data Scientist, Fathom Data

Wyciek danych?

Metoda fit()

Tylko dla danych treningowych.

Metoda transform()

Dla danych testowych i treningowych.

Uczenie maszynowe z PySpark

Model z wyciekiem danych

Model, w którym dane testowe użyto do trenowania

Uczenie maszynowe z PySpark

Model bez wycieku danych

Model, w którym do trenowania użyto tylko danych treningowych

Uczenie maszynowe z PySpark

Pipeline

Pipeline składa się z szeregu operacji.

Pipeline z wieloma etapami

Można stosować każdą operację osobno... lub po prostu użyć pipeline!

Uczenie maszynowe z PySpark

Model samochodów: Kroki

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')
Uczenie maszynowe z PySpark

Model samochodów: Stosowanie kroków

Dane treningowe

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)
# Fit model to training data
regression = regression.fit(cars_train)

Dane testowe

cars_test  = indexer.transform(cars_test)
cars_test  = onehot.transform(cars_test)
cars_test  = assemble.transform(cars_test)
# Make predictions on testing data
predictions = regression.transform(cars_test)
Uczenie maszynowe z PySpark

Model samochodów: Pipeline

Połączenie kroków w pipeline.

from pyspark.ml import Pipeline

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

Dane treningowe

pipeline = pipeline.fit(cars_train)

Dane testowe

predictions = pipeline.transform(cars_test)
Uczenie maszynowe z PySpark

Model samochodów: Etapy

Dostęp do poszczególnych etapów za pomocą atrybutu .stages.

# The LinearRegression object (fourth stage -> index 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])
Uczenie maszynowe z PySpark

Pipeline usprawnia przepływ pracy!

Uczenie maszynowe z PySpark

Preparing Video For Download...