Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
k_means_clustering.c File Reference

K Means Clustering Algorithm implemented. More...

#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
Include dependency graph for k_means_clustering.c:

Data Structures

struct  observation
 
struct  cluster
 

Macros

#define _USE_MATH_DEFINES   /* required for MS Visual C */
 

Typedefs

typedef struct observation observation
 
typedef struct cluster cluster
 

Functions

int calculateNearst (observation *o, cluster clusters[], int k)
 
void calculateCentroid (observation observations[], size_t size, cluster *centroid)
 
clusterkMeans (observation observations[], size_t size, int k)
 
void printEPS (observation pts[], size_t len, cluster cent[], int k)
 
static void test ()
 
void test2 ()
 
int main ()
 

Detailed Description

K Means Clustering Algorithm implemented.

This file has K Means algorithm implemmented It prints test output in eps format

Note: Though the code for clustering works for all the 2D data points and can be extended for any size vector by making the required changes, but note that the output method i.e. printEPS is only good for polar data points i.e. in a circle and both test use the same.

Author
Lakhan Nad

Function Documentation

◆ test2()

test2 ( )

A function to test the kMeans function Generates 1000000 points in a circle of radius 20.0 with center at (0,0) and cluster them into 11 clusters

Output for 1000000 points divided in 11 clusters

305  {
306  size_t size = 1000000L;
307  observation* observations = (observation*)malloc(sizeof(observation) * size);
308  double maxRadius = 20.00;
309  double radius = 0;
310  double ang = 0;
311  size_t i = 0;
312  for (; i < size; i++) {
313  radius = maxRadius * ((double)rand() / RAND_MAX);
314  ang = 2 * M_PI * ((double)rand() / RAND_MAX);
315  observations[i].x = radius * cos(ang);
316  observations[i].y = radius * sin(ang);
317  }
318  int k = 11; // No of clusters
319  cluster* clusters = kMeans(observations, size, k);
320  printEPS(observations, size, clusters, k);
321  // Free the accquired memory
322  free(observations);
323  free(clusters);
324 }
observation
Definition: k_means_clustering.c:38
L
Definition: list.h:8
observation::x
double x
abscissa of 2D data point
Definition: k_means_clustering.c:39
cluster
Definition: k_means_clustering.c:51
observation::y
double y
ordinate of 2D data point
Definition: k_means_clustering.c:40