我希望将Collaborative Filtering with Python通过余弦相似度转换为调整余弦相似度。
基于余弦相似度的实现如下所示:
import pandas as pd
import numpy as np
from scipy.spatial.distance import cosine
from scipy.spatial.distance import pdist, squareform
data = pd.read_csv("C:\\Sample.csv")
data_germany = data.drop("Name", 1)
data_ibs = pd.DataFrame(index=data_germany.columns,columns=data_germany.columns)
for i in range(0,len(data_ibs.columns)) :
for j in range(0,len(data_ibs.columns)) :
data_ibs.ix[i,j] = 1-cosine(data_germany.ix[:,i],data_germany.ix[:,j])
data_neighbours = pd.DataFrame(index=data_ibs.columns,columns=range(1,6))
for i in range(0,len(data_ibs.columns)):
data_neighbours.ix[i,:] = data_ibs.ix[0:,i].sort_values(ascending=False)[:5].index
df = data_neighbours.head().ix[:,2:6]
print df
正在使用的 Sample.csv 看起来像:
其中1
表示用户购买了特定水果,相反0
表示用户没有购买特定水果
当我运行上面的代码时,这就是我得到的:
其中行是水果,列是相似性排名(按降序排列)。在此示例中,Pear
与Apple
最相似,Melon
是第二个最相似的,依此类推。
我在调整余弦相似度上遇到了this post,我尝试将该方法集成到我的代码中。在这种情况下,数据是用户给水果的评分:
这是我的尝试:
data_ibs = pd.DataFrame(index=data_germany.columns,columns=data_germany.columns)
M_u = data_ibs.mean(axis=1)
M = np.asarray(data_ibs)
item_mean_subtracted = M - M_u[:, None]
for i in range(0,len(data_ibs.columns)) :
for j in range(0,len(data_ibs.columns)) :
data_ibs.ix[i,j] = 1 - squareform(pdist(item_mean_subtracted.T, "cosine")) ### error
data_neighbours = pd.DataFrame(index=data_ibs.columns,columns=range(1,6))
for i in range(0,len(data_ibs.columns)):
data_neighbours.ix[i,:] = data_ibs.ix[0:,i].sort_values(ascending=False)[:5].index
df = data_neighbours.head().ix[:,2:6]
但是我被卡住了。我的问题是:如何将调整余弦相似度成功应用到此样本中?
答案 0 :(得分:2)
这是基于NumPy的解决方案。
首先,我们将评级数据存储到数组中:
fruits = np.asarray(['Apple', 'Orange', 'Pear', 'Grape', 'Melon'])
M = np.asarray(data.loc[:, fruits])
然后我们计算调整后的余弦相似度矩阵:
M_u = M.mean(axis=1)
item_mean_subtracted = M - M_u[:, None]
similarity_matrix = 1 - squareform(pdist(item_mean_subtracted.T, 'cosine'))
最后,我们按照相似性的递减顺序对结果进行排序:
indices = np.fliplr(np.argsort(similarity_matrix, axis=1)[:,:-1])
result = np.hstack((fruits[:, None], fruits[indices]))
<强>样本强>
In [49]: M
Out[49]:
array([[ 0, 10, 0, 1, 0],
[ 6, 0, 0, 0, 2],
[ 1, 0, 20, 0, 1],
[ 0, 3, 6, 0, 18],
[ 3, 0, 2, 0, 0],
[ 0, 2, 0, 5, 0]])
In [50]: np.set_printoptions(precision=2)
In [51]: similarity_matrix
Out[51]:
array([[ 1. , 0.01, -0.41, 0.48, -0.44],
[ 0.01, 1. , -0.57, 0.37, -0.26],
[-0.41, -0.57, 1. , -0.56, -0.19],
[ 0.48, 0.37, -0.56, 1. , -0.51],
[-0.44, -0.26, -0.19, -0.51, 1. ]])
In [52]: result
Out[52]:
array([['Apple', 'Grape', 'Orange', 'Pear', 'Melon'],
['Orange', 'Grape', 'Apple', 'Melon', 'Pear'],
['Pear', 'Melon', 'Apple', 'Grape', 'Orange'],
['Grape', 'Apple', 'Orange', 'Melon', 'Pear'],
['Melon', 'Pear', 'Orange', 'Apple', 'Grape']],
dtype='|S6')