Created
March 16, 2020 14:04
-
-
Save rrobby86/2b9797cbe429a9a28e28b6b4b180aec0 to your computer and use it in GitHub Desktop.
Recommendation con NumPy: soluzioni esercizi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # SOLUZIONE 1 | |
| for uid, iid in purchases_set: | |
| row = user_indices[uid] | |
| col = item_indices[iid] | |
| purchases[row, col] = 1 | |
| # SOLUZIONE 2a | |
| # per ogni coppia ID, nome | |
| for uid, name in users.items(): | |
| # ottengo l'indice del vettore | |
| i = user_indices[uid] | |
| # inserisco il nome nel vettore | |
| user_names[i] = name | |
| # SOLUZIONE 2b | |
| item_names = np.empty(n_items, dtype=object) | |
| for iid, name in items.items(): | |
| item_names[item_indices[iid]] = name | |
| # SOLUZIONE 3a | |
| user_purchases.mean() | |
| # SOLUZIONE 3b | |
| item_purchases.max() | |
| # SOLUZIONE 3c | |
| user_names[user_purchases.argmax()] | |
| # SOLUZIONE 3d | |
| (user_purchases >= 50).sum() | |
| # SOLUZIONE 3e | |
| item_names[item_purchases >= 35] | |
| # SOLUZIONE 4a | |
| common_purchases.max() | |
| # SOLUZIONE 4b | |
| user_names[common_purchases[user_indices[7661]].argmax()] | |
| # SOLUZIONE 5 | |
| interest[purchases_bool] = 0 | |
| # SOLUZIONE 6 | |
| with open("purchases-2014.csv", "r") as f: | |
| for uid, iid in csv.reader(f, delimiter=";"): | |
| purchases_updated[user_indices[int(uid)], item_indices[int(iid)]] = 1 | |
| # SOLUZIONE 7 | |
| new_purchases = purchases_updated - purchases | |
| # SOLUZIONE 8 | |
| hits = suggestions * new_purchases | |
| # SOLUZIONE 9a | |
| satisfied_users = hits.max(1) | |
| # SOLUZIONE 9b | |
| satisfied_users.sum() | |
| # SOLUZIONE 9c | |
| satisfied_users.mean() | |
| # SOLUZIONE EXTRA | |
| # a | |
| random_interest = np.random.random((n_users, n_items)) | |
| # b | |
| random_interest[purchases_bool] = 0 | |
| # c | |
| random_interest_ranking = (-random_interest).argsort(1).argsort(1) | |
| random_suggestions = (random_interest_ranking < N).astype(np.int) | |
| # d | |
| random_hits = random_suggestions * new_purchases | |
| randomly_satisfied_users = random_hits.sum(1) | |
| # e | |
| randomly_satisfied_users.mean() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment