[英]python : TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'str'
I want to build a model data using this code : 我想使用以下代码构建模型数据:
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 variable is a data result from clustering process that contains userid, itemid, and rating, `then ratingMatrix is a dictionary which contains key (user), subkey(item) and rating filename变量是包含用户ID,ItemID和Rating的聚类过程的数据结果,`然后ratingMatrix是一个包含键(用户),子键(item)和Rating的字典
10 dict 1 {'255': 3.0}
. 。 Neighbor contains a similarity data. 邻居包含相似性数据。
0 tuple 2 (1.0, '9790')
I want to build a model data using those things above, I run the function with this code 我想使用上述内容构建模型数据,并使用以下代码运行该函数
modeldata(filename, ratingMatrix)
but, I get this error : 但是,我得到这个错误:
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'
Do you have any idea what's wrong with this code ? 您知道这段代码有什么问题吗? where is the error from, and how can I make it work? 错误从哪里来,我该如何解决?
Thank you for your help.... 谢谢您的帮助....
The error you getting is within the modeldata function and more specifically the opening statement for writing to a file. 您得到的错误是在modeldata函数内,更具体地说是在写入文件的开始语句内。 The error is stating that you can't add using the "+" a string ".txt" to "numpy.ndarray" which looks like it is coming from the variable filename. 该错误表明您无法使用“ +”在“ numpy.ndarray”中添加字符串“ .txt”,该字符串看起来像是来自变量文件名。 Make sure your filename variable is the actual file name you want to write to and not numpy array. 确保您的文件名变量是您要写入的实际文件名,而不是numpy数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.