Il kernel RBF

Support Vector Machines in R

Kailash Awati

Instructor

RBF kernel in breve

  • Funzione decrescente della distanza tra due punti nell'insieme di dati.
  • Simula l'algoritmo k-NN.
Support Vector Machines in R

Capitolo 4.3 - Kernel RBF - influenza in funzione della distanza tra punti

Support Vector Machines in R

Creare una SVM con il kernel RBF

  • Crea una SVM con kernel RBF per un insieme di dati complesso
svm_model <- svm(y ~ ., 
                data = trainset, 
                type = "C-classification",
                kernel = "radial")
  • Calcola l'accuratezza train/test e tracciala sul training set.
pred_train <- predict(svm_model, trainset)
mean(pred_train == trainset$y)
0.93125
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
0.9416667
#plot decision boundary
plot(svm_model, trainset)
Support Vector Machines in R

Capitolo 4.3 - insieme di dati complesso, kernel RBF predefinito, grafico con svm.plot

Support Vector Machines in R

Raffinare il confine di decisione

  • Ottimizza gamma e cost con tune.svm()
# Tune parameters
tune_out <- tune.svm(x = trainset[,-3],
                     y = trainset[,3],
                     gamma = 5 * 10 ^ (-2:2),
                     cost = c(0.01, 0.1, 1, 10, 100),
                     type = "C-classification",
                     kernel = "radial")
  • Stampa i parametri migliori
# Print best values of cost and gamma
tune_out$best.parameters$cost
1
tune_out$best.parameters$gamma
5
Support Vector Machines in R

Il modello ottimizzato

  • Crea il modello ottimizzato con best.parameters
svm_model <- svm(y ~ ., data = trainset, 
              type = "C-classification", 
              kernel = "radial",
              cost = tune_out$best.parameters$cost,
              gamma = tune_out$best.parameters$gamma)
  • Calcola l'accuratezza sul test
mean(pred_test == testset$y)
0.95
  • Traccia il confine di decisione
plot(svm_model, trainset)
Support Vector Machines in R

Capitolo 4.3 - insieme di dati complesso, kernel RBF ottimizzato, grafico con svm.plot

Support Vector Machines in R

Esercitiamoci!

Support Vector Machines in R

Preparing Video For Download...