12. SVM
Pkg.add("SVM")
using SVM
using RDatasets
!
# Read iris data
iris = data("datasets", "iris")
!
# SVM format expects observations in columns and features in rows
X = matrix(iris[:, 1:4])’
p, n = size(X)
!
# SVM format expects positive and negative examples to +1/-1
Y = [species == "setosa" ? 1.0 : -1.0 for species in iris[:, "Species"]]
!
# Select a subset of the data for training, test on the rest.
train = randbool(n)
!
# We'll fit a model with all of the default parameters
model = svm(X[:,train], Y[train])
!
# And now evaluate that model on the testset
accuracy = nnz(predict(model, X[:,~train]) .== Y[~train])/nnz(~train)