|
85 | 85 | "source": [ |
86 | 86 | "from implicit.nearest_neighbours import bm25_weight\n", |
87 | 87 | "\n", |
88 | | - "# weight the matrix, both to reduce impact of users that have played the same artist thousands of times\n", |
89 | | - "# and to reduce the weight given to popular items\n", |
| 88 | + "# weight the matrix, both to reduce impact of users that have played the same artist thousands\n", |
| 89 | + "# of times and to reduce the weight given to popular items\n", |
90 | 90 | "artist_user_plays = bm25_weight(artist_user_plays, K1=100, B=0.8)\n", |
91 | 91 | "\n", |
92 | | - "# get the transpose since the most of the functions in implicit expect (user, item) sparse matrices instead of (item, user)\n", |
| 92 | + "# get the transpose since the most of the functions in implicit expect (user, item) sparse\n", |
| 93 | + "# matrices instead of (item, user)\n", |
93 | 94 | "user_plays = artist_user_plays.T.tocsr()" |
94 | 95 | ] |
95 | 96 | }, |
|
293 | 294 | "# Use pandas to display the output in a table, pandas isn't a dependency of implicit otherwise\n", |
294 | 295 | "import numpy as np\n", |
295 | 296 | "import pandas as pd\n", |
296 | | - "pd.DataFrame({\"artist\": artists[ids], \"score\": scores, \"already_liked\": np.isin(ids, user_plays[userid].indices)})" |
| 297 | + "\n", |
| 298 | + "pd.DataFrame(\n", |
| 299 | + " {\n", |
| 300 | + " \"artist\": artists[ids],\n", |
| 301 | + " \"score\": scores,\n", |
| 302 | + " \"already_liked\": np.isin(ids, user_plays[userid].indices),\n", |
| 303 | + " }\n", |
| 304 | + ")" |
297 | 305 | ] |
298 | 306 | }, |
299 | 307 | { |
|
423 | 431 | ], |
424 | 432 | "source": [ |
425 | 433 | "# get related items for the beatles (itemid = 25512)\n", |
426 | | - "ids, scores= model.similar_items(252512)\n", |
| 434 | + "ids, scores = model.similar_items(252512)\n", |
427 | 435 | "\n", |
428 | 436 | "# display the results using pandas for nicer formatting\n", |
429 | 437 | "pd.DataFrame({\"artist\": artists[ids], \"score\": scores})" |
|
0 commit comments