diff --git a/lab3/assignment2.R b/lab3/assignment2.R index f71fbf8775cc51c21d89677b5d00fb4459015771..0f0353db865508bce261626f6f9d3cc1d67b78ca 100644 --- a/lab3/assignment2.R +++ b/lab3/assignment2.R @@ -28,6 +28,7 @@ times <- c( "24:00:00" ) +# Convert time strings to POSIXlt st$time <- strptime(st$time, format = "%H:%M:%S") gaussian_kernel <- function(x, h) { @@ -80,9 +81,7 @@ date_diff_ignoring_year <- function(date1, date2) { diff <- abs(as.numeric(date1-date2)) # If difference is greater than half a year, go other way - if (diff >= 183) { - diff <- 366 - diff - } + if (diff >= 183) diff <- 366 - diff return(diff) } @@ -106,14 +105,14 @@ for (time in times) { }, st_temp$latitude, st_temp$longitude) - date_kernels <- mapply(function(x_i) { - dist <- date_diff_ignoring_year(date, x_i) + date_kernels <- mapply(function(date_i) { + dist <- date_diff_ignoring_year(date, date_i) gaussian_kernel(dist, h_date) }, st_temp$date) - time_kernels <- mapply(function(x_i) { - dist <- as.numeric(difftime(time, x_i, units = "hours")) + time_kernels <- mapply(function(time_i) { + dist <- as.numeric(difftime(time, time_i, units = "hours")) gaussian_kernel(dist, h_time) }, st_temp$time) diff --git a/lab3/assignment3.R b/lab3/assignment3.R index 18d9602eba53ae4b4581b67b14fbc747a7e07106..bc09c532276d4e7448968ebe442e2ef89ce48b6d 100644 --- a/lab3/assignment3.R +++ b/lab3/assignment3.R @@ -98,16 +98,14 @@ k <- NULL for (i in 1:10) { # We produce predictions for just the first 10 points in the dataset. k2 <- inte - data_point <- as.numeric(spam[i, -58]) + new_data_point <- as.numeric(spam[i, -58]) for (j in 1:length(sv)) { support_vector <- as.numeric(spam[sv[j], -58]) - kernel_value <- rbf_kernel(support_vector, data_point) + kernel_value <- rbf_kernel(support_vector, new_data_point) k2 <- k2 + co[j] * kernel_value } print(k2) k <- c(k, sign(k2)) } - -# Only first correct, close to decision boundary (0.006292512). k predict(filter3, spam[1:10, -58], type = "decision") \ No newline at end of file