Repository navigation
Expand file tree
/
Copy pathHandwrittendigitRecognition.java
More file actions
119 lines (100 loc) · 4.27 KB
/
Copy pathHandwrittendigitRecognition.java
File metadata and controls
119 lines (100 loc) · 4.27 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
public class HandwrittendigitRecognition{
// Class representing a single sample (pixel values + label)
static class Sample {
double[] pixels; // Array of pixel values
int label; // Label (digit)
// Constructor
Sample(double[] pixels, int label) {
this.pixels = pixels;
this.label = label;
}
}
// Method to load the dataset (CSV format) into a list of Samples
public static List<Sample> loadDataset(String filename) throws IOException {
List<Sample> dataset = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
int label = Integer.parseInt(values[0]); // The first value is the label
double[] pixels = new double[784]; // 28x28 flattened image = 784 pixels
for (int i = 0; i < 784; i++) {
pixels[i] = Integer.parseInt(values[i + 1]) / 255.0; // Normalize pixel values
}
dataset.add(new Sample(pixels, label));
}
br.close();
return dataset;
}
// Method to calculate Euclidean distance between two samples
public static double euclideanDistance(double[] a, double[] b) {
double sum = 0;
for (int i = 0; i < a.length; i++) {
sum += Math.pow(a[i] - b[i], 2);
}
return Math.sqrt(sum);
}
// k-NN algorithm: Classify the test sample based on the k nearest neighbors
public static int knn(List<Sample> trainSet, double[] testSample, int k) {
// List of distances (distance, label)
List<Map.Entry<Double, Integer>> distances = new ArrayList<>();
// Calculate the distance between the test sample and each training sample
for (Sample trainSample : trainSet) {
double dist = euclideanDistance(testSample, trainSample.pixels);
distances.add(new AbstractMap.SimpleEntry<>(dist, trainSample.label));
}
// Sort by distance (ascending order)
distances.sort(Comparator.comparing(Map.Entry::getKey));
// Perform majority voting (take the label with the most occurrences)
int[] votes = new int[10]; // There are 10 possible labels (0-9)
for (int i = 0; i < k; i++) {
int label = distances.get(i).getValue();
votes[label]++;
}
// Find the label with the maximum votes
int predictedLabel = 0;
for (int i = 1; i < votes.length; i++) {
if (votes[i] > votes[predictedLabel]) {
predictedLabel = i;
}
}
return predictedLabel;
}
// Method to calculate accuracy on the test set
public static double calculateAccuracy(List<Sample> testSet, List<Sample> trainSet, int k) {
int correct = 0;
for (Sample testSample : testSet) {
int predictedLabel = knn(trainSet, testSample.pixels, k);
if (predictedLabel == testSample.label) {
correct++;
}
}
return (double) correct / testSet.size() * 100; // Return accuracy as percentage
}
public static void main(String[] args) {
try {
// Load dataset (e.g., train.csv and test.csv files)
List<Sample> trainSet = loadDataset("train.csv");
List<Sample> testSet = loadDataset("test.csv");
// Set k (number of nearest neighbors)
int k = 3;
// Calculate accuracy
double accuracy = calculateAccuracy(testSet, trainSet, k);
System.out.println("Accuracy: " + accuracy + "%");
// Example: Predict a single test sample (you can select any index)
Sample testSample = testSet.get(0);
int predictedLabel = knn(trainSet, testSample.pixels, k);
System.out.println("Predicted label for the first test sample: " + predictedLabel);
} catch (IOException e) {
e.printStackTrace();
}
}
}