From 94f678534a5c8a3fee286614114dcd7094a17921 Mon Sep 17 00:00:00 2001 From: Chris Hodapp Date: Sat, 1 Feb 2020 19:50:12 -0500 Subject: [PATCH] Try to fix code outputs from old post? --- .../2018-04-08-recommender-systems-1/index.md | 161 +++++++++--------- hugo_blag/static/css/custom.css | 8 + 2 files changed, 87 insertions(+), 82 deletions(-) create mode 100644 hugo_blag/static/css/custom.css diff --git a/hugo_blag/content/posts/2018-04-08-recommender-systems-1/index.md b/hugo_blag/content/posts/2018-04-08-recommender-systems-1/index.md index 7537ff1..e730188 100644 --- a/hugo_blag/content/posts/2018-04-08-recommender-systems-1/index.md +++ b/hugo_blag/content/posts/2018-04-08-recommender-systems-1/index.md @@ -110,8 +110,7 @@ Below is just to inspect that data appears to be okay: ml.info() {{< / highlight >}} -
- +
     
     RangeIndex: 20000263 entries, 0 to 20000262
     Data columns (total 4 columns):
@@ -121,9 +120,7 @@ ml.info()
     time        datetime64[ns]
     dtypes: datetime64[ns](1), float32(1), int32(2)
     memory usage: 381.5 MB
-    
-
- + {{}} @@ -134,9 +131,9 @@ ml.describe() -
- | user_id | movie_id | rating -|--------|---------|------- +
+| user_id | movie_id | rating
+|---------|----------|-------
 count|2.000026e+07|2.000026e+07|2.000026e+07
 mean|6.904587e+04|9.041567e+03|3.494030e+00
 std|4.003863e+04|1.978948e+04|9.998490e-01
@@ -145,7 +142,7 @@ min|1.000000e+00|1.000000e+00|5.000000e-01
 50%|6.914100e+04|2.167000e+03|3.500000e+00
 75%|1.036370e+05|4.770000e+03|4.000000e+00
 max|1.384930e+05|1.312620e+05|5.000000e+00
-
+ @@ -159,7 +156,7 @@ ml[:10] -
+
  | user_id | movie_id | rating | time
 |--------|---------|-------|-----
 0|1|2|3.5|2005-04-02 23:53:47
@@ -172,7 +169,7 @@ ml[:10]
 7|1|223|4.0|2005-04-02 23:46:13
 8|1|253|4.0|2005-04-02 23:35:40
 9|1|260|4.0|2005-04-02 23:33:46
-
+ @@ -187,11 +184,11 @@ max_user, max_movie, max_user * max_movie -
+
     
     (138494, 131263, 18179137922)
     
