In [54]:
import numpy as np
from math import exp, pow, sqrt
from numpy.linalg import inv
from functools import reduce
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import ListedColormap
In [55]:
def rbf(inp, out, center):
def euclidean_norm(x1, x2):
return sqrt(((x1 - x2)**2).sum(axis=0))
def gaussian (x, c):
return exp(+1 * pow(euclidean_norm(x, c), 2))
R = np.ones((len(inp), (len(center) + 1)))
for i, iv in enumerate(inp):
for j, jv in enumerate(center):
R[i, j] = (gaussian(inp[i], center[j]))
Rt = R.transpose()
RtR = Rt.dot(R)
iRtR = inv(RtR)
oneR = iRtR.dot(Rt)
a = oneR.dot(out)
def rbf_interpolation(x):
sum = np.ones(len(center) + 1)
for i, iv in enumerate(center):
sum[i] = gaussian(x, iv)
y = a * sum
return reduce((lambda x, y: x + y), y)
return rbf_interpolation
In [35]:
inp = np.array([2, 3, 4])
out = np.array([3, 6, 5])
center = np.array([2, 4])
rbf_instance = rbf(inp, out, center)
In [36]:
input_test = input_test = np.linspace(0,10,100)
output_test = list(map(rbf_instance, input_test))
In [37]:
plt.plot(input_test, output_test)
plt.plot(inp, out, 'ro')
plt.ylabel('expected vs predicted')
plt.savefig("rbf1.svg")
plt.show()
In [63]:
inp = np.array([2, 3, 4, 5])
out = np.array([3, 1, 5, -2])
center = np.array([2, 3, 4])
rbf_instance = rbf(inp, out, center)
In [64]:
input_test = np.linspace(-5,10,100)
output_test = list(map(rbf_instance, input_test))
In [65]:
# plt.plot(input_test, output_test)
plt.plot(inp, out, 'ro')
plt.ylabel('expected vs predicted')
plt.savefig("interpolate1.svg")
plt.show()
In [60]:
inp = np.array([2, 3, 4, 5])
out = np.array([3, 1, 5, -2])
center = np.array([2, 3, 4])
rbf_instance = rbf(inp, out, center)
In [58]:
input_test = input_test = np.linspace(-5,15,100)
output_test = list(map(rbf_instance, input_test))
In [59]:
plt.plot(input_test, output_test)
plt.plot(inp, out, 'ro')
plt.ylabel('expected vs predicted')
plt.savefig("rbf3.svg")
plt.show()
In [44]:
inp = np.array([np.array([1,1]), np.array([0,1]), np.array([0,0]), np.array([1,0])])
out = np.array([ 0, 1, 0, 1])
center = np.array([ np.array([1,1]), np.array([0,0])])
rbf_instance = rbf(inp, out, center)
In [45]:
inp_test = np.array([np.array([1,1]),
np.array([0,1]),
np.array([0,0]),
np.array([1,0])])
output = map(rbf_instance, inp_test)
In [46]:
def colorize(output):
c = [None]* len(output)
for i, iv in enumerate(output):
if (output[i] > 0):
c[i] = 'blue'
else:
c[i] = 'red'
return c
In [53]:
inp_x = [1, 0, 0, 1]
inp_y = [1, 1, 0, 0]
c = colorize(output)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
xx, yy = np.meshgrid(np.arange(0, 1, 0.02), np.arange(0, 1, 0.02))
cm_bright = ListedColormap(['#FF0000', '#0000FF'])
ax.scatter(xx[:, 0], yy[:, 1], output, cmap=cm_bright, depthshade=False)
plt.savefig("rbf_xor.svg")
plt.show()
In [ ]: