Skip to content

Instantly share code, notes, and snippets.

@samionb
Created April 17, 2023 08:18
Show Gist options
  • Select an option

  • Save samionb/03e0fc30b03cd0bff184ca24b4b51b0a to your computer and use it in GitHub Desktop.

Select an option

Save samionb/03e0fc30b03cd0bff184ca24b4b51b0a to your computer and use it in GitHub Desktop.
KMeans test
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cassert>
// KMeans function here
int main()
{
// Set up random data
const int num_points = 100;
const int num_dimensions = 2;
std::vector<std::vector<float>> data(num_points, std::vector<float>(num_dimensions));
std::srand(std::time(nullptr));
for (int i = 0; i < num_points; i++) {
for (int j = 0; j < num_dimensions; j++) {
data[i][j] = std::rand() / static_cast<float>(RAND_MAX);
}
}
// Run KMeans
const int k = 3;
const int max_iterations = 100;
const auto centroids = KMeans(data, k, max_iterations);
// Ensure that the number of centroids is correct
assert(centroids.size() == k);
// Ensure that each centroid has the correct number of dimensions
for (const auto& centroid : centroids) {
assert(centroid.size() == num_dimensions);
}
// Ensure that each data point is assigned to the correct cluster
for (int i = 0; i < num_points; i++) {
int closest_cluster = 0;
float min_distance = std::numeric_limits<float>::max();
for (int j = 0; j < k; j++) {
float distance = 0.0f;
for (int d = 0; d < num_dimensions; d++) {
distance += std::pow(data[i][d] - centroids[j][d], 2);
}
distance = std::sqrt(distance);
if (distance < min_distance) {
min_distance = distance;
closest_cluster = j;
}
}
assert(closest_cluster == cluster_assignment[i]);
}
std::cout << "All tests passed!\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment