Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.87 KB | None | 0 0
  1. ---
  2. title: "Homework 3: Regression Theory"
  3. output: html_notebook
  4. author: JPeruggia
  5. date: '03/25/2018'
  6. ---
  7.  
  8. ** Probelms
  9.  
  10. **** 1. Data Preprocessing
  11.  
  12. ```{r}
  13.  
  14. training_data <- read.csv('mnist_train.csv', header=FALSE)
  15. test_data <- read.csv('mnist_test.csv', header=FALSE)
  16. training_df <- as.data.frame(t(training_data))
  17. test_df <- as.data.frame(t(test_data))
  18.  
  19. # set the name of the column for classifcation value.
  20. names(training_df)[785] <- "Classification"
  21. names(test_df)[785] <- "Classification"
  22.  
  23. # partition the data.
  24. train_0_1 <- training_df[(training_df$Classification == 0) | (training_df$Classification == 1),]
  25. train_3_5 <- training_df[(training_df$Classification == 3) | (training_df$Classification == 5), ]
  26. test_0_1 <- test_df[(test_df$Classification == 0) | (test_df$Classification == 1),]
  27. test_3_5 <- test_df[(test_df$Classification == 3) | (test_df$Classification == 5), ]
  28.  
  29. #Dimensions of the Training set with value of 0 or 1
  30. print (dim(train_0_1))
  31. #Dimensions of the Training set with values of 3 or 5
  32. print (dim(train_3_5))
  33. #Dimensions of the Testing set with the values of 0 or 1
  34. print (dim(test_0_1))
  35. #Dimensions of the Testing set with the values of 3 or 5
  36. print (dim(test_3_5))
  37.  
  38. # seperate the true class lables out.
  39. train_labels_0_1 <- train_0_1$Classification
  40. train_labels_3_5 <- traintrain_3_5_35_df$Classification
  41. test_labels_0_1 <- test_0_1$Classification
  42. test_labels_3_5 <- test_3_5$Classification
  43. # remove them from the original dfs.
  44. train_data_0_1 <- subset(train_0_1, select = names(train_0_1) != "Classification")
  45. train_data_3_5 <- subset(train_3_5, select = names(train_3_5) != "Classification")
  46. test_data_0_1 <- subset(test_0_1, select = names(test_0_1) != "Classification")
  47. test_data_3_5 <- subset(test_3_5, select = names(test_3_5) != "Classification")
  48.  
  49. # Visualize one image from each class to make sure you have read it in correctly.
  50.  
  51.  
  52. m <- matrix(
  53.   unlist(train_data_0_1[!is.na(match(train_labels_0_1, 0)), ][1,]),
  54.   byrow = TRUE,
  55.   nrow = 28,
  56.   ncol = 28
  57. )
  58. t_image = image(z = m, col=gray(0:255/255))
  59. print(t_image)
  60.  
  61.  
  62. #image_for_digit(train_01_df,train_01_labels, 0, "Classification of: 0")
  63. #image_for_digit(train_01_df, train_01_labels, 1, "Classification of: 1")
  64. #image_for_digit(train_35_df, train_35_labels, 3, "Classification of: 3")
  65. #image_for_digit(train_35_df, train_35_labels, 5, "Classification of: 5")
  66.  
  67.  
  68. ```
  69.  
  70. Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Cmd+Option+I*.
  71.  
  72. When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Cmd+Shift+K* to preview the HTML file).
  73.  
  74. The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement