Singular value decomposition (SVD)

การสร้าง Recommendation Engine ด้วย Python

Rob O'Callaghan

Director of Data

SVD ทำอะไร

เมทริกซ์สี่เหลี่ยมผืนผ้าแบบกระจาย

การสร้าง Recommendation Engine ด้วย Python

SVD ทำอะไร

เมทริกซ์สี่เหลี่ยมผืนผ้าแบบกระจายและแฟกเตอร์แรก

การสร้าง Recommendation Engine ด้วย Python

SVD ทำอะไร

เมทริกซ์สี่เหลี่ยมผืนผ้าแบบกระจายและสองแฟกเตอร์

การสร้าง Recommendation Engine ด้วย Python

SVD ทำอะไร

เมทริกซ์สี่เหลี่ยมผืนผ้าแบบกระจายและแฟกเตอร์ทั้งหมด

การสร้าง Recommendation Engine ด้วย Python

เตรียมข้อมูล

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]])
การสร้าง Recommendation Engine ด้วย Python

เตรียมข้อมูล

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
     ...               ...                       ...                     ...
การสร้าง Recommendation Engine ด้วย Python

นำ 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)
การสร้าง Recommendation Engine ด้วย Python

นำ 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    ]),
การสร้าง Recommendation Engine ด้วย Python

สร้างเมทริกซ์สุดท้าย

เมทริกซ์แฟกเตอร์สามตัวที่ได้จาก SVD

การสร้าง Recommendation Engine ด้วย Python

สร้างเมทริกซ์สุดท้าย

เมทริกซ์แฟกเตอร์สามตัวที่ได้จาก SVD

การสร้าง Recommendation Engine ด้วย Python

สร้างเมทริกซ์สุดท้าย

เมทริกซ์แฟกเตอร์สามตัวที่ได้จาก SVD

การสร้าง Recommendation Engine ด้วย Python

สร้างเมทริกซ์สุดท้าย

เมทริกซ์แฟกเตอร์สามตัวจาก SVD และผลคูณของเมทริกซ์เหล่านั้น

การสร้าง Recommendation Engine ด้วย Python

คำนวณผลคูณใน Python

recalculated_ratings =        np.dot(U, sigma)     

การสร้าง Recommendation Engine ด้วย Python

คำนวณผลคูณใน 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      ...   ]
 [ ...        ...        ...      ...   ]]
การสร้าง Recommendation Engine ด้วย Python

บวกค่าเฉลี่ยกลับเข้าไป

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      ...   ]
 [ ...        ...        ...      ...   ]]
การสร้าง Recommendation Engine ด้วย Python

มาฝึกกันเถอะ!

การสร้าง Recommendation Engine ด้วย Python

Preparing Video For Download...