| 108 | return gamma0, gamma1 |
| 109 | |
| 110 | def M_step(muo, mu1, gamma0, gamma1, dataSetArr): |
| 111 | #依据算法9.2计算各个值 |
| 112 | #这里没什么花样,对照书本公式看看这里就好了 |
| 113 | mu0_new = np.dot(gamma0, dataSetArr) / np.sum(gamma0) |
| 114 | mu1_new = np.dot(gamma1, dataSetArr) / np.sum(gamma1) |
| 115 | |
| 116 | sigmod0_new = math.sqrt(np.dot(gamma0, (dataSetArr - muo)**2) / np.sum(gamma0)) |
| 117 | sigmod1_new = math.sqrt(np.dot(gamma1, (dataSetArr - mu1)**2) / np.sum(gamma1)) |
| 118 | |
| 119 | alpha0_new = np.sum(gamma0) / len(gamma0) |
| 120 | alpha1_new = np.sum(gamma1) / len(gamma1) |
| 121 | |
| 122 | #将更新的值返回 |
| 123 | return mu0_new, mu1_new, sigmod0_new, sigmod1_new, alpha0_new, alpha1_new |
| 124 | |
| 125 | |
| 126 | def EM_Train(dataSetList, iter = 500): |