《Python数据分析与挖掘实战》第12章转换0-1矩阵代码修改

基于物品的协同过滤推荐

书中第12章的推荐系统主要采用协同过滤算法,通过Jaccard相似系数,计算物品之间相似度,完成计算后构成物品之间的相似度矩阵,最后推荐算法会给用户推荐最相似的K个物品。

1. 遇到的问题

在构建0-1矩阵的过程中速度太慢

解决:通过构建数字和原矩阵的行列字段名字典替换掉了原矩阵的行列字段名称,在构建0-1矩阵过程中采用D[b][a]这样的方式替换了D.loc的方法,最后成功得到结果。
整体代码可以参考另一位博主的链接:https://blog.csdn.net/u012063773/article/details/79324194

num_class = 10333
list1 = list(data['realIP'].value_counts().index)
list2 = []
for i in range(num_class):
    list2.append(i)

dict_new = dict(zip(list2,list1))
print(dict_new)
data2 = data.copy()
list3 = []
new_dict = {v:k for k,v in dict_new.items()}
for i in data2['realIP']:
    a = new_dict[i]
#     print(a)
    list3.append(a)

data2['realIP']= list3
data['fullURL'].value_counts().index
num_class1 = 4339
list4 = list(data['fullURL'].value_counts().index)
list5 = []
for i in range(num_class1):
    list5.append(i)
dict_new1 = dict(zip(list4,list5))
# print(dict_new1)
list6=[]
for i in data2['fullURL']:
    a = dict_new1[i]
    list6.append(a)
data2['fullURL']= list6
import numpy as np
realIP = data2['realIP'].value_counts().index
realIP = [i for i in np.sort(realIP)]
fullURL = data2['fullURL'].value_counts().index
fullURL = [i for i in np.sort(fullURL)]
D = pd.DataFrame([], index = realIP, columns = fullURL )

sum_1 = len(data2)
for i in range(sum_1):
    a, b = data2.iloc[i, ]
#     print(i)
    D[b][a] = 1 
D.fillna(0,inplace = True)
D.index=list(dict_new[i] for i in D.index)
dict_new2 = {v:k for k,v in dict_new1.items()}

D.columns=list(dict_new2[i] for i in D.columns)
E = D.sort_index()
E.sort_index(axis=1, inplace = True)
E.to_csv('zero_one_1.csv')
E.head(20)

有些麻烦不过还是可以得出正确结果的。

你可能感兴趣的:(《Python数据分析与挖掘实战》第12章转换0-1矩阵代码修改)