본문 바로가기

프로그래머스 데브 코스/TIL

[6기] 프로그래머스 인공지능 데브코스 119일차 TIL

1228

[17주차 - Day4] Recommendation system

추천 엔진 만들기 과제

# 모델 기반 CF 추천 시스템 만들기
amazon_ratings1 = amazon_ratings.head(10000)

ratings_utility_matrix = amazon_ratings1.pivot_table(values='Rating', index='UserId', columns='ProductId', fill_value=0)
ratings_utility_matrix.head()

 

X = ratings_utility_matrix.T
X.head()

 

from sklearn.decomposition import TruncatedSVD

SVD = TruncatedSVD(n_components=10)
decomposed_matrix = SVD.fit_transform(X)
decomposed_matrix.shape

correlation_matrix = np.corrcoef(decomposed_matrix)
correlation_matrix.shape

 

 


여기서부터는 직접 구현

!pip install surprise

!wget "https://grepp-reco-test.s3.ap-northeast-2.amazonaws.com/ratings_Beauty.csv"

from surprise import Dataset
from surprise import Reader
from collections import defaultdict

reader = Reader(line_format='user item rating  timestamp', sep=',', skip_lines=1)
data = Dataset.load_from_file('ratings_Beauty.csv', reader=reader)
from surprise import SVD
from surprise import NormalPredictor
from surprise.model_selection import GridSearchCV


param_grid = {
    'n_epochs': [20, 30],
    'lr_all': [0.005, 0.01],
    'n_factors': [50, 100]
}

gs = GridSearchCV(SVD, param_grid, measures=['rmse', 'mae'], cv=3)
gs.fit(data)

print("Best RMSE score attained: ", gs.best_score['rmse'])
print("Best RMSE params: ", gs.best_params['rmse'])
print("Best MAE score attained: ", gs.best_score['mae'])
print("Best MAE params: ", gs.best_params['mae'])

- 베스트 파라미터값 기반으로 다시 한 번 더 베스트 파라미터값 가지는 학습 시작

 

from surprise import SVD
from surprise import NormalPredictor
from surprise.model_selection import GridSearchCV


param_grid = {
    'n_epochs': [50],
    'lr_all': [0.01, 0.007, 0.004],
    'n_factors': [15, 20, 25]
}

gs = GridSearchCV(SVD, param_grid, measures=['rmse', 'mae'], cv=3)
gs.fit(data)

print("Best RMSE score attained: ", gs.best_score['rmse'])
print("Best RMSE params: ", gs.best_params['rmse'])
print("Best MAE score attained: ", gs.best_score['mae'])
print("Best MAE params: ", gs.best_params['mae'])