Build Support Vector Models (SVM) with different kernels

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.

Yes you can loop around with different parameters but its better to use-

  1. GridSearchCV: If you wanna try all the given permutations and combinations.
    Grid Search Short Guide : https://towardsdatascience.com/gridsearchcv-for-beginners-db48a90114ee

  2. RandomizedSearchCV: If you wanna try all the given parameters randomly and its okay if some values are missed.

Thank you very much!

Do you know how i would store them in the lists?

Store the outcome of the folds in these lists

means = []
stds = []
params = []

I don’t know what you want to store in those lists. If you’re talking about the best parameters and the corresponding score values. You can use model.get_params() to get the current parameters used in the model you’re training and model.score() for the score.

Just use .append() method to store the values to relevant lists.