Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package view.centerPanels; import controller.Data; import java.io.BufferedWriter; import java.io.FileWriter; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import model.clasification.ClassificationIO; import view.main.MainGUI; import weka.classifiers.Evaluation; import weka.classifiers.bayes.NaiveBayes; import weka.core.Instances; import weka.core.SerializationHelper; import weka.core.converters.ConverterUtils.DataSource; import weka.core.SerializationHelper; /** * * @author Dusan Dudukovic */ public class ClassificationPnlCenterTrainNew extends javax.swing.JPanel { /** * Creates new form ClassificationPnlCenter2 */ private NaiveBayes nb; public ClassificationPnlCenterTrainNew() { initComponents(); Instances data = Data.getInstance().getInstances(); try { nb = new NaiveBayes(); data.setClassIndex(data.numAttributes() - 1); Evaluation eval = new Evaluation(data); nb.buildClassifier(data); eval.evaluateModel(nb, data); jTextArea1.setText(eval.toMatrixString()); // System.out.println(eval.toMatrixString()); } catch (Exception ex) { JOptionPane.showMessageDialog(this, ex); } setSize(MainGUI.getInstance().getPnlCenter().getWidth(), MainGUI.getInstance().getPnlCenter().getHeight()); setVisible(true); } public NaiveBayes getNb() { return nb; } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jBtnSaveModel = new javax.swing.JButton(); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); jBtnSaveModel.setText("Save model"); jBtnSaveModel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jBtnSaveModelActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addComponent(jBtnSaveModel).addGap(0, 0, Short.MAX_VALUE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE) .addComponent(jBtnSaveModel).addContainerGap())); }// </editor-fold>//GEN-END:initComponents private void jBtnSaveModelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSaveModelActionPerformed try { String fileName = JOptionPane.showInputDialog(this, " Enter model name: "); boolean ok = ClassificationIO.updateTxtFile(fileName); if (!ok) { JOptionPane.showMessageDialog(this, "This name already exist.\nTry with another name."); return; } Instances instances = Data.getInstance().getInstances(); ClassificationIO.writeInstanceObject(fileName, instances); ClassificationIO.writeNaiveBayesObject(fileName, nb); JOptionPane.showMessageDialog(this, "Successful"); } catch (Exception e) { JOptionPane.showMessageDialog(this, e); } }//GEN-LAST:event_jBtnSaveModelActionPerformed private Object makeObj(final String item) { return new Object() { public String toString() { return item; } }; } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jBtnSaveModel; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; // End of variables declaration//GEN-END:variables }