Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Olá, comecei a estudar Machile Learning há alguns meses. Com isso comecei a estudar Redes Neurais. Tentei escrever um script para dá uma olhada no comportamento do Perceptron para entender melhor, no entanto, quando comecei a escrever o código e rodei o mesmo me deparei com o seguinte erro: AttributeError: 'Perceptron' object has no attribute 'fit' #133

Closed
Gustavo-Ufersa opened this issue Dec 10, 2022 · 3 comments

Comments

@Gustavo-Ufersa
Copy link

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from mlxtend.plotting import plot_decision_regions

Criação da classe perceptron:

class Perceptron:
def init(self, eta=0.01, n_iter=10):
self.eta = eta
self.n_iter = n_iter

Métodos da classe:

def fit(self, X, y):
self.w_ = np.zeros(1 + X.shape[1])
self.errors_ = []

for _ in range(self.n_iter):
    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)

def predict(self, X):
return np.where(self.net_input(X) >= 0.0, 1, -1)
def net_input(self, X):
return np.dot(X, self.w_[1:] + self.w_[0])

Criando o dataset

df = pd.read_csv('https://archive.ics.uci.edu/ml/''machine-learning-databases/iris/iris.data', header=None, encoding='utf-8')
df.tail()
y = df.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', -1, 1)
X = df.iloc[0:100, [0, 2]].values

criando o plot

plt.scatter(X[:50, 0], X[:50, 1], color ='red', marker='o', label = 'setosa')
plt.scatter(X[50:100, 0], X[50:100, 1], color = 'blue', marker='o', label = 'versicolor')
plt.xlabel('pental lenght ')
plt.ylabel('sepal lenght ')
plt.legend(loc='upper left')
plt.show()

Treinamento do algoritmo

ppn = Perceptron(eta=0.1, n_iter=10)
ppn.fit(X, y)

@Enya191113
Copy link

Enya191113 commented Dec 10, 2022 via email

@zenetio
Copy link

zenetio commented Dec 11, 2022 via email

@Enya191113
Copy link

Enya191113 commented Feb 25, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants