em 算法 java_java - EM算法

本文介绍了一段用Java编写的机器翻译程序,主要展示了如何使用EM算法计算掷硬币的概率,并通过JTable动态更新期望值。代码中包括文本输入、计算步骤和期望值表格的生成。

1 importjava.awt.Dimension;2 importjava.awt.EventQueue;3 importjava.awt.Toolkit;4 importjava.awt.event.ActionEvent;5 importjava.awt.event.ActionListener;6 importjava.util.ArrayList;7

8 importjava.lang.Math;9 importjava.text.DecimalFormat;10

11 importjavax.swing.JButton;12 importjavax.swing.JFrame;13 importjavax.swing.JLabel;14 importjavax.swing.JScrollPane;15 importjavax.swing.JTable;16 importjavax.swing.JTextField;17

18

19 public classMachineTranslation{20 private static final long serialVersionUID = 2904270580467455923L;21 public static voidmain(String[] args) {22 EventQueue.invokeLater(newRunnable() {23 public voidrun() {24 Display frame = newDisplay();25 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);26 frame.setVisible(true);27 }28 });29 }30 }31

32 class Display extendsJFrame {33 private static final int DEFAULT_WIDTH = 532;34 private static final int DEFAULT_HEIGHT = 508;35

36 privateJTextField textField_3;37 privateJTextField textField_2;38 privateJTextField textField_1;39 private static final long serialVersionUID = -2794537679802534502L;40

41 privateJTable table;42 privateJTextField textField;43 private finalJButton emButton;44

45 publicDisplay() {46 super();47 getContentPane().setLayout(null);48 setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);49

50

51 textField = new JTextField(",,,");52 textField.setBounds(61, 42, 237, 33);53 getContentPane().add(textField);54

55 emButton = newJButton();56 emButton.setText("EM");57 emButton.setBounds(344, 44, 106, 28);58 getContentPane().add(emButton);59

60

61 final JLabel wLabel = newJLabel();62 wLabel.setText("λ");63 wLabel.setBounds(61, 98, 25, 18);64 getContentPane().add(wLabel);65

66 final JLabel p1Label = newJLabel();67 p1Label.setText("P1");68 p1Label.setBounds(198, 98, 25, 18);69 getContentPane().add(p1Label);70

71 final JLabel p2Label = newJLabel();72 p2Label.setText("P2");73 p2Label.setBounds(349, 98, 25, 18);74 getContentPane().add(p2Label);75

76 textField_1 = newJTextField();77 textField_1.setText("0.3");78 textField_1.setBounds(86, 96, 87, 22);79 getContentPane().add(textField_1);80

81 textField_2 = newJTextField();82 textField_2.setText("0.3");83 textField_2.setBounds(229, 96, 87, 22);84 getContentPane().add(textField_2);85

86 textField_3 = newJTextField();87 textField_3.setText("0.6");88 textField_3.setBounds(380, 96, 87, 22);89 getContentPane().add(textField_3);90

91 setTitle("掷硬币EM算法之实现");92

93 Toolkit tk =this.getToolkit();//得到窗口工具条

94 Dimension dm =tk.getScreenSize();95 this.setLocation((int)(dm.getWidth()-DEFAULT_WIDTH)/2,(int)(dm.getHeight()-DEFAULT_HEIGHT)/2);//显示在屏幕中央

96

97

98 emButton.addActionListener(newActionListener() {99 public voidactionPerformed(ActionEvent event) {100 EmAlgorithm em = newEmAlgorithm(textField.getText(),textField_1.getText(),textField_2.getText(),textField_3.getText());101 em.maximizeExpectation();102 final JScrollPane scrollPane = newJScrollPane();103 scrollPane.setBounds(37, 121, 453, 261);104 getContentPane().add(scrollPane);105

106 table= newJTable(em.getCells(),em.getColumnNames());107 scrollPane.setViewportView(table);108 System.out.println(1E-300-1E-301>0);109 }110 });111 }112 }113

114 classEmAlgorithm{115 privateString str;116 private ArrayList sub=new ArrayList();117 private ArrayList w=new ArrayList();118 private ArrayList P1=new ArrayList();119 private ArrayList P2=new ArrayList();120 private ArrayList>p;121

122 privateObject[][] cells;123 privateString[] columnNames;124

125 publicEmAlgorithm(String str, String str1,String str2, String str3){126 this.str=str;127 w.add(Double.parseDouble(str1));128 P1.add(Double.parseDouble(str2));129 P2.add(Double.parseDouble(str3));130

131 textSplit();132 p=new ArrayList>();133 for(int j = 0; j < sub.size(); ++j)134 p.add(new ArrayList());135 System.out.println(p);136 }137 public voidtextSplit(){138 String[] sList;139 sList=str.substring(1,str.length()-1).split(">,

141 for (int i = 0; i < sList.length; i++){142 sub.add(newSub(sList[i]));143 }144 System.out.println(sub);145 }146 public voidmaximizeExpectation(){147 int iterate=0;148 if (!P1.get(P1.size() - 1).equals(P2.get(P2.size() - 1))) {149 do{150 iterate++;151 compute();152 } while (Math.abs(P1.get(P1.size() - 1) - P1.get(P1.size() - 2)) > 0.0000001);153 //}while(Math.abs(P1.get(P1.size()-1)-P1.get(P1.size()-2))>1.E-300);

154 }155 else{156 iterate=7;157 for(int i=0;i

163 DecimalFormat df_t=new DecimalFormat("0.0000");164

165 for(int i=0;i

187 for (int i = 0; i < sub.size(); i++) {188 float PP1 = (float) Math.pow(P1, sub.get(i).getH())189 * (float) Math.pow(1 -P1,sub.get(i).getT());190 float PP2 = (float) Math.pow(P2, sub.get(i).getH())191 * (float) Math.pow(1 -P2, sub.get(i).getT());192 p.get(i).add(w * PP1 / (w * PP1 + (1 - w) *PP2));193 }194 double sump=0;195 double sumP1=0;196 double sumP2=0;float sumpp=0;197

198 for(int i=0;i

219 publicCountHT(String str){220 char[] temp=str.toCharArray();221 for(int j=0;j

241 classSub{242 privateString str;243 private inthCount;244 private inttCount;245

246 publicSub(String str){247 this.str=str;248 CountHT countHT=newCountHT(str);249 hCount=countHT.getH();250 tCount=countHT.getT();251 }252 publicString toString(){253 return str+" "+"hCount="+hCount+" "+"tCount="+tCount;254 }255 public intgetH(){256 returnhCount;257 }258 public intgetT(){259 returntCount;260 }261 }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值