Advertisement
marichan022

r nesto

Jan 23rd, 2022
860
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. rm(list=ls())
  2.  
  3. library(e1071)
  4. library(caTools)
  5. library(ggplot2)
  6. library(caret)
  7.  
  8.  
  9. setwd('C:/Users/mbano/Documents/Faks/Usluge zasnovane na lokaciji/r-svm-classification')
  10. data = read.csv(paste('./datasets/clean datasets/svm_data.csv', sep = ''), head = TRUE, sep = ',', colClasses = 'character', skip = 0)
  11.  
  12. print(head(data, 5))
  13. plot_data = data
  14. data = plot_data[c('x', 'y', 'z', 'class')]
  15. print(head(data, 5))
  16. data$x = as.numeric(data$x)
  17. data$y = as.numeric(data$y)
  18. data$z = as.numeric(data$z)
  19. # data$t = as.numeric(as.POSIXct(data$t))
  20. data$class = factor(data$class, levels = c(0, 1, 2))
  21. set.seed(123)
  22. split = sample.split(data$class, SplitRatio = 0.75)
  23. training_set = subset(data, split == TRUE)
  24. test_set = subset(data, split == FALSE)
  25.  
  26. classifier = svm(formula = class ~ .,
  27.                  data = training_set,
  28.                  type = 'C-classification',
  29.                  kernel = 'linear',
  30.                  cross = 10)
  31.  
  32. print(head(test_set[-4]), 5)
  33. y_pred = predict(classifier, newdata = test_set[-4])
  34. y_train_pred = predict(classifier, newdata = training_set[-4])
  35.  
  36. cm = table(test_set[, 4], y_pred)
  37. cm2 = table(training_set[, 4], y_train_pred )
  38.  
  39. print(cm)
  40. print(cm2)
  41.  
  42. #plot only if there are two columns for classification
  43. # plot(classifier, training_set)
  44.  
  45. #plot two by two variables
  46. plot(classifier, training_set, x ~ y)
  47. plot(classifier, training_set, y ~ z)
  48. plot(classifier, training_set, x ~ z)
  49.  
  50. cv_accuracies <- classifier$accuracies
  51. all.equal(mean(cv_accuracies), classifier$tot.accuracy)
  52. print(mean(cv_accuracies))
  53.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement