-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassifyTest.java
More file actions
67 lines (55 loc) · 2.08 KB
/
Copy pathClassifyTest.java
File metadata and controls
67 lines (55 loc) · 2.08 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
package de.uni_passau.fim.se2.sa.readability.utils;
import weka.classifiers.Evaluation;
import weka.core.Instances;
import org.junit.jupiter.api.*;
import java.io.*;
import java.nio.file.Files;
import static org.junit.jupiter.api.Assertions.*;
public class ClassifyTest {
private File testCSV;
/**
* Sets up a small dummy CSV file compatible with Weka.
* Includes numeric attributes and a nominal class label ("Y"/"N").
*/
@BeforeEach
public void setup() throws IOException {
testCSV = File.createTempFile("weka_test_data", ".csv");
// Minimal readable CSV header + 2 examples
String csvContent = """
Feature1,Feature2,Feature3,Truth
1.1,0.1,0.1,Y
1.2,0.2,0.2,N
1.3,0.3,0.3,Y
1.4,0.4,0.4,N
1.5,0.5,0.5,Y
1.6,0.6,0.6,N
1.7,0.7,0.7,Y
1.8,0.8,0.8,N
1.9,0.9,0.9,Y
2.0,1.0,1.0,N
""";
Files.writeString(testCSV.toPath(), csvContent);
}
/**
* Tests whether the dataset loads successfully and the class index is set correctly.
*/
@Test
public void testDatasetLoading() throws IOException {
Instances dataset = Classify.loadDataset(testCSV);
assertNotNull(dataset, "Dataset should not be null");
assertEquals(4, dataset.numAttributes(), "Dataset should have 4 columns");
assertEquals(3, dataset.classIndex(), "Class attribute should be last column (index 3)");
assertEquals("Truth", dataset.classAttribute().name(), "Class attribute should be named 'Truth'");
}
/**
* Tests training and 10-fold cross-validation on a minimal dataset using Logistic Regression.
*/
@Test
public void testTrainingAndEvaluation() throws Exception {
Instances dataset = Classify.loadDataset(testCSV);
Evaluation eval = Classify.trainAndEvaluate(dataset);
assertNotNull(eval, "Evaluation object should not be null");
assertTrue(eval.pctCorrect() >= 0.0 && eval.pctCorrect() <= 100.0, "Accuracy should be a valid percentage");
assertTrue(eval.weightedFMeasure() >= 0.0, "F1 score should be non-negative");
}
}