mahout聚类模型主要存在于包org.apache.mahout.clustering中,主要包含Model、ClusterObservations和GaussianAccumulator几部分。
一、Model
Model主要定义了以下几个函数:
double pdf(O x);//计算x属于此模型的概率
void observe(O x);//将加入此模型
void observe(O x, double weight);//将带权重的x加入模型
void observe(Model<O> x);//将模型加入模型
void computeParameters();//计算参数,后面会讲,基本上是更新S0,S1,S2
long getNumObservations();//获取此模型中样本个数,上一次更新参数到现在添加的样本数目
long getTotalObservations();//获取样本总数
Model<VectorWritable> sampleFromPosterior();//抽样
接口Cluster又继承了接口Model,在里面添加了几个属性和几个函数:
String CLUSTERED_POINTS_DIR = "clusteredPoints";//所有样本点存放目录
String INITIAL_CLUSTERS_DIR = "clusters-0";//初始簇存放目录
String CLUSTERS_DIR = "clusters-";//每次迭代后的簇存放目录前缀
String FINAL_ITERATION_SUFFIX = "-final";//最终的簇存放目录的后缀
/**
* 簇编号
*/
int getId();
/**
* 簇的中心
*/
Vector getCenter();
/**
* 簇的半径
*/
Vector getRadius();
/**
* 簇的描述
*/
String asFormatString(String[] bindings);
/**
* 簇是否合并
*/
boolean isConverged();
抽象类AbstractCluster实现了接口Cluster,其中定义了一些变量:
private int id;//簇编号
private long numObservations;//此簇的样本数目
private long totalObservations;//总共的样本数目
private Vector center;//聚类中心
private Vector radius;//聚类半径
private double s0;//点的权重和
private Vector s1;//点的加权和
private Vector s2;//点的平方的加权和
此类中主要需要关注的是observe,不过无论其参数怎么变,目的就只有一个,进行S0,S1和S2的叠加。还有一个方法是computeParameters,其实就是更新一下S0,S1和S2。
二、ClusterObservations
ClusterObservations类为一个辅助类,记录了combinerState,S0,S1和S2几个参数,并提供了get*方法,对于combinerState还提供了incrementCombinerState方法,每次incrementCombinerState的值增加1。
private int combinerState;//结合状态
private double s0;//各个点权重之和
private Vector s1;//各个点权重加权和
private Vector s2;//各个点权重加权平方和
GaussianAccumulator接口定义了获取样本集一些统计信息的方法,大部分是get*方法,只有observe和compute是用来更新这些参数。
/**
* 样本个数
* @return the number of observations
*/
double getN();
/**
* 样本均值
* @return the mean of the observations
*/
Vector getMean();
/**
* 样本标准差
* @return the std of the observations
*/
Vector getStd();
/**
* 样本标准差的均值
* @return the average of the vector std elements
*/
double getAverageStd();
/**
* 样本方差
* @return the variance of the observations
*/
Vector getVariance();
/**
* Observe the vector
*
* @param x
* a Vector
* @param weight
* the double observation weight (usually 1.0)
*/
void observe(Vector x, double weight);
/**
* Compute the mean, variance and standard deviation
*/
void compute();
实现这个接口的是类RunningSumsGaussianAccumulator和类OnlineGaussianAccumulator,里面全部是一些数学计算,就不讲了。