0

I have a data set X and labels y for training and scoring a sklearn.SVC model. The data is split into X_train and X_test. I run a for-loop to find the best possible value combination (i.e. best score) for two SVC parameters: C and gamma. I can print out the highest score, but how do I print the C and gamma values that were used for this particular score?

for C in np.arange(0.05, 2.05, 0.05):
    for gamma in np.arange(0.001, 0.101, 0.001):
        model = SVC(kernel='rbf', gamma=gamma, C=C)
        model.fit(X_train, y_train)
        score = model.score(X_test, y_test)
        if score > best_score:
            best_score = score
print('Highest Accuracy Score: ', best_score)   
Sociopath
  • 13,068
  • 19
  • 47
  • 75
Alex K.
  • 51
  • 1
  • 1
  • 7

2 Answers2

1

You can change it to:

   if score > best_score:
        best_score = score
        best_C = C
        best_gamma = gamma 

Or create a tuple:

if score > best_score:
    best_score = score, C, gamma 
macro_controller
  • 1,469
  • 1
  • 14
  • 32
1

Store them?

best_C = None
best_gamma = None
for C in np.arange(0.05, 2.05, 0.05):
    for gamma in np.arange(0.001, 0.101, 0.001):
        model = SVC(kernel='rbf', gamma=gamma, C=C)
        model.fit(X_train, y_train)
        score = model.score(X_test, y_test)
        if score > best_score:
            best_score = score
            best_C = C
            best_gamma = gamma
print('Highest Accuracy Score: ', best_score)  
print(best_C)
print(best_gamma)
Mathieu
  • 5,410
  • 6
  • 28
  • 55