import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
import gradio as gr
from PIL import Image
def calculate_score(clf):
xx, yy = np.meshgrid(np.linspace(-3, 3, 500), np.linspace(-3, 3, 500))
X_test = np.c_[xx.ravel(), yy.ravel()]
Y_test = np.logical_xor(xx.ravel() > 0, yy.ravel() > 0)
return clf.score(X_test, Y_test)
def getColorMap(kernel, gamma):
# prepare the training dataset
np.random.seed(0)
X = np.random.randn(300, 2)
Y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0)
# fit the model
clf = svm.NuSVC(kernel=kernel, gamma=gamma)
clf.fit(X, Y)
#create a grid for the plotting the decision function
xx, yy = np.meshgrid(np.linspace(-3, 3, 500), np.linspace(-3, 3, 500))
# plot the decision function for each datapoint on the grid
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.figure(figsize=(10, 4))
plt.imshow(
Z,
interpolation="nearest",
extent=(xx.min(), xx.max(), yy.min(), yy.max()),
aspect="auto",
origin="lower",
cmap=plt.cm.PuOr_r,
)
contours = plt.contour(xx, yy, Z, levels=[0], linewidths=2, linestyles="dashed")
plt.scatter(X[:, 0], X[:, 1], s=30, c=Y, cmap=plt.cm.Paired, edgecolors='k')
plt.title(f"Decision function for Non-Linear SVC with the {kernel} kernel and '{gamma}' gamma ", fontsize='14') #title
plt.xlabel("X",fontsize='13') #adds a label in the x axis
plt.ylabel("Y",fontsize='13') #adds a label in the y axis
return plt, calculate_score(clf)
#XOR_TABLE markdown text
XOR_TABLE = """
A | B | A XOR B |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |