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 bigdataproject; import java.util.List; import org.apache.commons.math3.linear.BlockRealMatrix; import org.apache.commons.math3.ml.clustering.Cluster; import org.apache.commons.math3.ml.clustering.DBSCANClusterer; import org.apache.commons.math3.ml.clustering.DoublePoint; import org.apache.commons.math3.ml.clustering.KMeansPlusPlusClusterer; import org.apache.commons.math3.ml.distance.EuclideanDistance; import org.jfree.ui.RefineryUtilities; /** * * @author raffaele */ public class MainJFrame extends javax.swing.JFrame { boolean kMeans = true; double[][] matrix; public MainJFrame() { initComponents(); } /** * 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() { jComboBox1 = new javax.swing.JComboBox(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jLabel3 = new javax.swing.JLabel(); jSpinner1 = new javax.swing.JSpinner(); jLabel4 = new javax.swing.JLabel(); jCheckBox1 = new javax.swing.JCheckBox(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jLabel7 = new javax.swing.JLabel(); jSpinner2 = new javax.swing.JSpinner(); jCheckBox2 = new javax.swing.JCheckBox(); jLabel8 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Big Data Clustering Project"); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "KMeans", "DBSCAN" })); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); jLabel1.setText("Select Clustering Algorithm: "); jLabel2.setText("Big Data Clustering Project"); jButton1.setText("Compute!"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel3.setText("KMeans Options:"); jSpinner1.setModel(new javax.swing.SpinnerNumberModel(2, 2, 30, 1)); jLabel4.setText("Value of k"); jCheckBox1.setText("Auto"); jCheckBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jCheckBox1ActionPerformed(evt); } }); jLabel5.setText("DBScan Options:"); jLabel6.setText("Value of eps:"); jTextField1.setText("0.0"); jTextField1.setEnabled(false); jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField1ActionPerformed(evt); } }); jLabel7.setText("Value of mPnt:"); jSpinner2.setModel(new javax.swing.SpinnerNumberModel(2, 1, 10, 1)); jSpinner2.setEnabled(false); jCheckBox2.setText("Auto"); jCheckBox2.setEnabled(false); jCheckBox2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jCheckBox2ActionPerformed(evt); } }); jLabel8.setForeground(new java.awt.Color(246, 9, 9)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(jLabel1).addGap( 18, 18, 18).addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3).addComponent(jLabel5) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel6).addComponent(jLabel7)) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup().addGap(18, 18, 18) .addComponent(jTextField1)) .addGroup(layout.createSequentialGroup().addGap(18, 18, 18) .addComponent(jSpinner2, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)))) .addGroup(layout.createSequentialGroup() .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(35, 35, 35) .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(29, 29, 29).addComponent(jCheckBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(jCheckBox2) .addGap(67, 67, 67)) .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap()) .addGroup(layout.createSequentialGroup().addGap(91, 91, 91).addComponent(jLabel2) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(18, 18, 18).addComponent(jLabel2).addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(jCheckBox1).addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(13, 13, 13).addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jCheckBox2)) .addGap(11, 11, 11) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel7) .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton1).addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(16, Short.MAX_VALUE))); pack(); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed this.jSpinner1.setEnabled(!this.jCheckBox1.isSelected()); }//GEN-LAST:event_jCheckBox1ActionPerformed private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox2ActionPerformed this.jLabel8.setText(""); this.jSpinner2.setEnabled(!this.jCheckBox2.isSelected()); this.jTextField1.setEnabled(!this.jCheckBox2.isSelected()); }//GEN-LAST:event_jCheckBox2ActionPerformed private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jTextField1ActionPerformed private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed this.jLabel8.setText(""); this.jCheckBox1.setSelected(false); this.jCheckBox2.setSelected(false); kMeans = this.jComboBox1.getSelectedIndex() == 0; if (kMeans) { this.jSpinner1.setEnabled(true); this.jCheckBox1.setEnabled(true); this.jTextField1.setEnabled(false); this.jSpinner2.setEnabled(false); this.jCheckBox2.setEnabled(false); } else { this.jSpinner1.setEnabled(false); this.jCheckBox1.setEnabled(false); this.jTextField1.setEnabled(true); this.jSpinner2.setEnabled(true); this.jCheckBox2.setEnabled(true); } }//GEN-LAST:event_jComboBox1ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed this.jLabel8.setText(""); ReadDataSet read = new ReadDataSet(); read.readFromFile(); read.filter(); matrix = read.getMatrix(); PCA pca = new PCA(matrix); double[][] matrix2DPCA = pca.reduceDimensions(); BlockRealMatrix pcaMatrix = new BlockRealMatrix(matrix2DPCA); BlockRealMatrix pcaMatrixTranspose = pcaMatrix.transpose(); List<DoublePoint> list = read.getCollection(read.getHashMap(pcaMatrixTranspose.getData())); List<Cluster<DoublePoint>> clusterList; if (kMeans) { int k; if (this.jCheckBox1.isSelected()) { KMeansKFinder kFinder = new KMeansKFinder(list); k = kFinder.find(0.15); } else k = (int) this.jSpinner1.getValue(); KMeansPlusPlusClusterer kmeans = new KMeansPlusPlusClusterer(k, 1000, new EuclideanDistance()); clusterList = kmeans.cluster(list); } else { int minPts; double eps; if (this.jCheckBox2.isSelected()) { minPts = 6; //KDistances dist = new KDistances(pcaMatrixTranspose.getData()); //dist.calculateDistances(); //dist.getKSortedNearestNeighbors(minPts); //dist.printKdistances(); eps = 1.0; } else { minPts = (int) this.jSpinner2.getValue(); try { eps = Double.parseDouble(this.jTextField1.getText()); } catch (NumberFormatException e) { this.jLabel8.setText("Wrong eps Value"); return; } } DBSCANClusterer dbscan = new DBSCANClusterer(eps, minPts); clusterList = dbscan.cluster(list); } final ScatterPlot demo = new ScatterPlot("Big Data Clustering Project", matrix2DPCA, clusterList); demo.pack(); RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true); }//GEN-LAST:event_jButton1ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); System.err.println(ex); } java.awt.EventQueue.invokeLater(() -> { new MainJFrame().setVisible(true); }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JCheckBox jCheckBox1; private javax.swing.JCheckBox jCheckBox2; private javax.swing.JComboBox jComboBox1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JSpinner jSpinner1; private javax.swing.JSpinner jSpinner2; private javax.swing.JTextField jTextField1; // End of variables declaration//GEN-END:variables }