Python - 清理数据以运行先验算法

标签 python data-cleaning apriori

我有一组文章中使用的所有单词的主列表,现在我正在尝试计算每篇文章中主列表中每个单词的出现次数。然后我将尝试在数据上建立一些关联规则。例如,我的数据可能如下所示:

master_wordlist = ['dog', 'cat', 'hat', 'bat', 'big']
article_a = ['dog', 'cat', 'dog','big']
article_b = ['dog', 'hat', 'big', 'big', 'big']

我需要将我的数据转换为这种格式:

Article        dog    cat    hat    bat    big
article_a      2      1      0      0      1
article_b      1      0      1      0      3

我正在努力进行这种转换,我一直在使用 nltk,但我不知道如何获得包含不存在单词的计数。任何帮助将不胜感激!

最佳答案

您可以在此处使用collections.Counter:

from collections import Counter
master_wordlist = ['dog', 'cat', 'hat', 'bat', 'big']
article_a = ['dog', 'cat', 'dog','big']
article_b = ['dog', 'hat', 'big', 'big', 'big']

c_a = Counter(article_a)
c_b = Counter(article_b)

print [c_a[x] for x in master_wordlist]
print [c_b[x] for x in master_wordlist]

输出:

[2, 1, 0, 0, 1]
[1, 0, 1, 0, 3]

关于Python - 清理数据以运行先验算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16511279/

相关文章:

python - 是否有推荐的做法或框架来实现 google-app-engine db.Model/ndb.Model 的记录级权限)?

python-3.x - 如何识别200多个数值变量中的类别变量?

python - 用中位数替换 NaN 值?

评估 elat 和 apriori 项集时删除具有空值的行

python - 它叫什么,通过 NLP 从 HTML 中提取地址

python - Tkinter 网格不工作

python - 如何将元音的大写保持在同一位置?

Python Pandas : How to drop the *correct* duplicate row?

r - 将 r 中的数据帧转换为事务或 itemMatrix?

Python Apyori 按提升排序