我有一个购买数据集
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专业人员/学生。
答案 0 :(得分: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组。