시작하기무료로 시작하기

ALS 모델 구축하기

첫 번째 ALS 모델을 지정해 보겠습니다. 아래 코드를 완성해 첫 번째 ALS 모델을 만들어 보세요.

ratings 데이터 프레임에서 사용자, 영화, 평점이 들어 있는 열 이름을 확인하려면 .columns 메서드를 사용할 수 있다는 점을 기억하세요. Spark가 ALS를 정확히 수행하려면 이 열 이름을 알아야 합니다.

이 연습은 강의의 일부입니다

PySpark로 추천 엔진 만들기

강의 보기

연습 안내

  • ALS 모델을 만들기 전에 데이터를 학습용 데이터와 테스트용 데이터로 분할해야 합니다. randomSplit() 메서드를 사용해 ratings 데이터프레임을 각각 0.8/0.2 비율로 training_datatest_data로 나누고, 난수 생성기의 seed42로 설정하세요.
  • 어떤 열이 userCol, itemCol, ratingCol인지 Spark에 알려주세요. 필요하다면 .columns 메서드를 사용하세요. 하이퍼파라미터를 완성합니다. rank는 10, maxIter는 15, regParam(lambda)은 .1, coldStartStrategy"drop", nonnegative 인수는 True, 데이터가 명시적 평점을 포함하므로 implicitPrefs 인수는 False로 설정하세요.
  • 이제 제공된 training_data에 대해 als.fit() 메서드를 호출해 ratings 데이터의 training_data 부분에 als 모델을 학습시키세요. 학습된 모델 이름은 model로 하세요.
  • 제공된 test_data에 대해 model.transform() 메서드를 호출해 ratings 데이터의 test_data 부분에 대한 예측을 생성하세요. 예측 결과의 이름은 test_predictions로 하세요. .show() 메서드를 호출해 test_predictions를 확인해 보셔도 됩니다.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Split the ratings dataframe into training and test data
(training_data, test_data) = ratings.____([____, ____], seed=42)

# Set the ALS hyperparameters
from pyspark.ml.recommendation import ALS
als = ALS(userCol="____", itemCol="____", ratingCol="____", rank =____, maxIter =____, regParam =____,
          coldStartStrategy="____", nonnegative =____, implicitPrefs = ____)

# Fit the mdoel to the training_data
____ = ____.fit(____)

# Generate predictions on the test_data
____ = ____.transform(____)
test_predictions.show()
코드 편집 및 실행