Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of SVM, Decision Tree, and Random Forest algorithms #211

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
188 changes: 188 additions & 0 deletions ml/svm.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
module ml

import math
import rand

pub struct SVMConfig {
pub mut:
max_iterations int = 1000
learning_rate f64 = 0.01
tolerance f64 = 1e-6
c f64 = 1.0 // Regularization parameter
}

pub struct DataPoint {
pub mut:
x []f64
y int
}

pub struct SVMModel {
pub mut:
support_vectors []DataPoint
alphas []f64
b f64
kernel KernelFunction @[required]
config SVMConfig
}

pub struct SVM {
pub mut:
model &SVMModel = unsafe { nil }
kernel KernelFunction @[required]
config SVMConfig
}

type KernelFunction = fn ([]f64, []f64) f64

fn vector_dot(x []f64, y []f64) f64 {
mut sum := 0.0
for i := 0; i < x.len; i++ {
sum += x[i] * y[i]
}
return sum
}

fn vector_subtract(x []f64, y []f64) []f64 {
mut result := []f64{len: x.len}
for i := 0; i < x.len; i++ {
result[i] = x[i] - y[i]
}
return result
}

pub fn linear_kernel(x []f64, y []f64) f64 {
return vector_dot(x, y)
}

pub fn polynomial_kernel(degree int) KernelFunction {
return fn [degree] (x []f64, y []f64) f64 {
return math.pow(vector_dot(x, y) + 1.0, f64(degree))
}
}

pub fn rbf_kernel(gamma f64) KernelFunction {
return fn [gamma] (x []f64, y []f64) f64 {
diff := vector_subtract(x, y)
return math.exp(-gamma * vector_dot(diff, diff))
}
}

pub fn SVM.new(kernel KernelFunction, config SVMConfig) &SVM {
return &SVM{
kernel: kernel
config: config
}
}

pub fn (mut s SVM) train(data []DataPoint) {
s.model = train_svm(data, s.kernel, s.config)
}

pub fn (s &SVM) predict(x []f64) int {
return predict(s.model, x)
}

pub fn train_svm(data []DataPoint, kernel KernelFunction, config SVMConfig) &SVMModel {
mut model := &SVMModel{
support_vectors: []DataPoint{}
alphas: []f64{len: data.len, init: 0.0}
b: 0.0
kernel: kernel
config: config
}

mut passes := 0
for {
mut num_changed_alphas := 0
for i in 0 .. data.len {
ei := predict_raw(model, data[i].x) - f64(data[i].y)
if (data[i].y * ei < -model.config.tolerance && model.alphas[i] < model.config.c)
|| (data[i].y * ei > model.config.tolerance && model.alphas[i] > 0) {
j := rand.int_in_range(0, data.len - 1) or { panic(err) }
ej := predict_raw(model, data[j].x) - f64(data[j].y)

alpha_i_old := model.alphas[i]
alpha_j_old := model.alphas[j]

mut l, mut h := 0.0, 0.0
if data[i].y != data[j].y {
l = math.max(0.0, model.alphas[j] - model.alphas[i])
h = math.min(model.config.c, model.config.c + model.alphas[j] - model.alphas[i])
} else {
l = math.max(0.0, model.alphas[i] + model.alphas[j] - model.config.c)
h = math.min(model.config.c, model.alphas[i] + model.alphas[j])
}

if l == h {
continue
}

eta := 2 * model.kernel(data[i].x, data[j].x) - model.kernel(data[i].x,
data[i].x) - model.kernel(data[j].x, data[j].x)

if eta >= 0 {
continue
}

model.alphas[j] = alpha_j_old - f64(data[j].y) * (ei - ej) / eta
model.alphas[j] = math.max(l, math.min(h, model.alphas[j]))

if math.abs(model.alphas[j] - alpha_j_old) < 1e-5 {
continue
}

model.alphas[i] = alpha_i_old +
f64(data[i].y * data[j].y) * (alpha_j_old - model.alphas[j])

b1 := model.b - ei - f64(data[i].y) * (model.alphas[i] - alpha_i_old) * model.kernel(data[i].x,
data[i].x) - f64(data[j].y) * (model.alphas[j] - alpha_j_old) * model.kernel(data[i].x,
data[j].x)

b2 := model.b - ej - f64(data[i].y) * (model.alphas[i] - alpha_i_old) * model.kernel(data[i].x,
data[j].x) - f64(data[j].y) * (model.alphas[j] - alpha_j_old) * model.kernel(data[j].x,
data[j].x)

if 0 < model.alphas[i] && model.alphas[i] < model.config.c {
model.b = b1
} else if 0 < model.alphas[j] && model.alphas[j] < model.config.c {
model.b = b2
} else {
model.b = (b1 + b2) / 2
}

num_changed_alphas++
}
}

if num_changed_alphas == 0 {
passes++
} else {
passes = 0
}

if passes >= model.config.max_iterations {
break
}
}

for i in 0 .. data.len {
if model.alphas[i] > 0 {
model.support_vectors << data[i]
}
}

return model
}

fn predict_raw(model &SVMModel, x []f64) f64 {
mut sum := 0.0
for i, sv in model.support_vectors {
sum += model.alphas[i] * f64(sv.y) * model.kernel(x, sv.x)
}
return sum + model.b
}

pub fn predict(model &SVMModel, x []f64) int {
return if predict_raw(model, x) >= 0 { 1 } else { -1 }
}
127 changes: 127 additions & 0 deletions ml/svm_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
module ml

import math

fn test_vector_dot() {
x := [1.0, 2.0, 3.0]
y := [4.0, 5.0, 6.0]
result := vector_dot(x, y)
assert math.abs(result - 32.0) < 1e-6
}

fn test_vector_subtract() {
x := [1.0, 2.0, 3.0]
y := [4.0, 5.0, 6.0]
result := vector_subtract(x, y)
assert result == [-3.0, -3.0, -3.0]
}

fn test_linear_kernel() {
x := [1.0, 2.0, 3.0]
y := [4.0, 5.0, 6.0]
result := linear_kernel(x, y)
assert math.abs(result - 32.0) < 1e-6
}

fn test_polynomial_kernel() {
x := [1.0, 2.0, 3.0]
y := [4.0, 5.0, 6.0]
kernel := polynomial_kernel(3)
result := kernel(x, y)
expected := math.pow(32.0 + 1.0, 3)
assert math.abs(result - expected) < 1e-6
}

fn test_rbf_kernel() {
x := [1.0, 2.0, 3.0]
y := [4.0, 5.0, 6.0]
gamma := 0.5
kernel := rbf_kernel(gamma)
result := kernel(x, y)
expected := math.exp(-gamma * 27.0)
assert math.abs(result - expected) < 1e-6
}

fn test_svm_new() {
config := SVMConfig{}
svm := SVM.new(linear_kernel, config)
assert svm.kernel == linear_kernel
assert svm.config == config
}

fn test_svm_train_and_predict() {
mut svm := SVM.new(linear_kernel, SVMConfig{})
data := [
DataPoint{[2.0, 3.0], 1},
DataPoint{[1.0, 1.0], -1},
DataPoint{[3.0, 4.0], 1},
DataPoint{[0.0, 0.0], -1},
]
svm.train(data)

for point in data {
prediction := svm.predict(point.x)
assert prediction == point.y
}
}

fn test_train_svm() {
data := [
DataPoint{[2.0, 3.0], 1},
DataPoint{[1.0, 1.0], -1},
DataPoint{[3.0, 4.0], 1},
DataPoint{[0.0, 0.0], -1},
]
config := SVMConfig{}
model := train_svm(data, linear_kernel, config)

for point in data {
prediction := predict(model, point.x)
assert prediction == point.y
}
}

fn test_predict_raw() {
data := [
DataPoint{[2.0, 3.0], 1},
DataPoint{[1.0, 1.0], -1},
]
config := SVMConfig{}
model := train_svm(data, linear_kernel, config)

result := predict_raw(model, [2.0, 3.0])
assert result > 0

result2 := predict_raw(model, [1.0, 1.0])
assert result2 < 0
}

fn test_predict() {
data := [
DataPoint{[2.0, 3.0], 1},
DataPoint{[1.0, 1.0], -1},
DataPoint{[3.0, 4.0], 1},
DataPoint{[0.0, 0.0], -1},
]
config := SVMConfig{}
model := train_svm(data, linear_kernel, config)

for point in data {
prediction := predict(model, point.x)
assert prediction == point.y
}
}

fn main() {
test_vector_dot()
test_vector_subtract()
test_linear_kernel()
test_polynomial_kernel()
test_rbf_kernel()
test_svm_new()
test_svm_train_and_predict()
test_train_svm()
test_predict_raw()
test_predict()
println('All tests passed successfully!')
}
Loading