0

I need to print some values of p using the print this loop.

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  

If I try to print p outside the loop fow example with

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

it works, but when I put p[i] in the print in the for loop I have problems with the types. How can I solve?

 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')
Rory Daulton
  • 21,934
  • 6
  • 42
  • 50

1 Answers1

0

Make an array of strings:

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

I can index elements and join them with Python string `+':

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

But I can't join arrays with the string dtype that way:

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']"
hpaulj
  • 221,503
  • 14
  • 230
  • 353