From b2201644d15c50d352f5ce547057c749c3671edb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Felix=20Ramnel=C3=B6v?= <felra653@student.liu.se>
Date: Wed, 11 Dec 2024 15:01:42 +0100
Subject: [PATCH] Lab 3: Fixed error for rbf kernel in assignment 3

---
 lab3/assignment3.R | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lab3/assignment3.R b/lab3/assignment3.R
index 5ebddf8..fe91786 100644
--- a/lab3/assignment3.R
+++ b/lab3/assignment3.R
@@ -89,8 +89,8 @@ err3
 
 # 3. Implementation of SVM predictions.
 
-gaussian_kernel <- function(x, sigma = 0.05) {
-  exp(-(x ^ 2) / (2 * sigma ^ 2))
+rbf_kernel <- function(x1, x2, lambda = 0.05) {
+  exp(-(dist(rbind(x1,x2)) ^ 2) * lambda)
 }
 
 sv <- alphaindex(filter3)[[1]]
@@ -103,7 +103,7 @@ for (i in 1:10) {
   data_point <- spam[i, -58]
   for (j in 1:length(sv)) {
     support_vector <- spam[sv[j], -58]
-    kernel_value <- gaussian_kernel(sum((support_vector - data_point)^2), sigma = 0.05)
+    kernel_value <- rbf_kernel(support_vector, data_point)
     k2 <- k2 + co[j] * kernel_value
   }
   print(k2)
-- 
GitLab