from sklearn.feature_extraction.text import TfidfVectorizer documents = [ "reset password token expired", "reset account password link", "invoice payment receipt delayed", "payment reminder invoice sent", ] vectorizer = TfidfVectorizer( lowercase=True, stop_words="english", ngram_range=(1, 2), ) matrix = vectorizer.fit_transform(documents) feature_names = vectorizer.get_feature_names_out() new_documents = ["password reset link sent"] new_matrix = vectorizer.transform(new_documents) print(f"training documents: {matrix.shape[0]}") print(f"tf-idf matrix shape: {matrix.shape}") print("first features:", ", ".join(feature_names[:8])) print("learned vocabulary contains:", "password" in vectorizer.vocabulary_) print(f"new document shape: {new_matrix.shape}") print("new document non-zero weights:") row = new_matrix[0] for column_index in row.nonzero()[1]: print(f" {feature_names[column_index]}: {row[0, column_index]:.3f}")