Thursday, 23 July 2015

Read data from csv file, and Smooth the data by binning methods.


   Read data from csv file, and Smooth the data by binning methods.
Code:
package binning;
import java.io.BufferedReader;
import java.io.FileReader;
public class NewJFrame extends javax.swing.JFrame {
    public NewJFrame() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jLabel1.setText("No of Bin");
        jButton1.setText("Click");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(95, 95, 95)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(79, 79, 79))
            .addGroup(layout.createSequentialGroup()
                .addGap(143, 143, 143)
                .addComponent(jButton1)
                .addContainerGap(204, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(75, 75, 75)                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(68, 68, 68)
                .addComponent(jButton1)
                .addContainerGap(116, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    String csvFile = "D:/p1.csv";
     BufferedReader br;
     String line;
     String cvsSplitBy = ",";
        int value[] = new int [100];
        int i=0;
        int temp=0;
        try
        {
                br = new BufferedReader(new FileReader(csvFile));
          while ((line = br.readLine()) != null)
                {
                    value[i]=Integer.parseInt(line);
                    System.out.print(value[i]+",");
                    i++;                   
          }
                br.close();
                System.out.println("\nSort Data");
                for(int j=0;j<i;j++)
                {
                    for(int k=j+1;k<i;k++)
                    {
                        if(value[j]>value[k])
                        {
                            temp=value[j];
                            value[j]=value[k];
                            value[k]=temp;
                        }
                    }
                }
                for(int j =0;j<i;j++)
                {
                    System.out.print(value[j]+",");
                }
                int bin=Integer.parseInt(jTextField1.getText());
                int count=0;
                int b1[] = new int [10];
                int b2[] = new int[10];
                int b3[] = new int [10];
                for(int j=0;j<i;j++)
                {
                    if(count<(i/bin))
                    {
                        b1[count]=value[j];
                        count++;
                    }
                    else if(count>=(i/bin) && count<((i*2)/bin))
                    {
                        System.out.print("b2");
                         b2[count-(i/bin)]=value[j];
                         count++;
                    }
                    else
                    {
                         b3[count-(i*2/bin)]=value[j];
                         count++;
                    }
                }
                System.out.println("\nEqual Partition");
                System.out.print("Bin 1:");
                for(int j=0;j<i/bin;j++)
                    System.out.print(b1[j]+",");
                System.out.print("\nBin 2:");
                for(int j=0;j<i/bin;j++)
                    System.out.print(b2[j]+",");
                System.out.print("\nBin 3:");
                for(int j=0;j<i/bin;j++)
                    System.out.print(b3[j]+",");
                System.out.print("\n\nBining by Mean Value");
                int mean_b1 =find_mean(b1,i/bin);
                int mean_b2 =find_mean(b2,i/bin);
                int mean_b3 =find_mean(b3,i/bin);
                System.out.print("\nBin 1:"+mean_b1+","+mean_b1+","+mean_b1);
                System.out.print("\nBin 2:"+mean_b2+","+mean_b2+","+mean_b2);
                System.out.print("\nBin 3:"+mean_b3+","+mean_b3+","+mean_b3);
                 System.out.print("\n\nBinning by Median Value");
                int m_b1 =find_median(b1,i/bin);
                int m_b2 =find_median(b2,i/bin);
                int m_b3 =find_median(b3,i/bin);
                System.out.print("\nBin 1:"+m_b1+","+m_b1+","+m_b1);
                System.out.print("\nBin 2:"+m_b2+","+m_b2+","+m_b2);
                System.out.print("\nBin 3:"+m_b3+","+m_b3+","+m_b3);
                System.out.print("\n\nBinning by Boundary Value");
                System.out.print("\nBin 1:");
                for(int j=0;j<i/bin;j++)
                {
                    int a = b1[j] - b1 [0];
                    int b =b1[i/bin-1]-b1[j];
                    if(a>b)
                        System.out.print(b1[i/bin-1]+",");
                    else
                        System.out.print(b1[0]+",");
                }
                System.out.print("\nBin 2:");
                for(int j=0;j<i/bin;j++)
                {
                    int a = b2[j] - b2[0];
                    int b =b2[i/bin-1]-b2[j];
                    if(a>b)
                        System.out.print(b2[i/bin-1]+",");
                    else
                        System.out.print(b2[0]+",");
                }
                System.out.print("\nBin 3:");
                for(int j=0;j<i/bin;j++)
                {
                    int a = b3[j] - b3 [0];
                    int b =b3[i/bin-1]-b3[j];
                    if(a>b)
                        System.out.print(b3[i/bin-1]+",");
                    else
                        System.out.print(b3[0]+",");
                }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
}
int find_mean(int value[],int i)
{
    int mean;
    int sum=0;
    for(int j=0;j<i;j++)
    {
        sum=sum+value[j];
    }
    mean=Math.round(sum/i);
    return mean;
}
int find_median(int value[],int i)
{
    int median;
    if(i%2!=0)
    {
        int index=Math.round(i/2);
        median=value[index];
    }
    else
    {
        int index=i/2;
     
        median=(value[index-1]+value[index])/2;
    }
    return median;
}
    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 ex) {
         java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
        private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
}
Output: