Implicit matplotlib python graphs
Good time of day!
There is a function of two variables (the decision function):
def dec_func(self, x, y):
res = 0
for i, w in enumerate(self.W):
res += w * self.F[i](Point(x, y))
return res
We need to plot this function when res = 0. That is, we need to get y from the list X for each x, so that when the function is executed, the result is 0.
UPD:
There is an implemented function that, given X, can get approximately correct Y (with a certain accuracy):
def plot_data(self, start=-15, end=15, step=1):
X = [i for i in range(start, end, step)]
Y = []
x_to_remove = []
for x in X:
k = 0
y_old = 0
step = 0.05
old_res = self.dec_func(x, y_old)
while abs(old_res) > 0.01:
if k > 3000:
break
k += 1
y = y_old + step
res = self.dec_func(x, y)
if abs(old_res) > abs(res):
y_old = y
step *= 1.2
else:
y_old = y
step = -step / 2
old_res = res
if k > 3000:
print(y_old, x, old_res)
x_to_remove.append(x)
else:
Y.append(y_old)
for x in x_to_remove:
X.remove(x)
return X, Y
But the problem is that this implementation does not allow get multiple y values for a single x.
1
Author: Ильдан Киамов, 2016-05-22
1 answers
This is how you can draw a graph for such functions.
import matplotlib.pyplot
from numpy import arange
from numpy import meshgrid
delta = 0.025
xrange = arange(-50.0, 50.0, delta)
yrange = arange(-50.0, 50.0, delta)
X, Y = meshgrid(xrange, yrange)
F = dec_func(X, Y)
matplotlib.pyplot.contour(X, Y, F, [0])
1
Author: Ильдан Киамов, 2016-05-25 07:20:30