Pkg.update()
Pkg.add("Optim")
Pkg.add("JuMP")
Pkg.add("Cbc") # for LP and MILP (mixed-integer linear program)
INFO: Updating METADATA... INFO: Updating Plotly... INFO: Updating UCIMLRepo... INFO: Computing changes... INFO: No packages to install, update or remove INFO: Nothing to be done INFO: Nothing to be done INFO: Nothing to be done
Pkg.add("RDatasets")
INFO: Nothing to be done
using RDatasets
iris = dataset("datasets", "iris")
SepalLength | SepalWidth | PetalLength | PetalWidth | Species | |
---|---|---|---|---|---|
1 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
2 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
3 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
4 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
5 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
6 | 5.4 | 3.9 | 1.7 | 0.4 | setosa |
7 | 4.6 | 3.4 | 1.4 | 0.3 | setosa |
8 | 5.0 | 3.4 | 1.5 | 0.2 | setosa |
9 | 4.4 | 2.9 | 1.4 | 0.2 | setosa |
10 | 4.9 | 3.1 | 1.5 | 0.1 | setosa |
11 | 5.4 | 3.7 | 1.5 | 0.2 | setosa |
12 | 4.8 | 3.4 | 1.6 | 0.2 | setosa |
13 | 4.8 | 3.0 | 1.4 | 0.1 | setosa |
14 | 4.3 | 3.0 | 1.1 | 0.1 | setosa |
15 | 5.8 | 4.0 | 1.2 | 0.2 | setosa |
16 | 5.7 | 4.4 | 1.5 | 0.4 | setosa |
17 | 5.4 | 3.9 | 1.3 | 0.4 | setosa |
18 | 5.1 | 3.5 | 1.4 | 0.3 | setosa |
19 | 5.7 | 3.8 | 1.7 | 0.3 | setosa |
20 | 5.1 | 3.8 | 1.5 | 0.3 | setosa |
21 | 5.4 | 3.4 | 1.7 | 0.2 | setosa |
22 | 5.1 | 3.7 | 1.5 | 0.4 | setosa |
23 | 4.6 | 3.6 | 1.0 | 0.2 | setosa |
24 | 5.1 | 3.3 | 1.7 | 0.5 | setosa |
25 | 4.8 | 3.4 | 1.9 | 0.2 | setosa |
26 | 5.0 | 3.0 | 1.6 | 0.2 | setosa |
27 | 5.0 | 3.4 | 1.6 | 0.4 | setosa |
28 | 5.2 | 3.5 | 1.5 | 0.2 | setosa |
29 | 5.2 | 3.4 | 1.4 | 0.2 | setosa |
30 | 4.7 | 3.2 | 1.6 | 0.2 | setosa |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
size(iris)
(150,5)
class = iris[:Species]
species = unique(class)
#show(species)
color = ["red" for i=1:size(iris,1)]
color[class.==species[2]] = "blue"
color[class.==species[3]] = "yellow"
color
150-element Array{ASCIIString,1}: "red" "red" "red" "red" "red" "red" "red" "red" "red" "red" "red" "red" "red" ⋮ "yellow" "yellow" "yellow" "yellow" "yellow" "yellow" "yellow" "yellow" "yellow" "yellow" "yellow" "yellow"
using PyPlot
fig = figure("iris_scatterplot",figsize=(15,5))
ax = subplot(121)
for i=1:length(species)
idx = class.==species[i];
scatter(iris[idx,:SepalLength], iris[idx,:SepalWidth],color=color[idx],s=30,alpha=0.5,label=species[i])
end
title("IRIS Data")
xlabel("SepalLength")
ylabel("SepalWidth")
grid("on")
legend(loc=4, fontsize="small")
ax = subplot(122)
for i=1:length(species)
idx = class.==species[i];
scatter(iris[idx,:PetalLength], iris[idx,:PetalWidth],color=color[idx],s=30,alpha=0.5,label=species[i])
end
title("IRIS Data")
xlabel("PetalLength")
ylabel("PetalWidth")
grid("on")
legend(loc=4, fontsize="small")
PyObject <matplotlib.legend.Legend object at 0x31d398b50>
x = Array(iris[:,1:(end-1)])
m, p = size(x)
y = -ones(m)
@printf "Making binary classification problem: '%s (+1)' vs. the rest (-1)" species[3]
y[class.==species[3]] = 1
C = 10/m;
Making binary classification problem: 'virginica (+1)' vs. the rest (-1)
using JuMP
using Cbc # NLP solver inc. QP, QCQP
svm = Model(solver=CbcSolver())
@defVar(svm, w[i=1:p])
@defVar(svm, b)
@defVar(svm, ξ[i=1:m] >= 0)
@defVar(svm, ζ[i=1:p])
@setObjective(svm, Min, sum{ ζ[i], i=1:p } + C*sum{ ξ[i], i=1:m})
for i=1:m
@addConstraint(svm, ξ[i] >= 1 - y[i]*( sum{w[j]*x[i,j], j=1:p} + b))
end
for i=1:p
@addConstraint(svm, ζ[i] >= w[i])
@addConstraint(svm, ζ[i] >= -w[i])
end
status = solve(svm)
w = getValue(w)
b = getValue(b);
pred = [Float64((x[i,:]*w + b)[1]) for i=1:m]
correct = y.*pred
@printf "Prediction accuracy on the training set = %.2f %%" sum(correct .> 0) / m
Prediction accuracy on the training set = 0.95 %