Skip to content

Commit b5cdd1f

Browse files
authored
Merge pull request #2 from max-acc/work-in-progress
Merge work-in-progress branch
2 parents a0c4757 + 7fe38a1 commit b5cdd1f

File tree

7 files changed

+182
-26
lines changed

7 files changed

+182
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
out/

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,44 @@
1-
# hello-world
1+
# <p align="center"><b>Classification of float values in Java</b></p>
2+
3+
4+
## Description
5+
6+
7+
## Getting Started
8+
9+
10+
### Dependencies
11+
12+
13+
### Installing
14+
15+
16+
### Executing program
17+
18+
19+
### Error Codes
20+
21+
22+
## Scripts
23+
24+
25+
## Help
26+
27+
28+
## Authors
29+
30+
Contributors names and contact info
31+
32+
* Max Wenk
33+
* [@max-acc](https://github.com/max-acc)
34+
35+
## Version History
36+
37+
38+
## License
39+
40+
This project is licensed under the "GNU Affero General Public License v3.0" License - see the LICENSE.md file for details.
41+
42+
## Acknowledgments
43+
44+
* [README Template](https://gist.github.com/DomPizzie/7a5ff55ffa9081f2de27c315f5018afc)

src/Main.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public static void main (String[] args) throws Exception {
2121
ob.dataSubdivision();
2222
//System.out.println(ob.feedback()[0][2]);
2323
ob.distanceClassification();
24+
25+
ob.evaluateResults();
2426
}
2527

2628
}

src/classification/ClassificationOfFloatValues.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public class ClassificationOfFloatValues {
2727
private boolean dataSubdivisionBool = false; // Data has been divided into training and test data
2828
private String MLAlgorithm; // Variable for saving which machine learning algorithm has been used
2929

30+
// --- Classification result data variables
31+
private String[][][] predictedTestData;
32+
private int[][] sortedProbability;
33+
3034

3135
// Function to add the members of the class
3236
public float[][] output() { return this.predictorData; }
@@ -83,6 +87,13 @@ public void dataValidation (float trainingData) {
8387

8488

8589
// --- Functions for evaluating the machine learning results -------------------------------------------------------
90+
public void evaluateResults() {
91+
DATA_evaluation evaluationObject = new DATA_evaluation(this.testDataResults,
92+
this.columnCount - this.numberOfTrainingData,
93+
this.predictedTestData,
94+
this.sortedProbability);
95+
evaluationObject.confusionMatrix();
96+
}
8697
public void confusionMatrix() {
8798
if (this.MLAlgorithm == "DistanceClassification") {
8899
System.out.println("nice confusion");
@@ -134,6 +145,10 @@ public void distanceClassification (){
134145
// Testing the distance classification model
135146
classificationObject.setTestData(this.testDataPredictors, this.testDataResults, this.rowCount, this.columnCount - this.numberOfTrainingData);
136147
classificationObject.testModel();
148+
149+
// Get the test data
150+
this.predictedTestData = classificationObject.getPredictedTestData();
151+
this.sortedProbability = classificationObject.getSortedProbability();
137152
}
138153

139154
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package classification;
2+
3+
public class DATA_evaluation {
4+
private String[][][] predictedTestData;
5+
private int[][] sortedProbability;
6+
7+
private int columnCount;
8+
private int numberOfClasses;
9+
private String [] testDataResults;
10+
private int[][] confustionMatrix = new int[3][2];
11+
12+
protected DATA_evaluation(String[] testDataResults, int columnCount, String[][][] predictedTestData, int[][] sortedProbability) {
13+
this.testDataResults = testDataResults;
14+
this.columnCount = columnCount;
15+
this.predictedTestData = predictedTestData;
16+
this.sortedProbability = sortedProbability;
17+
}
18+
19+
protected void confusionMatrix() {
20+
System.out.println(this.testDataResults[0]);
21+
System.out.println(this.columnCount);
22+
System.out.println(this.predictedTestData[0][0][0]);
23+
System.out.println(this.predictedTestData[0][0][1]);
24+
System.out.println(this.predictedTestData[0][1][0]);
25+
System.out.println(this.predictedTestData[0][1][1]);
26+
System.out.println(this.predictedTestData[0][2][0]);
27+
System.out.println(this.predictedTestData[0][2][1]);
28+
System.out.println(this.sortedProbability[0][0]);
29+
System.out.println(this.sortedProbability[0][1]);
30+
System.out.println(this.sortedProbability[0][2]);
31+
32+
// Resetting the confusion matrix
33+
for (int i = 0; i < 3; i++) {
34+
for (int j = 0; j < 2; j++) {
35+
this.confustionMatrix[i][j] = 0;
36+
}
37+
}
38+
39+
for (int i = 0; i < this.columnCount; i++) {
40+
if (this.testDataResults[i].equals(this.predictedTestData[i][this.sortedProbability[i][0]][0])) {
41+
this.confustionMatrix[this.sortedProbability[i][0]][0]++;
42+
}
43+
else {
44+
this.confustionMatrix[this.sortedProbability[i][0]][1]++;
45+
}
46+
}
47+
48+
49+
50+
for (int i = 0; i < 3; i++) {
51+
for (int j = 0; j < 2; j++) {
52+
System.out.print(this.confustionMatrix[i][j] + " ");
53+
}
54+
System.out.println();
55+
}
56+
57+
58+
}
59+
}

src/classification/DistanceClassification.java

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)