Neural Networks can’t Multiply Sequence by non-int of type 'float'

Asked

Viewed 208 times

1

Hello, I need to implement a perceptron to linearly classify 2 species (Iris).

I’ve already scoured the internet for solutions and I can’t get out of the problem

I took a code on the internet and tried to apply it to my csv just to see if it would give some result but since I’m a beginner in python I don’t know how to get around the situation. If anyone can give me a light thank you.

LINK iris.csv

import numpy as np
import pandas as pd

class Perceptron(object):

    def __init__(self, eta=0.01, epochs=50):
        self.eta = eta
        self.epochs = epochs

    def train(self, X, y):

        self.w_ = np.zeros(1 + X.shape[1])
        self.errors_ = []

        for _ in range(self.epochs):
            errors = 0
            for xi, target in zip(X, y):
                update = self.eta * (target - self.predict(xi))
                self.w_[1:] +=  update * xi
                self.w_[0] +=  update
                errors += int(update != 0.0)
            self.errors_.append(errors)
        return self

    def net_input(self, X):
        return np.dot(X, self.w_[1:]) + self.w_[0]

    def predict(self, X):
        return np.where(self.net_input(X) >= 0.0, 1, -1)

df = pd.read_csv('/home/DIRETORIO/iris.csv', header=None)

# setosa and versicolor
y = df.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', -1, 1)

# sepal length and petal length
X = df.iloc[0:100, [0,2]].values

%matplotlib inline
import matplotlib.pyplot as plt
from mlxtend.plotting import plot_decision_regions

ppn = Perceptron(epochs=10, eta=0.1)

ppn.train(X, y)
print('Weights: %s' % ppn.w_)
plot_decision_regions(X, y, clf=ppn)
plt.title('Perceptron')
plt.xlabel('sepal length [cm]')
plt.ylabel('petal length [cm]')
plt.show()

plt.plot(range(1, len(ppn.errors_)+1), ppn.errors_, marker='o')
plt.xlabel('Iterations')
plt.ylabel('Misclassifications')
plt.show()

-- ERROR WHEN I TRY TO RUN IN JUPYTER (LINE 25)--

TypeError                                 Traceback (most recent call 
last)
<ipython-input-10-c97f88eafcb5> in <module>
  5 ppn = Perceptron(epochs=10, eta=0.1)
  6 
----> 7 ppn.train(X, y)
  8 print('Weights: %s' % ppn.w_)
  9 plot_decision_regions(X, y, clf=ppn)

<ipython-input-8-7b4ff7d686b6> in train(self, X, y)
 15             errors = 0
 16             for xi, target in zip(X, y):
---> 17                 update = self.eta * (target - 
self.predict(xi))
 18                 self.w_[1:] +=  update * xi
 19                 self.w_[0] +=  update

<ipython-input-8-7b4ff7d686b6> in predict(self, X)
 26 
 27     def predict(self, X):
---> 28         return np.where(self.net_input(X) >= 0.0, 1, -1)

<ipython-input-8-7b4ff7d686b6> in net_input(self, X)
 23 
 24     def net_input(self, X):
---> 25         return np.dot(X, self.w_[1:]) + self.w_[0]
 26 
 27     def predict(self, X):

TypeError: can't multiply sequence by non-int of type 'float'
  • Do you have to implement perceptron? even if copying? can’t use a lib? Also post the error that is occurring.

  • Check the type of value that pd.read_csv() returns, use the type() function, and post a snippet of its . CSV

  • @Sidon Yes I have to implement it I can not use other libs only the ones I am already using. I added the error in the post, I had forgotten. Thanks

  • @Fourzerofive I didn’t understand it very well but if I did it right returned <class 'pandas.core.frame.Dataframe'> csv data is all float

  • Use type to test a single return element

  • So. This error is relatively easy to discover the cause, with some tests someone will end up helping you, I just do not understand what the error has to do with "neural networks" I suggest changing the title.

Show 1 more comment

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.