Skip to content

Instantly share code, notes, and snippets.

View RyotaBannai's full-sized avatar
🛴
Man's soil is still rich enough to direct his own life.

Ryota Bannai RyotaBannai

🛴
Man's soil is still rich enough to direct his own life.
View GitHub Profile
@RyotaBannai
RyotaBannai / mlp.py
Last active February 5, 2019 04:34 — forked from miloharper/main.py
A two layer neural network written in Python, which trains itself to solve a variation of the XOR problem.
from numpy import exp, array, random, dot, reshape
from autograd import grad
class NeuronLayer():
def __init__(self, neuron_n, inputs_n):
self.weights = 2 * random.random((inputs_n, neuron_n)) - 1
class NeuralNetwork():
def __init__(self, layer1, layer2):
self.layer1 = layer1
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
c=['#415952', '#f35134', '#243AB5']
def dataset_ (n=200, idx_outlier=0, ydistance=5):
rng = np.random.RandomState(4)
data = np.dot(rng.rand(2, 2), rng.randn(2, n)).T
data[idx_outlier:idx_outlier+1,1] = ydistance
return data
N=200
inx=10
i = dataset_(N, inx)
import numpy as np
x=np.array([1,4,7,2,5,7,7,8,4,6,8,30])
z=(x-x.mean(axis=0))/x.std()
#
for l in (z < z.mean() - 3*z.std(),
z > z.mean() + 3*z.std()):
if np.any(z[l]):
print(z[l])
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
plt.style.use('ggplot')
fig, (ax1, ax2) = plt.subplots(ncols=2,figsize=(16,6))
plt.xlim((0, 10))
plt.ylim((0, 7))
plt.tight_layout(w_pad=1.5)
#red line
N=60
mu=0
sd=2
np.random.seed(0)
ran = np.random.normal(size=N)
error1 = sd**2 * ran + mu
error2 = sd*.5 * ran + mu
lin = np.linspace(-15., 15., num=N)
import scipy.odr as odr
def odr_line(B, x):
y = B[0]*x + B[1]*x**2
return y
def perform_odr(x, y, xerr, yerr):
quadr = odr.Model(odr_line)
mydata = odr.Data(x, y, wd=1./xerr, we=1./yerr)
#mydata = odr.Data(x, y)
import numpy.linalg as la
def tls(X,y):
if X.ndim is 1:
n = 1 # the number of variable of X
X = X.reshape(len(X),1)
else:
n = np.array(X).shape[1]
Z = np.vstack((X.T,y)).T