标签:
杂谈:当年未竟之遗憾,小段代码笔记。当时苦思冥想未解当中奥妙,如今轻松信手拈来,丝毫不费力气。感叹机缘未到纵使踏破铁鞋也无用,转角回眸,那人却在灯火阑珊处。
package plsa; public class Plsa { double[][][] Pz_dw; double[][] Pz_d; double[][] Pw_z; int[][] nd_w; int kz, kd, kw; public Plsa(int kz, int kd, int kw, int[][] nd_w) { this.kz = kz; this.kd = kd; this.kw = kw; this.Pz_dw = new double[kz][kd][kw]; this.Pz_d = new double[kz][kd]; this.Pw_z = new double[kw][kz]; for (int i = 0; i < kz; i++) { for (int j = 0; j < kd; j++) { for (int k = 0; k < kw; k++) { this.Pz_dw[i][j][k] = (double) 1 / kz / kd / kw; } } } for (int i = 0; i < kz; i++) { for (int j = 0; j < kd; j++) { this.Pz_d[i][j] = (double) 1 / kz / kd; } } for (int i = 0; i < kw; i++) { for (int j = 0; j < kz; j++) { this.Pw_z[i][j] = (double) 1 / kw / kz; } } this.nd_w = nd_w; } public void run() { for (int i = 0; i < 10; i++) { MStep(); } for (int i = 0; i < kz; i++) { for (int j = 0; j < kd; j++) { System.out.printf("%.2f\t", Pz_d[i][j]); } System.out.println(); } } public void EStep() { for (int z = 0; z < kz; z++) { for (int d = 0; d < kd; d++) { for (int w = 0; w < kw; w++) { double Pt = 0; for (int ptz = 0; ptz < kz; ptz++) { Pt += Pz_d[ptz][d] * Pw_z[w][ptz]; } Pz_dw[z][d][w] = Pz_d[z][d] * Pw_z[w][z] / Pt; } } } } public void MStep() { for (int w = 0; w < kw; w++) { for (int z = 0; z < kz; z++) { double Pta = 0; for (int id = 0; id < kd; id++) { Pta += nd_w[id][w] * Pz_dw[z][id][w]; } double Ptb = 0; for (int id = 0; id < kd; id++) { for (int iw = 0; iw < kw; iw++) { Ptb += nd_w[id][iw] * Pz_dw[z][id][iw]; } } Pw_z[w][z] = Pta / Ptb; EStep(); } } for (int z = 0; z < kz; z++) { for (int d = 0; d < kd; d++) { double Pta = 0; for (int iw = 0; iw < kw; iw++) { Pta += nd_w[d][iw] * Pz_dw[z][d][iw]; } double Ptb = 0; for (int iw = 0; iw < kw; iw++) { for (int iz = 0; iz < kz; iz++) { Ptb += nd_w[d][iw] * Pz_dw[iz][d][iw]; } } Pz_d[z][d] = Pta / Ptb; EStep(); } } } }
标签:
原文地址:http://my.oschina.net/keyven/blog/512500