파이프라인

PySpark로 하는 Machine Learning

Andrew Collier

Data Scientist, Fathom Data

누수?

fit() 메서드

학습 데이터에만 사용.

transform() 메서드

학습/테스트 데이터에 사용.

PySpark로 하는 Machine Learning

누수 있는 모델

테스트 데이터를 학습에 사용한 모델

PySpark로 하는 Machine Learning

누수 없는 모델

학습 데이터만 학습에 사용한 모델

PySpark로 하는 Machine Learning

파이프라인

파이프라인은 일련의 작업으로 구성됩니다.

여러 단계가 있는 파이프라인

각 작업을 개별로 적용할 수도 있고, 파이프라인 하나로 적용할 수도 있습니다!

PySpark로 하는 Machine Learning

자동차 모델: 단계

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')
PySpark로 하는 Machine Learning

자동차 모델: 단계 적용

학습 데이터

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)
# 학습 데이터로 모델 적합
regression = regression.fit(cars_train)

테스트 데이터

cars_test  = indexer.transform(cars_test)
cars_test  = onehot.transform(cars_test)
cars_test  = assemble.transform(cars_test)
# 테스트 데이터로 예측 생성
predictions = regression.transform(cars_test)
PySpark로 하는 Machine Learning

자동차 모델: 파이프라인

단계를 파이프라인으로 결합합니다.

from pyspark.ml import Pipeline

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

학습 데이터

pipeline = pipeline.fit(cars_train)

테스트 데이터

predictions = pipeline.transform(cars_test)
PySpark로 하는 Machine Learning

자동차 모델: 단계

.stages 속성으로 각 단계를 확인합니다.

# LinearRegression 객체(네 번째 단계 -> 인덱스 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])
PySpark로 하는 Machine Learning

파이프라인으로 워크플로우를 간소화!

PySpark로 하는 Machine Learning

Preparing Video For Download...