📄 dm.cs
字号:
using System;
namespace DM.NaiveBayes
{
/// <summary>
/// Summary description for DM.
/// </summary>
public abstract class DataMining
{
/// <summary>
/// Imports a training dataset from an xml file for the Naive Bayes Classifier
/// </summary>
/// <param name="sampleXmlFilePath">
/// Full path pointing to an xml file containing training data for the Naive Bayes Classifier
/// </param>
/// <returns>
/// A System.Data.DataSet containing training data for the Naive Bayes Classifier
/// </returns>
public abstract System.Data.DataSet Import(string sampleXmlFilePath);
/// <summary>
/// Create a statistics table
/// </summary>
/// <param name="attributesXmlFilePath">
/// Full path pointing to an xml file that describes the attributes of the training data
/// </param>
/// <returns>
/// A System.Data.Dataset containing the attributes of the DataSet to train and classify
/// </returns>
public abstract System.Data.DataSet CreateStatistics(string attributesXmlFilePath);
public abstract System.Data.DataSet Train(System.Data.DataSet sampleDataSet, System.Data.DataSet statisicsDataSet);
public abstract System.Data.DataSet Classify (System.Data.DataSet newDataSet, System.Data.DataSet trainingDataSet);
public abstract void Export(System.Data.DataSet data, string exportXmlFilePath);
public bool Process(string sampleXmlFilePath, string attributesXmlFilePath, System.Data.DataSet newDataSet)
{
try
{
System.Data.DataSet importData = Import(sampleXmlFilePath);
System.Data.DataSet statsData = CreateStatistics(attributesXmlFilePath);
System.Data.DataSet trainData = Train(importData, statsData);
System.Data.DataSet classifiedData = Classify(newDataSet, trainData);
return true;
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
return false;
}
}
}
public class NaiveBayes : DataMining
{
private System.Data.DataSet _statistics = null;
private System.Data.DataRow [] _classValues = null;
private System.Data.DataRow [] _attributeValues = null;
public override System.Data.DataSet Import(string xmlFilePath)
{
System.Data.DataSet importData = new System.Data.DataSet();
importData.ReadXml(xmlFilePath, System.Data.XmlReadMode.InferSchema);
return importData;
}
public override System.Data.DataSet CreateStatistics(string attributeValueFilePath)
{
//ts.tableCollection[0].rowCollection[0][1] = "NOMINAL" -- TYPE
//ts.tableCollection[0].rowCollection[0][1] = "CUSTOMERINCOME" -- NAME
//ts.tableCollection[0].rowCollection[0][2] = "LOW" -- VALUE
System.Data.DataSet statisicsDataSet = new System.Data.DataSet();
statisicsDataSet.ReadXml(attributeValueFilePath, System.Data.XmlReadMode.InferSchema);
System.Data.DataRow [] cType = statisicsDataSet.Tables[0].Select("TYPE=" + "'" + "CLASS" + "'", "TYPE ASC");
this._classValues = cType;
this._statistics = statisicsDataSet;
for(int count = 0; count < cType.Length; count++)
{
//Add Columns
_statistics.Tables[0].Columns.Add( "D-" + cType[count][2], typeof(string) );
_statistics.Tables[0].Columns.Add( "P-" + cType[count][2], typeof(string) );
}
foreach(System.Data.DataColumn dc in _statistics.Tables[0].Columns)
{
System.Diagnostics.Debug.WriteLine(dc.ColumnName.ToString());
}
statisicsDataSet = null;
cType = null;
return _statistics;
}
public override System.Data.DataSet Train(System.Data.DataSet sampleDataSet, System.Data.DataSet statisicsDataSet)
{
//get the row of attributes and values
System.Data.DataRow [] cType = statisicsDataSet.Tables[0].Select("TYPE <> " + "'" + "CLASS" + "'", "NAME ASC");
this._attributeValues = cType;
System.Text.StringBuilder filters = null;
System.Text.StringBuilder summary = null;
string [] filter = new string [cType.Length];
int [] total = new int [cType.Length];
for(int count = 0; count < cType.Length; count++)
{
filter[count] = this._attributeValues[count][1].ToString() + " = " + "'" + this._attributeValues[count][2].ToString() + "'";
for(int cCount = 0; cCount < this._classValues.Length; cCount++)
{
filters = new System.Text.StringBuilder (24);
filters.Append(filter[count].ToString() + " and " + this._classValues[cCount][1].ToString() + " = " + "'" + this._classValues[cCount][2].ToString() + "'");
total[count] = sampleDataSet.Tables[0].Select(filters.ToString()).Length;
System.Diagnostics.Debug.WriteLine("Filter : " + filters.ToString() + " Total : " + total[count].ToString());
summary = new System.Text.StringBuilder (24);
summary.Append("NAME = " + "'" + this._attributeValues[count][1].ToString() + "'" + " and " + "VALUE = " + "'" + this._attributeValues[count][2].ToString() + "'");
//update the right row in the statistics table
System.Data.DataRow [] row = statisicsDataSet.Tables[0].Select(summary.ToString());
System.Diagnostics.Debug.WriteLine("Summary Filter : " + summary.ToString() + " : " + row.Length.ToString() + " rows selected" );
//TODO: Not completed
}
}
cType = null;
filters = null;
filter = null;
return statisicsDataSet;
}
public override System.Data.DataSet Classify(System.Data.DataSet newDataSet, System.Data.DataSet trainingDataSet)
{
System.Diagnostics.Debug.WriteLine("TODO: Classify Method");
return null;
}
public override void Export(System.Data.DataSet data, string exportXmlFilePath)
{
System.Diagnostics.Debug.WriteLine("TODO: Export Method");
return;
}
}
public class Classifier : NaiveBayes
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -