-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.txt
69 lines (61 loc) · 3.04 KB
/
a.txt
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
I write a Python code that recommends games for a specific user.
I have a data "recdata.csv" with columns "uid" - user id, "id" - game id, "owned" - if game is owned by user.
I have another data "gamesdata.csv" where I have columns "id" - game id, "title" - game title.
Write a function that gets user id, finds his owned games in recdata.csv, turns those "id" of games into titles from gamesdata.csv and returns a list of those titles.
Then I need another function that is modified version of this:
def print_user_games_and_predictions(
user_id: int,
user_item_matrix: pd.DataFrame,
reduced_matrix: np.ndarray,
model: nn.Module,
transformer: TruncatedSVD,
scaler: MinMaxScaler,
# rec_data_file: str,
games_data_file: str,
n_recommendations: int = 5,
) -> None:
# Load the games data and create a dictionary to map game ids to titles
games_data = pd.read_csv(games_data_file)
id_to_title = games_data.set_index("id")["title"].to_dict()
# Get the index of the user_id in the matrix
user_index = user_item_matrix.index.get_loc(user_id)
user_reduced_vector = reduced_matrix[user_index]
# Get the model predictions in the reduced space
with torch.no_grad():
user_reduced_vector_tensor = torch.tensor(
user_reduced_vector, dtype=torch.float32
).unsqueeze(0)
predicted_reduced_vector_tensor = model(user_reduced_vector_tensor)
predicted_reduced_vector = predicted_reduced_vector_tensor.numpy().squeeze()
# Inverse transformations: denormalize and inverse SVD
denormalized_predicted_vector = scaler.inverse_transform([predicted_reduced_vector])
reconstructed_predicted_vector = transformer.inverse_transform(
denormalized_predicted_vector
).squeeze()
# Get the actual and predicted games for the user
user_actual_games = user_item_matrix.loc[user_id]
user_predicted_games = pd.Series(
reconstructed_predicted_vector, index=user_actual_games.index
)
actual_games_sorted = user_actual_games.sort_values(ascending=False)
predicted_games_sorted = user_predicted_games.sort_values(ascending=False)
# Save the interacted games and top 5 recommended games to a single file
with open("9-NN-recs.txt", "w") as f:
f.write(f"User id: {user_id}\n\n")
f.write("Interacted games:\n")
for game_id, owned in actual_games_sorted.iteritems():
if owned == 1.0:
if game_id in id_to_title:
f.write(f"{id_to_title[game_id]} (id: {game_id})\n")
else:
f.write(f"Game not found in 'gamesdata.csv' (id: {game_id})\n")
f.write("\nTop 5 recommended games:\n")
for game_id, score in predicted_games_sorted.head(
n_recommendations
).iteritems():
if game_id in id_to_title:
f.write(f"{id_to_title[game_id]} (id: {game_id}, score: {score})\n")
else:
f.write(
f"Game not found in 'gamesdata.csv' (id: {game_id}, score: {score})\n"
)