使用 ID3 算法进行预测,Accord.Net 框架
Posted
技术标签:
【中文标题】使用 ID3 算法进行预测,Accord.Net 框架【英文标题】:Prediction using ID3 algorithm,Accord.Net framework 【发布时间】:2016-06-12 05:09:12 【问题描述】:在更改商品的价格、折扣和广告时,我正在使用以下代码来查找给定商品的预期销售额。这是通过使用 Accord.Net 库的 ID3 算法实现的。
namespace PnredictionSales
public partial class WebForm1 : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
DataTable data = new DataTable("Sales prediction Example");
data.Columns.Add("RowKey");
data.Columns.Add("Brand");
data.Columns.Add("PriceRange");
data.Columns.Add("Discount");
data.Columns.Add("Advertisement");
data.Columns.Add("ExpSales");
// data.Columns.Add("Wind");
// data.Columns.Add("PlayTennis");
data.Rows.Add("D1", "Highland", "R1", "yes", "No", "B");
data.Rows.Add("D2", "Highland", "R1", "yes", "yes", "C");
data.Rows.Add("D3", "Anchor", "R1", "yes", "No", "B");
data.Rows.Add("D4", "Flora", "R2", "yes", "No", "B");
data.Rows.Add("D5", "Flora", "R3", "No", "No", "A");
data.Rows.Add("D6", "Flora", "R3", "No", "yes", "A");
data.Rows.Add("D7", "Anchor", "R3", "No", "yes", "A");
data.Rows.Add("D8", "Highland", "R2", "yes", "No", "B");
data.Rows.Add("D9", "Highland", "R3", "No", "No", "A");
data.Rows.Add("D10", "Flora", "R2", "No", "No", "B");
data.Rows.Add("D11", "Highland", "R2", "No", "yes", "B");
data.Rows.Add("D12", "Anchor", "R2", "yes", "yes", "A");
data.Rows.Add("D13", "Anchor", "R1", "No", "No", "B");
data.Rows.Add("D14", "Flora", "R2", "yes", "yes", "A");
Codification codebook = new Codification(data);
DecisionVariable[] attributes =
new DecisionVariable("Brand", 3), new DecisionVariable("PriceRange",3),
new DecisionVariable("Discount",2),new DecisionVariable("Advertisement",2)
;
int classCount=3; // 2 possible output values for playing tennis: yes or no
DecisionTree tree = new DecisionTree(attributes, classCount);
// Create a new instance of the ID3 algorithm
ID3Learning id3learning = new ID3Learning(tree);
// Translate our training data into integer symbols using our codebook:
DataTable symbols = codebook.Apply(data);
int[][] inputs = symbols.ToIntArray("Brand", "PriceRange","Discount","Advertisement");
int[] outputs = symbols.ToIntArray("ExpSales").GetColumn(0);
// Learn the training instances!
id3learning.Run(inputs, outputs);
int[] query = codebook.Translate("Flora","R1","yes","No");
int output = tree.Compute(query.ToDouble());
string answer = codebook.Translate("ExpSales", output); // answer will be "No".
Label1.Text = answer;
我的问题是:
当我将任何字符串值放入int[] query = codebook.Translate("fff","eee","ffg","qqq");
时,它会给我一个输出。我想这是什么原因?我的方法错了吗?
另外我想知道在数据表中组织数据以获得准确结果的最低要求是什么。
【问题讨论】:
【参考方案1】:我尝试运行您的代码 - 但我得到一个异常而不是任何输出。看着它,我认为问题在于,当您创建 Codification 时,您没有指定要包含哪些列,因此它包含 RowKey 列,然后没有任何内容。而是使用以下代码创建编码:
Codification codebook = new Codification(data, "Brand", "PriceRange", "Discount", "Advertisement", "ExpSales");
然后它似乎工作了。
当我再次尝试您的 int[] query = codebook.Translate("fff","eee","ffg","qqq");
示例时,我得到了一个异常(因为这些值不存在) - 所以我认为您必须有一个异常处理程序来隐藏这些问题。
就获得准确结果的最少数据而言 - 这实际上取决于数据的复杂程度以及它包含多少噪音。您需要针对一组数据训练模型,然后针对一组完全不同的数据测试其准确性,以衡量其是否有效。
【讨论】:
以上是关于使用 ID3 算法进行预测,Accord.Net 框架的主要内容,如果未能解决你的问题,请参考以下文章
如何使用朴素贝叶斯和主成分分析(C#、Accord.NET)对文档进行分类