Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

sample clustering of articles using kmeans and trncatedSVD

# Perform the necessary imports
from sklearn.decomposition import TruncatedSVD
from sklearn.cluster import KMeans
from sklearn.pipeline import make_pipeline

# Create a TruncatedSVD instance: svd
svd = TruncatedSVD(n_components=50)

# Create a KMeans instance: kmeans
kmeans = KMeans(n_clusters=6)

# Create a pipeline: pipeline
pipeline = make_pipeline(svd, kmeans)

# Import pandas
import pandas as pd

# Fit the pipeline to articles
pipeline.fit(articles)

# Calculate the cluster labels: labels
labels = pipeline.predict(articles)

# Create a DataFrame aligning labels and titles: df
df = pd.DataFrame({'label': labels, 'article': titles})

# Display df sorted by cluster label
print(df.sort_values('label'))
Comment

PREVIOUS NEXT
Code Example
Python :: python project structure 
Python :: set DJANGO_SETTINGS_MODULE=mysite.settings django-admin 
Python :: if you have a list and the user input one of the keys then output its value 
Python :: rich import in python 
Python :: como colocar uma variavel no print python 
Python :: Implementing the hashing trick 
Python :: print command in python 
Python :: python raccourci mettre paragraphe commentaire 
Python :: docker python no module named 
Python :: Understand the most appropriate graph to use for your dataset visualization 
Python :: printing range of index in python 
Python :: what will be the output of the following python code? x = 123 for i in x: print(i) 
Python :: python check if variable is module 
Python :: Mapping using dictionary 
Python :: how to access github folder in python code using github https link 
Python :: python setup specify c++ version 
Python :: hoow to print python 
Python :: python dataframe update if not new row 
Python :: how to draw squircle python 
Python :: add values to add value in a matplotlib image 
Python :: moving element to the start ofa list python 
Python :: django rest framework foreign key relation giving error in serializer 
Python :: na.kalman in python 
Python :: username__icontains in django 
Python :: related name django 
Python :: Return a new RDD containing only the elements that satisfy a predicate. 
Python :: how to create a joystick in pyqt4 
Python :: how to upgrade python from 2.7 to 2.9 on ubuntu 14.04 
Python :: get the factorial of a number on python 
Python :: c++ to python converter online 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =