Created
March 29, 2020 09:36
-
-
Save 4OH4/bea12f336b154e25ac33acbc76cd5f0a to your computer and use it in GitHub Desktop.
Basic TF-idf model using Scikit-learn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.feature_extraction.text import TfidfVectorizer | |
from sklearn.metrics.pairwise import linear_kernel | |
search_terms = 'fruit and vegetables' | |
documents = ['cars drive on the road', 'tomatoes are actually fruit'] | |
doc_vectors = TfidfVectorizer().fit_transform([search_terms] + documents) | |
cosine_similarities = linear_kernel(doc_vectors[0:1], doc_vectors).flatten() | |
document_scores = [item.item() for item in cosine_similarities[1:]] | |
# [0.0, 0.190] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment