Welcome

首页 / 数据库 / SQLServer / SQL Server 2005 Analysis Services数据挖掘算法扩展方法

SQL Server 2005 Analysis Services数据挖掘算法扩展方法2009-12-27 博客园 Yin.Pu@CQUSoft本文是对英文原文SQL Server Data Mining Managed Plug-In Algorithms Tutorial的部分翻译及整理,主要是描述SSAS数据挖掘算法的基本扩展方法和开发过程。本文的内容只是原文的一部分,如果想了解更多信息可以下载原文。英文原文在本文附件中下载。

SSAS为我们提供了九种数据挖掘算法,但是在应用中我们需要根据实际问题设计适当的算法,这个时候就需要扩展SSAS,使它能应用更多的算法,SSAS有比较好的可扩展性,它提供了一个完整的机制来进行扩展,只要继承一些类并按适当的方法进行注册就可以在SSAS中使用自己的算法了。下面我将通过实例分别用几篇文章来介绍一下如何开发SSAS算法插件。本文介绍的算法插件开发方法是基于托管代码的,是用C#开发的(算法插件也可以用C++开发,并且SQLSERVER2005的案例中附带C++版本的代码stub)。整个过程大至为六个步骤。在开始开发之前需要做一些准备工作,就是要去下载一个用C++编写的COM组件,叫DMPluginWrapper(可以通过下载本文附带的附件获得),它作为SSAS与算法插件的中间层,用于处理SSAS与算法插件之间的交互以及封装从SSAS到算法插件的参数和从算法插件到SSAS的处理结果。DMPluginWrapper、SSAS和算法插件之间的关系可以由下图来描述。

图表 1: DMPluginWrapper、SSAS和算法插件之间的关系

下面开始创建算法扩展的项目。

首先新建一个类库项目(名为AlgorithmPlugin)将刚才的DMPluginWrapper项目引用到新建的这个AlgorithmPlugin类库项目中。你可以选择为这个类库项目进行程序集签名,这样就可以将其注册到GAC中。另外还要为DMPluginWrapper添加后生成脚本将程序集注册到GAC,参考脚本如下(根据机器具体设置而定):

"C:WINDOWSMicrosoft.NETFrameworkv2.0.50727RegAsm.exe" $(TargetPath)
"C:Program FilesMicrosoft Visual Studio 8SDKv2.0Bingacutil.exe" /u $(TargetName)
"C:Program FilesMicrosoft Visual Studio 8SDKv2.0Bingacutil.exe" /if $(TargetPath)

如果第一行脚本不能正确运行的话,算法插件是不能被SQLSERVER分析服务器识别的。另外两行脚本就是将算法程序集注册到GAC。

接下来的几个步骤主要是继承一些基类的工作,包括AlgorithmMetadataBase类、AlgorithmBase类和ICaseProcessor接口和AlgorithmNavigationBase类。首先在AlgorithmPlugin中新建一个类文件并命名为Metadata,为这个类添加ComVisible、MiningAlgorithmClass(typeof(Algorithm))和Guid属性(Algorithm是下面要创建的算法类),并为Guid属性指定一个GUID编码。这个类要继承于AlgorithmMetadataBase类。现在要做的事情就是覆盖基类的方法。下面是所有需要覆盖的方法(对于较简单的实现写在表格中):

方法名实现(参考)备注
GetServiceNamereturn "MyFirstAlgorithmPlugin"这个方法的返回值中不能带有空格字符
GetServiceDescriptionreturn "Sample Algorithm Plugin";
GetServiceTypePlugInServiceType.ServiceTypeOther;
GetViewerTypereturn string.Empty
GetScalingreturn MiningScaling.Medium;用于指定算法适用的规模,这个值不会被服务器使用而是显示在模式行集中,为用户提供算法的一些相关信息。
GetTrainingComplexityreturn MiningTrainingComplexity.Low用于指定算法训练适用的复杂度,这个值不会被服务器使用而是显示在模式行集中,为用户提供算法的一些相关信息。
GetPredictionComplexityreturn MiningPredictionComplexity.Low用于指定预测复杂度,这个值不会被服务器使用而是显示在模式行集中,为用户提供算法的一些相关信息。
GetSupportsDMDimensionsretrun false;
GetSupportsDrillThroughreturn false;指定这个算法是否支持钻透功能。
GetDrillThroughMustIncludeChildrenreturn false;
GetCaseIdModeledreturn false;
GetMarginalRequirementsreturn MarginalRequirements.AllStats
GetParametersCollectionreturn null;算法参数,因为本文中的例子没有参数,所以这里返回空。
GetSupInputContentTypesMiningColumnContent[] arInputContentTypes = new MiningColumnContent[]

{

MiningColumnContent.Discrete,

MiningColumnContent.Continuous,

MiningColumnContent.Discretized,

MiningColumnContent.NestedTable,

MiningColumnContent.Key

};



return arInputContentTypes;
指定算法所支持的输入属性的数据类型,如连续型、离散型等。
GetSupPredictContentTypesMiningColumnContent[] arPredictContentTypes = new MiningColumnContent[]

{

MiningColumnContent.Discrete,

MiningColumnContent.Continuous,

MiningColumnContent.Discretized,

MiningColumnContent.NestedTable,

MiningColumnContent.Key

};

return arPredictContentTypes;
与上一个方法类似,这里是指定预测属性所支持的数据类型。
GetSupportedStandardFunctionsSupportedFunction[] arFuncs

= new SupportedFunction[] {

SupportedFunction.PredictSupport,

SupportedFunction.PredictHistogram,

SupportedFunction.PredictProbability,

SupportedFunction.PredictAdjustedProbability,

SupportedFunction.PredictAssociation,

SupportedFunction.PredictStdDev,

SupportedFunction.PredictVariance,

SupportedFunction.RangeMax,

SupportedFunction.RangeMid,

SupportedFunction.RangeMin,

SupportedFunction.DAdjustedProbability,

SupportedFunction.DProbability,

SupportedFunction.DStdDev,

SupportedFunction.DSupport,

SupportedFunction.DVariance,

// content-related functions

SupportedFunction.IsDescendent,

SupportedFunction.PredictNodeId,

SupportedFunction.IsInNode,

SupportedFunction.DNodeId,

};

return arFuncs;
指定DMX所支持的函数。
CreateAlgorithmreturn new Algorithm();返回算法实例,Algorithm是接下来要创建的类。