-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmeans.h
More file actions
47 lines (40 loc) · 2.06 KB
/
kmeans.h
File metadata and controls
47 lines (40 loc) · 2.06 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
/****************************************************************
* Header File: kmeans.h
* Description: kmeans cluster library for 1D float data
* ---------- ---------- ----------------------------
* Tassio BORGES 24-APR-2022 - Created
*
* This class implements a kmeans clustering algorithm for 1D float data.
* The functions can calculate the clusters and return the members of each cluster
* With a kmeans clustering, one can implement Radial Basis Functions
****************************************************************/
#ifndef KMEANS_H
#define KMEANS_H
#include<iostream>
#include<fstream>
#include<vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
class KMeans
{
int num_clusters; //Number of Clusters
int data_size; //Size of data
vector<float> centroids; // vector containing the centroids
vector<float> data; //input dataset to cluster
vector<vector<float>> R; // 2D Vector containing the centroids and the points assigned to them
vector<vector<int>> indices;
void shuffle(int *arr,size_t n); // function to help "randomize" the initial clusters selection
float distance(float in1, float in2); //calculate the distance between the points
void recalculate_centroid(int c_index, int d_index,vector<vector<float>> vIn); // Function to recalculate the centroid as the mean of all its points
public:
KMeans(int n_clusters, int data_size_in); // Initialize the object with the number of cluster and the data to cluster
void def_initial_centroids(vector<float> data_in ,int data_size_in, int n_clusters, char method); // "Randomly" select the initial clusters
void clustering(vector<float> data_in); // Perform the clustering
void print_centroids(); // print the centroids
void print_members(int ind_centroid); //print the cluster members
vector<float> get_members(int ind_centroid); // return the members of a cluster
vector<int> get_index_members(int ind_centroid); // return the indices of the cluster members, indices are related to the original dataset
};
#endif // KMEANS_H