Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does python simulate EM algorithm

Shulou Source: shulou.com Published: 2022-06-01 20:59:24 09月19日 Update

This article introduces the relevant knowledge of "how python simulates the EM algorithm". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The first example of the simulation textbook is to use the EM algorithm to estimate the parameters of the three coin models.

The question is introduced: there are three coins: a, B, C. Now first throw A, according to the results of A to choose, A for the front to choose B, A for the negative to choose C. Then toss it with the selected coin. Results [1,1,0,1,0,0,1,1,1]. Question: how to estimate the probability of three coins appearing on the front of the coin, that is, the parameters of the three coin model, under the premise that the coin flip result can only be observed, but the process cannot be observed.

EM algorithm solution code:

Import numpy as npimport mathclass EM: def _ init__ (self, prob): self.pro_A, self.pro_B, self.pro_C = prob # e_step def pmf (self, I): pro_1 = self.pro_A * math.pow (self.pro_B, data [I]) * math.pow ((1-self.pro_B)) 1-data [I]) pro_2 = (1-self.pro_A) * math.pow (self.pro_C, data [I]) * math.pow ((1-self.pro_C), 1-data [I]) return pro_1 / (pro_1 + pro_2) # m_step def fit (self, data): count = len (data) print ('init prob: {}, {}) {} '.format (self.pro_A, self.pro_B Self.pro_C)) for d in range (count): # _ = yield # highly recommended for yield: https://developer.ibm.com/zh/articles/os-cn-python-yield/ _ pmf = [self.pmf (k) for k in range (count)] pro_A = 1 / count * sum (_ pmf) pro_B = sum ([_ PMF [k] * data [k] for k in range (count)]) / sum ([_ PMF [k] for k in range (count)]) pro_C = sum ([(1-_ pmf [k]) * data [k] for k in range (count)])) / sum ([(1-_ pmf [k]) for k in range (count)]) print ('{} / {} pro_a: {: .3f} Pro_b: {: .3f}, pro_c: {: .3f} '.format (d + 1, count, pro_A, pro_B, pro_C) self.pro_A = pro_A self.pro_B = pro_B self.pro_C = pro_Cdata = [1, 1, 0, 1, 0, 0, 1, 1] # because the EM algorithm is related to the initial value There are two different sets of initial values to experience # the first group # em = EM (prob= [0.5,0.5,0.5]) # f = em.fit (data) # if you need the following code # next (f) # to execute to the yield card master above with yield#, that is, only initialization # next (f) # is completed and continues until the next yield. That is, an iteration was completed # the second group em = EM (prob= [0.4,0.6,0.7]) f2 = em.fit (data)

Results:

When not using yield:

Init prob:0.4, 0.6, 0.71/10 pro_a:0.406, pro_b:0.537, pro_c:0.6432/10 pro_a:0.406, pro_b:0.537, pro_c:0.6433/10 pro_a:0.406, pro_b:0.537, pro_c:0.6434/10 pro_a:0.406, pro_b:0.537, pro_c:0.6435/10 pro_a:0.406, pro_b:0.537 Pro_c:0.6436/10 pro_a:0.406, pro_b:0.537, pro_c:0.6437/10 pro_a:0.406, pro_b:0.537, pro_c:0.6438/10 pro_a:0.406, pro_b:0.537, pro_c:0.6439/10 pro_a:0.406, pro_b:0.537, pro_c:0.64310/10 pro_a:0.406, pro_b:0.537, pro_c:0.643

When using yield, output the above statement one by one.

This is the end of the content of "how python simulates EM algorithm". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Coins algorithms three results code content parameters more models fronts knowledge processes problems facets relevance observation output different practical learned Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Microsoft Apple Shulou Information Linux