Untitled

 avatar
unknown
python
2 years ago
1.6 kB
2
Indexable
import numpy as np
import matplotlib.pyplot as plt
import numpy.linalg as la

n = 200
p = 2
X = 2 * (np.random.rand(n, p) - .5)
y = np.sign(X[:, 1] - (X[:, 0] ** 2 / 2 + np.sin(X[:, 0] * 7) / 2))
plt.figure(1)
plt.scatter(X[:, 0], X[:, 1], 50, c=y)
plt.colorbar()
plt.xlabel('feature 1')
plt.ylabel('feature 2')
plt.title('2d training samples colored by label ')
plt.show()

sigma_square = 2
lam = 1
norms2 = np.array(la.norm(X, axis=1)).T ** 2  # squared norm of each training sample
innerProds = X @ X.T
dist2 = np.matrix(norms2).T @ np.ones([1, n]) + np.ones([n, 1]) @ np.matrix(
    norms2) - 2 * innerProds  # squared distances between each pair of training samples
print(dist2)
K = np.exp(-dist2 / (2 * sigma_square))
alpha = (K + lam * np.identity(n)).I @ y
yhat = K @ alpha.T

y2 = np.array(np.sign(yhat))
plt.figure(2)
plt.scatter(X[:, 0], X[:, 1], 50, c=y2)
plt.colorbar()
plt.xlabel('feature 1')
plt.ylabel('feature 2')
plt.title('2d training samples colored by PREDICTED label ')
plt.show()

ntest = 2000
Xtest = 2 * (np.random.rand(ntest, p) - .5)
norms2_test = np.array(la.norm(Xtest, axis=1)).T ** 2
innerProds_test = Xtest @ X.T
dist2_test = np.matrix(norms2_test).T @ np.ones([1, n]) + np.ones([
    ntest, 1]) @ np.matrix(norms2) - 2 * innerProds_test
K_test = np.exp(-dist2_test / (2 * sigma_square))

ytest = K_test @ alpha.T
y3 = np.array(np.sign(ytest))
plt.figure(3)
plt.scatter(Xtest[:, 0], Xtest[:, 1], 50, c=np.array(ytest))
plt.colorbar()
plt.xlabel('feature 1')
plt.ylabel('feature 2')
plt.title('2d test samples colored by PREDICTED label ( before taking sign )')
plt.show()
Editor is loading...