From 3e67bb6862450d2c1acd4bccccaab9200b38791d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Ramnel=C3=B6v?= <felra653@student.liu.se> Date: Wed, 11 Dec 2024 08:10:35 +0100 Subject: [PATCH] Lab 3: Updated distance computations for assignment 3 --- lab3/assignment2.R | 22 +++++++++++++--------- lab3/assignment3.R | 5 ++--- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lab3/assignment2.R b/lab3/assignment2.R index b0e655d..259d892 100644 --- a/lab3/assignment2.R +++ b/lab3/assignment2.R @@ -29,39 +29,41 @@ times <- c( ) -st <- st[st$date <= date, ] # Filter out posterior dates st$time <- strptime(st$time, format = "%H:%M:%S") gaussian_kernel <- function(x, h) { exp(-(x ^ 2) / (2 * h ^ 2)) } -dist = seq(0, 300000, length.out = 1000) +x = seq(0, 300000, length.out = 1000) plot( - gaussian_kernel(dist, h_distance), + gaussian_kernel(x, h_distance), type = 'l', xlab = "Physical distance", ylab = "Kernel value", main = "Gaussian distance kernel" ) +grid() -dist = seq(0, 60) +x = seq(0, 60) plot( - gaussian_kernel(dist, h_date), + gaussian_kernel(x, h_date), type = 'l', xlab = "Distance in days", ylab = "Kernel value", main = "Gaussian date kernel" ) +grid() -dist = seq(0, 18) +x = seq(0, 18) plot( - gaussian_kernel(dist, h_time), + gaussian_kernel(x, h_time), type = 'l', xlab = "Distance in hours", ylab = "Kernel value", main = "Gaussian time kernel" ) +grid() temp_add <- c() temp_mult <- c() @@ -70,7 +72,7 @@ for (time in times) { time <- strptime(time, format = "%H:%M:%S") - # Filter out posterior time + # Filter out posterior dates and time st_temp <- st[st$date < date | (st$date == date & st$time <= time), ] @@ -117,6 +119,7 @@ axis( at = 1:length(times), labels = times ) +grid() plot( temp_mult, @@ -130,4 +133,5 @@ axis( 1, at = 1:length(times), labels = times -) \ No newline at end of file +) +grid() \ No newline at end of file diff --git a/lab3/assignment3.R b/lab3/assignment3.R index 53af0fe..47b8abc 100644 --- a/lab3/assignment3.R +++ b/lab3/assignment3.R @@ -90,10 +90,9 @@ err3 # 3. Implementation of SVM predictions. gaussian_kernel <- function(x_i, x_star, sigma) { - return(exp(-sum((x_i - x_star)^2) / (2 * sigma^2))) + exp(-(dist(rbind(x_i, x_star)) ^ 2) / (2 * sigma ^ 2)) } - sv <- alphaindex(filter3)[[1]] co <- coef(filter3)[[1]] inte <- -b(filter3) @@ -119,4 +118,4 @@ for (i in 1:10) { # Only first correct, close to decision boundary (0.006292512). k -predict(filter3,spam[1:10,-58], type = "decision") \ No newline at end of file +predict(filter3, spam[1:10, -58], type = "decision") \ No newline at end of file -- GitLab