-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReadCSV.java
More file actions
1498 lines (1169 loc) · 44.7 KB
/
ReadCSV.java
File metadata and controls
1498 lines (1169 loc) · 44.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.lang.reflect.Array;
import java.util.Arrays;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.jfree.ui.RefineryUtilities;
import weka.classifiers.Classifier;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.functions.LinearRegression;
import weka.classifiers.lazy.LWL;
import weka.classifiers.meta.AdaBoostM1;
import weka.classifiers.rules.OneR;
import weka.classifiers.trees.DecisionStump;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import java.text.*;
public class ReadCSV {
static double graphNum =95;
static boolean meanchoice = false;
static boolean medianchoice = false;
static boolean modechoice = false;
static boolean rangechoice = false;
static boolean minchoice = false;
static boolean maxchoice = false;
static boolean iqrchoice = false;
static boolean outlierchoice = false;
static boolean correlationchoice = false;
static boolean sdchoice = false;
static boolean variancechoice = false;
static int[] currentData;
public static void main(String[] args) {
// String csvFile = "gfa25.csv";
// String csvFile2 = "us_state_emplchange_2011-2012.txt";
//String csvFile3 = "test-3.csv";
// int max=10000;
// DecimalFormat df = new DecimalFormat("#.###");
JFrame jf = new JFrame("Menu");
final JFileChooser fc = new JFileChooser();
//final File selectedFile;
//JFrame.setDefaultLookAndFeelDecorated(true);
//JDialog.setDefaultLookAndFeelDecorated(true);
jf.setSize(600, 300);
jf.setLocation(200, 200);
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
JPanel attachPanel = new JPanel();
JButton attachButton = new JButton("Select File");
attachPanel.add(attachButton);
JPanel optionPanel = new JPanel();
final ButtonGroup choicegroup = new ButtonGroup();
JRadioButton radioButton;
optionPanel.add(radioButton = new JRadioButton("Graph"));
radioButton.setActionCommand("Graph");
choicegroup.add(radioButton);
final JPanel statPanel = new JPanel();
statPanel.add(new JCheckBox("Mean"));
statPanel.add(new JCheckBox("Median"));
statPanel.add(new JCheckBox("Mode"));
statPanel.add(new JCheckBox("Range"));
statPanel.add(new JCheckBox("Min"));
statPanel.add(new JCheckBox("Max"));
statPanel.add(new JCheckBox("Interquartile Range"));
statPanel.add(new JCheckBox("Outliers"));
statPanel.add(new JCheckBox("Correlation"));
statPanel.add(new JCheckBox("Standard Deviation"));
statPanel.add(new JCheckBox("Variance"));
JPanel confirmPanel = new JPanel();
JButton confirmButton = new JButton("Analyze");
confirmPanel.add(confirmButton);
Container content = jf.getContentPane();
content.setLayout(new GridLayout(1, 1));
content.add(attachPanel);
content.add(optionPanel);
content.add(statPanel);
content.add(confirmPanel);
attachButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int value = fc.showOpenDialog(null);
if (value == JFileChooser.APPROVE_OPTION) {
File selectedFile = fc.getSelectedFile();
//String response = choicegroup.getSelection().getActionCommand();
//System.out.println("You selected " + response);
Component[] components = statPanel.getComponents();
for(int i=0; i<components.length; i++) {
JCheckBox cb = (JCheckBox) components[i];
if(cb.isSelected()) {
//System.out.println("Also selected: " + cb.getText());
if(cb.getText().equals("Mean")) {
System.out.println("I may never make it to this point.");
meanchoice = true;
}
if(cb.getText().equals("Median")) {
medianchoice = true;
}
if(cb.getText().equals("Mode")) {
modechoice = true;
}
if(cb.getText().equals("Range")) {
rangechoice = true;
}
if(cb.getText().equals("Min")) {
minchoice = true;
}
if(cb.getText().equals("Max")) {
maxchoice = true;
}
if(cb.getText().equals("Interquartile Range")) {
iqrchoice = true;
}
if(cb.getText().equals("Outliers")) {
outlierchoice = true;
}
if(cb.getText().equals("Correlation")) {
correlationchoice = true;
}
if(cb.getText().equals("Standard Deviation")) {
sdchoice = true;
}
if(cb.getText().equals("Variance")) {
variancechoice = true;
}
}
}
try {
convert(selectedFile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(selectedFile.getName());
}
}
});
confirmButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
}
});
jf.setVisible(true);
// for (int x = 0; x < list2.length; x++) {
// System.out.println(Arrays.toString(list2[x]));
// }
//File csvFile3 = fc.getSelectedFile();
//char[] type1 = checkType(list1);
//char[] type2 = checkType(list2);
//double[][] dList1 = to2dDouble(list1, type1);
//double[][] dList2 = to2dDouble(list2, type2);
/*
char[] type3 = checkType(list3);
double[][] dList3 = to2dDouble(list3, type3);
String[] names3 = nameList(list3, type3);
corAnalysis(dList3, names3);
graphNum =63;
//String[] names1 = nameList(list1, type1);
//String[] names2 = nameList(list2, type2);
barLine(dList3, names3);
pieModel(dList3, names3);
String title = names3[0] + " Vs " + names3[1];
//String title = names1[0] + " Vs " + names1[1];
//LineChart_AWT chart1 = new LineChart_AWT(dList3[0], dList3[1], title, names3[0], names3[1]);
//chart1.pack( );
//RefineryUtilities.centerFrameOnScreen( chart1 );
//chart1.setVisible( true );
for(int x=0; x<dList3.length-1; x++) {
for(int y=x+1; y<dList3.length; y++) {
LineChart_AWT chart1 = new LineChart_AWT(dList3[x], dList3[y], title, names3[x], names3[y]);
chart1.pack( );
RefineryUtilities.centerFrameOnScreen( chart1 );
chart1.setVisible( true );
}
}
*/
//corAnalysis(dList1, names1);
//corAnalysis(dList2, names2);
//corAnalysis(dList1, dList2, names1, names2);
// double[] one = arrayToDouble(list, 1, 5);
// //System.out.println(Arrays.toString(one));
// double[] two = arrayToDouble(list2, 1, 3);
// String[] te = list[4];
// Arrays.sort(te);
//// System.out.println("first " + te[te.length-1]);
//// double[] zero = arrayToDouble(list, 1, 0);
// System.out.println("The mean of "+ list[5][0] +" is " + df.format(findMean(one)));
// System.out.println("The mean of" + list2[3][0] +" is " + df.format(findMean(two)));
// System.out.println("Correlation between " + list2[3][0] +" and "+ list[5][0] +" is %" + df.format(Correlation(one, two)*100));
//
// LineChart_AWT chart1 = new LineChart_AWT( "Amount vs Count", one, list[5][0], numWords(max), "count");
// LineChart_AWT chart2 = new LineChart_AWT( "INIT_ESTB vs Count", two, list2[3][0], numWords(max), "count");
//
// chart1.pack( );
// RefineryUtilities.centerFrameOnScreen( chart1 );
// chart1.setVisible( true );
//
// chart2.pack( );
// RefineryUtilities.centerFrameOnScreen( chart2 );
// chart2.setVisible( true );
// System.out.println("The interquartile of "+ list[5][0] +" is " + df.format(interquartile(one)));
// System.out.println("The interquartile of" + list2[3][0] +" is " + df.format(interquartile(two)));
// System.out.println("The Median of "+ list[5][0] +" is " + df.format(findMedian(one)));
// System.out.println("The Median of" + list2[3][0] +" is " + df.format(findMedian(two)));
//
// System.out.println("The max of "+ list[5][0] +" is " + df.format(max(one)));
// System.out.println("The max of" + list2[3][0] +" is " + df.format(max(two)));
// System.out.println("The min of "+ list[5][0] +" is " + df.format(min(one)));
// System.out.println("The min of" + list2[3][0] +" is " + df.format(min(two)));
// System.out.println("The difference of "+ list[5][0] +" is " + df.format(difference(one)));
// System.out.println("The difference of" + list2[3][0] +" is " + df.format(difference(two)));
// range(one);
//
// System.out.println("Correlation between " + list2[3][0] +" and "+ list[5][0] +" is %" + df.format(Correlation(one, two)*100));
// System.out.println("Correlation between zero and two is %" + df.format(Correlation(two, zero)*100));
// System.out.println("Correlation between zero and one is %" + df.format(Correlation(one, zero)*100));
//
// System.out.println(Arrays.toString(findNumRows(list)));
// double[][] iList = to2dDouble(list);
// for (int x = 0; x < list.length; x++) {
// System.out.println(Arrays.toString(list[x]));
// }
// System.out.println("Employchange");
// for (int x = 0; x < list2.length; x++) {
// System.out.println(Arrays.toString(list2[x]));
// }
}
public static void convert(File selectedFile) throws Exception {
String filestring = selectedFile.getAbsolutePath();
int csv = countLines(filestring);
System.out.println("Number of lines counted in the csv file: " + csv);
String [][] list3 = csvReader(selectedFile, csv-1);
char[] type3 = checkType(list3);
double [][] dList3 = to2dDouble(list3, type3);
String[] names3 = nameList(list3, type3);
String[] time3 = getTime(list3, type3);
double [] results = decideGraph(time3, dList3);
//corAnalysis(dList3, names3);
DecimalFormat df = new DecimalFormat("#.###");
for(int d=0; d<results.length; d++) {
//System.out.println("Value of results array at index " + d + "is: " + results[d]);
results[d] = Double.valueOf(df.format(results[d] * 100));
//System.out.println("Value of results array now at index " + d + "is: " + results[d]);
}
int counter = 0;
double highest = 0;
for(int a=0; a<results.length-1; a++) {
System.out.println("Value of results array later at index " + a + "is: " + results[a]);
if((results[a] > results[a+1]) && (results[a] > highest)) {
counter = a;
highest = results[a];
}
else if(results[a] < results[a+1]) {
System.out.println("I've gotten inside the second else if statement!");
System.out.println("The value of a+1 is: " + results[a+1]);
counter = a+1;
highest = results[a+1];
System.out.println("The value of counter is: " + counter);
}
}
System.out.println("The value of counter is: " + counter);
graphNum =63;
//System.out.println("Value of bool variable meanchoice: " + meanchoice);
for(int i=0; i<dList3.length; i++) {
for(int j=0; j<dList3[i].length; j++) {
System.out.println("Index of dList: " + j + "and its value: " + dList3[i][j]);
}
}
System.out.println("Exited out of dList array");
if(meanchoice == true) {
for(int x=0; x<dList3.length; x++){
double averagevar = findMean(dList3[x]);
System.out.println("The mean of variable " + x + " is: " + averagevar);
}
}
if(medianchoice == true) {
for(int x=0; x<dList3.length; x++){
double medianvar = findMedian(dList3[x]);
System.out.println("The median of variable " + x + " is: " + medianvar);
}
}
if(modechoice == true) {
for(int x=0; x<dList3.length; x++){
double modevar = findMode(dList3[x]);
System.out.println("The mode of variable " + x + " is: " + modevar);
}
}
if(rangechoice == true) {
for(int x=0; x<dList3.length; x++){
double boundaryvar = range(dList3[x]);
System.out.println("The range of variable " + x + " is: " + boundaryvar);
}
}
if(minchoice == true) {
for(int x=0; x<dList3.length; x++){
double smallvar = min(dList3[x]);
System.out.println("The minimum value of variable " + x + " is: " + smallvar);
}
}
if(maxchoice == true) {
for(int x=0; x<dList3.length; x++){
double largevar = max(dList3[x]);
System.out.println("The maximum value of variable " + x + " is: " + largevar);
}
}
if(iqrchoice == true) {
for(int x=0; x<dList3.length; x++){
double [] summaryvar = interquartile(dList3[x]);
System.out.println("The interquartile range value of variable " + x + " is: " + summaryvar[1]);
}
}
if(outlierchoice == true) {
for(int x=0; x<dList3.length; x++){
//for(int y=x+1; y<dList3.length; y++){
double [] summary_x = interquartile(dList3[x]);
//double [] summary_y = interquartile(dList3[y]);
ArrayList<Double> modoutx = findMildOutliers(dList3[x], summary_x[0], summary_x[2], summary_x[1]);
//ArrayList<Double> modouty = findMildOutliers(dList3[y], summary_y[0], summary_y[2], summary_y[1]);
ArrayList<Double> extremeoutx = findExtremeOutliers(dList3[x], summary_x[0], summary_x[2], summary_x[1]);
//ArrayList<Double> extremeouty = findExtremeOutliers(dList3[y], summary_y[0], summary_y[2], summary_y[1]);
System.out.println("The mild outlier value(s) of variable " + x + " is: " + modoutx);
//System.out.println("The moderate outlier value(s) of variable" + y + "is: " + modouty);
System.out.println("The extreme outlier value(s) of variable " + x + " is: " + extremeoutx);
//System.out.println("The extreme outlier value(s) of variable" + y + "is: " + extremeouty);
//}
}
}
if(correlationchoice == true) {
for(int x=0; x<dList3.length-1; x++){
for(int y=x+1; y<dList3.length; y++){
double correlate = Correlation(dList3[x], dList3[y]);
System.out.println("The correlation between variables" + x + "and" + y + "is: " + correlate);
}
}
}
if(sdchoice == true) {
for(int x=0; x<dList3.length; x++){
double sdvar = findDeviation(dList3[x]);
System.out.println("The standard deviation of variable" + x + "is: " + sdvar);
}
}
if(variancechoice == true) {
for(int x=0; x<dList3.length-1; x++){
double vdvar = findVariance(dList3[x]);
System.out.println("The variance between variable" + x + "is: " + vdvar);
}
}
if(counter == 0) {
list3 = csvReader(selectedFile, csv);
type3 = checkType(list3);
dList3 = to2dDouble(list3, type3);
names3 = nameList(list3, type3);
corAnalysis(dList3, names3);
for(int x=0; x<dList3.length-1; x++) {
for(int y=x+1; y<dList3.length; y++) {
String title = names3[x] + " Vs " + names3[y];
list3 = csvReader(selectedFile, csv);
type3 = checkType(list3);
dList3 = to2dDouble(list3, type3);
names3 = nameList(list3, type3);
corAnalysis(dList3, names3);
//System.out.println("Value of dList3 in x: " + list[0]);
//System.out.println("Value of dList3 in y: " + list[1]);
BarChart_AWT chart1 = new BarChart_AWT(dList3[x], dList3[y], title, names3[x], names3[y]);
chart1.pack();
RefineryUtilities.centerFrameOnScreen(chart1);
chart1.setVisible(true);
}
}
//barLine(dList3, names3);
}
else if(counter == 1) {
for(int x=0; x<dList3.length-1; x++) {
for(int y=x+1; y<dList3.length; y++) {
String title = names3[x] + " Vs " + names3[y];
//System.out.println("Value of dList3 in x: " + dList3[x]);
//System.out.println("Value of dList3 in y: " + dList3[y]);
list3 = csvReader(selectedFile, csv);
type3 = checkType(list3);
dList3 = to2dDouble(list3, type3);
names3 = nameList(list3, type3);
corAnalysis(dList3, names3);
LineChart_AWT chart1 = new LineChart_AWT(dList3[x], dList3[y], title, names3[x], names3[y]);
chart1.pack( );
RefineryUtilities.centerFrameOnScreen( chart1 );
chart1.setVisible( true );
}
}
//lineChart(dList3, names3);
}
else if(counter == 2) {
for(int x=0; x<dList3.length-1; x++) {
for(int y=x+1; y<dList3.length; y++) {
String title = names3[x] + " Vs " + names3[y];
list3 = csvReader(selectedFile, csv);
type3 = checkType(list3);
dList3 = to2dDouble(list3, type3);
names3 = nameList(list3, type3);
corAnalysis(dList3, names3);
//System.out.println("Value of dList3 in x: " + list[0]);
//System.out.println("Value of dList3 in y: " + list[1]);
ScatterChart_AWT chart1 = new ScatterChart_AWT(dList3[x], dList3[y], title, names3[x], names3[y]);
chart1.pack();
RefineryUtilities.centerFrameOnScreen(chart1);
chart1.setVisible(true);
}
}
//scatterChart(dList3, names3);
}
else if(counter == 3) {
//System.out.println("I'm choosing the correct graph.");
//DecimalFormat df = new DecimalFormat("#.###");
//System.out.println("I've reached here!");
for(int x=0; x<dList3.length-1; x++) {
for(int y=x+1; y<dList3.length; y++) {
String title = names3[x] + "Vs" + names3[y];
list3 = csvReader(selectedFile, csv);
type3 = checkType(list3);
dList3 = to2dDouble(list3, type3);
names3 = nameList(list3, type3);
corAnalysis(dList3, names3);
//System.out.println("Reached here as well!");
//System.out.println("Value of dList3 in x: " + list[0]);
//System.out.println("Value of dList3 in y: " + list[1]);
PieChart_AWT chart1 = new PieChart_AWT(dList3[x], dList3[y], title, names3[x], names3[y]);
chart1.pack();
RefineryUtilities.centerFrameOnScreen(chart1);
chart1.setVisible(true);
}
}
//pieModel(dList3, names3);
}
}
public static double[] choosegraph(double a, double b, double c, double d, double e) throws Exception{
// System.out.println(a+", "+b+", "+c+", "+d+", "+e);
weka.core.Attribute Attribute1 = new Attribute("firstNumeric");
weka.core.Attribute Attribute2 = new Attribute("secondNumeric");
weka.core.Attribute Attribute3 = new Attribute("thirdNumeric");
weka.core.Attribute Attribute4 = new Attribute("fourthNumeric");
weka.core.Attribute Attribute5 = new Attribute("fifthNumeric");
ArrayList<String> types = new ArrayList<String>(4);
types.add("B");
types.add("S");
types.add("L");
types.add("P");
weka.core.Attribute classAttribute = new Attribute("GraphTypes", types);
ArrayList<Attribute> allAttributes = new ArrayList<Attribute>(6);
allAttributes.add(Attribute1);
allAttributes.add(Attribute2);
allAttributes.add(Attribute3);
allAttributes.add(Attribute4);
allAttributes.add(Attribute5);
allAttributes.add(classAttribute);
DataSource source = new DataSource("GraphTrainingData.csv");
weka.core.Instances trainingSet = source.getDataSet();
trainingSet.setClassIndex(5);
weka.classifiers.Classifier model = (Classifier) new NaiveBayes();
model.buildClassifier(trainingSet);
// Create an empty training set
Instances testSet = new Instances("Rel", allAttributes, 10);
// Set class index
testSet.setClassIndex(5);
Instance test = new DenseInstance(5);
test.setValue((Attribute)allAttributes.get(0), a);
test.setValue((Attribute)allAttributes.get(1), b);
test.setValue((Attribute)allAttributes.get(2), c);
test.setValue((Attribute)allAttributes.get(3), d);
test.setValue((Attribute)allAttributes.get(4), e);
testSet.add(test);
test.setDataset(trainingSet);
double[] results = model.distributionForInstance(test);
DecimalFormat df = new DecimalFormat("#.###");
System.out.println("Bar Graph: " + df.format(results[0]*100) + "%");
System.out.println("Line Graph: " + df.format(results[1]*100) + "%");
System.out.println("Scatter Graph: " + df.format(results[2]*100) + "%");
System.out.println("Pie Graph: " + df.format(results[3]*100) + "%");
// double result = model.classifyInstance(test);
// System.out.println(result);
double max = results[0];
for (int i = 1; i < results.length; i++) {
if (results[i] > max) {
max = results[i];
}
}
//
// if(max == 0){
// return "Line";
// }
// else if(max == 1){
// return "Scatter";
// }
// else if(max == 2){
// return "Bar";
// }
// else{
// return "Pie";
// }
return results;
}
public static void demoDecideGraph(String[] time, double[][] data){
//Between bar and scatter plot
//decided by outliers, numbers and range
int[] info = {0,0,0,0,0};
if (time == null){
info[0] =0;
}
else{
info[0] =uniques(time);
}
if(data.length ==1){
//check if sum is %10
info[1] =1;
for(int x =0; x<data.length; x++){
info[2] +=divTen(data[x]);
}
}
else{
//check all correlations
//how many times does one data set avg go into another
info[1] =data.length;
info[3]= checkHighCor(data, 65);
}
info[4] =data[0].length;
//check avg difference between points
//check for size of outliers
if((info[0]==0) && (info[2]==1) && (info[1]==1) ){
System.out.println("Rule Results: Pie");
}
else if((info[0]==0) && (info[4]>100) && (info[1]==2) ){
System.out.println("Rule Results: Scatter");
}
else if((info[0]<=5) && (info[4]<100) ){
System.out.println("Rule Results: Bar");
}
else{
System.out.println("Rule Results: Line");
}
}
public static double[] decideGraph(String[] time, double[][] data) throws Exception{
//Between bar and scatter plot
//decided by outliers, numbers and range
int[] info = {0,0,0,0,0};
if (time == null){
info[0] =0;
}
else{
info[0] =uniques(time);
}
if(data.length ==1){
//check if sum is %10
info[1] =1;
for(int x =0; x<data.length; x++){
info[3] +=divTen(data[x]);
}
}
else{
//check all correlations
//how many times does one data set avg go into another
info[1] =data.length;
info[2]= checkHighCor(data, 65);
}
info[4] =data[0].length;
// for(int x=0; x<info.length;x++){
// System.out.print(info[x]+ "\t");
// }
// System.out.println();
currentData = info;
return choosegraph(info[0], info[1], info[2], info[3], info[4]);
}
public static int countLines(String filename) throws IOException {
LineNumberReader reader = new LineNumberReader(new FileReader(filename));
int cnt = 0;
String lineRead = "";
while ((lineRead = reader.readLine()) != null) {}
cnt = reader.getLineNumber();
reader.close();
return cnt;
}
public static int divTen(double[] data){
double sum =0;
for(int x =0; x<data.length; x++){
sum +=data[x];
}
sum = Math.rint(sum);
if(sum%10==0){
return 1;
}
return 0;
}
public static int checkHighCor(double[][] data, int high){
int count=0;
for(int x =0; x<data.length; x++){
for(int y =x+1; y<data.length;y++){
if((Correlation(data[x], data[y])>high)||(Correlation(data[x], data[y])<(high*-1))){
count++;
}
}
}
return count;
}
public static int uniques(String[] times){
int uniques =0;
String[] found = new String[times.length];
for(int x =0; x<times.length; x++){
if(Arrays.asList(found).contains(times[x])==false){
found[uniques]= times[x];
uniques++;
}
}
return uniques;
}
public static String[] nameList(String[][] list, char[] types){
String[] names = new String[numD(types)];
int column =0;
for(int x =0; x<types.length;x++){
if(types[x]=='D'){
names[column] = list[x][0].substring(0, list[x][0].length()-1);
column++;
}
}
return names;
}
public static void corAnalysis(double[][] list, String[] names){
DecimalFormat df = new DecimalFormat("#.###");
for(int x =0; x<list.length-1; x++){
for(int y =x+1; y<list.length; y++){
double temp = Correlation(list[x], list[y])*100;
System.out.println("Correlation between column " + names[x] +" and column "+ names[y] +" is %" + df.format(temp));
if(Math.abs(temp) > graphNum){
twoLine(list[x], list[y], names[x], names[y]);
}
}
}
}
public static void corAnalysis(double[][] list1, double[][] list2, String[] names1, String[] names2){
DecimalFormat df = new DecimalFormat("#.###");
for(int x =0; x<list1.length; x++){
for(int y =0; y<list2.length; y++){
double temp = Correlation(list1[x], list2[y])*100;
System.out.println("Correlation between list 1 column " + names1[x] +" and list 2 column "+ names2[y] +" is %" + df.format(temp));
if(Math.abs(temp) > graphNum){
twoLine(list1[x], list2[y], names1[x], names2[y]);
}
}
}
}
public static void twoLine(double[] list1, double[] list2, String name1, String name2){
DecimalFormat df = new DecimalFormat("#.###");
double[] dif1 = pDifference(list1);
double[] dif2 = pDifference(list2);
//String title = name1 + " Vs " + name2;
//LineChart_AWT chart1 = new LineChart_AWT(list1, list2, title, name1, name2);
//chart1.pack( );
//RefineryUtilities.centerFrameOnScreen( chart1 );
//chart1.setVisible( true );
System.out.println("\tThe mean of " + name1 + " is " + df.format(findMean(list1)));
System.out.println("\tThe mean of " + name2 + " is " + df.format(findMean(list2)));
System.out.println("\tThe median of " + name1 + " is " + df.format(findMedian(list1)));
System.out.println("\tThe median of " + name2 + " is " + df.format(findMedian(list2)));
}
/*
public static void barLine(double[][] list, String[] names) {
DecimalFormat df = new DecimalFormat("#.###");
for(int x=0; x<list.length-1; x++) {
for(int y=x+1; y<list.length; y++) {
String title = names[x] + " Vs " + names[y];
//System.out.println("Value of dList3 in x: " + list[0]);
//System.out.println("Value of dList3 in y: " + list[1]);
BarChart_AWT chart1 = new BarChart_AWT(list[x], list[y], title, names[x], names[y]);
chart1.pack();
RefineryUtilities.centerFrameOnScreen(chart1);
chart1.setVisible(true);
}
}
}
*/
/*
public static void pieModel(double[][]list, String[] names) {
DecimalFormat df = new DecimalFormat("#.###");
System.out.println("I've reached here!");
for(int x=0; x<list.length-1; x++) {
for(int y=x+1; y<list.length; y++) {
String title = names[x] + "Vs" + names[y];
System.out.println("Reached here as well!");
//System.out.println("Value of dList3 in x: " + list[0]);
//System.out.println("Value of dList3 in y: " + list[1]);
PieChart_AWT chart1 = new PieChart_AWT(list[x], list[y], title, names[x], names[y]);
chart1.pack();
RefineryUtilities.centerFrameOnScreen(chart1);
chart1.setVisible(true);
}
}
}
*/
/*
public static void lineChart(double[][]list, String[] names) {
DecimalFormat df = new DecimalFormat("#.###");
for(int x=0; x<list.length; x++) {
for(int y=0; y<list[x].length; y++) {
System.out.print("Value of Column 1 and Column 2: " + list[x][y]);
}
}
for(int x=0; x<list.length-1; x++) {
for(int y=x+1; y<list.length; y++) {
String title = names[x] + " Vs " + names[y];
//System.out.println("Value of dList3 in x: " + dList3[x]);
//System.out.println("Value of dList3 in y: " + dList3[y]);
LineChart_AWT chart1 = new LineChart_AWT(list[x], list[y], title, names[x], names[y]);
chart1.pack( );
RefineryUtilities.centerFrameOnScreen( chart1 );
chart1.setVisible( true );
}
}
}
*/
public static void scatterChart(double[][]list, String[] names) {
DecimalFormat df = new DecimalFormat("#.###");
for(int x=0; x<list.length; x++) {
for(int y=0; y<list[x].length; y++) {
System.out.print("Value of Column 1 and Column 2: " + list[x][y]);
}
}
for(int x=0; x<list.length-1; x++) {
for(int y=x+1; y<list.length; y++) {
String title = names[x] + " Vs " + names[y];
//System.out.println("Value of dList3 in x: " + dList3[x]);
//System.out.println("Value of dList3 in y: " + dList3[y]);
ScatterChart_AWT chart1 = new ScatterChart_AWT(list[x], list[y], title, names[x], names[y]);
chart1.pack( );
RefineryUtilities.centerFrameOnScreen( chart1 );
chart1.setVisible( true );
}
}
}
public static double[] pDifference(double[] list){
double[] dif = new double[list.length-1];
for(int x =1; x<list.length; x++){
dif[x-1] = (list[x-1] -list[x])/(list[x-1]*100);
}
return dif;
}
public static char[] checkType(String[][] list){
char[] types = new char[list.length];
for(int x =0; x<types.length; x++){
types[x] = list[x][0].charAt(list[x][0].length()-1);
}
return types;
}
public static double[][] to2dDouble(String[][] list, char[] types){
double[][] dList = new double[numD(types)][list[0].length];
int column =0;
for(int x =0; x<types.length;x++){
if(types[x]=='D'){
dList[column] = arrayToDouble(list, 0, x);
column++;
}
}
return dList;
}
public static String[] getTime(String[][] list, char[] types){
String[] time = new String[list[0].length-1];
int column =0;
for(int x =0; x<types.length;x++){
if(types[x]=='T'){
for(int y =0; y<time.length;y++){
time[y] = list[x][y+1];
}
column++;
}