import numpy as np # Função de ativação Heaviside def heaviside(x): return 1 if x >= 0 else 0 # Classe Perceptron class Perceptron: def __init__(self, learning_rate=0.1, epochs=1000): self.learning_rate = learning_rate self.epochs = epochs self.weights = None self.bias = None def fit(self, X, y): n_samples, n_features = X.shape self.weights = np.zeros(n_features) self.bias = 0 for _ in range(self.epochs): for idx, x_i in enumerate(X): linear_output = np.dot(x_i, self.weights) + self.bias y_predicted = heaviside(linear_output) update = self.learning_rate * (y[idx] - y_predicted) self.weights += update * x_i self.bias += update def predict(self, X): linear_output = np.dot(X, self.weights) + self.bias return heaviside(linear_output) # Dados de exemplo X = np.array([[1, 1], [2, 2], [3, 3], [4, 4]]) y = np.array([0, 0, 1, 1]) # Treinando o Perceptron perceptron = Perceptron() perceptron.fit(X, y) # Fazendo previsões predictions = [perceptron.predict(x) for x in X] print(predictions)