rasbt / python-machine-learning-book-2nd-edition

The "Python Machine Learning (2nd edition)" book code repository and info resource
MIT License
7.12k stars 2.82k forks source link

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 closed 1 year ago

Gustavo-Ufersa commented 1 year ago

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 netinput(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 commented 1 year ago

您好!      已经收到您的邮件,我会尽快处理。祝身体健康,万事胜意!

zenetio commented 1 year ago

Ola, O Python é sensivel a identacao, que pode ser o problema do seu codigo. Pra ter certeza, melhor vc anexar o seu arquivo python .py com o codigo. Tks


De: Enya191113 @.> Enviado: sábado, 10 de dezembro de 2022 01:38 Para: rasbt/python-machine-learning-book-2nd-edition @.> Cc: Subscribed @.***> Assunto: Re: [rasbt/python-machine-learning-book-2nd-edition] 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...

您好! 已经收到您的邮件,我会尽快处理。祝身体健康,万事胜意!

― Reply to this email directly, view it on GitHubhttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Frasbt%2Fpython-machine-learning-book-2nd-edition%2Fissues%2F133%23issuecomment-1344969341&data=05%7C01%7C%7C5918e72c61eb4b59ba2808dada4f457d%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638062331260097625%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=cOVKLG1wyuXnGYzvBd78Az42Md4ZoP%2FDsAf2NuSL9VA%3D&reserved=0, or unsubscribehttps://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAAENGILORUV45CCWXFRSBPTWMPNKFANCNFSM6AAAAAASZ5V34E&data=05%7C01%7C%7C5918e72c61eb4b59ba2808dada4f457d%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C638062331260097625%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=iXF%2BOEJpOBgdba4y5Sq1Rgz3UwxskJqAe4yoSMu3TAs%3D&reserved=0. You are receiving this because you are subscribed to this thread.Message ID: @.***>

Enya191113 commented 1 year ago

您好!      已经收到您的邮件,我会尽快处理。祝身体健康,万事胜意!