-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
65 lines (51 loc) · 1.76 KB
/
Copy pathmain.cpp
File metadata and controls
65 lines (51 loc) · 1.76 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
#include <fstream>
#include "AgglomerativeHierarchicalCluster.h"
using namespace Eigen;
using namespace std;
/**
* @brief The main function of the program.
*
* This function reads data from a file, performs agglomerative hierarchical clustering on the data,
* and prints the resulting clusters.
*
* @return 0 if the program executed successfully, 1 otherwise.
*/
int main() {
// Define the dimensions of the matrix
int rows = 100;
int cols = 2;
// Create an Eigen::MatrixXd to hold the data
MatrixXd data(rows, cols);
// Open the input file
ifstream inputFile("../../sample_input.txt");
if (!inputFile.is_open()) {
cerr << "Error: Unable to open input file!" << endl;
return 1;
}
// Read data from the file into the Eigen::MatrixXd
for (int row = 0; row < rows; ++row) {
for (int col = 0; col < cols; ++col) {
if (!(inputFile >> data(row, col))) {
cerr << "Error: Failed to read data from input file!" << endl;
return 1;
}
}
}
// Close the input file
inputFile.close();
int K = 4; // Number of clusters desired
int M = 0; // Use single-linkage distance measure
// Create an instance of AgglomerativeHierarchicalClustering
AgglomerativeHierarchicalClustering clustering(data, K, M);
// Run the clustering algorithm
clustering.run_algorithm();
// Print the resulting clusters
clustering.print();
clustering.print_label();
//print cluster index for each data point
auto cluster_idx = clustering.cluster_idx();
for (int i = 0; i < cluster_idx.size(); ++i) {
cout << "Data point [" << data(i,0) << "," << data(i,1) << "] is in cluster " << cluster_idx[i] << endl;
}
return 0;
}