cs-decision-tree
1.0.1
Decision Trees ID3 and C45
Install-Package cs-decision-tree -Version 1.0.1
dotnet add package cs-decision-tree --version 1.0.1
<PackageReference Include="cs-decision-tree" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add cs-decision-tree --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
cs-decision-tree
Decision Trees (ID3, C45) implemented in C#
Install
Run the following nuget command:
Install-Package cs-decision-tree
Usage
The sample codes below shows how to use ID3 and C45 to do classification:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DecisionTree.Demo
{
using System.Xml;
using System.IO;
using DecisionTree;
using Lang;
public class DecisionTreeTest
{
public static List<DDataRecord> LoadSample()
{
XmlDocument doc = new XmlDocument();
doc.Load("database.xml");
List<DDataRecord> records = new List<DDataRecord>();
XmlElement xml_root = doc.DocumentElement;
foreach (XmlElement xml_level1 in xml_root.ChildNodes)
{
if (xml_level1.Name == "record")
{
String outlook = xml_level1.Attributes["outlook"].Value;
string temperature = xml_level1.Attributes["temperature"].Value;
string humidity = xml_level1.Attributes["humidity"].Value;
String windy = xml_level1.Attributes["windy"].Value;
String class_label = xml_level1.Attributes["class"].Value;
DDataRecord rec = new DDataRecord();
rec["outlook"]=outlook;
rec["windy"]=windy;
rec["temperature"]=temperature;
rec["humidity"]=humidity;
rec.Label = class_label;
records.Add(rec);
}
}
return records;
}
public static void RunC45()
{
List<DDataRecord> records = LoadSample();
C45<DDataRecord> algorithm = new C45<DDataRecord>();
algorithm.UpdateContinuousAttributes(records, "temperature");
algorithm.UpdateContinuousAttributes(records, "humidity");
algorithm.Train(records);
//algorithm.RulePostPrune(records); //post pruning using cross valiation set
Console.WriteLine("C4.5 Tree Built!");
for(int i=0; i<records.Count; i++)
{
DDataRecord rec=records[i];
Console.WriteLine("rec: ");
string[] feature_names = rec.FindFeatures();
foreach(string feature_name in feature_names)
{
Console.WriteLine(feature_name+" = " + rec[feature_name]);
}
Console.WriteLine("Label: " + rec.Label);
Console.WriteLine("Predicted Label: " + algorithm.Predict(records[i]));
Console.WriteLine();
}
}
public static void RunID3()
{
List<DDataRecord> X = LoadSample();
//As ID3 does not support continuous value, must do manually conversion
foreach (DDataRecord rec in X)
{
int temperature = int.Parse(rec["temperature"]);
int humidity = int.Parse(rec["humidity"]);
if (temperature < 75)
{
rec["temperature"]="< 75";
}
else
{
rec["temperature"]=">= 75";
}
if (humidity < 80)
{
rec["humidity"]="< 80";
}
else
{
rec["humidity"]=">= 80";
}
}
ID3<DDataRecord> algorithm = new ID3<DDataRecord>();
algorithm.Train(X);
//algorithm.ErrorReducePrune(Xval); //error reduce prune using cross valiation set
Console.WriteLine("ID3 Tree Built!");
for (int i = 0; i < X.Count; i++)
{
DDataRecord rec = X[i];
Console.WriteLine("rec: ");
string[] feature_names = rec.FindFeatures();
foreach(string feature_name in feature_names)
{
Console.WriteLine(feature_name + " = " + rec[feature_name]);
}
Console.WriteLine("Label: " + rec.Label);
Console.WriteLine("Predicted Label: " + algorithm.Predict(X[i]));
Console.WriteLine();
}
algorithm.WriteToXml("ID3.xml");
}
}
}
cs-decision-tree
Decision Trees (ID3, C45) implemented in C#
Install
Run the following nuget command:
Install-Package cs-decision-tree
Usage
The sample codes below shows how to use ID3 and C45 to do classification:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DecisionTree.Demo
{
using System.Xml;
using System.IO;
using DecisionTree;
using Lang;
public class DecisionTreeTest
{
public static List<DDataRecord> LoadSample()
{
XmlDocument doc = new XmlDocument();
doc.Load("database.xml");
List<DDataRecord> records = new List<DDataRecord>();
XmlElement xml_root = doc.DocumentElement;
foreach (XmlElement xml_level1 in xml_root.ChildNodes)
{
if (xml_level1.Name == "record")
{
String outlook = xml_level1.Attributes["outlook"].Value;
string temperature = xml_level1.Attributes["temperature"].Value;
string humidity = xml_level1.Attributes["humidity"].Value;
String windy = xml_level1.Attributes["windy"].Value;
String class_label = xml_level1.Attributes["class"].Value;
DDataRecord rec = new DDataRecord();
rec["outlook"]=outlook;
rec["windy"]=windy;
rec["temperature"]=temperature;
rec["humidity"]=humidity;
rec.Label = class_label;
records.Add(rec);
}
}
return records;
}
public static void RunC45()
{
List<DDataRecord> records = LoadSample();
C45<DDataRecord> algorithm = new C45<DDataRecord>();
algorithm.UpdateContinuousAttributes(records, "temperature");
algorithm.UpdateContinuousAttributes(records, "humidity");
algorithm.Train(records);
//algorithm.RulePostPrune(records); //post pruning using cross valiation set
Console.WriteLine("C4.5 Tree Built!");
for(int i=0; i<records.Count; i++)
{
DDataRecord rec=records[i];
Console.WriteLine("rec: ");
string[] feature_names = rec.FindFeatures();
foreach(string feature_name in feature_names)
{
Console.WriteLine(feature_name+" = " + rec[feature_name]);
}
Console.WriteLine("Label: " + rec.Label);
Console.WriteLine("Predicted Label: " + algorithm.Predict(records[i]));
Console.WriteLine();
}
}
public static void RunID3()
{
List<DDataRecord> X = LoadSample();
//As ID3 does not support continuous value, must do manually conversion
foreach (DDataRecord rec in X)
{
int temperature = int.Parse(rec["temperature"]);
int humidity = int.Parse(rec["humidity"]);
if (temperature < 75)
{
rec["temperature"]="< 75";
}
else
{
rec["temperature"]=">= 75";
}
if (humidity < 80)
{
rec["humidity"]="< 80";
}
else
{
rec["humidity"]=">= 80";
}
}
ID3<DDataRecord> algorithm = new ID3<DDataRecord>();
algorithm.Train(X);
//algorithm.ErrorReducePrune(Xval); //error reduce prune using cross valiation set
Console.WriteLine("ID3 Tree Built!");
for (int i = 0; i < X.Count; i++)
{
DDataRecord rec = X[i];
Console.WriteLine("rec: ");
string[] feature_names = rec.FindFeatures();
foreach(string feature_name in feature_names)
{
Console.WriteLine(feature_name + " = " + rec[feature_name]);
}
Console.WriteLine("Label: " + rec.Label);
Console.WriteLine("Predicted Label: " + algorithm.Predict(X[i]));
Console.WriteLine();
}
algorithm.WriteToXml("ID3.xml");
}
}
}
Release Notes
Decision Trees ID3 and C45 in .NET 4.6.1
Dependencies
This package has no dependencies.
Used By
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version History
Version | Downloads | Last updated |
---|---|---|
1.0.1 | 579 | 4/29/2018 |