Accord C4.5決策樹算法(C# C4.5決策樹算法)


        算法直接調用Accord.net Framework封裝好的方法,所以要下載Accord機器學習的DLL,下載地址見Accord官網。C4.5算法要引用的包括:
using Accord.IO;
using Accord.MachineLearning.DecisionTrees;
using Accord.MachineLearning.DecisionTrees.Learning;
using Accord.MachineLearning.DecisionTrees.Rules;
using Accord.Math;
using Accord.Statistics.Analysis;
using Accord.Statistics.Filters;
using AForge;
using System.Numerics;

      

        我是寫成winform窗體程序形式的,實現了五個按鈕的Click()事件,分別用於:

1. MenuFileOpen_Click ():導入測試數據集;

2. upAttributeType_Click():導入測試數據的屬性類別,屬性類別分為Continuous(數值型)和Symbolic(符號型);

3. upTestingData_Click():導入訓練數據集;

4. btnCreate_Click():建立分類模型;

5. btnTestingRun_Click():測試模型。



算法代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Accord.IO;
using Accord.MachineLearning.DecisionTrees;
using Accord.MachineLearning.DecisionTrees.Learning;
using Accord.MachineLearning.DecisionTrees.Rules;
using Accord.Math;
using Accord.Statistics.Analysis;
using Accord.Statistics.Filters;
using AForge;
using System.Numerics;



namespace 粗糙集屬性選擇_V1._0.DecisionTrees
{
public partial class DecisionTreesMainForm : DockContent
{
// 初始化樹
DecisionTree tree;

//訓練數據表
DataTable myDT = new DataTable();
//屬性類別表
 DataTable myDTAttType = new DataTable();
//測試數據表
 DataTable myTestDT = new DataTable();

//屬性數值化的矩陣
Codification codebook;


public DecisionTreesMainForm()
{
InitializeComponent();

dgvLearningSource.AutoGenerateColumns = true;
dgvPerformance.AutoGenerateColumns = false;

openFileDialog.InitialDirectory = Path.Combine(Application.StartupPath, "Resources");
}


/// <summary>
/// 進行學習並創建決策樹
/// </summary>
///
private void btnCreate_Click(object sender, EventArgs e)
{
if (dgvLearningSource.DataSource == null)
{
MessageBox.Show("請先導入訓練數據!");
return;
}
if (attributeTypeGridView.DataSource == null) {
MessageBox.Show("請先導入數據的屬性類型!");
return;
}

// 完成和給定的數據保存修改
dgvLearningSource.EndEdit();

// MessageBox.Show(myDT.Columns.Count + "");

//初始化columnNames
int myDTColNum = myDT.Columns.Count;
String[] colNM = new string[myDTColNum];

for (int i = 0; i < myDTColNum; i++) {
colNM[i] = myDT.Columns[i].ColumnName;
}


//把屬性類別放入hashMap
Dictionary<String, String> attTypeMap = new Dictionary<string, string>();
for (int i = 0, attTypeSum = myDTAttType.Rows.Count;i < attTypeSum ; i++) {
attTypeMap.Add(myDTAttType.Rows[i][0].ToString(),myDTAttType.Rows[i][1].ToString());
}


// 對屬性進行數值化
try
{
codebook = new Codification(myDT, colNM);
}
catch (Exception ex) {
MessageBox.Show("運行出現異常!" + ex.Message);
}

DecisionVariable[] attributes = new DecisionVariable[myDTColNum - 1];
for (int i = 0; i < myDTColNum - 1; i++)
{
String getAttType;
attTypeMap.TryGetValue(colNM[i], out getAttType);
//Console.WriteLine("----" + getAttType);
if (getAttType.Equals("Symbolic"))
{
attributes[i] = new DecisionVariable(colNM[i], codebook[colNM[i]].Symbols);
}
else {
attributes[i] = new DecisionVariable(colNM[i], DecisionVariableKind.Continuous);
}

}


int classCount = codebook[colNM[myDTColNum - 1]].Symbols;

  // 創建決策樹
tree = new DecisionTree(attributes, classCount);

//創建C4.5決策樹
C45Learning c45 = new C45Learning(tree);

// 進行訓練學習
DataTable symbols = codebook.Apply(myDT);
String[] inputColStr = new string[myDTColNum - 1];
for (int i = 0; i < myDTColNum - 1; i++) {
inputColStr[i] = colNM[i];
// MessageBox.Show(inputColStr[i]);
}
double[][] inputs = symbols.ToArray(inputColStr);
int[] outputs = symbols.ToArray<int>(colNM[myDTColNum - 1]);

double error = c45.Run(inputs, outputs);

//MessageBox.Show(codebook.Translate("A1","yong")+"");



// 顯示樹結構
decisionTreeView1.TreeSource = tree;

lbStatus.Text = "樹創建完成,可以點擊其它選項查看結果!";

}


/// <summary>
/// 測試模型.
/// </summary>
///
private void btnTestingRun_Click(object sender, EventArgs e)
{
if (tree == null)
{
MessageBox.Show("請先建立模型!");
return;
}

if (dgvTestingSource.DataSource == null) {
MessageBox.Show("請先上傳測試數據!");
return;
}


int testDTRowSum = myTestDT.Rows.Count;
int testDTColSum = myTestDT.Columns.Count;

int[] expected = new int[testDTRowSum];
for (int i = 0; i < testDTRowSum; i++) {
//將測試數據的每一條記錄的類別轉化成決策樹中的類別(類標)
 expected[i] = codebook.Translate(myTestDT.Columns[testDTColSum - 1].ColumnName, myTestDT.Rows[i][testDTColSum - 1].ToString());
//Console.WriteLine("expected-----" + expected[i]);
}

int[] actual = new int[testDTRowSum];
String[] myTestDTcolumnsName = new string[testDTColSum];
for(int i=0;i < testDTColSum;i++){
myTestDTcolumnsName[i] = myTestDT.Columns[i].ColumnName;
}

 for (int i = 0; i < testDTRowSum; i++)
            {
                try
                {
                    actual[i] = tree.Compute(codebook.Translate(myTestDT.Rows[i], myTestDTcolumnsName));
                }
                catch (Exception ex)
                {
                    MessageBox.Show("出現異常!" + ex.Message);
                }
            }

            // 計算分類性能.
            ConfusionMatrix confusionMatrix = new ConfusionMatrix(actual, expected, 1, 0);
            dgvPerformance.DataSource = new[] { confusionMatrix };
        }





        //導入數據集
        private void MenuFileOpen_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string filename = openFileDialog.FileName;
                string extension = Path.GetExtension(filename);
                if (extension == ".xls" || extension == ".xlsx")
                {
                    ExcelReader db = new ExcelReader(filename, true, false);
                    TableSelectDialog t = new TableSelectDialog(db.GetWorksheetList());

                    if (t.ShowDialog(this) == DialogResult.OK)
                    {
                        DataTable tableSource = db.GetWorksheet(t.Selection);

                        this.dgvLearningSource.DataSource = tableSource;
                        myDT = tableSource.Copy();

                    }
                }
            }

            lbStatus.Text = "導入訓練數據集后,點擊“創建樹”按鈕后開始創建樹!";
        }

        //導入屬性類別
        private void upAttributeType_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string filename = openFileDialog.FileName;
                string extension = Path.GetExtension(filename);
                if (extension == ".xls" || extension == ".xlsx")
                {
                    ExcelReader db = new ExcelReader(filename, true, false);
                    TableSelectDialog t = new TableSelectDialog(db.GetWorksheetList());

                    if (t.ShowDialog(this) == DialogResult.OK)
                    {
                        DataTable tableSource = db.GetWorksheet(t.Selection);
                        this.attributeTypeGridView.DataSource = tableSource;
                        myDTAttType = tableSource.Copy();

                    }
                }
            }

        }

        //導入測試數據集
        private void upTestingData_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string filename = openFileDialog.FileName;
                string extension = Path.GetExtension(filename);
                if (extension == ".xls" || extension == ".xlsx")
                {
                    ExcelReader db = new ExcelReader(filename, true, false);
                    TableSelectDialog t = new TableSelectDialog(db.GetWorksheetList());

                    if (t.ShowDialog(this) == DialogResult.OK)
                    {
                        DataTable tableSource = db.GetWorksheet(t.Selection);
                        this.dgvTestingSource.DataSource = tableSource;
                        myTestDT = tableSource.Copy();
                    }
                }
            }
        }
    }
}

 


程序可以參考:https://github.com/accord-net/framework/blob/development/Unit%20Tests/Accord.Tests.MachineLearning/DecisionTrees/C45LearningTest.cs


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com