Try to fix code outputs from old post?
This commit is contained in:
parent
d8e9fcf749
commit
94f678534a
@ -110,8 +110,7 @@ Below is just to inspect that data appears to be okay:
|
|||||||
ml.info()
|
ml.info()
|
||||||
{{< / highlight >}}
|
{{< / highlight >}}
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
|
|
||||||
<class 'pandas.core.frame.DataFrame'>
|
<class 'pandas.core.frame.DataFrame'>
|
||||||
RangeIndex: 20000263 entries, 0 to 20000262
|
RangeIndex: 20000263 entries, 0 to 20000262
|
||||||
Data columns (total 4 columns):
|
Data columns (total 4 columns):
|
||||||
@ -121,9 +120,7 @@ ml.info()
|
|||||||
time datetime64[ns]
|
time datetime64[ns]
|
||||||
dtypes: datetime64[ns](1), float32(1), int32(2)
|
dtypes: datetime64[ns](1), float32(1), int32(2)
|
||||||
memory usage: 381.5 MB
|
memory usage: 381.5 MB
|
||||||
|
</pre>
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{{<highlight python>}}
|
{{<highlight python>}}
|
||||||
@ -134,9 +131,9 @@ ml.describe()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| user_id | movie_id | rating
|
| user_id | movie_id | rating
|
||||||
|--------|---------|-------
|
|---------|----------|-------
|
||||||
count|2.000026e+07|2.000026e+07|2.000026e+07
|
count|2.000026e+07|2.000026e+07|2.000026e+07
|
||||||
mean|6.904587e+04|9.041567e+03|3.494030e+00
|
mean|6.904587e+04|9.041567e+03|3.494030e+00
|
||||||
std|4.003863e+04|1.978948e+04|9.998490e-01
|
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
|
50%|6.914100e+04|2.167000e+03|3.500000e+00
|
||||||
75%|1.036370e+05|4.770000e+03|4.000000e+00
|
75%|1.036370e+05|4.770000e+03|4.000000e+00
|
||||||
max|1.384930e+05|1.312620e+05|5.000000e+00
|
max|1.384930e+05|1.312620e+05|5.000000e+00
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -159,7 +156,7 @@ ml[:10]
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| user_id | movie_id | rating | time
|
| user_id | movie_id | rating | time
|
||||||
|--------|---------|-------|-----
|
|--------|---------|-------|-----
|
||||||
0|1|2|3.5|2005-04-02 23:53:47
|
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
|
7|1|223|4.0|2005-04-02 23:46:13
|
||||||
8|1|253|4.0|2005-04-02 23:35:40
|
8|1|253|4.0|2005-04-02 23:35:40
|
||||||
9|1|260|4.0|2005-04-02 23:33:46
|
9|1|260|4.0|2005-04-02 23:33:46
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -187,11 +184,11 @@ max_user, max_movie, max_user * max_movie
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
|
|
||||||
(138494, 131263, 18179137922)
|
(138494, 131263, 18179137922)
|
||||||
|
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -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)))
|
print("%.2f%%" % (100 * ml.shape[0] / (max_user * max_movie)))
|
||||||
{{< / highlight >}}
|
{{< / highlight >}}
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
|
|
||||||
0.11%
|
0.11%
|
||||||
|
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
## 3.1. Aggregation
|
## 3.1. Aggregation
|
||||||
@ -242,7 +239,7 @@ movie_stats.sort_values("num_ratings", ascending=False)[:25]
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| movie_title | num_ratings | avg_rating | movie_id | | |
|
| movie_title | num_ratings | avg_rating | movie_id | | |
|
||||||
|------------|------------|-----------|---------|-|-|-
|
|------------|------------|-----------|---------|-|-|-
|
||||||
296|Pulp Fiction (1994)|67310.0|4.174231
|
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
|
608|Fargo (1996)|43272.0|4.112359
|
||||||
47|Seven (a.k.a. Se7en) (1995)|43249.0|4.053493
|
47|Seven (a.k.a. Se7en) (1995)|43249.0|4.053493
|
||||||
380|True Lies (1994)|43159.0|3.491149
|
380|True Lies (1994)|43159.0|3.491149
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -343,12 +340,12 @@ ml_mat_train
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
|
|
||||||
<138494x131263 sparse matrix of type '<class 'numpy.float32'>'
|
<138494x131263 sparse matrix of type '<class 'numpy.float32'>'
|
||||||
with 15000197 stored elements in Compressed Sparse Column format>
|
with 15000197 stored elements in Compressed Sparse Column format>
|
||||||
|
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -363,7 +360,7 @@ ml_train[:10]
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| user_id | movie_id | rating | time
|
| user_id | movie_id | rating | time
|
||||||
|--------|---------|-------|-----
|
|--------|---------|-------|-----
|
||||||
13746918|94976|7371|4.5|2009-11-04 05:51:26
|
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
|
15311014|105846|4226|4.5|2004-07-30 18:12:26
|
||||||
8514776|58812|1285|4.0|2000-04-24 20:39:46
|
8514776|58812|1285|4.0|2000-04-24 20:39:46
|
||||||
3802643|25919|3275|2.5|2010-06-18 00:48:40
|
3802643|25919|3275|2.5|2010-06-18 00:48:40
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -389,11 +386,11 @@ list(ml_train.iloc[:10].rating)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
|
|
||||||
[4.5, 3.0, 3.0, 4.5, 4.0, 2.5, 5.0, 4.5, 4.0, 2.5]
|
[4.5, 3.0, 3.0, 4.5, 4.0, 2.5, 5.0, 4.5, 4.0, 2.5]
|
||||||
|
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -407,11 +404,11 @@ movie_ids = list(ml_train.iloc[:10].movie_id)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
|
|
||||||
[4.5, 3.0, 3.0, 4.5, 4.0, 2.5, 5.0, 4.5, 4.0, 2.5]
|
[4.5, 3.0, 3.0, 4.5, 4.0, 2.5, 5.0, 4.5, 4.0, 2.5]
|
||||||
|
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -485,7 +482,7 @@ names.merge(ml_train[ml_train.user_id == target_user], right_on="movie_id", left
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| movie_title | user_id | movie_id | rating | time
|
| movie_title | user_id | movie_id | rating | time
|
||||||
|------------|--------|---------|-------|-----
|
|------------|--------|---------|-------|-----
|
||||||
4229884|Jumanji (1995)|28812|2|5.0|1996-09-23 02:08:39
|
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
|
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
|
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
|
4229960|Die Hard (1988)|28812|1036|5.0|1996-09-23 02:09:02
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -516,11 +513,11 @@ names[names.index == target_movie]
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| movie_title | movie_id |
|
| movie_title | movie_id |
|
||||||
|------------|---------|-
|
|------------|---------|-
|
||||||
586|Home Alone (1990)
|
586|Home Alone (1990)
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -541,7 +538,7 @@ users_df
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| movie_id_x | user_id | rating_x | rating_y
|
| movie_id_x | user_id | rating_x | rating_y
|
||||||
|-----------|--------|---------|---------
|
|-----------|--------|---------|---------
|
||||||
0|329|17593|3.0|4.0
|
0|329|17593|3.0|4.0
|
||||||
@ -555,7 +552,7 @@ users_df
|
|||||||
522688|2|126271|3.0|4.0
|
522688|2|126271|3.0|4.0
|
||||||
522689|595|82760|2.0|4.0
|
522689|595|82760|2.0|4.0
|
||||||
522690|595|18306|4.5|5.0
|
522690|595|18306|4.5|5.0
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -572,7 +569,7 @@ users_df
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| movie_id_x | user_id | rating_x | rating_y | rating_dev
|
| movie_id_x | user_id | rating_x | rating_y | rating_dev
|
||||||
|-----------|--------|---------|---------|-----------
|
|-----------|--------|---------|---------|-----------
|
||||||
0|329|17593|3.0|4.0|1.0
|
0|329|17593|3.0|4.0|1.0
|
||||||
@ -586,7 +583,7 @@ users_df
|
|||||||
522688|2|126271|3.0|4.0|1.0
|
522688|2|126271|3.0|4.0|1.0
|
||||||
522689|595|82760|2.0|4.0|2.0
|
522689|595|82760|2.0|4.0|2.0
|
||||||
522690|595|18306|4.5|5.0|0.5
|
522690|595|18306|4.5|5.0|0.5
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -604,7 +601,7 @@ names.join(rating_dev, how="inner").sort_values("rating_dev")
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| movie_title | rating_dev
|
| movie_title | rating_dev
|
||||||
|------------|-----------
|
|------------|-----------
|
||||||
318|Shawshank Redemption, The (1994)|-1.391784
|
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
|
173|Judge Dredd (1995)|0.518570
|
||||||
19|Ace Ventura: When Nature Calls (1995)|0.530155
|
19|Ace Ventura: When Nature Calls (1995)|0.530155
|
||||||
160|Congo (1995)|0.559034
|
160|Congo (1995)|0.559034
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -649,7 +646,7 @@ df.join(names, on="movie_id").sort_values("movie_title")
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| user_id | movie_id | rating | rating_adj | movie_title
|
| user_id | movie_id | rating | rating_adj | movie_title
|
||||||
|--------|---------|-------|-----------|------------
|
|--------|---------|-------|-----------|------------
|
||||||
4229920|28812|344|3.0|3.141987|Ace Ventura: Pet Detective (1994)
|
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)
|
4229892|28812|50|3.0|1.683520|Usual Suspects, The (1995)
|
||||||
4229903|28812|208|3.0|3.250881|Waterworld (1995)
|
4229903|28812|208|3.0|3.250881|Waterworld (1995)
|
||||||
4229919|28812|339|4.0|3.727966|While You Were Sleeping (1995)
|
4229919|28812|339|4.0|3.727966|While You Were Sleeping (1995)
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -688,11 +685,11 @@ df["rating_adj"].mean()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
|
|
||||||
4.087520122528076
|
4.087520122528076
|
||||||
|
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -706,11 +703,11 @@ ml_test[(ml_test.user_id == target_user) & (ml_test.movie_id == target_movie)]["
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
|
|
||||||
4.0
|
4.0
|
||||||
|
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -730,7 +727,7 @@ names.join(num_ratings, how="inner").sort_values("num_ratings")
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| movie_title | num_ratings
|
| movie_title | num_ratings
|
||||||
|------------|------------
|
|------------|------------
|
||||||
802|Phenomenon (1996)|3147
|
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
|
593|Silence of the Lambs, The (1991)|12120
|
||||||
480|Jurassic Park (1993)|13546
|
480|Jurassic Park (1993)|13546
|
||||||
356|Forrest Gump (1994)|13847
|
356|Forrest Gump (1994)|13847
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -785,7 +782,7 @@ df
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| user_id | movie_id | rating | rating_adj | num_ratings | rating_weighted
|
| user_id | movie_id | rating | rating_adj | num_ratings | rating_weighted
|
||||||
|--------|---------|-------|-----------|------------|----------------
|
|--------|---------|-------|-----------|------------|----------------
|
||||||
4229918|28812|329|4.0|3.767164|6365|23978.000326
|
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
|
4229912|28812|296|4.0|2.883755|11893|34296.500678
|
||||||
4229884|28812|2|5.0|4.954595|7422|36773.001211
|
4229884|28812|2|5.0|4.954595|7422|36773.001211
|
||||||
4229953|28812|595|4.0|3.515051|9036|31761.999825
|
4229953|28812|595|4.0|3.515051|9036|31761.999825
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -822,11 +819,11 @@ df["rating_weighted"].sum() / df["num_ratings"].sum()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
|
|
||||||
4.02968199025023
|
4.02968199025023
|
||||||
|
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -855,7 +852,7 @@ C & =M^\top M \\
|
|||||||
D &= \left(M^\top U - (M^\top U)^\top\right) /\ \textrm{max}(1, M^\top M)
|
D &= \left(M^\top U - (M^\top U)^\top\right) /\ \textrm{max}(1, M^\top M)
|
||||||
\end{align}
|
\end{align}
|
||||||
$$
|
$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
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$:
|
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)}
|
P(u)_j = \frac{[M_u \odot (C_j > 0)] \cdot (D_j + U_u) - U_{u,j}}{M_u \cdot (C_j > 0)}
|
||||||
$$
|
$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
$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.
|
$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)
|
\in S_{j,i}(\chi)} u_j - \sum_{u \in S_{j,i}(\chi)} u_i\right)
|
||||||
\end{split}
|
\end{split}
|
||||||
$$
|
$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
where:
|
where:
|
||||||
|
|
||||||
@ -933,7 +930,7 @@ matrix multiplication:
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
$$C=M^\top M$$
|
$$C=M^\top M$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
since $C\_{i,j}=card(S\_{j,i}(\chi))$ is the dot product of row $i$ of $M^T$ - which is column
|
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$.
|
$i$ of $M$ - and column $j$ of $M$.
|
||||||
@ -943,7 +940,7 @@ We still need the other half:
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
$$\sum_{u \in S_{j,i}(\chi)} u_j - \sum_{u \in S_{j,i}(\chi)} u_i$$
|
$$\sum_{u \in S_{j,i}(\chi)} u_j - \sum_{u \in S_{j,i}(\chi)} u_i$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
We can apply a similar trick here. Consider first what $\sum\_{u \in
|
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
|
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:
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
$$\sum_{u \in S_{j,i}(\chi)} u_j = M_i \cdot U_j$$
|
$$\sum_{u \in S_{j,i}(\chi)} u_j = M_i \cdot U_j$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
and as with $C$, since we want every pairwise dot product, this summation just
|
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,
|
equals element $(i,j)$ of $M^\top U$. The other half of the summation,
|
||||||
@ -970,13 +967,13 @@ the transpose of this matrix:
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
$$\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$$
|
$$\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$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
So, finally, we can compute an entire deviation matrix at once like:
|
So, finally, we can compute an entire deviation matrix at once like:
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
$$D = \left(M^\top U - (M^\top U)^\top\right) /\ M^\top M$$
|
$$D = \left(M^\top U - (M^\top U)^\top\right) /\ M^\top M$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
where $/$ is Hadamard (i.e. elementwise) division, and $D\_{j,i} = \textrm{dev}\_{j,i}$.
|
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)
|
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)
|
||||||
$$
|
$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
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$.
|
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
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
$$P(u)_j = \frac{[M_u \odot (C_j > 0)] \cdot (D_j + U_u) - U_{u,j}}{M_u \cdot (C_j > 0)}$$
|
$$P(u)_j = \frac{[M_u \odot (C_j > 0)] \cdot (D_j + U_u) - U_{u,j}}{M_u \cdot (C_j > 0)}$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
#### 5.2.2.4. Approximation
|
#### 5.2.2.4. Approximation
|
||||||
|
|
||||||
@ -1006,7 +1003,7 @@ The paper also gives a formula that is a suitable approximation for larger data
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
$$p^{S1}(u)_j = \bar{u} + \frac{1}{card(R_j)}\sum_{i\in R_j} \textrm{dev}_{j,i}$$
|
$$p^{S1}(u)_j = \bar{u} + \frac{1}{card(R_j)}\sum_{i\in R_j} \textrm{dev}_{j,i}$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
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$.
|
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
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
|
|
||||||
(4.0875210502743862, 4.0875210502743862)
|
(4.0875210502743862, 4.0875210502743862)
|
||||||
|
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -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))
|
print("Testing error: MAE={:.3f}, RMSE={:.3f}".format(err_mae_test, err_rms_test))
|
||||||
{{< / highlight >}}
|
{{< / highlight >}}
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
|
|
||||||
Training error: MAE=0.640, RMSE=0.834
|
Training error: MAE=0.640, RMSE=0.834
|
||||||
Testing error: MAE=0.657, RMSE=0.856
|
Testing error: MAE=0.657, RMSE=0.856
|
||||||
|
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
# 6. "SVD" algorithm
|
# 6. "SVD" algorithm
|
||||||
@ -1172,7 +1169,7 @@ In that sense, $P$ and $Q$ give us a model in which ratings are an interaction b
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
$$\hat{r}_{ui}=q_i^\top p_u$$
|
$$\hat{r}_{ui}=q_i^\top p_u$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
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.
|
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$
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
$$\hat{r}_{ui}=\mu + b_i + b_u + q_i^\top p_u$$
|
$$\hat{r}_{ui}=\mu + b_i + b_u + q_i^\top p_u$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
This is the basic model we'll implement, and the same one described in the references at the top.
|
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:
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
$$\hat{r}_{ui}=\mu + b_i + b_u + q_i^\top p_u$$
|
$$\hat{r}_{ui}=\mu + b_i + b_u + q_i^\top p_u$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
where:
|
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)
|
\frac{\partial E}{\partial b_i} &= 2 \sum_{r_{ui}} \left(\lambda b_i + r_{ui} - \hat{r}_{ui}\right)
|
||||||
\end{split}
|
\end{split}
|
||||||
$$
|
$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
Gradient with respect to $p_u$ proceeds similarly:
|
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
|
\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}
|
\end{split}
|
||||||
$$
|
$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
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}$:
|
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) \\
|
\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}
|
\end{split}
|
||||||
$$
|
$$
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
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.
|
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)
|
svd40.train(movies_train, users_train, ratings_train, epoch_callback=at_epoch)
|
||||||
{{< / highlight >}}
|
{{< / highlight >}}
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
|
|
||||||
6982/s 8928/s 10378/s 12877/s 15290/s 11574/s 13230/s
|
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
|
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
|
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
|
Epoch 20/20; Training: MAE=0.549 RMSE=0.717, Testing: MAE=0.600 RMSE=0.787
|
||||||
|
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -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)
|
svd4.train(ml_train["movie_id"].values, ml_train["user_id"].values, ml_train["rating"].values, epoch_callback=at_epoch)
|
||||||
{{< / highlight >}}
|
{{< / highlight >}}
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
|
|
||||||
48199/s 33520/s 16937/s 13842/s 13607/s 15574/s 15431/s
|
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
|
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
|
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
|
Epoch 20/20; Training: MAE=0.599 RMSE=0.783, Testing: MAE=0.618 RMSE=0.809
|
||||||
|
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
To limit the data, we can use just the top movies (by number of ratings):
|
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,:])
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15
|
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15
|
||||||
|--|--|--|--|--|--|--|--|--|--|---|---|---|---|---|---
|
|--|--|--|--|--|--|--|--|--|--|---|---|---|---|---|---
|
||||||
0||||||||||||||||
|
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|||||
|
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|||||
|
14|||||||Babe; Babe: Pig in the City||||Twilight|||||
|
||||||
15||||||||||||||||
|
15||||||||||||||||
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -1632,7 +1629,7 @@ latent_factor_grid(svd4.q[2:,:])
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15
|
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15
|
||||||
|--|--|--|--|--|--|--|--|--|--|---|---|---|---|---|---
|
|--|--|--|--|--|--|--|--|--|--|---|---|---|---|---|---
|
||||||
0||||||||||||||||
|
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||
|
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|||||||
|
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|||||
|
15|||||||Bride of Chucky (Child's Play 4)||||Texas Chainsaw Massacre|||||
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -1673,7 +1670,7 @@ bias.iloc[:10]
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| movie_title | num_ratings | avg_rating | bias | movie_id | | | |
|
| movie_title | num_ratings | avg_rating | bias | movie_id | | | |
|
||||||
|------------|------------|-----------|-----|---------|-|-|-|-
|
|------------|------------|-----------|-----|---------|-|-|-|-
|
||||||
318|Shawshank Redemption, The (1994)|63366.0|4.446990|1.015911
|
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
|
50|Usual Suspects, The (1995)|47006.0|4.334372|0.910651
|
||||||
102217|Bill Hicks: Revelations (1993)|50.0|3.990000|0.900622
|
102217|Bill Hicks: Revelations (1993)|50.0|3.990000|0.900622
|
||||||
527|Schindler's List (1993)|50054.0|4.310175|0.898633
|
527|Schindler's List (1993)|50054.0|4.310175|0.898633
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -1700,7 +1697,7 @@ bias.iloc[:-10:-1]
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| movie_title | num_ratings | avg_rating | bias | movie_id | | | |
|
| movie_title | num_ratings | avg_rating | bias | movie_id | | | |
|
||||||
|------------|------------|-----------|-----|---------|-|-|-|-
|
|------------|------------|-----------|-----|---------|-|-|-|-
|
||||||
8859|SuperBabies: Baby Geniuses 2 (2004)|209.0|0.837321|-2.377202
|
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
|
4775|Glitter (2001)|685.0|1.124088|-2.047287
|
||||||
31698|Son of the Mask (2005)|467.0|1.252677|-2.022763
|
31698|Son of the Mask (2005)|467.0|1.252677|-2.022763
|
||||||
5739|Faces of Death 6 (1996)|174.0|1.261494|-2.004086
|
5739|Faces of Death 6 (1996)|174.0|1.261494|-2.004086
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -1760,7 +1757,7 @@ pd.DataFrame.from_records(
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div class=result>
|
<pre class="result">
|
||||||
| Library | Algorithm | MAE (test) | RMSE (test)
|
| Library | Algorithm | MAE (test) | RMSE (test)
|
||||||
|--------|----------|-----------|------------
|
|--------|----------|-----------|------------
|
||||||
0||Slope One|0.656514|0.856294
|
0||Slope One|0.656514|0.856294
|
||||||
@ -1768,7 +1765,7 @@ pd.DataFrame.from_records(
|
|||||||
2|Surprise|Random|1.144775|1.433753
|
2|Surprise|Random|1.144775|1.433753
|
||||||
3|Surprise|Slope One|0.704730|0.923331
|
3|Surprise|Slope One|0.704730|0.923331
|
||||||
4|Surprise|SVD|0.694890|0.900350
|
4|Surprise|SVD|0.694890|0.900350
|
||||||
</div>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
8
hugo_blag/static/css/custom.css
Normal file
8
hugo_blag/static/css/custom.css
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
pre.result {
|
||||||
|
background-color: #DDD;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
code {
|
||||||
|
background-color: #EEE
|
||||||
|
}
|
||||||
|
*/
|
||||||
Loading…
x
Reference in New Issue
Block a user