机器学习库(MLL)是一些用于分类、回归和数据聚类的类和函数。
大部分分类和回归算法是用C++类来实现。尽管这些算法有一些不同的特性(像处理missing measurements的能力,或者categorical input variables等),这些类之间有一些相同之处。这些相同之处在类 CvStatModel 中被定义,其他 ML 类都是从这个类中继承。
CvStatModel
ML库中的统计模型基类。 数据挖掘实验室
class CvStatModel
{
public:
/* CvStatModel(); */
/* CvStatModel( const CvMat* train_data ... ); */
virtual ~CvStatModel();
virtual void clear()=0;
/* virtual bool train( const CvMat* train_data, [int tflag,] ..., const CvMat* responses, ...,
[const CvMat* var_idx,] ..., [const CvMat* sample_idx,] ...
[const CvMat* var_type,] ..., [const CvMat* missing_mask,] <misc_training_alg_params> ... )=0;
*/
/* virtual float predict( const CvMat* sample ... ) const=0; */
virtual void save( const char* filename, const char* name=0 )=0;
virtual void load( const char* filename, const char* name=0 )=0;
virtual void write( CvFileStorage* storage, const char* name )=0;
virtual void read( CvFileStorage* storage, CvFileNode* node )=0;
};
在上面的声明中,一些函数被注释掉。实际上,一些函数没有一个单一的API(缺省的构造函数除外),然而,在本节後面描述的语法和定义方面有一些相似之处,好像他们是基类的一部分一样。
CvStatModel::CvStatModel
缺省构造函数
CvStatModel::CvStatModel();
数据挖掘研究院
ML中的每个统计模型都有一个无参数构造函数。This constructor is useful for 2-stage model construction, when the default constructor is followed by train() or load(). 数据挖掘研究院
CvStatModel::CvStatModel(...)
Training constructor 数据挖掘实验室
CvStatModel::CvStatModel( const CvMat* train_data ... ); */ 数据挖掘实验室
Most ML classes provide single-step construct+train constructor. This constructor is equivalent to the default constructor, followed by the train() method with the parameters that passed to the constructor.
CvStatModel::~CvStatModel
Virtual destructor 数据挖掘研究院
CvStatModel::~CvStatModel();
数据挖掘研究院
The destructor of the base class is declared as virtual, so it is safe to write the following code:
CvStatModel* model;
if( use_svm )
model = new CvSVM(... /* SVM params */);
else
model = new CvDTree(... /* Decision tree params */);
...
delete model;
Normally, the destructor of each derived class does nothing, but calls the overridden method clear() that deallocates all the memory. 数据挖掘研究院
CvStatModel::clear
释放内存,重置模型状态 数据挖掘研究院
void CvStatModel::clear();
数据挖掘研究院
The method clear does the same job as the destructor, i.e. it deallocates all the memory occupied by the class members. But the object itself is not destructed, and it can be reused further. This method is called from the destructor, from the train methods of the derived classes, from the methods load(), read() etc., or even explicitly by user. 数据挖掘研究院
CvStatModel::save
将模型保存到文件
void CvStatModel::save( const char* filename, const char* name=0 );
数据挖掘研究院
The method save stores the complete model state to the specified XML or YAML file with the specified name or default name (that depends on the particular class). Data persistence functionality from cxcore is used. 数据挖掘研究院
CvStatModel::load
从文件中装载模型 数据挖掘实验室
void CvStatModel::load( const char* filename, const char* name=0 ); 数据挖掘研究院
The method load loads the complete model state with the specified name (or default model-dependent name) from the specified XML or YAML file. The previous model state is cleared by clear(). 数据挖掘研究院
Note that the method is virtual, therefore any model can be loaded using this virtual method. However, unlike the C types of OpenCV that can be loaded using generic cvLoad(), in this case the model type must be known anyway, because an empty model, an instance of the appropriate class, must be constructed beforehand. This limitation will be removed in the later ML versions.
CvStatModel::write
将模型写入file storage 数据挖掘研究院
void CvStatModel::write( CvFileStorage* storage, const char* name ); 数据挖掘研究院
The method write stores the complete model state to the file storage with the specified name or default name (that depends on the particular class). The method is called by save().
CvStatModel::read
Reads the model from file storage
void CvStatMode::read( CvFileStorage* storage, CvFileNode* node );
The method read restores the complete model state from the specified node of the file storage. The node must be located by user, for example, using the function cvGetFileNodeByName(). The method is called by load(). 数据挖掘研究院
The previous model state is cleared by clear(). 数据挖掘实验室
CvStatModel::train
训练模型 数据挖掘实验室
bool CvStatMode::train( const CvMat* train_data, [int tflag,] ..., const CvMat* responses, ...,
[const CvMat* var_idx,] ..., [const CvMat* sample_idx,] ...
[const CvMat* var_type,] ..., [const CvMat* missing_mask,] <misc_training_alg_params> ... );
这个函数利用输入的特征向量和对应的反应值(responses)来训练统计模型。特征向量和其对应的反应值都是用矩阵来表示。缺省情况下,特征向量都以行向量被保存在train_data中,也就是所有的特征向量元素都是连续存储。不过,一些算法可以处理转置表示,即特征向量用列向量来表示,所有特征向量的相同位置的元素连续存储。如果两种排布方式都支持,这个函数的参数tflag可以使用下面的取值: 数据挖掘实验室
- tflag=CV_ROW_SAMPLE
- 表示特征向量以行向量存储;
- tflag=CV_COL_SAMPLE
- 表示特征向量以列向量存储;
训练数据必须是32fC1(32位的浮点数,单信道)格式 反应值通常是以向量方式存储(一个行,或者一个列向量),存储格式为32sC1(仅在分类问题中)或者32fC1格式,每个输入特征向量对应一个值(虽然一些算法,比如某几种神经网络,反应值为向量)。 数据挖掘研究院
对于分类问题,反应值是离散的类别标签;对于回归问题,反应值是被估计函数的输出值。一些算法只能处理分类问题,一些只能处理回归问题,另一些算法这两类问题都能处理。In the latter case the type of output variable is either passed as separate parameter, or as a last element of var_type vector: CV_VAR_CATEGORICAL means that the output values are discrete class labels, CV_VAR_ORDERED(=CV_VAR_NUMERICAL) means that the output values are ordered, i.e. 2 different values can be compared as numbers, and this is a regression problem The types of input variables can be also specified using var_type. Most algorithms can handle only ordered input variables.
ML中的很多模型也可以仅仅使用选择特征的子集,或者(并且)使用选择样本的子集来训练。为了让用户易于使用,train函数通常包含var_idx和sample_idx参数。var_idx指定感兴趣的特征,sample_idx指定感兴趣的样本。这两个向量可以是整数(32sC1)向量,例如以0为开始的索引,或者8位(8uC1)的使用的特征或者样本的掩码。用户也可以传入NULL指针,用来表示训练中使用所有变量/样本。 数据挖掘实验室
除此之外,一些算法支持数据缺失情况,也就是某个训练样本的某个特征值未知(例如,他们忘记了在周一测量病人A的温度)。参数missing_mask,一个8位的同train_data同样大小的矩阵掩码,用来指示缺失的数据(掩码中的非零值)。
通常来说,在执行训练操作前,可以用clear()函数清除掉早先训练的模型状态。然而,一些函数可以选择用新的数据更新模型,而不是将模型重置,一切从头再来。 数据挖掘研究院
CvStatModel::predict
预测样本的response 数据挖掘研究院
float CvStatMode::predict( const CvMat* sample[, <prediction_params>] ) const;
数据挖掘研究院
这个函数用来预测一个新样本的反应值(response)。在分类问题中,这个函数返回类别编号;在回归问题中,返回函数值。输入的样本必须传给train_data的训练样本同样大小。如果训练中使用了var_idx参数,一定记住在predict函数中使用跟训练特征一致的特征。 数据挖掘研究院
後缀const是说预测不会影响模型的内部状态,所以这个函数可以很安全地从不同的线程调用。

