การสร้างระบบแนะนำภาพยนตร์จาก plot

Feature Engineering for NLP in Python

Rounak Banik

Data Scientist

ระบบแนะนำภาพยนตร์

ชื่อเรื่อง ภาพรวม
Shanghai Triad เด็กชายจากต่างจังหวัดที่มีความสัมพันธ์กับแก๊งอาชญากรในเซี่ยงไฮ้ถูกลุงพาเข้าเมืองในยุค 1930s เพื่อรับใช้เมียน้อยของหัวหน้าแก๊ง
Cry, the Beloved Country บาทหลวงชาวแอฟริกาใต้ออกตามหาลูกชายที่หลงทางและก่ออาชญากรรมในเมืองใหญ่
Feature Engineering for NLP in Python

ระบบแนะนำภาพยนตร์

get_recommendations("The Godfather")
1178               The Godfather: Part II
44030    The Godfather Trilogy: 1972-1990
1914              The Godfather: Part III
23126                          Blood Ties
11297                    Household Saints
34717                   Start Liquidation
10821                            Election
38030                          Goodfellas
17729                   Short Sharp Shock
26293                  Beck 28 - Familjen
Name: title, dtype: object
Feature Engineering for NLP in Python

ขั้นตอน

  1. การประมวลผลข้อความ
  2. สร้างเวกเตอร์ tf-idf
  3. สร้างเมทริกซ์ cosine similarity
Feature Engineering for NLP in Python

ฟังก์ชัน recommender

  1. รับชื่อภาพยนตร์ เมทริกซ์ cosine similarity และ series ของ indices เป็น arguments
  2. ดึงคะแนน cosine similarity แบบ pairwise ของภาพยนตร์นั้น
  3. เรียงคะแนนจากมากไปน้อย
  4. แสดงชื่อเรื่องที่ตรงกับคะแนนสูงสุด
  5. ละเว้นคะแนน similarity สูงสุด (ค่า 1)
Feature Engineering for NLP in Python

การสร้างเวกเตอร์ tf-idf

# Import TfidfVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer

# Create TfidfVectorizer object
vectorizer = TfidfVectorizer()

# Generate matrix of tf-idf vectors
tfidf_matrix = vectorizer.fit_transform(movie_plots)
Feature Engineering for NLP in Python

การสร้างเมทริกซ์ cosine similarity

# Import cosine_similarity
from sklearn.metrics.pairwise import cosine_similarity

# Generate cosine similarity matrix
cosine_sim = cosine_similarity(tfidf_matrix, tfidf_matrix)
array([[1.        , 0.27435345, 0.23092036, ..., 0.        , 0.        ,
        0.00758112],
       [0.27435345, 1.        , 0.1246955 , ..., 0.        , 0.        ,
        0.00740494],
       ...,
       [0.00758112, 0.00740494, 0.        , ..., 0.        , 0.        ,
        1.        ]])
Feature Engineering for NLP in Python

ฟังก์ชัน linear_kernel

  • ขนาดของเวกเตอร์ tf-idf มีค่าเท่ากับ 1
  • คะแนน cosine ระหว่างเวกเตอร์ tf-idf สองตัวคือผลคูณแบบ dot product
  • ช่วยลดเวลาการคำนวณได้อย่างมีนัยสำคัญ
  • ใช้ linear_kernel แทน cosine_similarity
Feature Engineering for NLP in Python

การสร้างเมทริกซ์ cosine similarity

# Import cosine_similarity
from sklearn.metrics.pairwise import linear_kernel

# Generate cosine similarity matrix
cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix)
array([[1.        , 0.27435345, 0.23092036, ..., 0.        , 0.        ,
        0.00758112],
       [0.27435345, 1.        , 0.1246955 , ..., 0.        , 0.        ,
        0.00740494],
       ...,
       [0.00758112, 0.00740494, 0.        , ..., 0.        , 0.        ,
        1.        ]])
Feature Engineering for NLP in Python

ฟังก์ชัน get_recommendations

get_recommendations('The Lion King', cosine_sim, indices)
7782                      African Cats
5877    The Lion King 2: Simba's Pride
4524                         Born Free
2719                          The Bear
4770     Once Upon a Time in China III
7070                        Crows Zero
739                   The Wizard of Oz
8926                   The Jungle Book
1749                 Shadow of a Doubt
7993                      October Baby
Name: title, dtype: object
Feature Engineering for NLP in Python

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

Feature Engineering for NLP in Python

Preparing Video For Download...