-
+ @@ -202,11 +199,11 @@ Computing what percent we have of all 'possible' ratings (i.e. every single movi print("%.2f%%" % (100 * ml.shape[0] / (max_user * max_movie))) {{< / highlight >}} -
+
     
     0.11%
     
-
+ ## 3.1. Aggregation @@ -242,7 +239,7 @@ movie_stats.sort_values("num_ratings", ascending=False)[:25] -
+
  | movie_title | num_ratings | avg_rating | movie_id |  |  | 
 |------------|------------|-----------|---------|-|-|-
 296|Pulp Fiction (1994)|67310.0|4.174231
@@ -270,7 +267,7 @@ movie_stats.sort_values("num_ratings", ascending=False)[:25]
 608|Fargo (1996)|43272.0|4.112359
 47|Seven (a.k.a. Se7en) (1995)|43249.0|4.053493
 380|True Lies (1994)|43159.0|3.491149
-
+ @@ -343,12 +340,12 @@ ml_mat_train -
+
     
     <138494x131263 sparse matrix of type ''
     	with 15000197 stored elements in Compressed Sparse Column format>
     
-
+ @@ -363,7 +360,7 @@ ml_train[:10] -
+
  | user_id | movie_id | rating | time
 |--------|---------|-------|-----
 13746918|94976|7371|4.5|2009-11-04 05:51:26
@@ -376,7 +373,7 @@ ml_train[:10]
 15311014|105846|4226|4.5|2004-07-30 18:12:26
 8514776|58812|1285|4.0|2000-04-24 20:39:46
 3802643|25919|3275|2.5|2010-06-18 00:48:40
-
+ @@ -389,11 +386,11 @@ list(ml_train.iloc[:10].rating) -
+
     
     [4.5, 3.0, 3.0, 4.5, 4.0, 2.5, 5.0, 4.5, 4.0, 2.5]
     
-
+ @@ -407,11 +404,11 @@ movie_ids = list(ml_train.iloc[:10].movie_id) -
+
     
     [4.5, 3.0, 3.0, 4.5, 4.0, 2.5, 5.0, 4.5, 4.0, 2.5]
     
-
+ @@ -485,7 +482,7 @@ names.merge(ml_train[ml_train.user_id == target_user], right_on="movie_id", left -
+
  | movie_title | user_id | movie_id | rating | time
 |------------|--------|---------|-------|-----
 4229884|Jumanji (1995)|28812|2|5.0|1996-09-23 02:08:39
@@ -499,7 +496,7 @@ names.merge(ml_train[ml_train.user_id == target_user], right_on="movie_id", left
 4229957|Independence Day (a.k.a. ID4) (1996)|28812|780|5.0|1996-09-23 02:09:02
 4229959|Phenomenon (1996)|28812|802|5.0|1996-09-23 02:09:02
 4229960|Die Hard (1988)|28812|1036|5.0|1996-09-23 02:09:02
-
+ @@ -516,11 +513,11 @@ names[names.index == target_movie] -
+
  | movie_title | movie_id | 
 |------------|---------|-
 586|Home Alone (1990)
-
+ @@ -541,7 +538,7 @@ users_df -
+
  | movie_id_x | user_id | rating_x | rating_y
 |-----------|--------|---------|---------
 0|329|17593|3.0|4.0
@@ -555,7 +552,7 @@ users_df
 522688|2|126271|3.0|4.0
 522689|595|82760|2.0|4.0
 522690|595|18306|4.5|5.0
-
+ @@ -572,7 +569,7 @@ users_df -
+
  | movie_id_x | user_id | rating_x | rating_y | rating_dev
 |-----------|--------|---------|---------|-----------
 0|329|17593|3.0|4.0|1.0
@@ -586,7 +583,7 @@ users_df
 522688|2|126271|3.0|4.0|1.0
 522689|595|82760|2.0|4.0|2.0
 522690|595|18306|4.5|5.0|0.5
-
+ @@ -604,7 +601,7 @@ names.join(rating_dev, how="inner").sort_values("rating_dev") -
+
  | movie_title | rating_dev
 |------------|-----------
 318|Shawshank Redemption, The (1994)|-1.391784
@@ -628,7 +625,7 @@ names.join(rating_dev, how="inner").sort_values("rating_dev")
 173|Judge Dredd (1995)|0.518570
 19|Ace Ventura: When Nature Calls (1995)|0.530155
 160|Congo (1995)|0.559034
-
+ @@ -649,7 +646,7 @@ df.join(names, on="movie_id").sort_values("movie_title") -
+
  | user_id | movie_id | rating | rating_adj | movie_title
 |--------|---------|-------|-----------|------------
 4229920|28812|344|3.0|3.141987|Ace Ventura: Pet Detective (1994)
@@ -673,7 +670,7 @@ df.join(names, on="movie_id").sort_values("movie_title")
 4229892|28812|50|3.0|1.683520|Usual Suspects, The (1995)
 4229903|28812|208|3.0|3.250881|Waterworld (1995)
 4229919|28812|339|4.0|3.727966|While You Were Sleeping (1995)
-
+ @@ -688,11 +685,11 @@ df["rating_adj"].mean() -
+
     
     4.087520122528076
     
-
+ @@ -706,11 +703,11 @@ ml_test[(ml_test.user_id == target_user) & (ml_test.movie_id == target_movie)][" -
+
     
     4.0
     
-
+ @@ -730,7 +727,7 @@ names.join(num_ratings, how="inner").sort_values("num_ratings") -
+
  | movie_title | num_ratings
 |------------|------------
 802|Phenomenon (1996)|3147
@@ -754,7 +751,7 @@ names.join(num_ratings, how="inner").sort_values("num_ratings")
 593|Silence of the Lambs, The (1991)|12120
 480|Jurassic Park (1993)|13546
 356|Forrest Gump (1994)|13847
-
+ @@ -785,7 +782,7 @@ df -
+
  | user_id | movie_id | rating | rating_adj | num_ratings | rating_weighted
 |--------|---------|-------|-----------|------------|----------------
 4229918|28812|329|4.0|3.767164|6365|23978.000326
@@ -809,7 +806,7 @@ df
 4229912|28812|296|4.0|2.883755|11893|34296.500678
 4229884|28812|2|5.0|4.954595|7422|36773.001211
 4229953|28812|595|4.0|3.515051|9036|31761.999825
-
+ @@ -822,11 +819,11 @@ df["rating_weighted"].sum() / df["num_ratings"].sum() -
+
     
     4.02968199025023
     
-
+ @@ -855,7 +852,7 @@ C & =M^\top M \\ D &= \left(M^\top U - (M^\top U)^\top\right) /\ \textrm{max}(1, M^\top M) \end{align} $$ - + where $/$ is Hadamard (i.e. elementwise) division, and $\textrm{max}$ is elementwise maximum with 1. Then, the below gives the prediction for how user $u$ will rate movie $j$: @@ -863,7 +860,7 @@ where $/$ is Hadamard (i.e. elementwise) division, and $\textrm{max}$ is element $$ P(u)_j = \frac{[M_u \odot (C_j > 0)] \cdot (D_j + U_u) - U_{u,j}}{M_u \cdot (C_j > 0)} $$ - + $D_j$ and $C_j$ are row $j$ of $D$ and $C$, respectively. $M_u$ and $U_u$ are column $u$ of $M$ and $U$, respectively. $\odot$ is elementwise multiplication. @@ -894,7 +891,7 @@ S_{j,i}(\chi)} u_j - u_i = \frac{1}{card(S_{j,i}(\chi))}\left(\sum_{u \in S_{j,i}(\chi)} u_j - \sum_{u \in S_{j,i}(\chi)} u_i\right) \end{split} $$ - + where: @@ -933,7 +930,7 @@ matrix multiplication:
$$C=M^\top M$$ -
+ since $C\_{i,j}=card(S\_{j,i}(\chi))$ is the dot product of row $i$ of $M^T$ - which is column $i$ of $M$ - and column $j$ of $M$. @@ -943,7 +940,7 @@ We still need the other half:
$$\sum_{u \in S_{j,i}(\chi)} u_j - \sum_{u \in S_{j,i}(\chi)} u_i$$ -
+ We can apply a similar trick here. Consider first what $\sum\_{u \in S\_{j,i}(\chi)} u\_j$ means: It is the sum of only those ratings of @@ -961,7 +958,7 @@ $M\_j$ (consider the definition of $M\_j$) computes this, and so:
$$\sum_{u \in S_{j,i}(\chi)} u_j = M_i \cdot U_j$$ -
+ and as with $C$, since we want every pairwise dot product, this summation just equals element $(i,j)$ of $M^\top U$. The other half of the summation, @@ -970,13 +967,13 @@ the transpose of this matrix:
$$\sum_{u \in S_{j,i}(\chi)} u_j - \sum_{u \in S_{j,i}(\chi)} u_i = M^\top U - (M^\top U)^\top = M^\top U - U^\top M$$ -
+ So, finally, we can compute an entire deviation matrix at once like:
$$D = \left(M^\top U - (M^\top U)^\top\right) /\ M^\top M$$ -
+ where $/$ is Hadamard (i.e. elementwise) division, and $D\_{j,i} = \textrm{dev}\_{j,i}$. @@ -990,7 +987,7 @@ Finally, the paper gives the formula to predict how user $u$ will rate movie $j$ $$ P(u)_j = \frac{1}{card(R_j)}\sum_{i\in R_j} \left(\textrm{dev}_{j,i}+u_i\right) = \frac{1}{card(R_j)}\sum_{i\in R_j} \left(D_{j,i} + U_{u,j} \right) $$ - + where $R\_j = \{i | i \in S(u), i \ne j, card(S\_{j,i}(\chi)) > 0\}$, and $S(u)$ is the set of movies that user $u$ has rated. To unpack the paper's somewhat dense notation, the summation is over every movie $i$ that user $u$ rated and that at least one other user rated, except movie $j$. @@ -998,7 +995,7 @@ We can apply the usual trick yet one more time with a little effort. The summati
$$P(u)_j = \frac{[M_u \odot (C_j > 0)] \cdot (D_j + U_u) - U_{u,j}}{M_u \cdot (C_j > 0)}$$ -
+ #### 5.2.2.4. Approximation @@ -1006,7 +1003,7 @@ The paper also gives a formula that is a suitable approximation for larger data
$$p^{S1}(u)_j = \bar{u} + \frac{1}{card(R_j)}\sum_{i\in R_j} \textrm{dev}_{j,i}$$ -
+ where $\bar{u}$ is user $u$'s average rating. This doesn't change the formula much; we can compute $\bar{u}$ simply as column means of $U$. @@ -1048,11 +1045,11 @@ To show that it actually gives the same result as above, and that the approximat -
+
     
     (4.0875210502743862, 4.0875210502743862)
     
-
+ @@ -1130,12 +1127,12 @@ print("Training error: MAE={:.3f}, RMSE={:.3f}".format(err_mae_train, err_rms_t print("Testing error: MAE={:.3f}, RMSE={:.3f}".format(err_mae_test, err_rms_test)) {{< / highlight >}} -
+
     
     Training error: MAE=0.640,  RMSE=0.834
     Testing error:  MAE=0.657,  RMSE=0.856
     
-
+ # 6. "SVD" algorithm @@ -1172,7 +1169,7 @@ In that sense, $P$ and $Q$ give us a model in which ratings are an interaction b
$$\hat{r}_{ui}=q_i^\top p_u$$ -
+ However, some things aren't really interactions. Some movies are just (per the ratings) overall better or worse. Some users just tend to rate everything higher or lower. We need some sort of bias built into the model to comprehend this. @@ -1180,7 +1177,7 @@ Let's call $b_i$ the bias for movie $i$, $b_u$ the bias for user $u$, and $\mu$
$$\hat{r}_{ui}=\mu + b_i + b_u + q_i^\top p_u$$ -
+ This is the basic model we'll implement, and the same one described in the references at the top. @@ -1190,7 +1187,7 @@ More formally, the prediction model is:
$$\hat{r}_{ui}=\mu + b_i + b_u + q_i^\top p_u$$ -
+ where: @@ -1218,7 +1215,7 @@ $$ \frac{\partial E}{\partial b_i} &= 2 \sum_{r_{ui}} \left(\lambda b_i + r_{ui} - \hat{r}_{ui}\right) \end{split} $$ - + Gradient with respect to $p_u$ proceeds similarly: @@ -1232,7 +1229,7 @@ p_u}q_i^\top p_u \right) + 2 \lambda p_u \\ \frac{\partial E}{\partial p_u} &= 2 \sum_{r_{ui}} \lambda p_u - \left(r_{ui} - \hat{r}_{ui}\right)q_i^\top \end{split} $$ - + Gradient with respect to $b\_u$ is identical form to $b\_i$, and gradient with respect to $q\_i$ is identical form to $p\_u$, except that the variables switch places. The full gradients then have the standard form for gradient descent, i.e. a summation of a gradient term for each individual data point, so they turn easily into update rules for each parameter (which match the ones in the Surprise link) after absorbing the leading 2 into learning rate $\gamma$ and separating out the summation over each data point. That's given below, with $e\_{ui}=r\_{ui} - \hat{r}\_{ui}$: @@ -1245,7 +1242,7 @@ $$ \frac{\partial E}{\partial q_i} &= 2 \sum_{r_{ui}} \lambda q_i - e_{ui}p_u^\top\ \ \ &\longrightarrow q_i' &= q_i - \gamma\frac{\partial E}{\partial q_i} &= q_i + \gamma\left(e_{ui}p_u - \lambda q_i \right) \\ \end{split} $$ - + The code below is a direct implementation of this by simply iteratively applying the above equations for each data point - in other words, stochastic gradient descent. @@ -1380,7 +1377,7 @@ svd40 = SVDModel(max_movie, max_user, ml["rating"].mean(), num_factors=num_facto svd40.train(movies_train, users_train, ratings_train, epoch_callback=at_epoch) {{< / highlight >}} -
+
     
     6982/s 8928/s 10378/s 12877/s 15290/s 11574/s 13230/s 
     Epoch 01/20; Training: MAE=0.674 RMSE=0.874, Testing: MAE=0.677 RMSE=0.879
@@ -1423,7 +1420,7 @@ svd40.train(movies_train, users_train, ratings_train, epoch_callback=at_epoch)
     52078/s 18671/s 9292/s 11493/s 12515/s 11760/s 13039/s 
     Epoch 20/20; Training: MAE=0.549 RMSE=0.717, Testing: MAE=0.600 RMSE=0.787
     
-
+ @@ -1444,7 +1441,7 @@ svd4 = SVDModel(max_movie, max_user, ml["rating"].mean(), 4) svd4.train(ml_train["movie_id"].values, ml_train["user_id"].values, ml_train["rating"].values, epoch_callback=at_epoch) {{< / highlight >}} -
+
     
     48199/s 33520/s 16937/s 13842/s 13607/s 15574/s 15431/s 
     Epoch 01/20; Training: MAE=0.674 RMSE=0.875, Testing: MAE=0.677 RMSE=0.878
@@ -1487,7 +1484,7 @@ svd4.train(ml_train["movie_id"].values, ml_train["user_id"].values, ml_train["ra
     6090/s 11341/s 15532/s 18298/s 17158/s 14908/s 16898/s 
     Epoch 20/20; Training: MAE=0.599 RMSE=0.783, Testing: MAE=0.618 RMSE=0.809
     
-
+ To limit the data, we can use just the top movies (by number of ratings): @@ -1595,7 +1592,7 @@ latent_factor_grid(svd4.q[:2,:]) -
+
  | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15
 |--|--|--|--|--|--|--|--|--|--|---|---|---|---|---|---
 0||||||||||||||||
@@ -1614,7 +1611,7 @@ latent_factor_grid(svd4.q[:2,:])
 13||||||||Sound of Music; Spy Kids 2: The Island of Lost...|Bring It On; Legally Blonde|Fly Away Home; Parent Trap|Sense and Sensibility; Sex and the City|||||
 14|||||||Babe; Babe: Pig in the City||||Twilight|||||
 15||||||||||||||||
-
+ @@ -1632,7 +1629,7 @@ latent_factor_grid(svd4.q[2:,:]) -
+
  | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15
 |--|--|--|--|--|--|--|--|--|--|---|---|---|---|---|---
 0||||||||||||||||
@@ -1651,7 +1648,7 @@ latent_factor_grid(svd4.q[2:,:])
 13||||||Nightmare on Elm Street 4: The Dream Master; F...|Wes Craven's New Nightmare (Nightmare on Elm S...|Friday the 13th; Exorcist III|Candyman; Texas Chainsaw Massacre 2|Mars Attacks!; Halloween|Evil Dead II (Dead by Dawn); Re-Animator|Night of the Living Dead; Dead Alive (Braindead)||Eraserhead||
 14|||||||Nightmare on Elm Street 3: Dream Warriors; Fre...|Hellbound: Hellraiser II|Nightmare on Elm Street|||||||
 15|||||||Bride of Chucky (Child's Play 4)||||Texas Chainsaw Massacre|||||
-
+ @@ -1673,7 +1670,7 @@ bias.iloc[:10] -
+
  | movie_title | num_ratings | avg_rating | bias | movie_id |  |  |  | 
 |------------|------------|-----------|-----|---------|-|-|-|-
 318|Shawshank Redemption, The (1994)|63366.0|4.446990|1.015911
@@ -1686,7 +1683,7 @@ bias.iloc[:10]
 50|Usual Suspects, The (1995)|47006.0|4.334372|0.910651
 102217|Bill Hicks: Revelations (1993)|50.0|3.990000|0.900622
 527|Schindler's List (1993)|50054.0|4.310175|0.898633
-
+ @@ -1700,7 +1697,7 @@ bias.iloc[:-10:-1] -
+
  | movie_title | num_ratings | avg_rating | bias | movie_id |  |  |  | 
 |------------|------------|-----------|-----|---------|-|-|-|-
 8859|SuperBabies: Baby Geniuses 2 (2004)|209.0|0.837321|-2.377202
@@ -1712,7 +1709,7 @@ bias.iloc[:-10:-1]
 4775|Glitter (2001)|685.0|1.124088|-2.047287
 31698|Son of the Mask (2005)|467.0|1.252677|-2.022763
 5739|Faces of Death 6 (1996)|174.0|1.261494|-2.004086
-
+ @@ -1760,7 +1757,7 @@ pd.DataFrame.from_records( -
+
  | Library | Algorithm | MAE (test) | RMSE (test)
 |--------|----------|-----------|------------
 0||Slope One|0.656514|0.856294
@@ -1768,7 +1765,7 @@ pd.DataFrame.from_records(
 2|Surprise|Random|1.144775|1.433753
 3|Surprise|Slope One|0.704730|0.923331
 4|Surprise|SVD|0.694890|0.900350
-
+ diff --git a/hugo_blag/static/css/custom.css b/hugo_blag/static/css/custom.css new file mode 100644 index 0000000..3aa991b --- /dev/null +++ b/hugo_blag/static/css/custom.css @@ -0,0 +1,8 @@ +pre.result { + background-color: #DDD; +} +/* +code { + background-color: #EEE +} +*/