簡體   English   中英

python:TypeError:+不支持的操作數類型:'numpy.ndarray'和'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變量是包含用戶ID,ItemID和Rating的聚類過程的數據結果,`然后ratingMatrix是一個包含鍵(用戶),子鍵(item)和Rating的字典

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函數內,更具體地說是在寫入文件的開始語句內。 該錯誤表明您無法使用“ +”在“ numpy.ndarray”中添加字符串“ .txt”,該字符串看起來像是來自變量文件名。 確保您的文件名變量是您要寫入的實際文件名,而不是numpy數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM