Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- import seaborn as sns
- import pandas as pd
- from sklearn.model_selection import train_test_split
- from sklearn.metrics import accuracy_score
- from random import random
- def plot_decision_boundary(perceptron, X, y, title):
- plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='blue', label='0')
- plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='orange', label='1')
- x1_values = np.array([X[:, 0].min(), X[:, 0].max()])
- x2_values = -(perceptron.weights[0] * x1_values + perceptron.bias) / perceptron.weights[1]
- plt.plot(x1_values, x2_values, label='Decision Boundary', color='green')
- plt.title(title)
- plt.xlabel('X1')
- plt.ylabel('X2')
- plt.legend()
- plt.grid(True)
- plt.show()
- def activation_function(x):
- # sigmoid, tanh, relu, leakyrelu
- return 1/(1+np.exp(-x))
- class Perceptron:
- # learning_rate - alpha
- def __init__(self, learning_rate = 0.003, n_iters = 15041):
- self.lr = learning_rate
- self.n_iters = n_iters
- self.activation_function = activation_function
- self.weights = None
- self.bias = None
- def fit(self, X, Y):
- n_samples, n_features = X.shape
- # self.weights = np.zeros(n_features)
- self.weights = np.array([])
- for i in range(n_features):
- self.weights = np.append(self.weights, random())
- # self.bias = 0
- self.bias = random()
- y_ = np.where(Y > 0, 1, 0)
- for iter_ in range(self.n_iters):
- for index, x_i in enumerate(X):
- linear_output = np.dot(x_i, self.weights) + self.bias
- y_predicted = self.activation_function(linear_output)
- # Perceptron update rule
- perceptron_update = self.lr * (y_[index]-y_predicted)
- self.weights += perceptron_update * x_i
- self.bias += perceptron_update
- def predict(self, x):
- linear_output = np.dot(x, self.weights) + self.bias
- y_predicted = self.activation_function(linear_output)
- return np.where(y_predicted > 0.5, 1, 0)
- data = pd.read_csv('iris.csv')
- data = data[data['species'] != 'setosa']
- data['target'] = np.where((data['species'] == 'virginica'), 0, 1)
- data = data.drop(columns=['species', 'sepal_width', 'sepal_length'])
- sns.pairplot(data, hue='target', palette='dark')
- plt.show()
- x_train, x_test, y_train, y_test = train_test_split(data.iloc[:, :-1], data['target'], test_size=0.2, random_state=42)
- model = Perceptron(learning_rate=0.1, n_iters=1000)
- model.fit(X=x_train.to_numpy(), Y=y_train.to_numpy())
- y_pred = model.predict(x_test)
- y_pred_train = model.predict(x_train)
- accuracy = accuracy_score(y_true=y_train, y_pred=y_pred_train)
- plot_decision_boundary(model, x_train.to_numpy(), y_train, f'Perceptron Train accuracy: {accuracy * 100:.2f}%')
- accuracy = accuracy_score(y_true=y_test, y_pred=y_pred)
- plot_decision_boundary(model, x_test.to_numpy(), y_test, f'Perceptron Test accuracy: {accuracy * 100:.2f}%')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement