<jupyter_start><jupyter_text>Read user data<jupyter_code>all_user_games = pd.read_csv('../data/processed/all_user_games.csv').dropna()
all_user_games = all_user_games[['steamid', 'appid']]
all_user_games.head()
all_user_games.head()
print(len(all_user_games.index))
user_data = pd.read_csv('../data/processed/user_info.csv')

#user_data.head()<jupyter_output><empty_output><jupyter_text>Read game data<jupyter_code>game_data = pd.read_csv('../data/processed/games_info_v3.csv')
#game_data[game_data.appid == 570].head()<jupyter_output><empty_output><jupyter_text>Read game genres as text<jupyter_code>game_genres_text = pd.read_csv('../data/processed/genres_text.csv')
#game_genres_text.head()<jupyter_output><empty_output><jupyter_text>Get OHE game genres<jupyter_code>genre_cols = [col for col in game_data.columns if 'Genre_' in col]
game_genres = game_data[['appid', 'Release_Date'] + genre_cols]
#game_genres.head()<jupyter_output><empty_output><jupyter_text>Get the most played game per user over a 2-week period as a starting pointThis game's genres are used as the starting point for the recommender system<jupyter_code>users_data_top_game = user_data.sort_values('playtime_2weeks', ascending=False).drop_duplicates('steamid')
#users_data_top_game.head()<jupyter_output><empty_output><jupyter_text>Create dataframes to use with similarity functionUsers time with their most played game and the game's genres in one dataframeGames time and their genres in one dataframe  User dataframe for similarity calc<jupyter_code>user_genres_sim = users_data_top_game.merge(game_genres, on='appid', how='inner', suffixes=('', '_y'))
user_genres_sim.drop(columns=['playtime_2weeks', 'playtime_forever'], inplace=True)
user_genres_sim.rename(columns = {'user_2weeks_playtime': 'time'}, inplace=True)
user_genres_sim = user_genres_sim.astype('int64')
#user_genres_sim.head()<jupyter_output><empty_output><jupyter_text>Games dataframe for similarity calc<jupyter_code>game_genres_sim = game_data[['average_forever', 'Release_Date'] + genre_cols].copy()
game_genres_sim.rename(columns = {'average_forever': 'time'}, inplace=True)
#game_genres_sim.head()<jupyter_output><empty_output><jupyter_text>Similarity values here!<jupyter_code># Gather user data
rand_val = random.randint(0, len(user_genres_sim.index)-1)
x_data = user_genres_sim.iloc[rand_val].copy()
x_user = x_data[['steamid', 'appid']]
x_data.drop(index=['steamid', 'appid'], axis=1, inplace=True)

# Gather game data
y_data = game_genres_sim

# Generate weights. The common genres are more heavily weighted.
weights = [1.5 if x_data[i] and 'Genre_' in i else 0.5 if 'Release_Date' in i else 1.0 for i in y_data.columns]
print(weights)

# Create cosine similarity using weights
sims = []
for index, row in y_data.iterrows():
    sims.append(1 - cosine(x_data, row, weights))
sims = pd.DataFrame(sims)

# Create dataframe and assign appid to the respective cosine value
cosine_values = pd.concat([sims, game_genres['appid']], axis=1)
cosine_values.rename(columns={0: 'similarity'}, inplace=True)
cosine_values.sort_values(by='similarity', ascending=False, inplace=True)
#display(cosine_values.head())<jupyter_output>[1.0, 0.5, 1.0, 1.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.5, 1.0, 1.0, 1.0]<jupyter_text>Merge top 10 similarity values with their respective titles and info<jupyter_code>cols = ['appid', 'Title', 'Release_Date', 'negative', 'positive', 'developer', 'publisher', 'Rating']
top_ten = cosine_values[:10]
rec_genres = top_ten.merge(game_data[['appid'] + genre_cols], on='appid', how='inner', suffixes=('', '_y'))
recommendations = top_ten.merge(game_data[cols], on='appid', how='inner', suffixes=('', '_y'))
recommendations = recommendations.merge(game_genres_text, on='appid', how='inner', suffixes=('', '_y'))
recommendations = recommendations.sort_values(by='similarity', ascending=False).reset_index(drop=True)
#recommendations.head(10)<jupyter_output><empty_output><jupyter_text>Info about the user's game<jupyter_code>user_info = users_data_top_game[users_data_top_game.steamid == x_user.steamid]
user_game_info = game_data[game_data['appid'] == int(user_info.appid)].copy()
user_game_info.drop(columns=genre_cols, axis=1, inplace=True)
user_game_info = user_game_info[cols].merge(game_genres_text, on='appid', how='inner')
#user_game_info.head()<jupyter_output><empty_output><jupyter_text>Print user game and recommendations<jupyter_code>print('Based on a playtime of %i minutes by user: %i and their recently most played game:' % (user_info.user_2weeks_playtime, x_user.steamid))
display(user_game_info)

print('\n')

print("Recommended games with similar playtime and genre, sorted by recommended:")
display(recommendations)


# Score based on the number of genres that are the same
x_genres = pd.DataFrame(x_data[genre_cols]).T
y_genres = rec_genres[genre_cols]

matches = 0.0
other_count = 0
for index, row in y_genres.iterrows():
    matches += (x_genres & row).sum(axis=1)
    other_count += row.sum(axis=0)
genre_score = matches/other_count
    
# Score is how many times any of the user's genres appear in any of the recommended games' genres
print("%i out of %i genres match" % (matches, other_count))<jupyter_output>Based on a playtime of 3813 minutes by user: 76561198028684278 and their recently most played game:
{'fraction_non_alphanumeric': 0.1359296011982775, 'fraction_numerical': 0.019472008987081072, 'mean_word_length': 7.815181518151816, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '365345', 'license': 'MIT', 'n_tokens_mistral': 2049, 'n_tokens_neox': 1932, 'n_words': 407, 'path': 'notebooks/recsys.ipynb', 'repo_name': 'nickeelmang/steam_recsys', 'size': '5341'}