0

我有一个购买数据集

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 中的机器学习创建这种分析。

例如,我猜它可能是距离

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

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

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

4

1 回答 1

0

此解决方案使用 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 中。

于 2018-05-04T08:19:01.353 回答