PySpark로 하는 Machine Learning
Andrew Collier
Data Scientist, Fathom Data
학습 데이터에만 사용.
학습/테스트 데이터에 사용.


파이프라인은 일련의 작업으로 구성됩니다.
각 작업을 개별로 적용할 수도 있고, 파이프라인 하나로 적용할 수도 있습니다!
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')
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)
단계를 파이프라인으로 결합합니다.
from pyspark.ml import Pipeline
pipeline = Pipeline(stages=[indexer, onehot, assemble, regression])
학습 데이터
pipeline = pipeline.fit(cars_train)
테스트 데이터
predictions = pipeline.transform(cars_test)
.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