1

我有以下格式的数据:

user,item,rating
1,1,3
1,2,2
2,1,2
2,4,1

依此类推,我想将其转换为矩阵形式

所以,输出是这样的

Item--> 1,2,3,4....
user
1       3,2,0,0....
2       2,0,0,1

....等等..

我如何在python中做到这一点?

谢谢

4

2 回答 2

2
data = [
    (1,1,3),
    (1,2,2),
    (2,1,2),
    (2,4,1),
]

#import csv
#with open('data.csv') as f:
#    next(f) # Skip header
#    data = [map(int, row) for row in csv.reader(f)]
#    # Python 3.x: map(int, row) -> tuple(map(int, row))

n = max(max(user, item) for user, item, rating in data) # Get size of matrix
matrix = np.zeros((n, n))
for user, item, rating in data:
    matrix[user-1][item-1] = rating # Convert to 0-based index.

for row in matrix:
    print(row)

印刷

[3, 2, 0, 0]
[2, 0, 0, 1]
[0, 0, 0, 0]
[0, 0, 0, 0]
于 2013-10-03T05:56:02.387 回答
1

与@falsetru 不同的方法,

您是否在写入文件时从文件中读取?

可能与字典一起使用

from collections import defaultdict
valdict=defaultdict(int)
nuser=0
nitem=0
for line in infile:
    eachline=line.strip().split(",")
    valdict[tuple(eachline[0:2])]=eachline[2]
    nuser=max(nuser,eachline[0])
    nitem=max(nitem,eachline[1])

towrite=",".join(range(1,nuser+1))+"\n"
for i in range(1:nuser+1):
    towrite+=str(i)
    for j in range(1:nitem+1):
        towrite+=","+str(valdict[i,j])
    towrite+="\n"

outfile.write(towrite)
于 2013-10-03T06:20:06.477 回答