ALS 모델 구축하기
첫 번째 ALS 모델을 지정해 보겠습니다. 아래 코드를 완성해 첫 번째 ALS 모델을 만들어 보세요.
ratings 데이터 프레임에서 사용자, 영화, 평점이 들어 있는 열 이름을 확인하려면 .columns 메서드를 사용할 수 있다는 점을 기억하세요. Spark가 ALS를 정확히 수행하려면 이 열 이름을 알아야 합니다.
이 연습은 강의의 일부입니다
PySpark로 추천 엔진 만들기
연습 안내
- ALS 모델을 만들기 전에 데이터를 학습용 데이터와 테스트용 데이터로 분할해야 합니다.
randomSplit()메서드를 사용해ratings데이터프레임을 각각 0.8/0.2 비율로training_data와test_data로 나누고, 난수 생성기의seed는42로 설정하세요. - 어떤 열이
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()