python : TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'str'
python : TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'str'
我想使用此代码构建模型数据:
def modeldata(filename, ratingMatrix):
result={}
itemmatrix = matrixconvert(ratingMatrix)
current = 0
total = len(itemmatrix)
for item in itemmatrix:
current+=1
if current%100--0: print ("%d / %d" % (current,total))
result[item] = neighbor
#print result
with open(filename+".csv", "wb") as f:
pickle.dump(result, f)
filename变量是聚类过程的数据结果,包含userid、itemid和rating,
`然后 ratingMatrix 是一个包含键(用户)、子键(项目)和评级
的字典
10 dict 1 {'255': 3.0}
。邻居包含相似性数据。
0 tuple 2 (1.0, '9790')
我想使用上面的那些东西构建模型数据,我 运行 使用此代码的函数
modeldata(filename, ratingMatrix)
但是,我得到这个错误:
1 / 306
.
.
304 / 306
305 / 306
306 / 306
Traceback (most recent call last):
File "<ipython-input-29-5af8931a8f1e>", line 1, in <module>
modeldata(filename, ratingMatrix)
File "<ipython-input-28-220883448026>", line 14, in modeldata
with open(filename+".txt", "wb") as f:
TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'str'
你知道这段代码有什么问题吗?错误来自哪里,我怎样才能让它工作?
感谢您的帮助....
您遇到的错误是在 modeldata 函数中,更具体地说是在写入文件的开始语句中。该错误表明您无法使用“+”将字符串“.txt”添加到 "numpy.ndarray",它看起来像是来自变量文件名。确保您的文件名变量是您要写入的实际文件名,而不是 numpy 数组。
我想使用此代码构建模型数据:
def modeldata(filename, ratingMatrix):
result={}
itemmatrix = matrixconvert(ratingMatrix)
current = 0
total = len(itemmatrix)
for item in itemmatrix:
current+=1
if current%100--0: print ("%d / %d" % (current,total))
result[item] = neighbor
#print result
with open(filename+".csv", "wb") as f:
pickle.dump(result, f)
filename变量是聚类过程的数据结果,包含userid、itemid和rating, `然后 ratingMatrix 是一个包含键(用户)、子键(项目)和评级
的字典10 dict 1 {'255': 3.0}
。邻居包含相似性数据。
0 tuple 2 (1.0, '9790')
我想使用上面的那些东西构建模型数据,我 运行 使用此代码的函数
modeldata(filename, ratingMatrix)
但是,我得到这个错误:
1 / 306
.
.
304 / 306
305 / 306
306 / 306
Traceback (most recent call last):
File "<ipython-input-29-5af8931a8f1e>", line 1, in <module>
modeldata(filename, ratingMatrix)
File "<ipython-input-28-220883448026>", line 14, in modeldata
with open(filename+".txt", "wb") as f:
TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'str'
你知道这段代码有什么问题吗?错误来自哪里,我怎样才能让它工作?
感谢您的帮助....
您遇到的错误是在 modeldata 函数中,更具体地说是在写入文件的开始语句中。该错误表明您无法使用“+”将字符串“.txt”添加到 "numpy.ndarray",它看起来像是来自变量文件名。确保您的文件名变量是您要写入的实际文件名,而不是 numpy 数组。