Skip to content

Instantly share code, notes, and snippets.

View mempirate's full-sized avatar

Jonas Bostoen mempirate

View GitHub Profile
Verifying my Blockstack ID is secured with the address 1Jzq7PkwGH34a6iF1PAL6QVuQqatY6MxZy https://explorer.blockstack.org/address/1Jzq7PkwGH34a6iF1PAL6QVuQqatY6MxZy
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (x - 1)
# Input matrix
training_inputs = np.array([[0,0,1],
[1,1,1],
[1,0,1],
[0,1,1]])
# Output array (transposed)
training_outputs = np.array([[0,1,1,0]]).T
# Set seed for pseudo-randomness
np.random.seed(1)
# Initialize synaptic weights randomly from -1 to 1 with 0 as the mean
synaptic_weights = 2 * np.random.random((3, 1)) - 1
# Train by iterating 10,000 times
for iteration in range(10000):
# Define input layer
input_layer = training_inputs
# Normalize the product of the input layer with the synaptic
# weights by using the sigmoid function to get outputs
outputs = sigmoid(np.dot(input_layer, synaptic_weights))
Random starting synaptic weights: Synaptic weights after training:
[-0.16595599] [ 9.67299303]
[ 0.44064899] [-0.2078435 ]
[-0.99977125] [-4.62963669]
Output after training:
[0.00966449]
[0.99211957]
# Dividing data up into training sets and test sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3)
# Creating the matrix of features and the vector of labels
X = df.iloc[:, :-1].values
y = df.iloc[:, 1].values
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
# Visualizing the training set results
plt.figure(figsize = (12, 6))
plt.scatter(X_train, y_train, alpha = 0.4, label = 'Observation Points')
plt.plot(X_train, regressor.predict(X_train), color = 'green', lw = 0.7, alpha = 0.8, label = 'Best Fit Line')
plt.title('Linear Regression (Training Set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.legend()