Last active
December 23, 2018 09:56
-
-
Save debonx/f71cf0848047aa6ef30297543d365d8a to your computer and use it in GitHub Desktop.
Example of Perceptron Logic Gates AND, OR and XOR to build Artificial Neural Networks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.linear_model import Perceptron | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from itertools import product | |
data = [[0, 0], [0, 1], [1, 0], [1, 1]] | |
# Set labels basing on gate type (AND, OR) | |
# Multiple perceptrons can solve XOR gates | |
labels = [0, 0, 0, 1] | |
x = [point[0] for point in data] | |
y = [point[1] for point in data] | |
plt.scatter(x, y, c=labels) | |
# Create Perceptron | |
classifier = Perceptron(max_iter=40) | |
classifier.fit(data, labels) | |
score = classifier.score(data, labels) | |
# Defining the decision points (100 between 0 and 1) | |
x_values = np.linspace(0, 1, 100) | |
y_values = np.linspace(0, 1, 100) | |
# Calculate every single combination | |
point_grid = list(product(x_values, y_values)) | |
# Get distances in absolute values | |
distances = classifier.decision_function(point_grid) | |
abs_distances = [abs(d) for d in distances] | |
# Create 2D array (matrix) | |
distance_matrix = np.reshape(abs_distances, (100, 100)) | |
# Draw heat map | |
heatmap = plt.pcolormesh(x_values, y_values, distance_matrix) | |
plt.colorbar(heatmap) | |
# Show The Matrix | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment