构建稀疏矩阵时出错 Python Scipy.sparse
Error in building sparse matrix Python Scipy.sparse
在我的代码中,我目前正在迭代并创建三个列表:
数据、行、列
构建一个稀疏矩阵(它表示用户 u 对项目 i 的评分矩阵,评分从 1 到 5),之后检查我的稀疏矩阵时,报告的评分出现奇怪的错误:一些值是大于 5 这是不可能的(我检查了文件,没有大于 5 的评分,我也检查了数据列表中的值,没有大于 5 的值,所以错误可能是在使用 sparse.coo_matrix(),
查看下面我的代码:
from scipy import sparse
import numpy as np
row = []
column = []
data= []
with open(filename, 'r') as f:
for line in f:
if not line[0].isdigit():
continue
line = line.strip()
elem = line.split(',')
userid = int(elem[0].strip())
businessid = int(elem[1].strip())
rating = float(elem[2].strip())
row.append(userid)
column.append(businessid)
data.append(rating)
#data = np.array(data)
"""checking if any rating in the file is greater than 5,
and there is not"""
for rating in data:
if rating > 5:
print rating
total = sparse.coo_matrix((data, (row, column)),dtype=float).tocsr()
""" Here I'm checking to see if
there is any rating over than 5 in the sparse matrix
and there is!"""
row = total.nonzero()[0]
column = total.nonzero()[1]
for u in range(len(row)):
indr = row[u]
indc = column[u]
if total[indr, indc] > 5:
print '---'
print total[indr, indc]
print indr
print indc
这是我文件的开头:
user,item,rating
480,0,5
16890,0,2
5768,0,4
319,1,1
4470,1,4
7555,1,5
8768,1,5
你知道为什么我在构建矩阵时会收到这个错误吗?
非常感谢!
Duplicate entries will be summed together
(我不知道为什么会这样。)
在我的代码中,我目前正在迭代并创建三个列表:
数据、行、列
构建一个稀疏矩阵(它表示用户 u 对项目 i 的评分矩阵,评分从 1 到 5),之后检查我的稀疏矩阵时,报告的评分出现奇怪的错误:一些值是大于 5 这是不可能的(我检查了文件,没有大于 5 的评分,我也检查了数据列表中的值,没有大于 5 的值,所以错误可能是在使用 sparse.coo_matrix(),
查看下面我的代码:
from scipy import sparse
import numpy as np
row = []
column = []
data= []
with open(filename, 'r') as f:
for line in f:
if not line[0].isdigit():
continue
line = line.strip()
elem = line.split(',')
userid = int(elem[0].strip())
businessid = int(elem[1].strip())
rating = float(elem[2].strip())
row.append(userid)
column.append(businessid)
data.append(rating)
#data = np.array(data)
"""checking if any rating in the file is greater than 5,
and there is not"""
for rating in data:
if rating > 5:
print rating
total = sparse.coo_matrix((data, (row, column)),dtype=float).tocsr()
""" Here I'm checking to see if
there is any rating over than 5 in the sparse matrix
and there is!"""
row = total.nonzero()[0]
column = total.nonzero()[1]
for u in range(len(row)):
indr = row[u]
indc = column[u]
if total[indr, indc] > 5:
print '---'
print total[indr, indc]
print indr
print indc
这是我文件的开头:
user,item,rating
480,0,5
16890,0,2
5768,0,4
319,1,1
4470,1,4
7555,1,5
8768,1,5
你知道为什么我在构建矩阵时会收到这个错误吗?
非常感谢!
Duplicate entries will be summed together
(我不知道为什么会这样。)