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.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javax.swing.JTextField; import view.main.MainGUI; import weka.clusterers.ClusterEvaluation; import weka.clusterers.FilteredClusterer; import weka.clusterers.SimpleKMeans; import weka.core.Instance; import weka.core.Instances; import weka.filters.unsupervised.attribute.Remove; /** * * @author Stupi */ public class ClusteringPredictPnlCenter extends javax.swing.JPanel { private ArrayList<JTextField> fields; private SimpleKMeans kMeans; /** * Creates new form ClusteringPredictPnlCenter */ public ClusteringPredictPnlCenter(SimpleKMeans simpleKMeans) { initComponents(); kMeans = simpleKMeans; fields = new ArrayList<>(); Instances instances = Data.getInstance().getInstances(); ClusteringAttributesPnl pnlAtt = new ClusteringAttributesPnl(instances, fields); pnlAttributes.add(pnlAtt); setSize(MainGUI.getInstance().getPnlCenter().getWidth(), MainGUI.getInstance().getPnlCenter().getHeight()); setVisible(true); } /** * 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() { jLabel1 = new javax.swing.JLabel(); pnlAttributes = new javax.swing.JPanel(); jLabel3 = new javax.swing.JLabel(); btnStart = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); textAreaResult = new javax.swing.JTextArea(); jLabel1.setText("Make prediction on the currently loaded and clustered data set."); javax.swing.GroupLayout pnlAttributesLayout = new javax.swing.GroupLayout(pnlAttributes); pnlAttributes.setLayout(pnlAttributesLayout); pnlAttributesLayout.setHorizontalGroup(pnlAttributesLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 266, Short.MAX_VALUE)); pnlAttributesLayout.setVerticalGroup(pnlAttributesLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 193, Short.MAX_VALUE)); jLabel3.setText("Enter the following attributes:"); btnStart.setText("Start"); btnStart.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnStartActionPerformed(evt); } }); textAreaResult.setColumns(20); textAreaResult.setRows(5); jScrollPane1.setViewportView(textAreaResult); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(22, 22, 22) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(pnlAttributes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(29, 29, 29)) .addGroup(layout.createSequentialGroup() .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 492, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(145, Short.MAX_VALUE))))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(25, 25, 25).addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(pnlAttributes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE) .addComponent(btnStart).addContainerGap())); }// </editor-fold>//GEN-END:initComponents private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed Instances test = new Instances(Data.getInstance().getInstances()); test.delete(); //proverava da li su dobro unete vrednosti //ako nesto nije doro uneseno nekaa iskoci JoptionPane //sta je lose uneseno, naziv aributa recimo for (int i = 0; i < fields.size(); i++) { String text = fields.get(i).getText().trim(); //prekace prazna pollja jer za klasterizaciju znaci da se ona ignorisu //to za klasifikaciju nije slucaj if (!text.equals("")) { if (test.attribute(i).isNominal()) { boolean correct = false; for (int j = 0; j < test.attribute(i).numValues(); j++) { if (text.equals(test.attribute(i).value(j))) { correct = true; } } if (!correct) { JOptionPane.showMessageDialog(this, "Incorrect format for attribute " + test.attribute(i).name()); break; } } if (test.attribute(i).isNumeric()) { try { double value = Double.parseDouble(text); } catch (Exception e) { JOptionPane.showMessageDialog(this, "Incorrect format for attribute " + test.attribute(i).name()); break; } } } } int numAttributes = test.numAttributes(); Instance instance = new Instance(numAttributes); //ovaj remove je potreban samo zaklasterizaciju String remove = ""; boolean hasRemove = false; for (int i = 0; i < fields.size(); i++) { String text = fields.get(i).getText().trim(); //vama ne sme da se pojavi prazan string if (text.equals("")) { remove = remove + (i + 1) + ","; hasRemove = true; } else { try { double value = Double.parseDouble(text); instance.setValue(i, value); } catch (Exception e) { instance.setValue(i, text); } } } if (hasRemove) { remove = remove.substring(0, remove.length() - 1); } //meni se InstanceS zove test a vama instances, ovako se dodaje ta jedna instanca test.add(instance); //sad radite vasu evaluaciju ovo je klaserizacija ostalo Remove removeFilter = new Remove(); removeFilter.setAttributeIndices(remove); FilteredClusterer filteredClusterer = new FilteredClusterer(); try { filteredClusterer.setClusterer(kMeans); filteredClusterer.setFilter(removeFilter); filteredClusterer.buildClusterer(Data.getInstance().getInstances()); } catch (Exception e) { } ClusterEvaluation eval = new ClusterEvaluation(); eval.setClusterer(filteredClusterer); try { eval.evaluateClusterer(test); } catch (Exception ex) { Logger.getLogger(ClusteringPredictPnlCenter.class.getName()).log(Level.SEVERE, null, ex); } String[] results = eval.clusterResultsToString().split("\n"); String cluster = results[results.length - 1].split(" ")[0]; textAreaResult.setText("This instance belongs to \ncluster number: " + cluster + ".\n\n" + "Take a look on visualization \nfor better feeleing about \nthis instance"); test.delete(); }//GEN-LAST:event_btnStartActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnStart; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JPanel pnlAttributes; private javax.swing.JTextArea textAreaResult; // End of variables declaration//GEN-END:variables }