-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRIS_Classification.cpp
More file actions
90 lines (70 loc) · 2.57 KB
/
IRIS_Classification.cpp
File metadata and controls
90 lines (70 loc) · 2.57 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
import ANNet;
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <unordered_map>
using namespace std;
static std::vector<double> oneHot(int classIndex, int totalClasses = 5) {
std::vector<double> label(totalClasses, 0);
label[classIndex] = 1;
return label;
}
static std::vector<std::pair<std::vector<double>, std::vector<double>>>
readCSVToDataset(const std::string& filename, int numFeatures, int numClasses) {
std::ifstream file(filename);
std::vector<std::pair<std::vector<double>, std::vector<double>>> dataset;
if (!file.is_open()) {
std::cerr << "Failed to open file: " << filename << std::endl;
return dataset;
}
std::unordered_map<std::string, int> labelMap = {
{"setosa", 0},
{"versicolor", 1},
{"virginica", 2}
};
std::string line;
while (std::getline(file, line)) {
std::stringstream ss(line);
std::string token;
std::vector<double> features;
for (int i = 0; i < numFeatures; ++i) {
if (!std::getline(ss, token, ',')) break;
features.push_back(std::stod(token));
}
if (!std::getline(ss, token, ',')) continue;
if (labelMap.find(token) == labelMap.end()) {
std::cerr << "Unknown label: " << token << std::endl;
continue;
}
int labelIndex = labelMap[token];
std::vector<double> oneHotLabel = oneHot(labelIndex, numClasses);
dataset.push_back({ features, oneHotLabel });
}
file.close();
return dataset;
}
int main() {
vector<pair<vector<double>, vector<double>>> dataset = readCSVToDataset("data.csv",4,3);
pair<vector<pair<vector<double>, vector<double>>>, vector<pair<vector<double>, vector<double>>>>
stratifiedDataset = stratifiedSampling(dataset);
vector<pair<vector<double>, vector<double>>> trainingDataset = stratifiedDataset.first;
vector<pair<vector<double>, vector<double>>> testingDataset = stratifiedDataset.second;
Network model;
model.InputLayer(dataset);
model.HiddenLayer(4);
model.HiddenLayer(4);
model.OutputLayer(3);
model.train(0.005, 0.001, 0.5, 200);
model.test(testingDataset);
model.evaluation();
cout << endl;
model.showMatrix();
cout << endl;
vector<double> inputs = { 6.9,3.2,5.5,2.2 };
int predictedClass = model.predict(inputs);
cout << endl;
cout << "Predicted Class : " << predictedClass + 1 << endl;
cout << endl;
return 0;
}