Singular value decomposition (SVD)

Building Recommendation Engines in Python

Rob O'Callaghan

Director of Data

What SVD does

Sparse rectangular matrix

Building Recommendation Engines in Python

What SVD does

Sparse rectangular matrix next to its first factor

Building Recommendation Engines in Python

What SVD does

Sparse rectangular matrix next to two of its factors

Building Recommendation Engines in Python

What SVD does

Sparse rectangular matrix next to its factors

Building Recommendation Engines in Python

Prepping our data

print(book_ratings_df.shape)
(220, 500)
avg_ratings = book_ratings_df.mean(axis=1)

print(avg_ratings)
array([[4.5 ],
       [3.5],
       [2.5],
       [3.5],
        ... 
       [2.2]])
Building Recommendation Engines in Python

Prepping our data

user_ratings_pivot_centered = user_ratings_df.sub(avg_ratings, axis=0)
user_ratings_df.fillna(0, inplace=True)

print(user_ratings_df)
          The Great Gatsby    The Catcher in the Rye    Fifty Shades of Grey                    
User_233               0.0                       0.0                     0.0
User_651               0.0                       0.5                    -0.5
User_965               0.5                      -0.5                     0.0
     ...               ...                       ...                     ...
Building Recommendation Engines in Python

Applying SVD

from scipy.sparse.linalg import svds

U, sigma, Vt = svds(user_ratings_pivot_centered)
print(U.shape)
(610, 6)
print(Vt.shape)
(6, 1000)
Building Recommendation Engines in Python

Applying SVD

print(sigma)
[3.0, 4.8, -12.6, -3.8, 8.2, 7.3]
sigma = np.diag(sigma)
print(sigma)
array([   3.0    ,   0.     ,   0.     ,   0.     ,   0.     ,   0.     ],
       [  0.     ,   4.8    ,   0.     ,   0.     ,   0.     ,   0.     ],
       [  0.     ,   0.     , -12.6    ,   0.     ,   0.     ,   0.     ],
       [  0.     ,   0.     ,   0.     ,  -3.8    ,   0.     ,   0.     ],
       [  0.     ,   0.     ,   0.     ,   0.     ,   8.2    ,   0.     ],
       [  0.     ,   0.     ,   0.     ,   0.     ,   0.     ,   7.3    ]),
Building Recommendation Engines in Python

Getting the final matrix

The three factor matrices found from SVD

Building Recommendation Engines in Python

Getting the final matrix

The three factor matrices found from SVD

Building Recommendation Engines in Python

Getting the final matrix

The three factor matrices found from SVD

Building Recommendation Engines in Python

Getting the final matrix

The three factor matrices found from SVD next to their product

Building Recommendation Engines in Python

Calculating the product in Python

recalculated_ratings =        np.dot(U, sigma)     

Building Recommendation Engines in Python

Calculating the product in Python

recalculated_ratings = np.dot(np.dot(U, sigma), Vt)
print(recalculated_ratings)
[[  0.1      -0.9       -3.6.     ...   ]
 [ -2.3       0.5       -0.5      ...   ]
 [  0.5      -0.5        2.0      ...   ]
 [ ...        ...        ...      ...   ]]
Building Recommendation Engines in Python

Add averages back

recalculated_ratings = recalculated_ratings + avg_ratings.values.reshape(-1, 1)
print(recalculated_ratings)
[[  4.6       3.6        0.9      ...   ]
 [  1.8       4.0        3.0      ...   ]
 [  3.0       2.0        4.5      ...   ]
 [ ...        ...        ...      ...   ]]
print(book_ratings_df)
[[  5.0       4.0         NA      ...   ]
 [   NA       4.0        3.0      ...   ]
 [  3.0       2.0         NA      ...   ]
 [ ...        ...        ...      ...   ]]
Building Recommendation Engines in Python

Let's practice!

Building Recommendation Engines in Python

Preparing Video For Download...