시작하기무료로 시작하기

형식 정리와 고유 사용자

R 데이터프레임을 살펴보세요. 각 열에 다른 영화가 있는 일반적인 "wide" 형식입니다. 또한 User와 영화 이름이 정수 형식이 아니라는 점도 확인하세요. 아래 단계에 따라 이 데이터를 ALS에 맞게 올바로 준비해 보세요.

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

PySpark로 추천 엔진 만들기

강의 보기

연습 안내

  • pyspark.sql.functions에서 monotonically_increasing_id 패키지를 임포트하고 .show() 메서드로 R 데이터프레임을 확인하세요.
  • to_long() 함수를 사용해 R 데이터프레임을 "long" 형식으로 변환하세요. 새 데이터프레임 이름은 ratings로 하세요.
  • 데이터프레임에서 .distinct() 사용자 전체를 담은 users라는 데이터프레임을 만들고, .coalesce(1) 메서드를 사용해 파티션을 하나로 통합하세요.
  • withColumn() 안에서 monotonically_increasing_id() 메서드를 사용해 각 사용자마다 고유한 정수가 들어 있는 새 열을 만드세요. 열 이름은 userId로 하세요. 마지막 데이터프레임에는 .persist() 메서드를 호출해 새 정수 ID가 유지되도록 하세요.

실습형 인터랙티브 연습

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

# Import monotonically_increasing_id and show R
from pyspark.sql.functions import ____
R.show()

# Use the to_long() function to convert the dataframe to the "long" format.
ratings = to_long(____)
ratings.show()

# Get unique users and repartition to 1 partition
users = ratings.select("____").____().____()

# Create a new column of unique integers called "userId" in the users dataframe.
users = users.withColumn("____", monotonically_increasing_id()).____()
users.show()
코드 편집 및 실행