text_html_output_1.png
from nltk.corpus import stopwords

from sklearn.decomposition import NMF

from sklearn.feature_extraction.text import TfidfVectorizer

import pandas as pd

import pandas as pd
df = pd.read_csv('../input/taylor_swift_lyrics.csv', encoding='latin-1')

songs = df.groupby('track_title').agg({'lyric': lambda x: ' '.join(x), 'year': 'mean'}).reset_index()

stop_words = stopwords.words('english')

from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer(stop_words=stop_words, min_df=0.1)
stop_words.extend(['back', 'said', 'come', 'things', 'get', 'oh', 'one', 'yeah', 'place', 'would', 'like', 'know', 'stay', 'go', 'let', 'cause', 'could', 'wanna', 'would', 'gonna'])

tfidf = vectorizer.fit_transform(songs['lyric'])

nmf = NMF(n_components=6)

topic_values = nmf.fit_transform(tfidf)

for topic_num, topic in enumerate(nmf.components_):
    message = 'Topc #{}:'.format(topic_num + 1)
    message += ' '.join([vectorizer.get_feature_names()[i] for i in topic.argsort()[:-11:-1]])
    print(message)