Example of HMM-LDA


In [1]:
import logging
from ptm.nltk_corpus import get_reuters_token_list_by_sentence
from ptm import HMM_LDA
from ptm.utils import get_top_words

logger = logging.getLogger('HMM_LDA')
logger.propagate=False

Read corpus

corpus is a nested list of documents, sentences, and word tokens, respectively.


In [2]:
n_docs = 1000
voca, corpus = get_reuters_token_list_by_sentence(num_doc=n_docs)
print('Vocabulary size', len(voca))


Vocabulary size 3851

Training HMM LDA


In [3]:
n_docs = len(corpus)
n_voca = len(voca)
n_topic = 50
n_class = 20
max_iter = 100
alpha = 0.1
beta = 0.01
gamma = 0.1
eta = 0.1
model = HMM_LDA(n_docs, n_voca, n_topic, n_class, alpha=alpha, beta=beta, gamma=gamma, eta=eta, verbose=False)
model.fit(corpus, max_iter=max_iter)

In [4]:
for ti in range(n_topic):
    top_words = get_top_words(model.TW, voca, ti, n_words=10)
    print('Topic', ti ,': ', ','.join(top_words))


Topic 0 :  will,on,its,must,throughout,same,by,traditional,loss,background
Topic 1 :  future,should,are,charge,higher,sulphur,first,an,company,letter
Topic 2 :  ready,same,be,basis,it,will,for,at,registered,capital
Topic 3 :  alone,great,specialty,would,unreasonable,falling,say,formed,top,declined
Topic 4 :  offer,do,although,on,over,would,much,by,fiscal,objective
Topic 5 :  barring,did,bearing,may,but,its,narrow,target,leading,same
Topic 6 :  for,two,meeting,may,still,at,six,whose,become,marked
Topic 7 :  stimulate,each,under,satisfied,at,transition,distribution,activity,for,provision
Topic 8 :  is,difficulty,effect,top,from,nine,price,deficit,agreed,only
Topic 9 :  for,country,pressure,increasing,will,government,its,quietly,nil,report
Topic 10 :  petroleum,per,expectation,pollard,weight,textile,from,cocoa,absorbing,remainder
Topic 11 :  should,but,set,shipment,much,term,same,be,practice,its
Topic 12 :  offer,present,at,this,they,help,name,an,time,show
Topic 13 :  would,rating,current,landing,year,long,market,after,when,its
Topic 14 :  six,goods,national,were,commodity,massive,use,merge,confirmed,days
Topic 15 :  trade,it,agreement,industry,those,town,from,we,number,with
Topic 16 :  other,year,worked,be,give,it,ago,are,proposal,progress
Topic 17 :  speculation,it,deficit,its,this,despite,an,up,large,government
Topic 18 :  cash,corn,over,preferred,with,about,still,least,association,overseas
Topic 19 :  trade,will,is,accrual,consider,similar,pressure,chairman,parcel,with
Topic 20 :  trade,current,fault,or,group,week,this,an,half,one
Topic 21 :  fiscal,for,turned,tone,similar,average,annual,it,closed,why
Topic 22 :  weak,raising,special,contract,profit,by,while,he,would,block
Topic 23 :  following,growth,crude,up,an,leading,business,fiscal,floating,impact
Topic 24 :  given,another,reserve,contract,harvest,ahead,an,textile,message,dividend
Topic 25 :  today,put,but,debt,market,seen,interest,concern,franc,week
Topic 26 :  no,from,six,market,particularly,earn,one,measured,tender,suspension
Topic 27 :  most,new,are,percentage,definitive,adequate,bread,business,minister,us
Topic 28 :  trade,central,chairman,beginning,last,had,condition,when,subject,added
Topic 29 :  certain,period,be,nil,end,issue,quarter,billion,vague,investigatory
Topic 30 :  expire,market,underground,it,reaction,sharply,together,nil,everything,government
Topic 31 :  exercisable,its,federal,growth,both,would,last,long,much,year
Topic 32 :  state,unchanged,quarter,increase,want,several,rolled,we,if,for
Topic 33 :  trade,with,being,more,is,total,principally,likely,number,margin
Topic 34 :  posted,this,share,next,subject,dealer,executive,two,interview,which
Topic 35 :  rise,group,friendly,be,sale,it,also,bank,for,or
Topic 36 :  based,premium,most,from,number,last,had,fourth,make,also
Topic 37 :  yen,stability,they,offering,billion,week,cut,under,trading,this
Topic 38 :  nil,about,bill,re,bank,chairman,be,strong,false,closed
Topic 39 :  trade,year,operating,line,say,equal,approach,price,search,strength
Topic 40 :  cake,be,move,here,budget,were,should,development,shortly,by
Topic 41 :  outstanding,exploration,its,government,number,for,all,account,monthly,week
Topic 42 :  industrial,them,short,its,loss,be,it,from,concern,each
Topic 43 :  had,responsible,an,unit,we,situation,well,ready,field,not
Topic 44 :  settle,trading,see,its,from,much,output,interbank,government,for
Topic 45 :  trade,situation,because,cost,priced,but,as,would,its,urgency
Topic 46 :  am,major,sugar,pose,t,by,memorandum,dropping,division,were
Topic 47 :  who,two,spokesman,cash,loss,kept,it,month,equity,daily
Topic 48 :  saw,he,gallon,would,we,sale,season,for,year,not
Topic 49 :  permit,be,billion,they,by,concerned,forward,overall,if,from

In [5]:
for ci in range(1, n_class):
    top_words = get_top_words(model.CW, voca, ci, n_words=10)
    print('Class', ci ,': ', ','.join(top_words))


Class 1 :  were,on,per,be,it,will,an,is,year,company
Class 2 :  at,was,have,billion,not,an,is,will,it,be
Class 3 :  trade,will,on,is,loss,be,have,it,from,this
Class 4 :  by,also,would,for,will,were,this,have,are,from
Class 5 :  the,about,be,on,year,company,would,by,with,loss
Class 6 :  the,he,billion,is,be,it,will,an,not,at
Class 7 :  the,he,from,were,an,loss,be,it,will,nil
Class 8 :  one,with,for,company,an,nil,billion,it,be,loss
Class 9 :  the,be,as,was,not,will,it,nil,at,an
Class 10 :  on,last,for,at,company,will,it,billion,be,by
Class 11 :  the,year,for,would,from,was,be,it,will,an
Class 12 :  or,are,it,will,for,not,at,billion,by,its
Class 13 :  as,is,not,company,were,will,it,be,loss,at
Class 14 :  was,its,it,be,quarter,for,billion,from,would,on
Class 15 :  market,last,is,with,on,would,share,by,billion,be
Class 16 :  last,on,an,its,loss,be,it,will,company,is
Class 17 :  the,trade,this,be,was,it,will,company,for,not
Class 18 :  the,last,will,from,billion,an,loss,be,it,its
Class 19 :  the,of,to,in,said,and,a,for,s,on

Function words belong to classes and content words belong to topics.

In this example, function words are not very well divided by their roles. As in the original paper, fine-tuning, sampling hyper-parameters or n-th order Markovian assumption may help to improve the performance.