Tương đồng dựa trên văn bản

Xây dựng Recommendation Engine bằng Python

Rob O'Callaghan

Director of Data

Khi thiếu thuộc tính rõ ràng

Ví dụ mô tả sản phẩm trên Amazon.

Xây dựng Recommendation Engine bằng Python

Tần suất thuật ngữ – nghịch đảo tần suất tài liệu

$$ \Large{\text{TF-IDF} = \frac{\frac{\text{Count of word occurrences}}{\text{Total words in document}}}{\log({\frac{\text{Number of docs word is in}}{\text{Total number of docs}}})}} $$

Xây dựng Recommendation Engine bằng Python

Dữ liệu của chúng ta

book_summary_df:

Sách Mô tả
The Hobbit "Bilbo Baggins sống giản dị với các hobbit đồng hương ở The Shire..."
The Great Gatsby "Lấy bối cảnh Thời kỳ Jazz ở New York, tiểu thuyết kể câu chuyện bi kịch của Jay ..."
A Game of Thrones "15 năm đã trôi qua kể từ cuộc nổi dậy của Robert, với cuộc chiến kéo dài chín năm ..."
Macbeth "Một tướng lĩnh Scotland dũng cảm nhận lời tiên tri từ ba mụ phù thủy ..."
... ...
Xây dựng Recommendation Engine bằng Python

Khởi tạo bộ vector hóa

from sklearn.feature_extraction.text import TfidfVectorizer
tfidfvec = TfidfVectorizer(        ,           )
Xây dựng Recommendation Engine bằng Python

Lọc dữ liệu

from sklearn.feature_extraction.text import TfidfVectorizer
tfidfvec = TfidfVectorizer(min_df=2,           )
Xây dựng Recommendation Engine bằng Python

Lọc dữ liệu

from sklearn.feature_extraction.text import TfidfVectorizer
tfidfvec = TfidfVectorizer(min_df=2, max_df=0.7)
Xây dựng Recommendation Engine bằng Python

Vector hóa dữ liệu

vectorized_data = tfidfvec.fit_transform(book_summary_df['Descriptions'])

print(tfidfvec.get_feature_names)
['age', 'ancient', 'angry', 'brave', 'battle', 'fellow', 'game', 'general', ...]
print(vectorized_data.to_array())
[[0.21,      0.53,    0.41,    0.64,     0.01,     0.02,     ...
 [0.31,      0.00,    0.42,    0.03,     0.00,     0.73,     ...
 [...,        ...,     ...,     ...,      ...,      ...,     ...
Xây dựng Recommendation Engine bằng Python

Định dạng dữ liệu

tfidf_df = pd.DataFrame(vectorized_data.toarray(),
                        columns=tfidfvec.get_feature_names())

tfidf_df.index = book_summary_df['Book']
print(tfidf_df)
                   | 'age'| 'ancient'| 'angry'| 'brave'| 'battle'| 'fellow'|...
|------------------|------|----------|--------|--------|---------|---------|...
| The Hobbit       |  0.21|      0.53|    0.41|    0.64|     0.01|     0.02|...
| The Great Gatsby |  0.31|      0.00|    0.42|    0.03|     0.00|     0.73|...
| A Game of Thrones|  0.61|      0.42|    0.77|    0.31|     0.83|     0.03|...
|               ...|   ...|       ...|     ...|     ...|      ...|      ...|...
Xây dựng Recommendation Engine bằng Python

Độ tương tự cosine

Khoảng cách cosine: $$cos(\theta)=\frac{A.B }{||A||\cdot||B||}$$

Xây dựng Recommendation Engine bằng Python

Độ tương tự cosine

from sklearn.metrics.pairwise import cosine_similarity

# Find similarity between all items
cosine_similarity_array = cosine_similarity(tfidf_summary_df)
# Find similarity between two items
cosine_similarity(tfidf_df.loc['The Hobbit'].values.reshape(1, -1),
                  tfidf_df.loc['Macbeth'].values.reshape(1, -1))
Xây dựng Recommendation Engine bằng Python

Ayo berlatih!

Xây dựng Recommendation Engine bằng Python

Preparing Video For Download...