Clustering newsgroups data using k-means

Up to this point, you should be very familiar with k-means clustering. Let's see what we are able to mine from the newsgroups dataset using this algorithm. We, herein, use all data from four categories as an example.

We first load the data from those newsgroups and preprocess it as we did in Chapter 2, Mining the 20 Newsgroups Dataset with Clustering and Topic Modeling Algorithms:

>>> from sklearn.datasets import fetch_20newsgroups>>> categories = [...     'alt.atheism',...     'talk.religion.misc',...     'comp.graphics',...     'sci.space',... ]>>> groups = fetch_20newsgroups(subset='all',                                     categories=categories)>>> labels = groups.target>>> label_names = groups.target_names>>> def is_letter_only(word):... for char ...

Get Python Machine Learning By Example - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.