gpt4 book ai didi

c# - 使用ID3算法、Accord.Net框架进行预测

转载 作者:行者123 更新时间:2023-11-30 09:36:55 25 4
gpt4 key购买 nike

我使用以下代码来查找给定商品在更改商品的价格、折扣和广告时的预期销售额。这是使用 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"); 时,它会给我一个输出。我想请问这是什么原因?我的做法有错吗?我还想知道在数据表中组织数据以获得准确结果的最低要求是什么。

最佳答案

我尝试运行你的代码 - 但我得到一个异常而不是任何输出。看着它,我认为问题在于,当您创建 Codification 时,您没有指定要包含哪些列,因此它包含 RowKey 列,然后没有任何内容排列。相反,使用以下命令创建编码:

Codification codebook = new Codification(data, "Brand", "PriceRange", "Discount", "Advertisement", "ExpSales");

然后它似乎就起作用了。

当我再次尝试您的 int[] query = codebook.Translate("fff","eee","ffg","qqq"); 示例时,我只是得到一个异常(因为这些值不存在) - 所以我认为你必须有一个异常处理程序来向你隐藏这些问题。

就获得准确结果的最少数据而言,这实际上取决于数据的复杂程度以及其中包含的噪声量。您需要针对一组数据训练模型,然后针对完全不同的数据集测试其准确性,以衡量其是否有效。

关于c# - 使用ID3算法、Accord.Net框架进行预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37770940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com