import pandas as pd

df = pd.read_csv('india-news-headlines.csv')
#nf = ' '.join(df['headline_text'].tolist())
Labels =  df['headline_category'][:1000]
News = df['headline_text'][:1000]
hf = pd.DataFrame({'Category':Labels, 'Headlines': News})

from sklearn.feature_extraction.text import TfidfVectorizer

tfidf = TfidfVectorizer()

features = tfidf.fit_transform(hf['Category']).toarray()

# Perform the necessary imports
from sklearn.decomposition import NMF
from sklearn.preprocessing import MaxAbsScaler, Normalizer
from sklearn.pipeline import make_pipeline

# Create a MaxAbsScaler: scaler
scaler = MaxAbsScaler()

# Create an NMF model: nmf
nmf = NMF(n_components=10)

# Create a Normalizer: normalizer
normalizer = Normalizer()

# Create a pipeline: pipeline
pipeline = make_pipeline(scaler, nmf, normalizer)

# Apply fit_transform to artists: norm_features
norm_features = pipeline.fit_transform(features)

# Import pandas
import pandas as pd

# Create a DataFrame: df
nf = pd.DataFrame(norm_features, index=Labels)

# Select row of 'Bruce Springsteen': artist
artist = nf.loc['unknown']

# Compute cosine similarities: similarities
similarities = nf.dot(artist.T)

# Display those with highest cosine similarity
print(similarities.nlargest( ))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.