1

我正在尝试比较一些推荐算法以用于研究目的。我将 sklearn 用于 knn,并将其用于 RBM,但我不知道如何使用RBM 为特定用户提出任何建议。我正在使用从 MovieLens 获取的数据集。

这是代码:

import pandas as pd

import numpy as np 

from scipy.sparse.linalg import svds

from sklearn.neural_network import BernoulliRBM

ratings_list = [i.strip().split("::") for i in open('C:\\Users\\admin\PycharmProjects\MasterProject\ml-1m\\ratings.dat', 'r').readlines()]

 users_list = [i.strip().split("::") for i in  open('C:\\Users\\admin\PycharmProjects\MasterProject\ml-1m\\users.dat', 'r').readlines()]


 movies_list = [i.strip().split("::") for i in  open('C:\\Users\\admin\PycharmProjects\MasterProject\ml-1m\movies.dat', 'r').readlines()]

ratings_df = pd.DataFrame(ratings_list, columns = ['UserID', 'MovieID', 'Rating', 'Timestamp'], dtype = int) 


movies_df =pd.DataFrame(movies_list, columns = ['MovieID', 'Title','Genres'])


movies_df['MovieID'] = movies_df['MovieID'].apply(pd.to_numeric)


R_df = ratings_df.pivot(index = 'UserID', columns ='MovieID', values = 'Rating').fillna(0)

R = R_df.as_matrix() 

user_ratings_mean = np.mean(R, axis = 1)

R_demeaned = R - user_ratings_mean.reshape(-1, 1)


modeli = BernoulliRBM() modeli.fit(R_demeaned) 

recoms = BernoulliRBM(batch_size=100, learning_rate=0.1, n_comp=20, n_iter=1000, random_state=None, verbose=0)

print(recoms)
4

0 回答 0