此例构建的是用户-评分-用户的元路径。索引号即对应的用户id。
import scipy.sparse as ss
from itertools import combinations, permutations
import numpy as np
import pandas as pd
def build_matrix(aa,l):
A = list(permutations(aa, 2))
print(A)
col = []
row = []
for i in range(len(A)):
row.append(A[i][0])
col.append(A[i][1])
'''
#https://www.cnblogs.com/hellojamest/p/11769467.html 关于稀疏矩阵知识详见此链接
# data = [1] * len(A)
# c = ss.coo_matrix((data, (row, col)), shape=(4, 4)) # 生成稀疏矩阵,shape是最后生成的矩阵大小。
'''
for i in range(len(row)):
for j in range(len(col)):
l[row[i],col[j]]=1
file_path = pd.read_excel(r"D:\BaiduNetdiskDownload\yelp\new10.xls",usecols=[8])
meta_data = []
index=0
for d in file_path['rating']:
meta_data.append(
d)
b=set(meta_data)
print(b)
newlist = []
l=ss.lil_matrix((len(meta_data),len(meta_data)))
for i in b:
newlist.append(i)
for i in range(len(newlist)):
indices = []
start=0
while newlist[i]!=-1:
try:
x = meta_data.index(newlist[i], start)
indices.append(x)
start = x + 1
except:
print(indices)
build_matrix(indices,l)
break
l[np.eye(len(meta_data),dtype=np.bool)]=0
print(l.toarray())
参考链接: 将对角线变成0
|