Ottimizzare le SVM

Support Vector Machines in R

Kailash Awati

Instructor

Obiettivo del tuning

  • Difficile scegliere a mano i parametri ottimali per kernel complessi.
  • Obiettivo: trovare il set di parametri ottimali con la funzione tune.svm().
Support Vector Machines in R

Tuning in breve

  • Come funziona:
    • definisci un intervallo di ricerca per ogni parametro. Esempi: cost = 10^(-1:3), gamma = c(0.1,1,10), coef0 = c(0.1,1,10)
    • Costruisci un modello SVM per ogni combinazione possibile e valuta l'accuratezza.
    • Restituisci la combinazione che dà l'accuratezza migliore.
  • Procedura computazionalmente pesante!
Support Vector Machines in R
  • Esegui il tuning del modello SVM per il dataset radialmente separabile creato prima
    • Lezione scorsa: SVM con kernel polinomiale
    • Accuratezza SVM ~94%.
  • Possiamo migliorare tarando gamma, cost e coef0?
tune_out <- tune.svm(x = trainset[,-3], y = trainset[,3], 
                     type = "C-classification", kernel = "polynomial", degree = 2,
                     cost = 10^(-1:2), gamma = c(0.1,1,10), coef0 = c(0.1,1,10))

#print out tuned parameters tune_out$best.parameters$cost tune_out$best.parameters$gamma tune_out$best.parameters$coef0
0.1
10
1
Support Vector Machines in R
  • Costruisci il modello SVM usando i parametri migliori trovati con tune.svm().
svm_model <- svm(y ~ ., data = trainset, type = "C-classification", kernel = "polynomial", degree = 2,
        cost = tune_out$best.parameters$cost,
        gamma = tune_out$best.parameters$gamma,
        coef0 = tune_out$best.parameters$coef0)
  • valuta l'accuratezza su training e test
pred_train <- predict(svm_model, trainset)
mean(pred_train == trainset$y)
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
1

0.9677419
#plot using svm plot
plot(svm_model, trainset)
Support Vector Machines in R

Capitolo 3.4 - dataset radialmente separabile, kernel quadratico ottimizzato, grafico con svm.plot

Support Vector Machines in R

Esercitiamoci!

Support Vector Machines in R

Preparing Video For Download...