Zupex
Zupex

Reputation: 23

Build SVMs with different kernels (RBF)

I have this in python


from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, roc_auc_score

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)

# The gamma parameter is the kernel coefficient for kernels rbf/poly/sigmoid
svm = SVC(gamma='auto', probability=True)

svm.fit(X_train,y_train.values.ravel())
prediction = svm.predict(X_test)
prediction_prob = svm.predict_proba(X_test)
print('Accuracy:', accuracy_score(y_test,prediction))
print('AUC:',roc_auc_score(y_test,prediction_prob[:,1]))

print(X_train)
print(y_train)

Now I want to build this with a different kernel rbf and store the values into arrays.

so something like this


def svm_grid_search(parameters, cv):
    
    # Store the outcome of the folds in these lists
    means = []
    stds = []
    params = []
    
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)
    
    for parameter in parameters: 
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)
        # The gamma parameter is the kernel coefficient for kernels rbf/poly/sigmoid
        svm = SVC(gamma=1,kernel ='rbf',probability=True)
        svm.fit(X_train,y_train.values.ravel())
        prediction = svm.predict(X_test)
        prediction_prob = svm.predict_proba(X_test)
        return means, stddevs, params

I know I want to loop around the parameters and then store the values into the lists. But I struggle how to do so ...

So what I try to do is to loop and then store the results of the SVM in the arrays with the kernel = parameter

I would be very thankful if you could help me out here.

Upvotes: 0

Views: 177

Answers (1)

qmeeus
qmeeus

Reputation: 2412

This is what GridSearchCV is for. Link here

See here for an example

Upvotes: 1

Related Questions