1

Memory based
get user-item matrix and calculate cosine similarity between $u_k, u_a$
$$sim^{cos}(u_k,u_a)=\frac{u_k\cdot u_a}{||u_k||\ ||u_a||}$$
calculate in python, each row of train_data_matrix represent a user

from sklearn.metrics.pairwise import pairwise_distances
user_similarity = pairwise_distances(train_data_matrix, metric='cosine')
item_similarity = pairwise_distances(train_data_matrix.T, metric='cosine')

to predict user $k$ rating item $m$
$$\hat{x}_{k,m}=\bar{x}_k+\frac{\sum_{i} sim^{cos}(u_k,u_i)(x_{i,m}-\bar{x}_i)}{\sum_{i} |sim^{cos}(u_k,u_i)|}$$

def predict(ratings, similarity, type='user'):
    if type == 'user':
        mean_user_rating = ratings.mean(axis=1)
        # axis=1 calculate the mean of every row
        ratings_diff = (ratings - mean_user_rating[:, np.newaxis])
        # np.newaxis create a new axis, changing to 2-d array
        pred = mean_user_rating[:, np.newaxis] + similarity.dot(ratings_diff) 
        / np.array([np.abs(similarity).sum(axis=1)]).T   
    elif type == 'item':
        pred = ratings.dot(similarity) / np.array([np.abs(similarity).sum(axis=1)])
    return pred


Reference
https://cambridgespark.com/co...


Lycheeee
0 声望1 粉丝

下一篇 »
Git