@@ -21,7 +21,8 @@ public class DistanceClassification {
2121 private  int  testDataRowCount ;
2222 private  int  testDataColumnCount ;
2323
24-  private  String [][] predictedTestData ;
24+  private  String [][][] predictedTestData ;
25+  private  int [][] sortedProbability ;
2526
2627
2728 protected  DistanceClassification (float  [][] trainingDataPredictors , String  [] trainingDataResults , int  rowCount , int  columnCount , float  density ) {
@@ -42,6 +43,14 @@ protected float[][][] getSortedClassificationData() {
4243 return  this .sortedClassificationData ;
4344 }
4445
46+  protected  String [][][] getPredictedTestData () {
47+  return  this .predictedTestData ;
48+  }
49+ 
50+  protected  int [][] getSortedProbability () {
51+  return  this .sortedProbability ;
52+  }
53+ 
4554 protected  float [][] getFeatureMean () {
4655 return  this .featureMean ;
4756 }
@@ -54,7 +63,7 @@ protected void setTestData(float[][] testDataPredictors, String[] testDataResult
5463 }
5564
5665 protected  void  testModel () {
57-  testClassifcationModel ();
66+  testClassificationModel ();
5867 }
5968
6069 private  void  getClassificationClasses () {
@@ -141,47 +150,68 @@ private void calcFeatureMean() {
141150 }
142151 }
143152
144-  private  void  testClassifcationModel () {
145-  this .predictedTestData  = new  String [testDataColumnCount ][2 ];
153+  private  void  testClassificationModel () {
154+  this .predictedTestData  = new  String [this .testDataColumnCount ][this .numberOfClasses ][2 ];
155+  this .sortedProbability  = new  int [this .testDataColumnCount ][this .numberOfClasses ];
146156
147157 // Check the distance for every class 
158+ 
148159 for  (int  i  = 0 ; i  < this .testDataColumnCount ; i ++) {
149160 float [][] tempDelta  = new  float [this .numberOfClasses ][this .rowCount ];
150161 for  (int  j  = 0 ; j  < this .numberOfClasses ; j ++) {
151162 for  (int  k  = 0 ; k  < this .rowCount  -1 ; k ++) {
152163 tempDelta [j ][k ] = this .featureMean [j ][k ] - this .testDataPredictors [i ][k ];
153164 }
154165
155-  tempDelta [j ][this .rowCount  -1 ] = (float ) Math .sqrt (
156-  Math .pow (tempDelta [j ][0 ], 2 ) +
157-  Math .pow (tempDelta [j ][1 ], 2 ) +
158-  Math .pow (tempDelta [j ][2 ], 2 ) +
159-  Math .pow (tempDelta [j ][3 ], 2 ));
160-  System .out .print (classes [j ] + " " );
161-  System .out .println (tempDelta [j ][this .rowCount  -1 ]);
162-  }
163- 
166+  this .predictedTestData [i ][j ][0 ] = this .classes [j ];
167+  float  tempCalcDistance  = 0 ;
164168
165-  /* 
166-  for (int j = 0; j < this.numberOfClasses; j++) { 
167169 for  (int  k  = 0 ; k  < this .rowCount  -1 ; k ++) {
168-  System.out.print(tempDelta[j][k]); 
169-  System.out.print(" "); 
170+  tempCalcDistance  += (float ) Math .pow (tempDelta [j ][k ], 2 );
170171 }
171-  System.out.println(); 
172-  } 
173-  */ 
174-  this .predictedTestData [i ][0 ] = "best result" ;
175-  this .predictedTestData [i ][1 ] = "probability" ;
176172
173+  tempDelta [j ][this .rowCount  -1 ] = (float ) Math .sqrt (tempCalcDistance );
174+  this .predictedTestData [i ][j ][1 ] = Float .toString (tempDelta [j ][this .rowCount  -1 ]);
177175
178-  //this.predictedTestData[i] = "best result"; 
179-  }
180- 
176+  System .out .print (" "  + this .predictedTestData [i ][j ][0 ] + " " );
177+  System .out .println (this .predictedTestData [i ][j ][1 ]);
181178
182179
183180
181+  }
182+  float  min  = Float .valueOf (this .predictedTestData [i ][0 ][1 ]);
184183
184+  int  tempIndex  = 0 ;
185+  this .sortedProbability [i ][0 ] = tempIndex ;
185186
187+  for  (int  j  = 0 ; j  < this .numberOfClasses ; j ++) {
188+  if  (min  > Float .valueOf (this .predictedTestData [i ][j ][1 ])) {
189+  min  = Float .valueOf (this .predictedTestData [i ][j ][1 ]);
190+  tempIndex  = j ;
191+  }
192+  }
193+  this .sortedProbability [i ][0 ] = tempIndex ;
194+  for  (int  j  = 1 ; j  < this .numberOfClasses ; j ++) {
195+  tempIndex  = 0 ;
196+  this .sortedProbability [i ][j ] = tempIndex ;
197+  min  = Float .valueOf (this .predictedTestData [i ][this .sortedProbability [i ][j -1 ]][1 ]);
198+  for  (int  k  = 0 ; k  < this .numberOfClasses ; k ++) {
199+  if  ((min  > Float .valueOf (this .predictedTestData [i ][k ][1 ]) &&
200+  Float .valueOf (this .predictedTestData [i ][k ][1 ]) > Float .valueOf (this .predictedTestData [i ][this .sortedProbability [i ][j -1 ]][1 ])) ||
201+  (Float .valueOf (this .predictedTestData [i ][k ][1 ]) > Float .valueOf (this .predictedTestData [i ][this .sortedProbability [i ][j -1 ]][1 ]) &&
202+  min  == Float .valueOf (this .predictedTestData [i ][this .sortedProbability [i ][j -1 ]][1 ]))) {
203+  if  (min  > Float .valueOf (this .predictedTestData [i ][k ][1 ]) &&
204+  Float .valueOf (this .predictedTestData [i ][k ][1 ]) > Float .valueOf (this .predictedTestData [i ][this .sortedProbability [i ][j -1 ]][1 ])) {
205+  }
206+  if  (Float .valueOf (this .predictedTestData [i ][k ][1 ]) > Float .valueOf (this .predictedTestData [i ][this .sortedProbability [i ][j -1 ]][1 ]) &&
207+  k  != this .sortedProbability [i ][j -1 ]) {
208+  }
209+  min  = Float .valueOf (this .predictedTestData [i ][k ][1 ]);
210+  tempIndex  = k ;
211+  }
212+  }
213+  this .sortedProbability [i ][j ] = tempIndex ;
214+  }
215+  }
186216 }
187217}
0 commit comments