import numpy as np
from sklearn.datasets import load_iris
from sklearn.metrics.pairwise import linear_kernel as sklinear_kernel
def linear_kernel(X, Y=None):
if Y is None:
Y = X
K = np.zeros((X.shape[0], Y.shape[0]))
for i in range(X.shape[0]):
for j in range(Y.shape[0]):
K[i, j] = np.dot(X[i], Y[j])
return K
X, _ = load_iris(return_X_y=True)
K1 = linear_kernel(X)
K2 = sklinear_kernel(X)
assert np.allclose(K1, K2)
def linear_kernel(X, Y=None):
if Y is None:
Y = X
return np.dot(X, Y.T)
X, _ = load_iris(return_X_y=True)
K1 = linear_kernel(X)
K2 = sklinear_kernel(X)
assert np.allclose(K1, K2)