Below I created a full reproducible example to compute the topic model for a given DataFrame.
import numpy as np
import pandas as pd
data = pd.DataFrame({'Body': ['Here goes one example sentence that is generic',
'My car drives really fast and I have no brakes',
'Your car is slow and needs no brakes',
'Your and my vehicle are both not as fast as the airplane']})
from sklearn.decomposition import LatentDirichletAllocation
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(lowercase = True, analyzer = 'word')
data_vectorized = vectorizer.fit_transform(data.Body)
lda_model = LatentDirichletAllocation(n_components=4,
learning_method='online',
random_state=0,
verbose=1)
lda_topic_matrix = lda_model.fit_transform(data_vectorized)
Question: How is it possible to filter documents by topic? If so, can documents have multiple topic tags, or is a threshold needed?
In the end, I like to tag every document with "1" depending on whether it has a high loading of topic 2 and topic 3, else "0".