Topic Modeling with Python

Let’s start talking about Data Mining! In today’s post, we are going to dive into Topic Modeling, a unique technique that extracts the topics from a text. It is a really impressive technique that has many appliances in the world of Data Science. The following post will go as follows. First I am going to give some basic definitions and explain what Topic Modeling is. Then, I will shortly refer to preprocessing, since I am going to dedicate a whole post for this. Continuing, I will present a Python algorithm and I will conclude with a visualization process. For the sake of this post, I am going to use a known dataset from lda python library called reuters and not my previous blog posts, since they are not that many. Let’s begin!

The code of this project will be uploaded soon and it will contain the preprocessing step too!

1. Topic Modeling, Definitions

In Wiki’s page, there is this definition.

A topic model is a type of statistical model for discovering the abstract "topics"
that occur in a collection of documents.

As we can see, Topic Model is the method of topic extraction from a document. For a human, to find the text’s topic is really easy. Even if the text is unreadable, only from some specific words, he/she will understand the topic. For a computer, this method is not that trivial, a computer cannot understand the meaning of words. If we pick two random words from a physical book and we give them to a computer, the computer cannot comprehend the difference, for example, the words the and Juliette. The computer must have some previous knowledge about the book or to be able to scrape/search/crawl/etc the internet or any other source of information and even then it will just deduct an analysis.

With topic modeling, a computer deducts a statistical analysis on a document and outputs a series of words that are relevant to that document(very roughly explanation). Let’s take a closer look.

There are several methods for performing  Topic Modeling, some of them are:

  • LSA
  • NMF
  • pLSA
  • LDA

In this post, we are going to see a well-known algorithm that is very flexible. The name of this algorithm is LDA  or Latent Dirichlet Allocation. A very good explanation is given by Christine Doig.

Check her out! She is amazing!

2. Preprocessing the Data

To perform LDA or every other Topic Modeling algorithm, you will need a nice text corpus. The corpus that you will need depends on the application. If you need to perform topic modeling on articles from CNN/BBC/or any other news website, you will need a good utility corpus like Wikipedia, because you will have to deal with different categories (sports, politics, food, movies, ….). At the bottom line, a good corpus will give you better results. I am not going to jump into details here because as I said before, I will write preprocessing on a different post. Here, for example, we can do the following:

  • Get title and content of all wiki pages
  • Get rid of short articles
  • tokenize the remaining articles
  • sort the words according to Tf-idf
  • perform stemming
  • remove a %, top% and bottom% from the sorted list
  • remove stopwords
  • keep the top % of the remaining list

These are some basic steps for preprocessing a text corpus, we will discuss more of them and in depth in a later post.

#This is the preprocessing step
X = lda.datasets.load_reuters()
vocab = lda.datasets.load_reuters_vocab()
titles = lda.datasets.load_reuters_titles()

3. Perform LDA

There are lots of implementations of LDA, here are some of them:

Assuming now that we have a very good corpus, we will perform topic modeling using lda algorithm for Python 2.7.

#First create the model
model = lda.LDA(n_topics=10, n_iter=500, random_state=1)
#Perform LDA
#Print the topics
topic_word = model.topic_word_
#Number of words per topic
n_top_words = 5
for i, topic_dist in enumerate(topic_word):
    topic_words = np.array(vocab)[np.argsort(topic_dist)][:-n_top_words:-1]
    print('Topic {}: {}'.format(i, ' '.join(topic_words)))

