Class MPNeuron, TypeError: unsupported operand type(s) for +: 'int' and 'str'

Hello,
I did same as Pratyush Sir did in the videos. But unable to figure out why getting this error?
I am pretty sure there is some problem in return from model part. How the types are different? How to resolve it?

class MPNeuron:
    def __init__(self):
        self.b = None

    def model(self,x):
        return(sum(x) >= self.b)

    def predict(self,x):
        Y=[]
        for x in X:
            result = self.model(x)
            Y.append(result)
        return np.array(Y)

    def fit(self,X,Y):
        accuracy={}
        for b in range(X.shape[1]+1):
            self.b=b
            Y_pred = self.predict(X)
            accuracy[b]=accuracy_score(Y_pred,Y)
        best_b=max(accuracy,key=accuracy.get)  # "key" argument gives max value among all keys of dictionary accuracy
        self.b = best_b
        print('Optimal value of b is',best_b)
        print('Highest accuracy is',accuracy[best_b])
mp_neuron=MPNeuron()
mp_neuron.fit(X_train_binarised,Y_train)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-74-2b7a9f6f4b30> in <module>()
      1 mp_neuron=MPNeuron()
----> 2 mp_neuron.fit(X_train_binarised,Y_train)

2 frames
<ipython-input-71-4271ceb4004c> in model(self, x)
      4 
      5     def model(self,x):
----> 6         return(sum(x) >= self.b)
      7 
      8     def predict(self,x):

TypeError: unsupported operand type(s) for +: 'int' and 'str'

What is the type of X_train_binarised? Is it dataframe or numpy array?
Try this before calling fit

X_train_binarised = X_train_binarised.values

If this works, remember you will have to do the same for X_test_binarised as well. (Look for notebook from lectures, where they do it)

If above doesn’t work, you can share google colab link to your code here.

type(X_train_binarised) is numpy.ndarray.
Link to code:
https://colab.research.google.com/drive/1MO0ltPvL24ftj_aptbx3s6YkltFhRwnv#scrollTo=ioVjie1sD39p&line=1&uniqifier=1

@Mugdha
I cannot access it, its asking for permission. Can you make the link available to all for now?

As I am new to Google Colaboratory, can you tell me how to do it?

You click ‘Share’ (top right), then you choose Get link.
There you can choose Anyone with the link and then share the link.

https://colab.research.google.com/drive/1MO0ltPvL24ftj_aptbx3s6YkltFhRwnv?usp=sharing

This is the problem code:

def predict(self,x):
    Y=[]
    for x in X:

In predict function, parameter is incorrectly specified as small x instead of capital X.
Try again, after changing to:

def predict(self, X)

Yeah it worked.
I don’t know how I got wrong there. I was wondering for so much time to catch a mistake.
Anyways, Thank You so much.

1 Like

Please help me out with the same problem :

https://drive.google.com/file/d/1sVbDBNTfxGmZKnKuAf2nRgYcuHVDC8e6/view?usp=sharing

I’m not able to open the notebook using this link

Convert X and Y to numpy arrays (from pandas dataframe/series) using following code.

X=X.values
Y=Y.values

You can insert it before call to function 'train_test_split'

Yes it worked , Thank you