Advertisement
HolyC0w

lab11

Apr 11th, 2023
1,600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.55 KB | None | 0 0
  1. # C:\Users\subhr\My Drive\College\Sem6\G ECE3502-IOT\Lab\lab11\Advertisement.csv
  2. library(datasets)
  3. library(caTools)
  4. library(party)
  5. library(dplyr)
  6. library(magrittr)
  7.  
  8.  
  9. path_to_file<-"C:\\Users\\subhr\\My Drive\\College\\Sem6\\G ECE3502-IOT\\Lab\\lab11\\Advertisement.csv"
  10. data=read.csv(path_to_file)
  11. data=data[,-1]
  12. data$Gender[data$Gender=="Male"]=as.integer(1)
  13. data$Gender[data$Gender=="Female"]=as.integer(0)
  14. data$Gender=as.integer(data$Gender)
  15. head(data)
  16. summary(data)
  17.  
  18.  
  19. sample_data = sample.split(data, SplitRatio = 0.6)
  20.  
  21. train_data <- subset(data, sample_data == TRUE)
  22.  
  23. test_data <- subset(data, sample_data == FALSE)
  24.  
  25. model<- ctree(Purchased ~ ., train_data)
  26. plot(model)
  27.  
  28.  
  29. # testing the people who are native speakers
  30. # and those who are not
  31.  
  32. #predict_model<-predict(ctree, test_data)
  33. predit_model<-predict(model,test_data)
  34. # creates a table to count how many are classified
  35. # as native speakers and how many are not
  36.  
  37. m_at <- table(test_data$Purchased, predit_model)
  38. m_at
  39.  
  40. ac_Test = sum(diag(m_at)) / sum(m_at)
  41.  
  42. print(paste('Accuracy for test is found to be', ac_Test))
  43.  
  44. library(rpart)
  45. set.seed(1234)
  46. train <- sample(nrow(data), 0.7 * nrow(data))
  47. data_train <- data[train, ]
  48. data_test <- data[-train, ]
  49. model <- rpart(Purchased ~ ., data = data_train, method = "class")
  50. library(rpart.plot)
  51. rpart.plot(model)
  52. predictions <- predict(model, data_test, type = "class")
  53. m_at=table(data_test$Purchased, predictions)
  54.  
  55. m_at
  56. ac_Test = sum(diag(m_at)) / sum(m_at)
  57. print(paste('Accuracy for test is found to be', ac_Test))
  58.  
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement