Skip to content
Snippets Groups Projects
Commit 750d287b authored by Felix Ramnelöv's avatar Felix Ramnelöv
Browse files

Lab 3: Code cleanup

parent 55b6bcd0
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment