JWS C Library
C language utility library
kmeans.c File Reference

Definition of kmeans clustering algorithm and associated functions. More...

#include <jwsc/config.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <limits.h>
#include <inttypes.h>
#include "jwsc/vector/vector.h"
#include "jwsc/matrix/matrix.h"
#include "jwsc/prob/pdf.h"
#include "jwsc/stat/kmeans.h"
Include dependency graph for kmeans.c:

Go to the source code of this file.

Functions

void kmeans_d (Matrix_d **means_out, Vector_i32 **members_out, const Matrix_d *features, uint32_t K, uint32_t N)
 K Means algorithm.
void init_kmeans_randomly_d (Matrix_d **means_out, const Matrix_d *features, uint32_t K)
 Randomly chooses k feature vectors to initialize the means with.

Detailed Description

Definition of kmeans clustering algorithm and associated functions.

Author:
Joseph Schlecht
License:
Creative Commons BY-NC-SA 3.0

Definition in file kmeans.c.


Function Documentation

void kmeans_d ( Matrix_d **  means_out,
Vector_i32 **  members_out,
const Matrix_d features,
uint32_t  K,
uint32_t  N 
)

K Means algorithm.

Implements a fairly standard k-means algorithm.

Currently, the algorithm handles < N mean membership as follows: if a mean has n < N membership, N-n feature vectors are choosen at random to be members. Note that the random number generator is assumed to already be seeded (rand() is used).

Parameters:
means_outResult parameter. If *means_out is NULL when the function is called, the means are initialized by calling init_means_randomly(). Otherwise, *means_out is assumed to contain initialized means. Contains the resulting means when the function returns.
members_outResult parameter. When the function returns, (*members_out)->elts[i] contains the mean number (row index into *means_out) for the ith feature vector.
featuresFeature vectors to assign membership to means.
KNumber of means.
NMinimum number of records per mean.
Warning:
If *means_out is non-NULL, it is assumed to be a matrix of initialized means.

Definition at line 85 of file kmeans.c.

void init_kmeans_randomly_d ( Matrix_d **  means_out,
const Matrix_d features,
uint32_t  K 
)

Randomly chooses k feature vectors to initialize the means with.

Parameters:
means_outResult parameter. When the function returns, contains the initialized means.
featuresFeature vectors to randomly choose from for mean initialization.
KNumber of means.
Note:
Assumes the random number generator has already been seeded (uses rand()).

Definition at line 237 of file kmeans.c.