I was creating a custom rbf function for the SVC class of sklearn as following:
def rbf_kernel(x, y, gamma):
dis = np.sqrt(((x.reshape(-1, 1)) - y.reshape(1, -1)) ** 2)
return np.exp(-(gamma*dis)**2)
def eval_kernel(kernel):
model = SVC(kernel=kernel, C=C, gamma=gamma, degree=degree, coef0=coef0)
model.fit(X_train, y_train)
X_test_predict = model.predict(X_test)
acc = (X_test_predict == y_test).sum() / y_test.shape[0]
return acc
for k1, k2 in [('rbf', lambda x, y: rbf_kernel(x, y, gamma))]:
acc1 = eval_kernel(k1)
acc2 = eval_kernel(k2)
assert(abs(acc1 - acc2) < eps)
The shape of X_train is (396, 10), y_train is (396, 10) and X_test is (132, 10). However, when I try to run it, I get an error saying:
ValueError: X.shape[1] = 3960 should be equal to 396, the number of samples at training time
It seems the errors are due to the difference in the dimension of X_test and X_train, but is there any way to fix this error?
Thank you in advance!