Skip to content

Commit 02de9c1

Browse files
author
Maximilian Wenk
committed
documentation of ClassificationOfFloatValues.java
1 parent 30c1c52 commit 02de9c1

File tree

3 files changed

+74
-47
lines changed

3 files changed

+74
-47
lines changed

src/Main.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ public class Main {
77
public static void main (String[] args) throws Exception {
88
// Creating of instance of second class present
99
// in the same project
10-
boolean index = true;
11-
boolean header = true;
1210
String dataset = "Iris_unordered_2.csv";
1311
dataset = "Dry_Bean_Dataset.csv";
1412

@@ -22,10 +20,13 @@ public static void main (String[] args) throws Exception {
2220
ob.dataValidation(0.7f);
2321
ob.dataProcessing();
2422
ob.dataSubdivision();
25-
//System.out.println(ob.feedback()[0][2]);
23+
2624
ob.distanceClassification();
2725

26+
ob.setEvaluation("Confusion Matrix");
27+
ob.setEvaluation("Simple Confusion Matrix");
28+
ob.setEvaluation("Normalized Confusion Matrix");
29+
2830
ob.evaluateResults();
2931
}
30-
3132
}

src/classification/ClassificationOfFloatValues.java

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class ClassificationOfFloatValues {
3232
private int[][] sortedProbability;
3333
private int numberOfClasses;
3434

35+
private boolean[] validationModel = {false, false, false};
36+
3537

3638
// Function to add the members of the class
3739
public float[][] output() { return this.predictorData; }
@@ -47,12 +49,12 @@ public ClassificationOfFloatValues(String dataset) throws Exception {
4749
this.datasetName = dataset;
4850
}
4951

50-
// --- Function for creating
52+
// --- Function for processing the data (reading and writing the data to an array)
5153
public void dataProcessing() throws Exception {
54+
// Get row and column count
5255
this.rowCount = CSVread.calcRowCount(this.datasetName, this.index);
5356
this.columnCount = CSVread.calcColumnCount(this.datasetName, this.index);
54-
//System.out.println(this.rowCount);
55-
//System.out.println(this.columnCount);
57+
// Get predictor and result data
5658
this.predictorData = CSVread.transformPredictorData(this.datasetName, this.index, this.header, this.columnCount, this.rowCount);
5759
this.resultData = CSVread.transformResultData(this.datasetName, this.index, this.header, this.columnCount, this.rowCount);
5860
this.dataProcessingBool = true;
@@ -77,58 +79,85 @@ public void dataSubdivision () {
7779

7880

7981
// --- Functions for additional user control -----------------------------------------------------------------------
80-
// --- Function for changing the ratio between training and testing data
82+
// --- Functions for setting the index and header data
8183
public void setIndex (boolean index) { this.index = index; }
84+
8285
public void setHeader (boolean header) { this.header = header; }
86+
87+
// --- Function for changing the data density to clear extreme values | Not working
8388
public void setDensity (float density) {this.density = density;}
89+
90+
// --- Function for changing the ratio between training and testing data
8491
public void dataValidation (float trainingData) {
8592
this.validation[0] = trainingData;
8693
this.validation[1] = 1- trainingData;
8794
}
95+
// --- Function for setting the validation model
96+
public void setEvaluation(String evaluationName) {
97+
if (evaluationName.equals("Confusion Matrix")) {
98+
this.validationModel[0] = true;
99+
}else if (evaluationName.equals("Simple Confusion Matrix")) {
100+
this.validationModel[1] = true;
101+
}else if (evaluationName.equals("Normalized Confusion Matrix")) {
102+
this.validationModel[2] = true;
103+
}
104+
}
88105

89106

90107
// --- Functions for evaluating the machine learning results -------------------------------------------------------
108+
// --- Function for printing confusion matrices
91109
public void evaluateResults() {
110+
// Creating an object to calculate confusion matrices
92111
DATA_evaluation evaluationObject = new DATA_evaluation(this.testDataResults,
93112
this.columnCount - this.numberOfTrainingData,
94113
this.predictedTestData,
95114
this.sortedProbability,
96115
this.numberOfClasses);
97-
System.out.println("\nSimple Confusion Matrix");
98-
evaluationObject.getConfusionMatrixSimple();
99-
System.out.println("\nNormalized Confusion Matrix");
100-
evaluationObject.getConfusionMatrixNormalized();
101-
}
102-
public void confusionMatrix() {
103-
if (this.MLAlgorithm == "DistanceClassification") {
104-
System.out.println("nice confusion");
116+
117+
// Printing a basic confusion matrix
118+
if (this.validationModel[0] == true) {
119+
System.out.println("\nConfusion Matrix");
120+
evaluationObject.getConfusionMatrix();
121+
}
122+
// Printing a simplified confusion matrix
123+
if (this.validationModel[1] == true) {
124+
System.out.println("\nSimple Confusion Matrix");
125+
evaluationObject.getConfusionMatrixSimple();
105126
}
106-
else {
107-
System.out.println("There is no algorithm");
127+
// Printing a normalized confusion matrix
128+
if (this.validationModel[2] == true) {
129+
System.out.println("\nNormalized Confusion Matrix");
130+
evaluationObject.getConfusionMatrixNormalized();
108131
}
132+
109133
}
110134

111135

112136
// --- Functions for private calculations --------------------------------------------------------------------------
113137
// --- Function for returning usable index data
114138
private int returnIndex() {
115139
// If there is an index it returns -1 because the usable data of the processed data has one element less
140+
// The index does not belong to the important data
116141
if (this.index == true) { return -1; } else { return 0; }
117142
}
118143

119-
//
144+
// --- Function for checking if all required processes have been completed successful before starting the
145+
// classification algorithms
120146
private boolean checkRequiredProcesses () {
147+
// dataProcessingBool: Process the CSV data (reading)
148+
// dataSubdivisionBool: Dividing the data into training and testing data
121149
if (this.dataProcessingBool == true &&
122150
this.dataSubdivisionBool == true) {
123151
return true;
152+
}else {
153+
if (this.dataProcessingBool == false) {
154+
System.out.println("Error 310 | The data has not been divided into training and testing data!");
155+
}
156+
if (this.dataSubdivisionBool == false) {
157+
System.out.println("Error 311 | The data has not been divided into training and testing data!");
158+
}
159+
return false;
124160
}
125-
if (this.dataProcessingBool == false) {
126-
System.out.println("Error 310 | The data has not been divided into training and testing data!");
127-
}
128-
if (this.dataSubdivisionBool == false) {
129-
System.out.println("Error 311 | The data has not been divided into training and testing data!");
130-
}
131-
return false;
132161
}
133162

134163

@@ -151,9 +180,10 @@ public void distanceClassification (){
151180
classificationObject.setTestData(this.testDataPredictors, this.testDataResults, this.rowCount, this.columnCount - this.numberOfTrainingData);
152181
classificationObject.testModel();
153182

183+
// Return the number of found classes
154184
this.numberOfClasses = classificationObject.getNumberOfClasses();
155185

156-
// Get the test data
186+
// Get the predicted text data
157187
this.predictedTestData = classificationObject.getPredictedTestData();
158188
this.sortedProbability = classificationObject.getSortedProbability();
159189
}

src/classification/DATA_evaluation.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ protected DATA_evaluation(String[] testDataResults, int columnCount, String[][][
2121
this.createConfusionMatrix = false;
2222
}
2323

24+
// --- Function for creating a basic confusion matrix for further calculations -------------------------------------
2425
private void confusionMatrix() {
2526

2627
// Resetting the confusion matrix
@@ -31,7 +32,6 @@ private void confusionMatrix() {
3132
}
3233

3334
for (int i = 0; i < this.columnCount; i++) {
34-
//System.out.println(this.testDataResults[i] + " " + this.predictedTestData[i][this.sortedProbability[i][0]][0]);
3535
if (this.testDataResults[i].equals(this.predictedTestData[i][this.sortedProbability[i][0]][0])) {
3636
this.confustionMatrix[this.sortedProbability[i][0]][this.sortedProbability[i][0]]++;
3737
}
@@ -42,26 +42,26 @@ private void confusionMatrix() {
4242
this.confustionMatrix[this.sortedProbability[i][0]][this.sortedProbability[i][j]]++;
4343
}
4444
}
45-
46-
47-
//this.confustionMatrix[this.sortedProbability[i][0]][1]++;
4845
}
49-
5046
}
47+
}
5148

49+
// --- Printing a basic confusion matrix
50+
protected void getConfusionMatrix() {
51+
if (!this.createConfusionMatrix) {
52+
confusionMatrix();
53+
}
5254

53-
/*
5455
for (int i = 0; i < this.numberOfClasses; i++) {
5556
for (int j = 0; j < this.numberOfClasses; j++) {
56-
System.out.print(this.confustionMatrix[i][j] + " ");
57+
System.out.print(confustionMatrix[i][j] + " ");
5758
}
5859
System.out.println();
5960
}
60-
*/
61-
6261
}
6362

64-
protected int[][] getConfusionMatrixSimple() {
63+
// --- Printing a simple confusion matrix
64+
protected void getConfusionMatrixSimple() {
6565
if (!this.createConfusionMatrix) {
6666
confusionMatrix();
6767
}
@@ -82,19 +82,18 @@ protected int[][] getConfusionMatrixSimple() {
8282
}
8383
}
8484

85-
86-
87-
for (int i = 0; i < 3; i++) {
85+
for (int i = 0; i < this.numberOfClasses; i++) {
8886
for (int j = 0; j < 2; j++) {
8987
System.out.print(confusionMatrixSimple[i][j] + " ");
9088
}
9189
System.out.println();
9290
}
9391

94-
95-
return confusionMatrixSimple;
92+
//return confusionMatrixSimple;
9693
}
97-
protected float[][] getConfusionMatrixNormalized() {
94+
95+
// --- Printing a normalized confusion matrix
96+
protected void getConfusionMatrixNormalized() {
9897
if (!this.createConfusionMatrix) {
9998
confusionMatrix();
10099
}
@@ -110,8 +109,6 @@ protected float[][] getConfusionMatrixNormalized() {
110109
continue;
111110
}
112111
for (int j = 0; j < this.numberOfClasses; j++) {
113-
//System.out.println(this.confustionMatrix[i][j]);
114-
//System.out.println(tempCount);
115112
confusionMatrixNormalized[i][j] = (float)this.confustionMatrix[i][j] / (float)tempCount;
116113
}
117114

@@ -124,7 +121,6 @@ protected float[][] getConfusionMatrixNormalized() {
124121
System.out.println();
125122
}
126123

127-
128-
return confusionMatrixNormalized;
124+
//return confusionMatrixNormalized;
129125
}
130126
}

0 commit comments

Comments
 (0)