Implementing the implicit part of QML in feature HS


hi this is my attempt to implement the first part of this paper
I hope i coded it well :smiley:
any feedback is much appreciated

import numpy as np
from sklearn.linear_model import LogisticRegression as lr
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

from sklearn.datasets import make_moons, make_blobs, make_circles
X,y = make_circles(n_samples=300,random_state=0,noise=0.05,factor=.6)
X_train, X_test, Y_train, Y_test = train_test_split(X, y,test_size=0.2,random_state=0)

def kernel(x1, x2,amp):

a = np.array((1/np.cosh(amp))*(1/np.cosh(amp)))          
b = 1-(np.exp(1j*(x1-x2))*np.tanh(amp)*np.tanh(amp))       

return abs(,np.sqrt(a/b)))

def gram(A, B,amp):

gram = np.zeros((len(A), len(B)))
for id1, x1 in enumerate(A):
    for id2, x2 in enumerate(B):
        gram[id1, id2] = kernel(x1, x2,amp)

return gram

gram_train = gram(X_train, X_train,.8)
gram_test = gram(X_test, X_train,.8)

model =lr(random_state=0), Y_train)

predictions_test = model.predict(gram_test)
accuracy_score(predictions_test, Y_test)

xx, yy = np.meshgrid(np.linspace(-1.4, 1.4, 20), np.linspace(-1.2, 1.4, 20))
X_grid = [np.array([x, y]) for x, y in zip(xx.flatten(), yy.flatten())]
tt=gram(X_grid, X_train,.8)

start plot

cm =

plot decision regions

predictions_grid = [model.predict(tt)]
Z = np.reshape(predictions_grid, xx.shape)
cnt = plt.contourf(xx, yy, Z, levels=np.arange(0., 1.1, 0.1), cmap=cm, alpha=.8)
plt.colorbar(cnt, ticks=[0, 0.5, 1])

plot data

plt.scatter(X_train[:, 0][Y_train==0], X_train[:, 1][Y_train==0], c=β€˜r’, marker=’^’, edgecolors=β€˜k’,label=β€˜train0’)
plt.scatter(X_train[:, 0][Y_train==1], X_train[:, 1][Y_train==1], c=β€˜b’, marker=’^’, edgecolors=β€˜k’,label=β€˜train1’)
plt.scatter(X_test[:, 0][Y_test==0], X_test[:, 1][Y_test==0], c=β€˜r’, marker=β€˜o’, edgecolors=β€˜k’,label=β€˜test0’)
plt.scatter(X_test[:, 0][Y_test==1], X_test[:, 1][Y_test==1], c=β€˜b’, marker=β€˜o’, edgecolors=β€˜k’,label=β€˜test1’)

#plt.ylim(-0.4, 0.4)
#plt.xlim(-0.4, 0.4)



is my understanding right? @josh