clsMiningModel
注意: |
---|
下一版本的 Microsoft SQL Server 将删除该功能。请不要在新的开发工作中使用该功能,并尽快修改当前还在使用该功能的应用程序。 |
Objects of ClassType clsMiningModel contain the definitions for data mining models that are contained in the MiningModels collection of a clsServer object. Each model contains a collection, called the Columns collection, of data mining columns (that is, objects of ClassType clsColumn) that correspond to the case table definition for the model. Each of these data mining columns can, in turn, contain its own collection of data mining columns in the Columns collection. Such columns are referred to as nested columns. In addition to the Columns collection, the mining model object also contains references to Roles, DataSources and CustomProperties collections, which are used in the same manner as their counterparts under the cube object.
备注
After you create a mining model object by invoking the AddNew method of the server object's MiningModels collection, define the structure of the mining model. The most important step in this process is to determine the model's subclass type by setting this property to either sbclsOLAP or sbclsRelational. Data mining models whose SubClassType is sbclsOLAP (that is, OLAP data mining models) are based on an OLAP cube. Data mining models whose SubClassType is sbclsRelational (that is, relational data mining models) are based on a table from a relational database. The choice of the model's SubClassType determines how the rest of the model's structure is defined. After that, you can determine other elements of the model design, such as the data mining algorithm that the model will use and the roles that will be associated with the model.
OLAP Data Mining Models
To establish the case set for an OLAP data mining model, set the CaseDimension property of the clsMiningModel object to a dimension within a cube; this automatically defines the case level that provides case key columns for the mining model as the last enabled and visible level in the selected dimension. Then set the individual columns in the Columns collection of the clsMiningModel object to refer to dimensions, members, member properties, and measures in the cube to be used for input and predictive information.
Relational Data Mining Models
To establish the case set for a relational data mining model, select the key columns from a table or view in a relational database and then add column objects to the Columns collection that refer to these key columns. Next, add columns that refer to other columns in the table to the Columns collection, to supply input and predictable information to the data mining model.
Examples
A. Creating an OLAP Mining Model Manually
The following example builds a mining model based on the Sales cube in the FoodMart 2000 sample database:
Public Sub CreateOlapMiningModel_1()
'------------------------------------------------------------------------
' Declarations - Identify all of the variables that will be needed to
' create the data mining model.
'------------------------------------------------------------------------
Dim dsoSvr As New DSO.Server ' Server object
Dim dsoDmm As DSO.MiningModel ' Note that because events are needed,
' this object is being invoked directly instead of through an MDStore interface..
Dim dsoColumn As DSO.Column
Dim dsoRole As DSO.Role
Dim dsoNestedCol As DSO.Column
Dim dsoCb As DSO.MDStore
Dim dsoDim As DSO.Dimension
Dim dsoLvl As DSO.Level
'------------------------------------------------------------------------
' Connect to the server and walk through the schema for the cube that the
' data mining model will be based on. Save the references to the
' subordinate objects that will be needed later in this example.
'------------------------------------------------------------------------
'Connect to the server on this computer.
dsoSvr.Connect "LocalHost"
'Select the FoodMart database.
Set dsoDb = dsoSvr.MDStores("Foodmart 2000")
'Select the Sales cube.
Set dsoCb = dsoDb.MDStores("Sales")
'Select the Customers dimensions.
Set dsoDim = dsoCb.Dimensions("Customers")
'Select the Name level of the Customers dimension.
Set dsoLvl = dsoDim.Levels("Name")
'------------------------------------------------------------------------
' Before the model is created, check for a previous incarnation of it.
' If it exists, delete it. Then create a new one.
' Give the new model a new data source, and give it a role.
' Then describe the model for browsing the schema, and declare the
' algorithm that will be used to predict with.
' Finally, set up the OLAP properties that will be needed by the model.
'------------------------------------------------------------------------
'Check for the existence of the model on this computer.
If Not dsoDb.MiningModels("CustSalesModel") Is Nothing Then
'If this model exists, delete it.
dsoDb.MiningModels.Remove "CustSalesModel"
End If
'Create a new mining model called CustSalesModel.
Set dsoDmm = dsoDb.MiningModels.AddNew("CustSalesModel", sbclsOlap)
'Designate FoodMart 2000 as the data source for this mining model.
dsoDmm.DataSources.AddNew "Foodmart 2000"
'Create a new mining model role called All Users.
Set dsoRole = dsoDmm.Roles.AddNew("All Users")
'Describe this new mining model.
dsoDmm.Description = "Analyzes the purchasing behavior of customers"
'use the Decision Trees algorithm in this model.
dsoDmm.MiningAlgorithm = "Microsoft_Decision_Trees"
'Declare that the Sales cube will be used as the source for this model.
dsoDmm.SourceCube = "Sales"
'Declare that the case dimension will be based on the Customers
'dimension from the Sales cube.
dsoDmm.CaseDimension = "Customers"
'Use the Name level of the Customers dimension for cases.
dsoDmm.CaseLevel = "Name"
'Let DSO figure out the training query by leaving this property blank.
dsoDmm.TrainingQuery = ""
'------------------------------------------------------------------------
' Add a new column, Customer Id, to the mining model
' and relate this column to the Name level of the Customers dimension.
' Describe the level's type and make it a key for the model.
'------------------------------------------------------------------------
'Add Customer Id as a new column in the model.
Set dsoColumn = dsoDmm.Columns.AddNew("Customer Id")
'Identify the level in Sales that this column is based on.
Set dsoColumn.SourceOlapObject = dsoLvl
'Identify the type of column this is.
dsoColumn.DataType = adInteger ' This enumeration is from ADO.
'Identify this column as a key.
dsoColumn.IsKey = True
'------------------------------------------------------------------------
' Add a new column to the mining model called Gender and relate this
' column to the Gender member property of the Name level of the
' Customers dimension. Declare that the data in this column is
' statistically discrete.
'------------------------------------------------------------------------
'Add another column to the model.
Set dsoColumn = dsoDmm.Columns.AddNew("Gender")
'Identify the member property of the Customers dimension
'that this column is based on.
'Set the column's description for browsers of the schema.
dsoColumn.Description = "Based on the Gender member property " & _
"of the Name level of the Customers dimension."
Set dsoColumn.SourceOlapObject = dsoLvl.MemberProperties("Gender")
'Identify its type.
dsoColumn.DataType = adWChar
'Make this column related to the Customer Id column.
dsoColumn.RelatedColumn = "Customer Id"
'Identify this column as one containing discrete data.
dsoColumn.ContentType = "DISCRETE"
'------------------------------------------------------------------------
' Add a new column to the mining model called Unit Sales and relate
' this column to the Sales cube measure of the same name. Set the
' columns data type to Integer, and identify the data content in it as
' being continuous and logarithmically normalized. Finally, identify this
' column as being predictable.
'------------------------------------------------------------------------
'Add another column to the model.
Set dsoColumn = dsoDmm.Columns.AddNew("Unit Sales")
'Identify this column as being based on the Unit Sales measure.
Set dsoColumn.SourceOlapObject = dsoCb.Measures("Unit Sales")
'Identify the column type.
dsoColumn.DataType = adInteger
'Identify this column's content as being continuous.
dsoColumn.ContentType = "CONTINUOUS"
'Identify the statistical distribution of this data.
dsoColumn.Distribution = "LOG_NORMAL"
'Identofy the column as being predictable.
dsoColumn.IsPredictable = True
'------------------------------------------------------------------------
' Save the mining model and update its LastUpdated property.
'------------------------------------------------------------------------
'Set the date of last update to today's date.
dsoDmm.LastUpdated = Now
'Save the model definition.
dsoDmm.Update
'------------------------------------------------------------------------
' Lock the cube, process it, and then unlock it.
' Note: During processing a number of events will be fired. These events
' are trapped by the database object's ReportAfter, Report Before,
' ReportProgress, and ReportError events.
'------------------------------------------------------------------------
'Because the model is about to be processed, it must be locked.
dsoDmm.LockObject olapLockProcess, "Processing the data mining model in sample code"
'Fully process the model.
dsoDmm.Process processFull
'Unlock the model after processing is complete.
dsoDmm.UnlockObject
End Sub
B. Creating an OLAP Mining Model Automatically
The following example automatically creates and OLAP mining model based on the Sales cube in the FoodMart2000 database:
Public Sub CreateOlapMiningModel_2()
'------------------------------------------------------------------------
' Declarations - Identify all of the variables that will be needed to
' create the data mining model.
'------------------------------------------------------------------------
Dim dsoSvr As New DSO.Server
Dim dsoDmm As DSO.MiningModel
Dim dsoColumn As DSO.Column
Dim dsoRole As DSO.Role
Dim dsoNestedCol As DSO.Column
'------------------------------------------------------------------------
' Before the model is created, check for a previous incarnation of it.
' If it exists, delete it. Then create a new one.
' Give the new model a new data source, and give it a role.
' Then describe the model for browsing of the schema, and declare the
' algorithm that will be used to predict with.
' Finally, set up the OLAP properties that the model will need.
'------------------------------------------------------------------------
dsoSvr.Connect "LocalHost"
Set dsoDb = dsoSvr.MDStores("Foodmart 2000")
If Not dsoDb.MiningModels("CustSales_Olap2") Is Nothing Then
dsoDb.MiningModels.Remove "CustSales_Olap2"
End If
Set dsoDmm = dsoDb.MiningModels.AddNew("CustSales_Olap2", sbclsOlap)
'Create a new mining model role called All Users.
Set dsoRole = dsoDmm.Roles.AddNew("All Users")
dsoDmm.Description = "Analyzes the purchasing behavior of customers"
dsoDmm.MiningAlgorithm = "Microsoft_Decision_Trees"
dsoDmm.SourceCube = "Sales"
dsoDmm.CaseDimension = "Customers"
dsoDmm.TrainingQuery = "" 'Let DSO figure out the training query.
'------------------------------------------------------------------------
' In this next step, the update method checks to see whether there are any
' columns in the columns collection. In this case, because there are not
' any, the update method will automatically add columns based on the
' structure of the Sales cube.
'------------------------------------------------------------------------
dsoDmm.Update 'Let DSO automatically populate the Columns collection.
'Enable the Products dimension.
'Set dsoColumn = dsoDmm.Columns("Products")
'dsoColumn.IsDisabled = False
'Make the Unit Sales measure predictable.
Set dsoColumn = dsoDmm.Columns("Unit Sales")
'Enable the column.
dsoColumn.IsDisabled = False
'Make the column predictable.
dsoColumn.IsPredictable = True
' Set the last updated date to today's date.
dsoDmm.LastUpdated = Now
' Save the model's metadata.
dsoDmm.Update
'------------------------------------------------------------------------
' Lock the cube, process it, and then unlock it.
' Note: During processing a number of events will be fired. These events
' are trapped by the database object's ReportAfter, ReportBefore,
' ReportProgress, and ReportError events.
'------------------------------------------------------------------------
'Because the model is about to be processed, lock it.
dsoDmm.LockObject olapLockProcess, "Processing the data mining model in sample code"
'Process the model.
dsoDmm.Process processFull
'Unlock the model.
dsoDmm.UnlockObject
End Sub
请参阅
参考
AddNew (clsCollection)
clsColumn