#Get the document titles and see the assigned topics
doc_topic = model.doc_topic_
for n in range(10):
    topic_most_pr = doc_topic[n].argmax()
    print("doc: {} topic: {}\n{}...".format(n,


The results are the following:

Topic 0: police church catholic women
Topic 1: elvis film music fans
Topic 2: yeltsin president political russian
Topic 3: city million century art
Topic 4: charles prince king diana
Topic 5: germany against french german
Topic 6: church people years first
Topic 7: pope mother teresa vatican
Topic 8: harriman u.s clinton churchill
Topic 9: died former life funeral

We can see that the topics are not making any sense whatsoever, but we can clearly get the sense of what the documents are talking about! With this kind of information we can manipulate and analyze the documents, for example, we can cluster the documents for a recommendation system.

Furthermore, we can see the first ten documents and the assigned topics:

doc: 0 topic: 4
0 UK: Prince Charles spearheads British royal revolution. LONDON 1996-08-20…
doc: 1 topic: 6
1 GERMANY: Historic Dresden church rising from WW2 ashes. DRESDEN, Germany 1996-08-21…
doc: 2 topic: 7
2 INDIA: Mother Teresa’s condition said still unstable. CALCUTTA 1996-08-23…
doc: 3 topic: 4
3 UK: Palace warns British weekly over Charles pictures. LONDON 1996-08-25…
doc: 4 topic: 7
4 INDIA: Mother Teresa, slightly stronger, blesses nuns. CALCUTTA 1996-08-25…
doc: 5 topic: 7
5 INDIA: Mother Teresa’s condition unchanged, thousands pray. CALCUTTA 1996-08-25…
doc: 6 topic: 7
6 INDIA: Mother Teresa shows signs of strength, blesses nuns. CALCUTTA 1996-08-26…
doc: 7 topic: 7
7 INDIA: Mother Teresa’s condition improves, many pray. CALCUTTA, India 1996-08-25…
doc: 8 topic: 7
8 INDIA: Mother Teresa improves, nuns pray for “miracle”. CALCUTTA 1996-08-26…
doc: 9 topic: 4
9 UK: Charles under fire over prospect of Queen Camilla. LONDON 1996-08-26…

4. Conclusion

We can clearly see that the topics were to the point. For the evaluation process, we can use several methods, for example, we can compute the distance between documents which translates to the similarity between documents. We can use cosine similarity or Jensen-Shannon Distance similarity to cluster the documents or use perplexity to see if the model is representative of the documents we are scoring on.

For the evaluation process, we can use several methods according to the needs of our application, for example, we can compute the distance between documents which translates to the similarity between documents. We can use cosine similarity or Jensen-Shannon Distance similarity to cluster the documents or use perplexity to see if the model is representative of the documents we are scoring on.

That’s all for today’s post! Please let me know if you have any question in the comments section below! Till next time, take care and bye bye!


Siaterlis Konstantinos

Mining the Social Media using Python 2.7


In this post, I will show you how to mine the Social Media, to be more precise Twitter! It is a very simple process and I will show you how to do it in Python 2.7 in a couple of steps.

Step 1 – Install Python Packages

First of all, let’s see the list with all the packages that we are going to use for this project:

Json is already implemented in Python >=2.7 and python-twitter installs all the appropriate packages. After that, you are ready to start!

Step 2 – Make a Twitter app

This is an easy step and I am going to walk you through it. First go here and log in to your twitter account. This is the development site of twitter, where you can build your own apps!

Click on the button “Create new app” at the top right corner. Fill in the blanks with your information and then click on “Create your Twitter application”. Here is an example.


After you have created your app, you will be redirected to the App’s homepage. Go to Keys and Access Tokens and click on “Create My Access Token” at the bottom of the page. At the top of your page, you can find your secret keys and at the bottom your access tokens. Here is an example.


Write down those keys and remember, those keys are secret! DO NOT SHARE! After that you need to adjust your app’s access level, just to avoid further validation (if you are going to use it for your own account you do not need to change this). Go to Permissions->Select “Read Only”->Update Settings. That’s it! Now we can now write code.

Step 3 – Get the Tweets

First of all, we want to import the appropriate packages.

import twitter
import json

Json is needed because the twitter API returns us the tweet in json format. For example:

{"created_at": "Wed Mar 01 09:44:29 +0000 2017",
 "hashtags": [], 
 "id": 836874776106926080,
 "id_str": "836874776106926080",
 "lang": "en",
 "media": [
     {... "text": "First blog post 
          "urls": [
                  {"expanded_url": "https://mydatam...", 
                   "url": "htt..."}], 
 "user": {"id": }, 
 "user_mentions": []}

We need to access the text field, so let’s see how we can accomplish that.

First, we need to connect to Twitter’s API. This is where we are going to use the API keys we generated earlier.

#create a class to be able to use it properly
class SampleTwitter:
    #declare class variables
    consumer_key = ''
    consumer_secret = ''
    access_token_key = ''
    access_token_secret = ''

    def __init__(self, consumer_key, consumer_secret, access_token_key,
        # Twitter tokens
        SampleTwitter.consumer_key = consumer_key
        SampleTwitter.consumer_secret = consumer_secret
        SampleTwitter.access_token_key = access_token_key
        SampleTwitter.access_token_secret = access_token_secret

As you can see I created a class because I am using this sampling a lot in my research, I suggest you do the same. When I am going to create my class object, I will parse the API keys. Next, in the SampleTwitter class, I created a method called getTweets() where I gave as input the account I want to sample. BE CAREFUL, there is a limit on how many tweets per day you can retrieve!

#use the python-twitter package to get the tweets
#where screen_name is name of the account you want to sample
def getTweets(self, screen_name):

    # Connect to twitter api
    api = twitter.Api(consumer_key=SampleTwitter.consumer_key, consumer_secret=SampleTwitter.consumer_secret, access_token_key=SampleTwitter.access_token_key, access_token_secret=SampleTwitter.access_token_secret)
    statuses = api.GetUserTimeline(screen_name=screen_name,
                                   count=200, include_rts=True,
                                   trim_user=False, exclude_replies=True)
    #Gather all tweets to a list
    tweets = []

    for i in statuses:
        #the tweets come ona jason format
        tweet = json.loads(str(i))

    return tweets

As you can see at line 15 and 16 I extract the tweet’s text from the json format. Also, I want to talk about the GetUserTimeline’s parameter at line 7. Here I sampled the last 200 tweets, without replies, without retweets and with the user handles. You can find all the parameters here.

Step 4 – Calling the class, iterate through tweets

Concluding, I created a file to retrieve the tweets.

#import your class
from sample import SampleTwitter

consumer_key = 'your consumer key'
consumer_secret = 'your consumer secret'
access_token_key = 'your access token key'
access_token_secret = 'your access token secret'
#create your object
sampling = SampleTwitter(consumer_key, consumer_secret, access_token_key, access_token_secret)
#call the getTweets() method with the account you want to sample
tweets = sampling.getTweets('siaterliskonsta')

#iterate through tweets
for tweet in tweets:
    print tweet


This is it! You can now sample twitter account, harvest tweets and process the results. Be careful tho, as I said before, there is a limit on how many tweets you can retrieve! Anyways, until next time, take care and have fun!


Siaterlis Konstantinos


P.S. The whole code of this post is here.