【问题标题】:Group users by purchases with ML in Python在 Python 中使用 ML 按购买对用户进行分组
【发布时间】:2018-05-04 07:49:36
【问题描述】:

我有一个购买数据集

user_id, item_id
==================
1, 1
1, 2
1, 3
2, 2
2, 3
3, 8
3, 9
4, 8
4, 9

由此,我想创建一些“集群”。从数据上看,用户1和2很相似,用户3和4也很相似。

我不知道如何使用 Python 中的机器学习创建这种分析。

例如,我猜它可能是与 like 的距离

   1, 2, 3, 4
1, -, ?, ?, ?
2, ?, -, ?, ?
3, ?, ?, -, ?
4, ?, ?, ?, -

这样我就可以确定每个用户与其他用户的相似程度。

我想要的是根据他们的购买来确定各种用户是否属于某些组。例如,如果一些用户购买了许多与婴儿相关的物品,他们可能是新妈妈/父亲,而购买许多与软件相关的书籍的用户可能是 IT 专业人士/学生。

【问题讨论】:

  • 你尝试过 k-means 聚类吗?

标签: python python-3.x algorithm machine-learning market-basket-analysis


【解决方案1】:

此解决方案使用 SciKit Learn:

import numpy as np
import pandas as pd
from sklearn.cluster import KMeans

#Putting in your data
data = {'user': [1,1,1,2,2,3,3,4,4], 'item':[1,2,3,2,3,8,9,8,9]}

#Turning it into a DataFrame in Pandas (useful if you have more than one attribute in real life    
df = pd.DataFrame(data)

#For this specific example you have to do a reshape because it is a single attribute you are putting in 
item = np.array(data['item']).reshape(-1,1)

#Using sklearn's kmeans to create 2 clusters... you can create as many as you want, but for this example that is the number that made sense
kmeans = KMeans(n_clusters=2)
kmeans.fit(item)

#This is so you can see the labels.  You can append the labels to the dataframe created earlier if you'd like
print(kmeans.labels_)

标签是:[0 0 0 0 0 1 1 1 1]。因此,第 0 组和第 1 组。它们按输入的顺序排列。因此,用户 1 和 2 在组 0 中,用户 3 和 4 在组 1 中。

【讨论】:

  • 谢谢!似乎 k-means 有效,但这段代码不是聚集交易而不是用户吗?我想说每个用户属于哪个集群。我不确定这是否可能,但这似乎非常接近!
  • 它通过交易对用户进行聚类...即,用户 1 和 2 聚集在一起,因为他们购买了相似的物品...同样,用户 3 和 4 聚集在一起,因为他们购买的物品相似的。我想你可以反过来说,物品 1/2/3 是相似的,因为它们是由用户 1/2 购买的,而物品 8/9 是相似的,因为它们是由用户 3/4 购买的。为此,只需将 data['item'] 更改为 data['user']...
猜你喜欢
  • 1970-01-01
  • 2011-12-14
  • 2018-05-15
  • 2018-06-04
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多