TypeError:ufunc'add'不包含签名匹配类型为dtype('<u72')dtype('<u72')=“” dtype('<u72')=“”

时间:2019-01-08 19:28:10

标签: python python-3.x numpy numpy-ufunc

=“”

我需要使用p这个循环来打印print的某些值。

for prediction in pred:
            j=0
            if(prediction == 1):
                print ("Titolo: " + target_playlist["song_title"][i] + ",  Artista:  "+ target_playlist["artist"][i] + ",   Percentuali:  "+ p[i])
                likedSongs= likedSongs + 1
            i = i +1  

如果我尝试使用循环示例之外的地方打印p

for i in range(len(p)):
            print("Predicted=%s" % (p[i]))

它可以工作,但是当我在for循环中将p[i]放入打印中时,我遇到了类型问题。我该怎么解决?

 if(algoritmo_scelto==1):
            pred = c.predict(target_playlist[features])
            p = c.predict_proba(target_playlist[features])
        if(algoritmo_scelto==2):
            pred = knn.predict(target_playlist[features])
            p = knn.predict_proba(target_playlist[features])
        if(algoritmo_scelto==3):
            pred = forest.predict(target_playlist[features])
            p = forest.predict_proba(target_playlist[features])
        if(algoritmo_scelto==4):
            pred = k_means.predict(target_playlist[features])
            p = k_means.predict_proba(target_playlist[features])

        likedSongs = 0
        i = 0

        for prediction in pred:
            j=0
            if(prediction == 1):
                print ("Titolo: " + target_playlist["song_title"][i] + ",  Artista:  "+ target_playlist["artist"][i] + ",   Percentuali:  "+ p[i])
                likedSongs= likedSongs + 1
            i = i +1   





    Exception in Tkinter callback
        Traceback (most recent call last):
          File "C:\Users\david\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1702, in __call__
            return self.func(*args)
          File "C:\Users\david\eclipse-workspace1\project\src\changp.py", line 179, in <lambda>
            command=lambda: par())
          File "C:\Users\david\eclipse-workspace1\project\src\changp.py", line 173, in par
            estraiPreferite(self.entry1.get(), var.get())
          File "C:\Users\david\eclipse-workspace1\project\src\changp.py", line 550, in estraiPreferite
            print ("Titolo: " + target_playlist["song_title"][i] + ",  Artista:  "+ target_playlist["artist"][i] + ",   Percentuali:  "+ p[j])
        TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U72') dtype('<U72') dtype('<U72')

1 个答案:

答案 0 :(得分:0)

制作一个字符串数组:

In [148]: arr = np.array(['a','bcd','ef'])
In [149]: arr
Out[149]: array(['a', 'bcd', 'ef'], dtype='<U3')

我可以索引元素并使用Python字符串`+'将它们连接起来:

In [150]: arr[0]+arr[1]
Out[150]: 'abcd'

但是我不能这样用字符串dtype连接数组:

In [151]: arr[[0]]+arr[[1]]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-151-3186795057b5> in <module>()
----> 1 arr[[0]]+arr[[1]]

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U3') dtype('<U3') dtype('<U3')

In [156]: arr[[0]],arr[[1]]
Out[156]: (array(['a'], dtype='<U3'), array(['bcd'], dtype='<U3'))
In [157]: 'foo %s %s'%(arr[[0]], arr[[1]])
Out[157]: "foo ['a'] ['bcd']"