Perceptron Code error: TypeError: zip argument #2 must support iteration

> def fit(self, X, y, epochs = 2):
>         self.w = np.ones(X.shape[1])
>         self.b = 1
>         for i in range(epochs):
>             for x,y in zip(X,y):
>                 y_pred = self.model(x)
>                 if y == 1 and y_pred == 0:
>                     self.w = self.w + x
>                     self.b = self.b + 1
>                 elif y == 0 and y_pred == 1:
>                     self.w = self.w - x
>                     self.b = self.b - 1

It’s giving me the following error:

> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
> <ipython-input-151-1afc1e4b06e3> in <module>
> ----> 1 perceptron.fit(X_train, y_train, 2)
> 
> <ipython-input-148-e4e7720338e7> in fit(self, X, y, epochs)
>      17         self.b = 1
>      18         for i in range(epochs):
> ---> 19             for x,y in zip(X,y):
>      20                 y_pred = self.model(x)
>      21                 if y == 1 and y_pred == 0:
> 
> TypeError: zip argument #2 must support iteration

But if i remove the following line:

> for i in range(epochs):

then the code compiles without any error

In your code, above you are using the same variable name y for two different things (the array and then individual entry/row in that array.

Try.renaming y as something different, for e.g. Y at the following places below:

  • parameter to fit. Change it to Y
  • argument y inside zip. Change it to Y.
2 Likes

thank you… I did that cause y is a one vector and X is a matrix…
Think i need to sharpen my python skills