【发布时间】:2019-05-09 11:22:58
【问题描述】:
试图找出项目之间的相关性,这些项目被结构化为数据集的一行。想要找出项目集之间频率的相关性。
我不得不承认我完全迷路了,到目前为止,我已经尝试在谷歌上搜索近 6 个小时来寻找解决方案。
已尝试说明以下数据:
#create a table
data = {'Customer': [1, 2, 3, 4],
'Order': ['1 Hamburger, 1 Soda',
'1 Soda, 1 Hamburger, 1 Fries',
'1 Pizza, 2 Soda',
'1 Soda, 1 Ice Cream']}
表:
Customer Order
0 1 1 Hamburger, 1 Soda
1 2 1 Soda, 1 Hamburger, 1 Fries
2 3 1 Pizza, 2 Soda
3 4 1 Soda, 1 Ice Cream
所以这里我们可以看到订单列中的商品没有分开。 所以我用逗号分隔它们。
new = df["Order"].str.split(",", n = -1, expand = True)
new.columns.astype('str')
new.rename(columns=lambda x: 'Item'+str(x), inplace=True)
在订单栏拆分商品后,我把商品前面的数字去掉了,如下:
for i in list(new):
new[i] = new[i].map(lambda x: x.lstrip()[1:] if x is not None else None)
返回此表:
Item0 Item1 Item2
0 Hamburger Soda None
1 Soda Hamburger Fries
2 Pizza Soda None
3 Soda Ice Cream None
到目前为止一切顺利(我认为),现在问题来了。
我想看看多久,例如汉堡包和苏打水是一起买的。 这意味着我可以查看项目之间的相关性,例如,我可以查看汉堡包和薯条之间的相关性。
到目前为止,我认为解决方案可能是这样的(必须手动制作表格,因为我在 Pandas 中没有找到方法):
example = {'Hamburger': [1,1,0,0],
'Soda': [1,1,1,1],
'Pizza': [0,0,1,0],
'Fries': [0,1,0,0],
'Ice Cream': [0,0,0,1]}
Hamburger Soda Pizza Fries Ice Cream
0 1 1 0 0 0
1 1 1 0 1 0
2 0 1 1 0 0
3 0 1 0 0 1
有没有很好的方式来展示物品的相关性?
我是否需要将其转换为例如0 和 1 就像我在上表中所做的那样,如果是这样,假设数据集是 100 万行,那么最好的方法是什么?
我也担心每行项目数量不均的影响,结果是否会因行的差异而出现偏差?
例如上表中汉堡包和苏打水在第 1 行,而在第 2 行还包括薯条,这对相关性有何影响?
【问题讨论】:
标签: python python-3.x pandas statistics