Furqan Shaikh
Furqan Shaikh

Reputation: 65

Use RBF Kernel with Chi-squared distance metric in SVM

How to achieve the title mentioned task. Do we have any parameter in RBF kernel to set the distance metric as chi-squared distance metric. I can see a chi2_kernel in the sk-learn library.

Below is the code that i have written.

import numpy as np
from sklearn import datasets
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, classification_report, confusion_matrix

from sklearn.preprocessing import Imputer
from numpy import genfromtxt
from sklearn.metrics.pairwise import chi2_kernel


file_csv = 'dermatology.data.csv'
dataset = genfromtxt(file_csv, delimiter=',')

imp = Imputer(missing_values='NaN', strategy='most_frequent', axis=1)
dataset = imp.fit_transform(dataset)

target = dataset[:, [34]].flatten()
data = dataset[:, range(0,34)]

X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.3)

# TODO : willing to set chi-squared distance metric instead. How to do that ?
clf = svm.SVC(kernel='rbf', C=1)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)

print(f1_score(y_test, y_pred, average="macro"))
print(precision_score(y_test, y_pred, average="macro"))
print(recall_score(y_test, y_pred, average="macro"))

Upvotes: 1

Views: 707

Answers (1)

lejlot
lejlot

Reputation: 66805

Are you sure you want to compose rbf and chi2? Chi2 on its own defines a valid kernel, and all you have to do is

clf = svm.SVC(kernel=chi2_kernel, C=1)

since sklearn accepts functions as kernels (however this will require O(N^2) memory and time). If you would like to compose these two it is a bit more complex, and you will have to implement your own kernel to do that. For a bit more control (and other kernels) you might also try pykernels, however there is no support for composing yet.

Upvotes: 1

Related Questions