-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitemSimilarity.py
More file actions
28 lines (20 loc) · 1.21 KB
/
Copy pathitemSimilarity.py
File metadata and controls
28 lines (20 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
# "This dataset is a list of user behaviors, with columns: user-id, game-title, behavior-name, value.
# The behaviors included are 'purchase' and 'play'.
# The value indicates the degree to which the behavior was performed.
# In the case of 'purchase' the value is always 1, and in the case of 'play' the value represents the number of hours the user has played the game."
user_df = pd.read_csv("Datasets/user_df_with_ratings.csv")
def standardize(row):
new_row = (row - row.mean()) / (row.max() - row.min())
return new_row
user_favourites = user_df.pivot_table(index=['userId'], columns=['game'], values='rating')
user_favourites.fillna(0, inplace = True)
user_favourites = user_favourites.apply(standardize)
game_similarity = cosine_similarity(user_favourites.T)
game_similarity_df = pd.DataFrame(game_similarity, index=user_favourites.columns, columns = user_favourites.columns)
def get_similar_games(game_name, user_raiting):
similar_games = game_similarity_df[game_name] * (user_raiting - 1.0)
similar_games = similar_games.sort_values(ascending=False)
return similar_games
print(get_similar_games("The Witcher 3 Wild Hunt", 5))