import matplotlib.pyplot as plt
import sys
sys.path.insert(0, 'startkit')
import startkit_asymmetric
import startkit_circle
import startkit_heart
import numpy as np
asym = np.load('asymmetric.npz')
lst = asym.files
for item in lst:
print(item)
print(asym[item])
x = asym['x']
y = asym['y']
filenames = [startkit_asymmetric, startkit_circle, startkit_heart]
names = ["asymmetric", "circle", "heart"]
for i, file in enumerate(filenames):
X, y = file.X, file.y
plt.figure(figsize=[10, 18])
plt.subplot(3,1,i+1)
pos = y == 1.
neg = y == -1.
Xpos = X[pos, :]
Xneg = X[neg, :]
plt.scatter(Xpos[:, 0], Xpos[:, 1], label='$y=+1$')
plt.scatter(Xneg[:, 0], Xneg[:, 1], label='$y=-1$')
### end viz_data ###
plt.legend()
plt.title(names[i])
plt.show()
subplotidx = 0
filename = startkit_heart
plt.figure(figsize=[12, 10])
for D in range(1, 15):
Xd_train = filename.assemble_feature(filename.X_train, D)
Xd_valid = filename.assemble_feature(filename.X_valid, D)
w = filename.lstsq(Xd_train, filename.y_train, filename.LAMBDA)
error_train = np.average(np.square(filename.y_train - Xd_train @ w))
error_valid = np.average(np.square(filename.y_valid - Xd_valid @ w))
condition = np.linalg.cond(Xd_valid.T @ Xd_valid + np.eye(Xd_valid.shape[1]))
if D in [2, 4, 6, 8, 10, 12]:
subplotidx += 1
plt.subplot(3,2, subplotidx)
filename.heatmap(lambda x, y: filename.assemble_feature(np.vstack([x, y]).T, D) @ w)
plt.title("D = %d" % D)
print("p = {:2d} train_error = {:10.6f} validation_error = {:10.6f} cond = {:14.6f}".
format(D, error_train, error_valid, condition))
plt.show();
subplotidx = 0
filename = startkit_heart
def poly_kernel(X, XT, D):
K = np.power(X @ XT + 1, D)
return K
plt.figure(figsize=[12, 10])
for D in range(1, 15):
K = poly_kernel(filename.X_train, filename.X_train.T, D) + filename.LAMBDA * np.eye(filename.X_train.shape[0])
c = np.linalg.solve(K, filename.y_train)
error_train = np.average(np.square(filename.y_train - poly_kernel(filename.X_train, filename.X_train.T, D) @ c))
error_valid = np.average(np.square(filename.y_valid - poly_kernel(filename.X_valid, filename.X_train.T, D) @ c))
print("p = {:2d} train_error = {:7.6f} validation_error = {:7.6f} cond = {:14.6f}".
format(D, error_train, error_valid, np.linalg.cond(K)))
if D in [2, 4, 6, 8, 10, 12]:
subplotidx += 1
plt.subplot(3,2,subplotidx)
filename.heatmap(lambda x, y: poly_kernel(np.column_stack([x, y]), filename.X_train.T, D) @ c)
plt.title("D = %d" % D)
plt.show()