You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
898 B

3 years ago
  1. library(geometry)
  2. perceptron.load.data <- function(filename) {
  3. lines <- sapply(readLines(filename), function(x) strsplit(x, " "))
  4. data.points <- list()
  5. data.labels <- c()
  6. for(i in 1:(length(lines))) {
  7. data.points[[i]] <- as.double(lines[[i]][1:3])
  8. data.labels[i] <- as.integer(lines[[i]][4])
  9. }
  10. data <- list(data.points, data.labels)
  11. names(data) <- c("point", "label")
  12. return(data)
  13. }
  14. perceptron.learn <- function(data.points, data.labels, weight) {
  15. repeat {
  16. L <- 0
  17. for(i in 1:length(data.labels)) {
  18. delta <- data.labels[i] - sign(dot(data.points[[i]], weight))
  19. if(delta != 0) {
  20. L <- L + 1
  21. weight <- weight + delta * data.points[[i]]
  22. }
  23. }
  24. if(L == 0) {
  25. break
  26. }
  27. }
  28. return(function(x) sign(dot(x, weight)))
  29. }