Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8198911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:03:42+00:00 2026-06-07T06:03:42+00:00

Recently, I implemented Gibbs sampling for LDA topic model on Python using numpy, taking

  • 0

Recently, I implemented Gibbs sampling for LDA topic model on Python using numpy, taking as a reference some code from a site. In each iteration of Gibbs sampling, we remove one (current) word, sample a new topic for that word according to a posterior conditional probability distribution inferred from the LDA model, and update word-topic counts, as follows:

for m, doc in enumerate(docs): #m: doc id
  for n, t in enumerate(doc): #n: id of word inside document, t: id of the word globally
    # discount counts for word t with associated topic z
    z = z_m_n[m][n]
    n_m_z[m][z] -= 1
    n_z_t[z, t] -= 1 
    n_z[z] -= 1
    n_m[m] -= 1

    # sample new topic for multinomial                
    p_z_left = (n_z_t[:, t] + beta) / (n_z + V * beta)
    p_z_right = (n_m_z[m] + alpha) / ( n_m[m] + alpha * K)
    p_z = p_z_left * p_z_right
    p_z /= numpy.sum(p_z)
    new_z = numpy.random.multinomial(1, p_z).argmax() 

    # set z as the new topic and increment counts
    z_m_n[m][n] = new_z
    n_m_z[m][new_z] += 1
    n_z_t[new_z, t] += 1
    n_z[new_z] += 1
    n_m[m] += 1

In the above code, we sample a new (single) z with the multinomial scipy function.

Now, I want to implement a Joint Sentiment Topic model of this paper. Now, I would need the following structures for keeping track of the needed counts:

3D matrix containing # occurrences for a word for each topic, for each sentiment
3D matrix containing # occurrences for a topic, for each sentiment, for each document
2D matrix containing # occurrences for a topic, for each sentiment
2D matrix containing # occurrences for a sentiment for each document

And now comes the problem: in this Gibbs sampler, for each word seen in a document both a new topic and a sentiment label are now sampled from a conditional posterior (page 4 equation 5 of the paper).
How could I “sample those 2 values” in Python now ?

Thanks in advance…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:03:44+00:00Added an answer on June 7, 2026 at 6:03 am

    Try this. Sampling from a joint distribution over topics and sentiment labels just means that the entire T x S matrix should sum to 1.

    docs=[[0,1],[0,0],[1,0,1]]
    D=len(docs)
    z_d_n=[[0 for _ in xrange(len(d))] for d in docs]
    l_d_n=[[0 for _ in xrange(len(d))] for d in docs]
    
    V=2
    T=2
    S=2
    n_m_j_k=numpy.zeros( (V,T,S) )
    n_j_k_d=numpy.zeros( (T,S,D) )
    n_j_k=numpy.zeros( (T,S) )
    n_k_d=numpy.zeros( (S,D) )
    n_d=numpy.zeros( (D) )
    
    beta=.1
    alpha=.1
    gamma=.1
    
    for d, doc in enumerate(docs): #d: doc id
        for n, m in enumerate(doc): #i: index of the word inside document, m: id of the word in the vocabulary
            # j is the topic
            j = z_d_n[d][n]
            # k is the sentiment
            k = l_d_n[d][n]
            n_m_j_k[m][j][k] += 1
            n_j_k_d[j][k][d] += 1
            n_j_k[j][k] += 1
            n_k_d[k][d] += 1
            n_d[d] += 1 
    
    for d, doc in enumerate(docs): #d: doc id
        for n, m in enumerate(doc): #i: index of the word inside document, m: id of the word in the vocabulary
            # j is the topic
            j = z_d_n[d][n]
            # k is the sentiment
            k = l_d_n[d][n]
            n_m_j_k[m][j][k] -= 1
            n_j_k_d[j][k][d] -= 1
            n_j_k[j][k] -= 1
            n_k_d[k][d] -= 1
            n_d[d] -= 1 
    
            # sample a new topic and sentiment label jointly
            # T is the number of topics
            # S is the number of sentiments
            p_left = (n_m_j_k[m] + beta) / (n_j_k + V * beta) # T x S array
            p_mid = (n_j_k_d[:,:,d] + alpha) / numpy.tile(n_k_d[:,d] + T * alpha, (T,1) )
            p_right = numpy.tile(n_k_d[:,d] + gamma,(T,1)) /  numpy.tile(n_d[d] + S * gamma,(T,S))
            p = p_left * p_mid * p_right
            p /= numpy.sum(p)
            new_jk = numpy.random.multinomial(1, numpy.reshape(p, (T*S) )).argmax()
            j=new_jk/T
            k=new_jk%T
    
            z_d_n[d][n]=j
            l_d_n[d][n]=k
            n_m_j_k[m][j][k] += 1
            n_j_k[j][k] += 1
            n_k_d[k][d] += 1
            n_d[d] += 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently implemented password reset on AD using python ldap module. This involved passing
I have recently implemented a MIDI Beatbox from the code in Head First Java
I recently implemented a quick T4 template to generate some data access related classes
I recently implemented a program using the Microsoft Accessibility API, but have since been
I've recently implemented some vectored exception handling to catch errors in our software. This
I have recently implemented Django's excellent cache framework. However from what I understand Django
Over Christmas I implemented some code to open a channel to my App Engine
I have recently implemented an MVC3 website and had to now include data from
the PHP framework I am using (Kohana) recently implemented the HMVC architecture. I have
Recently I implemented an interface which had some 130 members that i should implement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.