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 GUI; import SVM.Predict; import static SVM.Predict.data; import static SVM.Predict.error; import static SVM.Predict.mse; import static SVM.Predict.scc; import static SVM.Predict.sumv; import static SVM.Predict.sumvv; import static SVM.Predict.sumvy; import static SVM.Predict.sumy; import static SVM.Predict.sumyy; import static SVM.Predict.targets; import static SVM.Predict.total; import com.opencsv.CSVReader; import java.awt.CardLayout; import java.awt.Dimension; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import libsvm.svm; import libsvm.svm_model; import libsvm.svm_parameter; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.xy.XYDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; /** * * @author User */ public class Starts extends javax.swing.JFrame { private String splitArray[] = new String[844]; private double doubleArray[] = new double[844]; private String splitTarget[] = new String[844]; private double doubleTarget[] = new double[844]; DefaultTableModel tableModel = new DefaultTableModel(); /** * Creates new form Starts */ public Starts() throws FileNotFoundException, IOException { CSVReader reader = new CSVReader(new FileReader("data_astra.csv")); List myEntries = reader.readAll(); String[] columnnames = (String[]) myEntries.get(0); tableModel = new DefaultTableModel(columnnames, myEntries.size() - 1); int rowcount = tableModel.getRowCount(); for (int x = 0; x < rowcount + 1; x++) { int columnnumber = 0; // if x = 0 this is the first row...skip it... data used for columnnames if (x > 0) { for (String thiscellvalue : (String[]) myEntries.get(x)) { tableModel.setValueAt(thiscellvalue, x - 1, columnnumber); columnnumber++; } } } initComponents(); predict(); initGraphs(); this.setLocationRelativeTo(null); // CSVReader reader = new CSVReader(new FileReader("yourfile.csv")); // myEntries = reader.readAll(); } private void predict() { int i, predict_probability = 0; svm_parameter param = new svm_parameter(); param.svm_type = svm_parameter.EPSILON_SVR; param.kernel_type = svm_parameter.LINEAR; param.coef0 = 0; param.degree = 3; param.gamma = 0; param.nu = 0.5; param.cache_size = 40; param.eps = 1e-3; param.p = 0.1; param.shrinking = 1; param.probability = 0; try { BufferedReader input = new BufferedReader(new FileReader("astra.test.data")); DataOutputStream output = new DataOutputStream( new BufferedOutputStream(new FileOutputStream("astra.out"))); svm_model model = svm.svm_load_model("astra.model"); if (predict_probability == 1) { if (svm.svm_check_probability_model(model) == 0) { System.err.print("Model does not support probabiliy estimates\n"); System.exit(1); } } else if (svm.svm_check_probability_model(model) != 0) { System.out.print("Model supports probability estimates, but disabled in prediction.\n"); } Predict.predict(input, output, model, predict_probability); input.close(); output.close(); } catch (FileNotFoundException e) { System.out.println("File doesnt exitst"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Array out of bounds"); } catch (IOException e) { System.out.println("Something went wrong"); } // System.out.println(data); splitArray = data.split(","); for (int j = 0; j < splitArray.length; j++) { doubleArray[j] = Double.parseDouble(splitArray[j]); } splitTarget = targets.replace("null", "").split(","); for (int j = 0; j < splitTarget.length; j++) { doubleTarget[j] = Double.parseDouble(splitTarget[j]); } System.out.println(doubleTarget[3]); } private void initGraphs() { XYDataset datasets = createDataset(); JFreeChart chart = ChartFactory.createXYLineChart("", "Periods", "Price", datasets, PlotOrientation.VERTICAL, true, true, false); ChartPanel cp = new ChartPanel(chart); cp.setMouseWheelEnabled(true); cp.setSize(jPanel1.getWidth(), jPanel1.getHeight()); cp.setVisible(true); jPanel1.add(cp); jPanel1.validate(); } private XYSeriesCollection createDataset() { XYSeries Predicted = new XYSeries("Predicted"); XYSeries Actual = new XYSeries("Actual"); XYSeriesCollection datasets = new XYSeriesCollection(); for (int i = 0; i < doubleArray.length - 1; i++) { Predicted.add(i, doubleArray[i]); } for (int i = 0; i < doubleTarget.length - 1; i++) { Actual.add(i, doubleTarget[i]); } datasets.addSeries(Predicted); datasets.addSeries(Actual); return datasets; } /** * 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() { jPanel2 = new javax.swing.JPanel(); jPanel3 = new javax.swing.JPanel(); jLabel3 = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); jComboBox1 = new javax.swing.JComboBox<>(); jButton1 = new javax.swing.JButton(); jPanel4 = new javax.swing.JPanel(); jPanel1 = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jButton2 = new javax.swing.JButton(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); jLabel11 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel2.setLayout(new java.awt.CardLayout()); jLabel3.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N jLabel3.setText("StockVR"); jLabel1.setText("Predicting stocks with SVM"); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>( new String[] { "ASII.JK Astra International Tbk., Jakarta Stock Exchange" })); jButton1.setText("Get Data"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(169, 169, 169) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(28, 28, 28) .addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3).addComponent(jLabel1))) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel3Layout.createSequentialGroup().addGap(31, 31, 31).addComponent( jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(214, Short.MAX_VALUE))); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(142, 142, 142).addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jLabel1) .addGap(18, 18, 18) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton1).addContainerGap(231, Short.MAX_VALUE))); jPanel2.add(jPanel3, "card2"); jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder()); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE)); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 250, Short.MAX_VALUE)); jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel2.setText("ASII.JK Astra International Tbk., Jakarta Stock Exchange"); jLabel4.setText("MSE: "); jLabel5.setText("Standard Deviation:"); jLabel7.setText("Size:"); jButton2.setText("Return"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jLabel8.setText("jLabel8"); jLabel9.setText("jLabel9"); jLabel11.setText("jLabel11"); jTable1.setModel(tableModel); jScrollPane1.setViewportView(jTable1); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addContainerGap().addGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel4Layout.createSequentialGroup().addGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addComponent(jLabel7).addGap(321, 321, 321)) .addGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel11) .addGroup(jPanel4Layout.createSequentialGroup().addGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)) .addGap(34, 34, 34) .addGroup(jPanel4Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel9).addComponent(jLabel8))))) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(jPanel4Layout.createSequentialGroup().addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2))) .addContainerGap()) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)); jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(jButton2)) .addGap(18, 18, 18) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(jLabel8)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5).addComponent(jLabel9)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel7).addComponent(jLabel11)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))); jPanel2.add(jPanel4, "card3"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed CardLayout cardLayout = (CardLayout) (jPanel2.getLayout()); cardLayout.next(jPanel2); double size = doubleArray.length; jLabel8.setText(String.valueOf(mse)); jLabel9.setText(String.valueOf(scc)); jLabel11.setText(String.valueOf(size)); }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed CardLayout cardLayout = (CardLayout) (jPanel2.getLayout()); cardLayout.previous(jPanel2); }//GEN-LAST:event_jButton2ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ 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(Starts.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Starts.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Starts.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Starts.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { try { new Starts().setVisible(true); } catch (IOException ex) { Logger.getLogger(Starts.class.getName()).log(Level.SEVERE, null, ex); } } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JComboBox<String> jComboBox1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; // End of variables declaration//GEN-END:variables }