【问题标题】:Data Mining issue with the apriori algorithm in C#C#中先验算法的数据挖掘问题
【发布时间】:2015-09-12 13:49:42
【问题描述】:

我正在用 C# 创建自己的先验算法实现。对于这个项目,我不允许将其他库等用于先验算法。

下面是我的testData.json。请注意,这些是字符串,这意味着我的项目集可能不仅仅是像 A 这样的字符,而是像 candy 这样的单词。

注意:我将在测试时使用20 (20%) 支持。

{
    "transactions": [
        [ "B", "C" ],
        [ "B", "C", "D" ],
        [ "A", "D" ],
        [ "A", "B", "C", "D" ],
        [ "C", "D" ],
        [ "C", "D", "E" ],
        [ "A", "B" ]
    ]
}

当我单击一个按钮来处理数据以及我需要的值 minSupportminConfidence(还不需要)时,我将我的 JSON 反序列化为一个对象并将其保存到一个名为 database 的公共变量中数据库类。

public class Database
{
    public List<List<string>> transactions { get; set; }
}

单击按钮时,我调用方法GenerateCandidateItemSet() 这是我遇到问题的地方。

private Dictionary<string, int> C1 = new Dictionary<string, int>();
private void GenerateCandidateItemSet()
{
    foreach (List<string> transaction in database.transactions)
    {
        foreach (string item in transaction)
        {
            if (C1.ContainsKey(item))
            {
                C1[item]++;
            }
            else
            {
                C1.Add(item, 1);
            }
        }
    }

    // Check our frequency, remove items with low support
    foreach (string key in C1.Keys.ToList())
    {
        double frequency = (C1[key] * 100) / (database.transactions.Count);
        if (frequency < minSupport)
        {
            C1.Remove(key);
        }
    }

    // Pairing check stuff
    List<string[]> itemPairs = new List<string[]>();
    List<string> items = C1.Keys.ToList();

    foreach (string item in items)
    {
        // FIX THIS LOOP LATER TO CONTAIN ALL PAIRS
        List<string> itemArray = new List<string>();
        if (item != items.Last())
        {
            itemArray.Add(item);
            itemArray.Add(items[items.IndexOf(item) + 1]);
            itemPairs.Add(itemArray.ToArray());
        }
    }
    GenerateItemSetRecursive(itemPairs);
}

就在该部分之前://Pairing check stuff C1 的值是:

当循环完成时,我需要得到类似的东西:

BC, BD, BA, CD, CA, DA

如果我要插入AB, AD, BC, BD, CD,结果将是ABD, BCD等等。

基本上,我需要为交易找到Frequent Itemsets

问题:考虑到我的 itemPairs 只得到 BC, CD, DA,而不是 BC, BD, BA, CD, CA, DA,我知道我的逻辑是错误的。我的循环会是什么样子才能让它工作?

【问题讨论】:

    标签: c# json algorithm data-mining apriori


    【解决方案1】:

    正如你所指出的,C1.Keys.ToList() 给你{"B", "C", "D", "A"}

    您的代码正在做的是迭代该列表并添加下一个元素以创建一对(假设它不是最​​后一个元素。

    单步执行您的代码 - 您会看到第一次迭代为您提供{"B", "C"},下一次迭代为您提供{"C", "D"},之后的一次为您提供{"D", "A"}。最后一次迭代将针对列表的最后一个元素,因此items.Last() 将评估为真,并且不会添加任何内容。

    使您现在可以使用的一种简单方法是在损坏的循环中添加另一个循环。意图是,当您迭代 "B" 时,您不仅添加了 {"B", "C"},还添加了 {"B", "D"}{"B", "A"},同样,您对 "C" 的外部迭代将找到 {"C", "D"}{"C", "A"} .

    我希望这会有所帮助 - 如果您对此仍有疑问,请随时在 C# 聊天中联系我。

    【讨论】:

      猜你喜欢
      • 2013-01-29
      • 2011-05-04
      • 2011-07-28
      • 2011-07-25
      • 2011-08-25
      • 2012-08-04
      • 2015-05-22
      • 2011-01-13
      • 2019-04-24
      相关资源
      最近更新 更多