inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

(UPDATED) Python을 이용한 개인화 추천시스템 | 추천알고리즘 | 추천인공지능

사용자 집단별 추천 결과에서

해결된 질문

170

Code_Slave

작성한 질문수 10

1

거친코딩님의 rating_matrix에선 943X1633이 나오는데 

제 코드 결과값에선 943X1643이 나옵니다 ㅠㅠ

#데이터 읽어오기(user, item, data)
base_src = 'drive/MyDrive/RecoSys/Data'
u_user_src = os.path.join(base_src,'u.user')
u_cols = ['user_id','age','sex','occupation','zip_code']
users = pd.read_csv(u_user_src,
                    sep='|',
                    names=u_cols,
                    encoding='latin-1')
u_item_src = os.path.join(base_src,'u.item')
i_cols = ['movie_id','title','release date','video release date',
          'IMDB URL','unknown','Action','Adventure','Animation',
          'Children\'s','Comedy','Crime','Documentary,','Drama','Fantasy',
          'FilmNoir','Horror','Musical','Mystery','Romance', 'Sci-Fi','Thriller','War','Western']
movies = pd.read_csv(u_item_src,
                     sep='|',
                     names=i_cols,
                     encoding='latin-1')
u_data_src = os.path.join(base_src,'u.data')
r_cols = ['user_id', 'movie_id','rating','timestamp']
ratings = pd.read_csv(u_data_src,
                      sep = '\t',
                      names = r_cols,
                      encoding='latin-1')
#ratings DataFrame에서 timestamp 제거
ratings = ratings.drop('timestamp',axis=1)
movies = movies[['movie_id','title']]

#데이터 train, test set 분리
from sklearn.model_selection import train_test_split
x = ratings.copy()
y = ratings['user_id']

x_train, x_test, y_train, y_test = train_test_split(x,y,
                                                    test_size = 0.25,
                                                    stratify=y) #stratify : 계층화추출(골고루 뽑히도록)
#정확도(RMSE)를 계산하는 함수
def RMSE(y_true, y_pred):
  return np.sqrt(np.mean((np.array(y_true) - np.array(y_pred))**2))
#모델별 RMSE를 계산하는 함수
def score(model):
  id_pairs = zip(x_test['user_id'],x_test['movie_id'])
  y_pred = np.array([model(user,movie) for (user,movie) in id_pairs])
  y_true = np.array(x_test['rating'])
  return RMSE(y_true,y_pred)

# best_seller 함수를 이용한 정확도 계산
train_mean = x_train.groupby(['movie_id'])['rating'].mean()
def best_seller(user_id,movie_id):
  try :
    rating = train_mean[movie_id]
  except : #해당 영화 데이터가 train data set에 없을때
    rating = 3.0
  return rating
score(best_seller)

#성별에 따른 예측값 계산
merged_ratings = pd.merge(x_train,users)
users = users.set_index('user_id')
g_mean = merged_ratings[['movie_id','sex','rating']].groupby(['movie_id','sex'])['rating'].mean()
#g_mean
rating_matrix = x_train.pivot(index='user_id',
                             columns='movie_id',
                             values='rating')
rating_matrix

딥러닝 추천-시스템

답변 1

1

거친코딩

안녕하세요.

질문주신 것에 답변드립니다.

rating_matrix의 경우 x_train의 pivot_table 형태를 띄고 있습니다.

그렇다면 pivot_table의 shape[1] 는 x_train의 movie_id에 영향을 받을 것입니다.

그런데 x_train의 movie_id는 랜덤 샘플링으로 75:25로 분리 될 것인데,

이 와중에 겹치는 것도 있고 아닌 것도 있습니다.

그래서 매번 movie_id가 나눠지는 형태가 달라질 것이라서

학습자님의 코드를 다시 돌려보셔도 다른 shape를 가진 pivot_table이 나올 것입니다.

그래서 저의 결과(pivot_table의 shape)와 학습자님의 결과가 충분히 다를 수 있습니다.

답변이 되셨기를 바랍니다.

감사합니다.

-거친코딩 드림-

model.predict 의 결과 값 index가 왜 movie id ?

0

416

3

model.predict에 특정 user id로 상위 movie 5개 출력?

0

394

3

새로운 회원이 선택한 영화를 모델로 넘기는 방법 질문

0

311

1

올려주신 파일과 sparse matrix

0

346

1

Unable to allocate 55.9 GiB for an array with shape (25000, 100000, 3) and data type int64

0

1007

3

사용자 집단별 추천 코드

0

255

1

무비렌즈 최신 데이터를 이용하고싶은데요~

1

332

2

추천시스템의 이진데이터 적용

1

345

1

평가 관련

1

273

1

딥러닝 추천시스템 변수추가 부분 코드 관련 질문입니다.

1

3541

1

딥러닝을 위한 추천시스템 원핫 인코딩 질문

0

291

1

CF_knn_bias 실습에서 not in index error 발생합니다.

0

718

3

MergeError가 납니다

0

569

2

sparse matrix를 추천 알고리즘에 적용하기에서

0

289

1

코드 오류 질문입니다!

1

611

3

코드를 똑같이 따라햇을때

1

378

1

강의자료는 어디서 다운로드 받을 수 있나요?

0

324

1

train,test 분리 MF알고리즘에서 결과가 출력이 안되요 ㅠㅠ

1

398

1

알고리즘을 프로젝트에서 써보고 싶은데요

1

205

1

ML 방식 모델 활용방안에 대해서

1

252

1

그 외의 CF 정확도 개선방법에서

1

185

1

화면이 안나오는데 정상인가요?

1

215

1

아이템 기반 CF 코드 질문

1

311

3

실시간 처리 질문

1

209

1