#install.packages("keras")
library(keras)
library(ggplot2)
library(grid)
library(gridExtra)
trainpic <- read.csv("C://Users//malia//OneDrive//Desktop//data 605//train.csv")
testpic <- read.csv("C://Users//malia//OneDrive//Desktop//coop//test.csv")
m<-matrix(unlist(trainpic[11,-1]), nrow=28, byrow = T)
image(m, col=grey.colors(255))

digit<-function(x){
m<-matrix(unlist(x), nrow=28, byrow=T)
m<-t(apply(m, 2, rev))
image(m, col=grey.colors(255))
}
par(mfrow=c(3,4))
for(i in 1:10){
digit(trainpic[i, -1])
}

Cleaning Data:
# Transform data to be on a 0 - 1 scale
trainpic[,-1] <- (trainpic[,-1]/255)
Build Histogram
trainpic$label <- as.factor(trainpic$label)
trainpic$intensity <- apply(trainpic[,-1], 1, mean) #takes the mean of each row in train
intbylabel <- aggregate (trainpic$intensity, by = list(trainpic$label), FUN = mean)
plot <- ggplot(data=intbylabel, aes(x=Group.1, y = x)) +
geom_histogram(stat="identity")
## Warning: Ignoring unknown parameters: binwidth, bins, pad
plot + scale_x_discrete(limits=0:9) + xlab("digit label") +
ylab("average intensity")
## Warning: Continuous limits supplied to discrete scale.
## Did you mean `limits = factor(...)` or `scale_*_continuous()`?

As we can see there are some differences in intensity. The digit “1” is the less intense while the digit “0” is the most intense. So this new feature seems to have some predictive value.
# Get the principal components from PCA
pca_output <- prcomp(trainpic[, -1])
# Observe a summary of the output
summary(pca_output)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6 PC7
## Standard deviation 2.26931 1.9446 1.80159 1.68549 1.60777 1.50751 1.31557
## Proportion of Variance 0.09751 0.0716 0.06146 0.05379 0.04895 0.04303 0.03277
## Cumulative Proportion 0.09751 0.1691 0.23057 0.28436 0.33330 0.37634 0.40911
## PC8 PC9 PC10 PC11 PC12 PC13 PC14
## Standard deviation 1.23586 1.20885 1.11376 1.05295 1.04278 0.94823 0.94550
## Proportion of Variance 0.02892 0.02767 0.02349 0.02099 0.02059 0.01702 0.01693
## Cumulative Proportion 0.43803 0.46570 0.48918 0.51018 0.53077 0.54779 0.56472
## PC15 PC16 PC17 PC18 PC19 PC20 PC21
## Standard deviation 0.91379 0.88505 0.8348 0.82306 0.79208 0.78024 0.75248
## Proportion of Variance 0.01581 0.01483 0.0132 0.01283 0.01188 0.01153 0.01072
## Cumulative Proportion 0.58053 0.59536 0.6086 0.62139 0.63326 0.64479 0.65551
## PC22 PC23 PC24 PC25 PC26 PC27 PC28
## Standard deviation 0.73221 0.71384 0.69432 0.68467 0.66555 0.65479 0.64075
## Proportion of Variance 0.01015 0.00965 0.00913 0.00888 0.00839 0.00812 0.00777
## Cumulative Proportion 0.66567 0.67531 0.68444 0.69332 0.70171 0.70982 0.71760
## PC29 PC30 PC31 PC32 PC33 PC34 PC35
## Standard deviation 0.62541 0.60219 0.58948 0.58082 0.56261 0.55768 0.54592
## Proportion of Variance 0.00741 0.00687 0.00658 0.00639 0.00599 0.00589 0.00564
## Cumulative Proportion 0.72500 0.73187 0.73845 0.74484 0.75083 0.75672 0.76236
## PC36 PC37 PC38 PC39 PC40 PC41 PC42
## Standard deviation 0.53450 0.51858 0.50740 0.50115 0.49637 0.48909 0.48477
## Proportion of Variance 0.00541 0.00509 0.00487 0.00476 0.00467 0.00453 0.00445
## Cumulative Proportion 0.76777 0.77286 0.77774 0.78250 0.78716 0.79169 0.79614
## PC43 PC44 PC45 PC46 PC47 PC48 PC49
## Standard deviation 0.46998 0.45818 0.45064 0.44497 0.43664 0.42902 0.42155
## Proportion of Variance 0.00418 0.00397 0.00385 0.00375 0.00361 0.00349 0.00336
## Cumulative Proportion 0.80032 0.80430 0.80814 0.81189 0.81550 0.81899 0.82235
## PC50 PC51 PC52 PC53 PC54 PC55 PC56
## Standard deviation 0.41156 0.40817 0.40406 0.39384 0.38901 0.38506 0.3773
## Proportion of Variance 0.00321 0.00315 0.00309 0.00294 0.00287 0.00281 0.0027
## Cumulative Proportion 0.82556 0.82871 0.83180 0.83474 0.83761 0.84041 0.8431
## PC57 PC58 PC59 PC60 PC61 PC62 PC63
## Standard deviation 0.37468 0.36790 0.36612 0.36057 0.3558 0.35508 0.34669
## Proportion of Variance 0.00266 0.00256 0.00254 0.00246 0.0024 0.00239 0.00228
## Cumulative Proportion 0.84577 0.84833 0.85087 0.85333 0.8557 0.85812 0.86039
## PC64 PC65 PC66 PC67 PC68 PC69 PC70
## Standard deviation 0.34203 0.33613 0.32994 0.32730 0.32171 0.31978 0.31550
## Proportion of Variance 0.00222 0.00214 0.00206 0.00203 0.00196 0.00194 0.00188
## Cumulative Proportion 0.86261 0.86475 0.86681 0.86884 0.87080 0.87273 0.87462
## PC71 PC72 PC73 PC74 PC75 PC76 PC77
## Standard deviation 0.31405 0.30975 0.30564 0.30191 0.29619 0.29368 0.29123
## Proportion of Variance 0.00187 0.00182 0.00177 0.00173 0.00166 0.00163 0.00161
## Cumulative Proportion 0.87648 0.87830 0.88007 0.88179 0.88346 0.88509 0.88670
## PC78 PC79 PC80 PC81 PC82 PC83 PC84
## Standard deviation 0.28562 0.27848 0.27421 0.27297 0.2721 0.27078 0.26742
## Proportion of Variance 0.00154 0.00147 0.00142 0.00141 0.0014 0.00139 0.00135
## Cumulative Proportion 0.88824 0.88971 0.89113 0.89254 0.8940 0.89533 0.89669
## PC85 PC86 PC87 PC88 PC89 PC90 PC91
## Standard deviation 0.26433 0.26280 0.2617 0.25615 0.25409 0.2513 0.24734
## Proportion of Variance 0.00132 0.00131 0.0013 0.00124 0.00122 0.0012 0.00116
## Cumulative Proportion 0.89801 0.89932 0.9006 0.90186 0.90308 0.9043 0.90543
## PC92 PC93 PC94 PC95 PC96 PC97 PC98
## Standard deviation 0.24521 0.24349 0.2415 0.23897 0.23817 0.23421 0.23359
## Proportion of Variance 0.00114 0.00112 0.0011 0.00108 0.00107 0.00104 0.00103
## Cumulative Proportion 0.90657 0.90770 0.9088 0.90988 0.91096 0.91199 0.91303
## PC99 PC100 PC101 PC102 PC103 PC104 PC105
## Standard deviation 0.23152 0.2298 0.22689 0.22340 0.22264 0.21949 0.21890
## Proportion of Variance 0.00101 0.0010 0.00097 0.00095 0.00094 0.00091 0.00091
## Cumulative Proportion 0.91404 0.9150 0.91602 0.91696 0.91790 0.91881 0.91972
## PC106 PC107 PC108 PC109 PC110 PC111 PC112
## Standard deviation 0.21666 0.21357 0.21115 0.21006 0.20767 0.20396 0.20316
## Proportion of Variance 0.00089 0.00086 0.00084 0.00084 0.00082 0.00079 0.00078
## Cumulative Proportion 0.92061 0.92147 0.92232 0.92315 0.92397 0.92476 0.92554
## PC113 PC114 PC115 PC116 PC117 PC118 PC119
## Standard deviation 0.20263 0.20191 0.20006 0.19905 0.19695 0.19578 0.19436
## Proportion of Variance 0.00078 0.00077 0.00076 0.00075 0.00073 0.00073 0.00072
## Cumulative Proportion 0.92632 0.92709 0.92785 0.92860 0.92933 0.93006 0.93077
## PC120 PC121 PC122 PC123 PC124 PC125 PC126
## Standard deviation 0.1923 0.19131 0.19030 0.18949 0.18750 0.18615 0.18453
## Proportion of Variance 0.0007 0.00069 0.00069 0.00068 0.00067 0.00066 0.00064
## Cumulative Proportion 0.9315 0.93216 0.93285 0.93353 0.93420 0.93485 0.93550
## PC127 PC128 PC129 PC130 PC131 PC132 PC133
## Standard deviation 0.18318 0.18184 0.18073 0.17886 0.1786 0.17673 0.17590
## Proportion of Variance 0.00064 0.00063 0.00062 0.00061 0.0006 0.00059 0.00059
## Cumulative Proportion 0.93613 0.93676 0.93738 0.93798 0.9386 0.93918 0.93976
## PC134 PC135 PC136 PC137 PC138 PC139 PC140
## Standard deviation 0.17571 0.17433 0.17346 0.17266 0.17092 0.16799 0.16663
## Proportion of Variance 0.00058 0.00058 0.00057 0.00056 0.00055 0.00053 0.00053
## Cumulative Proportion 0.94035 0.94092 0.94149 0.94206 0.94261 0.94315 0.94367
## PC141 PC142 PC143 PC144 PC145 PC146 PC147
## Standard deviation 0.16603 0.16431 0.16333 0.1625 0.1617 0.16125 0.15994
## Proportion of Variance 0.00052 0.00051 0.00051 0.0005 0.0005 0.00049 0.00048
## Cumulative Proportion 0.94419 0.94470 0.94521 0.9457 0.9462 0.94670 0.94718
## PC148 PC149 PC150 PC151 PC152 PC153 PC154
## Standard deviation 0.15866 0.15833 0.15719 0.15676 0.15609 0.15524 0.15446
## Proportion of Variance 0.00048 0.00047 0.00047 0.00047 0.00046 0.00046 0.00045
## Cumulative Proportion 0.94766 0.94813 0.94860 0.94907 0.94953 0.94998 0.95043
## PC155 PC156 PC157 PC158 PC159 PC160 PC161
## Standard deviation 0.15407 0.15267 0.15223 0.14971 0.14899 0.14828 0.14783
## Proportion of Variance 0.00045 0.00044 0.00044 0.00042 0.00042 0.00042 0.00041
## Cumulative Proportion 0.95088 0.95133 0.95176 0.95219 0.95261 0.95303 0.95344
## PC162 PC163 PC164 PC165 PC166 PC167 PC168
## Standard deviation 0.14663 0.1451 0.14431 0.14426 0.14377 0.14197 0.14152
## Proportion of Variance 0.00041 0.0004 0.00039 0.00039 0.00039 0.00038 0.00038
## Cumulative Proportion 0.95385 0.9543 0.95464 0.95503 0.95542 0.95581 0.95619
## PC169 PC170 PC171 PC172 PC173 PC174 PC175
## Standard deviation 0.14083 0.14051 0.13909 0.13847 0.13775 0.13731 0.13654
## Proportion of Variance 0.00038 0.00037 0.00037 0.00036 0.00036 0.00036 0.00035
## Cumulative Proportion 0.95656 0.95694 0.95730 0.95766 0.95802 0.95838 0.95873
## PC176 PC177 PC178 PC179 PC180 PC181 PC182
## Standard deviation 0.13649 0.13504 0.13464 0.13437 0.13383 0.13298 0.13198
## Proportion of Variance 0.00035 0.00035 0.00034 0.00034 0.00034 0.00033 0.00033
## Cumulative Proportion 0.95909 0.95943 0.95978 0.96012 0.96046 0.96079 0.96112
## PC183 PC184 PC185 PC186 PC187 PC188 PC189
## Standard deviation 0.13111 0.13095 0.13020 0.13009 0.12943 0.12935 0.12814
## Proportion of Variance 0.00033 0.00032 0.00032 0.00032 0.00032 0.00032 0.00031
## Cumulative Proportion 0.96145 0.96177 0.96209 0.96241 0.96273 0.96305 0.96336
## PC190 PC191 PC192 PC193 PC194 PC195 PC196
## Standard deviation 0.12799 0.12730 0.1267 0.1258 0.1255 0.12462 0.12448
## Proportion of Variance 0.00031 0.00031 0.0003 0.0003 0.0003 0.00029 0.00029
## Cumulative Proportion 0.96367 0.96397 0.9643 0.9646 0.9649 0.96517 0.96546
## PC197 PC198 PC199 PC200 PC201 PC202 PC203
## Standard deviation 0.12439 0.12360 0.12268 0.12240 0.12184 0.12079 0.12031
## Proportion of Variance 0.00029 0.00029 0.00028 0.00028 0.00028 0.00028 0.00027
## Cumulative Proportion 0.96576 0.96605 0.96633 0.96661 0.96690 0.96717 0.96745
## PC204 PC205 PC206 PC207 PC208 PC209 PC210
## Standard deviation 0.11976 0.11886 0.11870 0.11783 0.11764 0.11751 0.11678
## Proportion of Variance 0.00027 0.00027 0.00027 0.00026 0.00026 0.00026 0.00026
## Cumulative Proportion 0.96772 0.96798 0.96825 0.96851 0.96878 0.96904 0.96930
## PC211 PC212 PC213 PC214 PC215 PC216 PC217
## Standard deviation 0.11641 0.11613 0.11584 0.11555 0.11511 0.11451 0.11435
## Proportion of Variance 0.00026 0.00026 0.00025 0.00025 0.00025 0.00025 0.00025
## Cumulative Proportion 0.96955 0.96981 0.97006 0.97032 0.97057 0.97081 0.97106
## PC218 PC219 PC220 PC221 PC222 PC223 PC224
## Standard deviation 0.11362 0.11335 0.11281 0.11265 0.11243 0.11225 0.11175
## Proportion of Variance 0.00024 0.00024 0.00024 0.00024 0.00024 0.00024 0.00024
## Cumulative Proportion 0.97131 0.97155 0.97179 0.97203 0.97227 0.97251 0.97275
## PC225 PC226 PC227 PC228 PC229 PC230 PC231
## Standard deviation 0.11070 0.11045 0.10997 0.10956 0.10924 0.10895 0.10803
## Proportion of Variance 0.00023 0.00023 0.00023 0.00023 0.00023 0.00022 0.00022
## Cumulative Proportion 0.97298 0.97321 0.97344 0.97366 0.97389 0.97412 0.97434
## PC232 PC233 PC234 PC235 PC236 PC237 PC238
## Standard deviation 0.10765 0.10715 0.10679 0.10654 0.10621 0.10568 0.10553
## Proportion of Variance 0.00022 0.00022 0.00022 0.00021 0.00021 0.00021 0.00021
## Cumulative Proportion 0.97456 0.97477 0.97499 0.97520 0.97542 0.97563 0.97584
## PC239 PC240 PC241 PC242 PC243 PC244 PC245
## Standard deviation 0.10484 0.10409 0.1037 0.1036 0.1032 0.1027 0.1023
## Proportion of Variance 0.00021 0.00021 0.0002 0.0002 0.0002 0.0002 0.0002
## Cumulative Proportion 0.97605 0.97625 0.9765 0.9767 0.9769 0.9771 0.9773
## PC246 PC247 PC248 PC249 PC250 PC251 PC252
## Standard deviation 0.1019 0.10143 0.10115 0.10087 0.10073 0.10024 0.09974
## Proportion of Variance 0.0002 0.00019 0.00019 0.00019 0.00019 0.00019 0.00019
## Cumulative Proportion 0.9775 0.97765 0.97785 0.97804 0.97823 0.97842 0.97861
## PC253 PC254 PC255 PC256 PC257 PC258 PC259
## Standard deviation 0.09933 0.09884 0.09876 0.09860 0.09825 0.09813 0.09787
## Proportion of Variance 0.00019 0.00018 0.00018 0.00018 0.00018 0.00018 0.00018
## Cumulative Proportion 0.97880 0.97898 0.97916 0.97935 0.97953 0.97971 0.97990
## PC260 PC261 PC262 PC263 PC264 PC265 PC266
## Standard deviation 0.09739 0.09660 0.09630 0.09610 0.09572 0.09545 0.09516
## Proportion of Variance 0.00018 0.00018 0.00018 0.00017 0.00017 0.00017 0.00017
## Cumulative Proportion 0.98008 0.98025 0.98043 0.98060 0.98078 0.98095 0.98112
## PC267 PC268 PC269 PC270 PC271 PC272 PC273
## Standard deviation 0.09507 0.09441 0.09423 0.09411 0.09371 0.09315 0.09304
## Proportion of Variance 0.00017 0.00017 0.00017 0.00017 0.00017 0.00016 0.00016
## Cumulative Proportion 0.98129 0.98146 0.98163 0.98180 0.98196 0.98213 0.98229
## PC274 PC275 PC276 PC277 PC278 PC279 PC280
## Standard deviation 0.09279 0.09257 0.09205 0.09200 0.09159 0.09146 0.09113
## Proportion of Variance 0.00016 0.00016 0.00016 0.00016 0.00016 0.00016 0.00016
## Cumulative Proportion 0.98245 0.98262 0.98278 0.98294 0.98309 0.98325 0.98341
## PC281 PC282 PC283 PC284 PC285 PC286 PC287
## Standard deviation 0.09060 0.09031 0.09007 0.08955 0.08926 0.08912 0.08872
## Proportion of Variance 0.00016 0.00015 0.00015 0.00015 0.00015 0.00015 0.00015
## Cumulative Proportion 0.98357 0.98372 0.98387 0.98403 0.98418 0.98433 0.98448
## PC288 PC289 PC290 PC291 PC292 PC293 PC294
## Standard deviation 0.08849 0.08802 0.08778 0.08704 0.08703 0.08689 0.08656
## Proportion of Variance 0.00015 0.00015 0.00015 0.00014 0.00014 0.00014 0.00014
## Cumulative Proportion 0.98462 0.98477 0.98492 0.98506 0.98520 0.98535 0.98549
## PC295 PC296 PC297 PC298 PC299 PC300 PC301
## Standard deviation 0.08642 0.08608 0.08548 0.08520 0.08487 0.08458 0.08449
## Proportion of Variance 0.00014 0.00014 0.00014 0.00014 0.00014 0.00014 0.00014
## Cumulative Proportion 0.98563 0.98577 0.98591 0.98605 0.98618 0.98632 0.98645
## PC302 PC303 PC304 PC305 PC306 PC307 PC308
## Standard deviation 0.08425 0.08393 0.08358 0.08310 0.08282 0.08239 0.08217
## Proportion of Variance 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013
## Cumulative Proportion 0.98659 0.98672 0.98685 0.98698 0.98711 0.98724 0.98737
## PC309 PC310 PC311 PC312 PC313 PC314 PC315
## Standard deviation 0.08189 0.08177 0.08154 0.08130 0.08093 0.08045 0.08037
## Proportion of Variance 0.00013 0.00013 0.00013 0.00013 0.00012 0.00012 0.00012
## Cumulative Proportion 0.98750 0.98762 0.98775 0.98787 0.98800 0.98812 0.98824
## PC316 PC317 PC318 PC319 PC320 PC321 PC322
## Standard deviation 0.08000 0.07989 0.07963 0.07928 0.07902 0.07868 0.07818
## Proportion of Variance 0.00012 0.00012 0.00012 0.00012 0.00012 0.00012 0.00012
## Cumulative Proportion 0.98836 0.98849 0.98861 0.98872 0.98884 0.98896 0.98908
## PC323 PC324 PC325 PC326 PC327 PC328 PC329
## Standard deviation 0.07805 0.07778 0.07746 0.07731 0.07682 0.07632 0.07620
## Proportion of Variance 0.00012 0.00011 0.00011 0.00011 0.00011 0.00011 0.00011
## Cumulative Proportion 0.98919 0.98931 0.98942 0.98953 0.98964 0.98975 0.98986
## PC330 PC331 PC332 PC333 PC334 PC335 PC336
## Standard deviation 0.07599 0.07593 0.07569 0.07545 0.07514 0.07507 0.07485
## Proportion of Variance 0.00011 0.00011 0.00011 0.00011 0.00011 0.00011 0.00011
## Cumulative Proportion 0.98997 0.99008 0.99019 0.99030 0.99041 0.99051 0.99062
## PC337 PC338 PC339 PC340 PC341 PC342 PC343
## Standard deviation 0.07428 0.07425 0.07378 0.07347 0.07317 0.07289 0.07259
## Proportion of Variance 0.00010 0.00010 0.00010 0.00010 0.00010 0.00010 0.00010
## Cumulative Proportion 0.99072 0.99083 0.99093 0.99103 0.99113 0.99124 0.99133
## PC344 PC345 PC346 PC347 PC348 PC349 PC350
## Standard deviation 0.07247 0.07215 0.07158 0.07156 0.07131 0.07069 0.07051
## Proportion of Variance 0.00010 0.00010 0.00010 0.00010 0.00010 0.00009 0.00009
## Cumulative Proportion 0.99143 0.99153 0.99163 0.99173 0.99182 0.99192 0.99201
## PC351 PC352 PC353 PC354 PC355 PC356 PC357
## Standard deviation 0.07010 0.06992 0.06954 0.06935 0.06913 0.06895 0.06879
## Proportion of Variance 0.00009 0.00009 0.00009 0.00009 0.00009 0.00009 0.00009
## Cumulative Proportion 0.99211 0.99220 0.99229 0.99238 0.99247 0.99256 0.99265
## PC358 PC359 PC360 PC361 PC362 PC363 PC364
## Standard deviation 0.06855 0.06830 0.06799 0.06783 0.06735 0.06702 0.06702
## Proportion of Variance 0.00009 0.00009 0.00009 0.00009 0.00009 0.00009 0.00009
## Cumulative Proportion 0.99274 0.99283 0.99292 0.99300 0.99309 0.99317 0.99326
## PC365 PC366 PC367 PC368 PC369 PC370 PC371
## Standard deviation 0.06686 0.06654 0.06610 0.06601 0.06587 0.06535 0.06488
## Proportion of Variance 0.00008 0.00008 0.00008 0.00008 0.00008 0.00008 0.00008
## Cumulative Proportion 0.99334 0.99343 0.99351 0.99359 0.99367 0.99376 0.99383
## PC372 PC373 PC374 PC375 PC376 PC377 PC378
## Standard deviation 0.06459 0.06434 0.06420 0.06396 0.06360 0.06325 0.06291
## Proportion of Variance 0.00008 0.00008 0.00008 0.00008 0.00008 0.00008 0.00007
## Cumulative Proportion 0.99391 0.99399 0.99407 0.99415 0.99422 0.99430 0.99437
## PC379 PC380 PC381 PC382 PC383 PC384 PC385
## Standard deviation 0.06261 0.06216 0.06209 0.06190 0.06177 0.06156 0.06135
## Proportion of Variance 0.00007 0.00007 0.00007 0.00007 0.00007 0.00007 0.00007
## Cumulative Proportion 0.99445 0.99452 0.99460 0.99467 0.99474 0.99481 0.99488
## PC386 PC387 PC388 PC389 PC390 PC391 PC392
## Standard deviation 0.06060 0.06045 0.06017 0.05987 0.05949 0.05926 0.05897
## Proportion of Variance 0.00007 0.00007 0.00007 0.00007 0.00007 0.00007 0.00007
## Cumulative Proportion 0.99495 0.99502 0.99509 0.99516 0.99523 0.99529 0.99536
## PC393 PC394 PC395 PC396 PC397 PC398 PC399
## Standard deviation 0.05859 0.05808 0.05764 0.05753 0.05690 0.05665 0.05642
## Proportion of Variance 0.00007 0.00006 0.00006 0.00006 0.00006 0.00006 0.00006
## Cumulative Proportion 0.99542 0.99549 0.99555 0.99561 0.99567 0.99573 0.99579
## PC400 PC401 PC402 PC403 PC404 PC405 PC406
## Standard deviation 0.05605 0.05572 0.05547 0.05523 0.05511 0.05451 0.05443
## Proportion of Variance 0.00006 0.00006 0.00006 0.00006 0.00006 0.00006 0.00006
## Cumulative Proportion 0.99585 0.99591 0.99597 0.99603 0.99609 0.99614 0.99620
## PC407 PC408 PC409 PC410 PC411 PC412 PC413
## Standard deviation 0.05389 0.05363 0.05338 0.05332 0.05297 0.05279 0.05250
## Proportion of Variance 0.00005 0.00005 0.00005 0.00005 0.00005 0.00005 0.00005
## Cumulative Proportion 0.99625 0.99631 0.99636 0.99642 0.99647 0.99652 0.99657
## PC414 PC415 PC416 PC417 PC418 PC419 PC420
## Standard deviation 0.05209 0.05203 0.05172 0.05143 0.05105 0.05101 0.05048
## Proportion of Variance 0.00005 0.00005 0.00005 0.00005 0.00005 0.00005 0.00005
## Cumulative Proportion 0.99663 0.99668 0.99673 0.99678 0.99683 0.99688 0.99692
## PC421 PC422 PC423 PC424 PC425 PC426 PC427
## Standard deviation 0.04993 0.04974 0.04935 0.04926 0.04906 0.04841 0.04799
## Proportion of Variance 0.00005 0.00005 0.00005 0.00005 0.00005 0.00004 0.00004
## Cumulative Proportion 0.99697 0.99702 0.99706 0.99711 0.99716 0.99720 0.99724
## PC428 PC429 PC430 PC431 PC432 PC433 PC434
## Standard deviation 0.04761 0.04743 0.04734 0.04700 0.04660 0.04625 0.04619
## Proportion of Variance 0.00004 0.00004 0.00004 0.00004 0.00004 0.00004 0.00004
## Cumulative Proportion 0.99729 0.99733 0.99737 0.99741 0.99745 0.99750 0.99754
## PC435 PC436 PC437 PC438 PC439 PC440 PC441
## Standard deviation 0.04602 0.04502 0.04475 0.04461 0.04449 0.04433 0.04390
## Proportion of Variance 0.00004 0.00004 0.00004 0.00004 0.00004 0.00004 0.00004
## Cumulative Proportion 0.99758 0.99761 0.99765 0.99769 0.99773 0.99776 0.99780
## PC442 PC443 PC444 PC445 PC446 PC447 PC448
## Standard deviation 0.04366 0.04318 0.04308 0.04285 0.04264 0.04239 0.04220
## Proportion of Variance 0.00004 0.00004 0.00004 0.00003 0.00003 0.00003 0.00003
## Cumulative Proportion 0.99784 0.99787 0.99791 0.99794 0.99798 0.99801 0.99804
## PC449 PC450 PC451 PC452 PC453 PC454 PC455
## Standard deviation 0.04188 0.04182 0.04120 0.04082 0.04065 0.04035 0.03991
## Proportion of Variance 0.00003 0.00003 0.00003 0.00003 0.00003 0.00003 0.00003
## Cumulative Proportion 0.99808 0.99811 0.99814 0.99817 0.99821 0.99824 0.99827
## PC456 PC457 PC458 PC459 PC460 PC461 PC462
## Standard deviation 0.03968 0.03944 0.03919 0.03906 0.03876 0.03863 0.03838
## Proportion of Variance 0.00003 0.00003 0.00003 0.00003 0.00003 0.00003 0.00003
## Cumulative Proportion 0.99830 0.99833 0.99835 0.99838 0.99841 0.99844 0.99847
## PC463 PC464 PC465 PC466 PC467 PC468 PC469
## Standard deviation 0.03769 0.03754 0.03679 0.03675 0.03663 0.03621 0.03614
## Proportion of Variance 0.00003 0.00003 0.00003 0.00003 0.00003 0.00002 0.00002
## Cumulative Proportion 0.99850 0.99852 0.99855 0.99857 0.99860 0.99862 0.99865
## PC470 PC471 PC472 PC473 PC474 PC475 PC476
## Standard deviation 0.03588 0.03578 0.03563 0.03532 0.03490 0.03461 0.03437
## Proportion of Variance 0.00002 0.00002 0.00002 0.00002 0.00002 0.00002 0.00002
## Cumulative Proportion 0.99867 0.99870 0.99872 0.99874 0.99877 0.99879 0.99881
## PC477 PC478 PC479 PC480 PC481 PC482 PC483
## Standard deviation 0.03391 0.03360 0.03347 0.03339 0.03313 0.03302 0.03275
## Proportion of Variance 0.00002 0.00002 0.00002 0.00002 0.00002 0.00002 0.00002
## Cumulative Proportion 0.99883 0.99886 0.99888 0.99890 0.99892 0.99894 0.99896
## PC484 PC485 PC486 PC487 PC488 PC489 PC490
## Standard deviation 0.03247 0.03232 0.03207 0.03160 0.03155 0.03131 0.03098
## Proportion of Variance 0.00002 0.00002 0.00002 0.00002 0.00002 0.00002 0.00002
## Cumulative Proportion 0.99898 0.99900 0.99902 0.99904 0.99906 0.99908 0.99909
## PC491 PC492 PC493 PC494 PC495 PC496 PC497
## Standard deviation 0.03073 0.03058 0.03034 0.03018 0.02917 0.02889 0.02879
## Proportion of Variance 0.00002 0.00002 0.00002 0.00002 0.00002 0.00002 0.00002
## Cumulative Proportion 0.99911 0.99913 0.99915 0.99916 0.99918 0.99920 0.99921
## PC498 PC499 PC500 PC501 PC502 PC503 PC504
## Standard deviation 0.02876 0.02842 0.02832 0.02815 0.02790 0.02763 0.02748
## Proportion of Variance 0.00002 0.00002 0.00002 0.00002 0.00001 0.00001 0.00001
## Cumulative Proportion 0.99923 0.99924 0.99926 0.99927 0.99929 0.99930 0.99932
## PC505 PC506 PC507 PC508 PC509 PC510 PC511
## Standard deviation 0.02739 0.02709 0.02705 0.02688 0.02661 0.02645 0.02637
## Proportion of Variance 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001
## Cumulative Proportion 0.99933 0.99934 0.99936 0.99937 0.99939 0.99940 0.99941
## PC512 PC513 PC514 PC515 PC516 PC517 PC518
## Standard deviation 0.02572 0.02552 0.02550 0.02542 0.02501 0.02481 0.02478
## Proportion of Variance 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001
## Cumulative Proportion 0.99942 0.99944 0.99945 0.99946 0.99947 0.99948 0.99950
## PC519 PC520 PC521 PC522 PC523 PC524 PC525
## Standard deviation 0.02474 0.02461 0.02418 0.02402 0.02372 0.02349 0.02341
## Proportion of Variance 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001
## Cumulative Proportion 0.99951 0.99952 0.99953 0.99954 0.99955 0.99956 0.99957
## PC526 PC527 PC528 PC529 PC530 PC531 PC532
## Standard deviation 0.02333 0.02282 0.02240 0.02216 0.02203 0.02188 0.02177
## Proportion of Variance 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001
## Cumulative Proportion 0.99958 0.99959 0.99960 0.99961 0.99962 0.99963 0.99964
## PC533 PC534 PC535 PC536 PC537 PC538 PC539
## Standard deviation 0.02170 0.02128 0.02120 0.02080 0.02056 0.02043 0.02019
## Proportion of Variance 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001
## Cumulative Proportion 0.99965 0.99966 0.99966 0.99967 0.99968 0.99969 0.99970
## PC540 PC541 PC542 PC543 PC544 PC545 PC546
## Standard deviation 0.02013 0.01983 0.01977 0.01960 0.01930 0.01914 0.01879
## Proportion of Variance 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001
## Cumulative Proportion 0.99970 0.99971 0.99972 0.99973 0.99973 0.99974 0.99975
## PC547 PC548 PC549 PC550 PC551 PC552 PC553
## Standard deviation 0.01871 0.01849 0.01844 0.01792 0.01784 0.01756 0.01729
## Proportion of Variance 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001
## Cumulative Proportion 0.99975 0.99976 0.99977 0.99977 0.99978 0.99978 0.99979
## PC554 PC555 PC556 PC557 PC558 PC559 PC560
## Standard deviation 0.01710 0.01695 0.01679 0.01658 0.01654 0.01642 0.01626
## Proportion of Variance 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001 0.00001
## Cumulative Proportion 0.99980 0.99980 0.99981 0.99981 0.99982 0.99982 0.99983
## PC561 PC562 PC563 PC564 PC565 PC566 PC567
## Standard deviation 0.01613 0.01592 0.01585 0.01548 0.01517 0.01504 0.01491
## Proportion of Variance 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
## Cumulative Proportion 0.99983 0.99984 0.99984 0.99985 0.99985 0.99985 0.99986
## PC568 PC569 PC570 PC571 PC572 PC573 PC574
## Standard deviation 0.01462 0.01453 0.01449 0.01423 0.01419 0.01413 0.01371
## Proportion of Variance 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
## Cumulative Proportion 0.99986 0.99987 0.99987 0.99987 0.99988 0.99988 0.99989
## PC575 PC576 PC577 PC578 PC579 PC580 PC581
## Standard deviation 0.01342 0.01335 0.01313 0.01284 0.01269 0.01267 0.01256
## Proportion of Variance 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
## Cumulative Proportion 0.99989 0.99989 0.99990 0.99990 0.99990 0.99991 0.99991
## PC582 PC583 PC584 PC585 PC586 PC587 PC588
## Standard deviation 0.01223 0.01196 0.01186 0.01176 0.01173 0.01159 0.01153
## Proportion of Variance 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
## Cumulative Proportion 0.99991 0.99991 0.99992 0.99992 0.99992 0.99992 0.99993
## PC589 PC590 PC591 PC592 PC593 PC594 PC595
## Standard deviation 0.01137 0.01118 0.01105 0.01094 0.01086 0.01077 0.01062
## Proportion of Variance 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
## Cumulative Proportion 0.99993 0.99993 0.99993 0.99994 0.99994 0.99994 0.99994
## PC596 PC597 PC598 PC599 PC600 PC601
## Standard deviation 0.01043 0.01015 0.009956 0.009703 0.009647 0.009634
## Proportion of Variance 0.00000 0.00000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion 0.99994 0.99995 0.999950 0.999950 0.999950 0.999950
## PC602 PC603 PC604 PC605 PC606 PC607
## Standard deviation 0.009344 0.009215 0.009167 0.009119 0.008841 0.008704
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion 0.999960 0.999960 0.999960 0.999960 0.999960 0.999960
## PC608 PC609 PC610 PC611 PC612 PC613
## Standard deviation 0.008576 0.008546 0.008229 0.008217 0.00809 0.007982
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.00000 0.000000
## Cumulative Proportion 0.999960 0.999970 0.999970 0.999970 0.99997 0.999970
## PC614 PC615 PC616 PC617 PC618 PC619
## Standard deviation 0.007899 0.007745 0.007562 0.007502 0.007282 0.007069
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion 0.999970 0.999970 0.999970 0.999980 0.999980 0.999980
## PC620 PC621 PC622 PC623 PC624 PC625
## Standard deviation 0.006978 0.006788 0.006746 0.006646 0.006611 0.006498
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion 0.999980 0.999980 0.999980 0.999980 0.999980 0.999980
## PC626 PC627 PC628 PC629 PC630 PC631
## Standard deviation 0.00643 0.00636 0.006158 0.006074 0.00606 0.006044
## Proportion of Variance 0.00000 0.00000 0.000000 0.000000 0.00000 0.000000
## Cumulative Proportion 0.99998 0.99998 0.999980 0.999990 0.99999 0.999990
## PC632 PC633 PC634 PC635 PC636 PC637
## Standard deviation 0.005874 0.005805 0.005726 0.00563 0.005412 0.005361
## Proportion of Variance 0.000000 0.000000 0.000000 0.00000 0.000000 0.000000
## Cumulative Proportion 0.999990 0.999990 0.999990 0.99999 0.999990 0.999990
## PC638 PC639 PC640 PC641 PC642 PC643
## Standard deviation 0.005238 0.005195 0.005117 0.005087 0.005045 0.004478
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion 0.999990 0.999990 0.999990 0.999990 0.999990 0.999990
## PC644 PC645 PC646 PC647 PC648 PC649
## Standard deviation 0.004433 0.004377 0.004317 0.004135 0.004133 0.004094
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion 0.999990 0.999990 0.999990 0.999990 0.999990 1.000000
## PC650 PC651 PC652 PC653 PC654 PC655
## Standard deviation 0.004026 0.003982 0.003856 0.003623 0.003603 0.003515
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## PC656 PC657 PC658 PC659 PC660 PC661
## Standard deviation 0.003487 0.003401 0.003295 0.003231 0.003166 0.003163
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## PC662 PC663 PC664 PC665 PC666 PC667
## Standard deviation 0.003135 0.003043 0.003004 0.002954 0.002435 0.002406
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## PC668 PC669 PC670 PC671 PC672 PC673
## Standard deviation 0.002188 0.002147 0.001991 0.001988 0.001908 0.001897
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
## Cumulative Proportion 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000
## PC674 PC675 PC676 PC677 PC678 PC679
## Standard deviation 0.001649 0.001593 0.00156 0.001502 0.001443 0.001417
## Proportion of Variance 0.000000 0.000000 0.00000 0.000000 0.000000 0.000000
## Cumulative Proportion 1.000000 1.000000 1.00000 1.000000 1.000000 1.000000
## PC680 PC681 PC682 PC683 PC684 PC685
## Standard deviation 0.001308 0.001114 0.001081 0.001045 0.00102 0.001011
## Proportion of Variance 0.000000 0.000000 0.000000 0.000000 0.00000 0.000000
## Cumulative Proportion 1.000000 1.000000 1.000000 1.000000 1.00000 1.000000
## PC686 PC687 PC688 PC689 PC690
## Standard deviation 0.0009648 0.0009502 0.0008958 0.000834 0.0007899
## Proportion of Variance 0.0000000 0.0000000 0.0000000 0.000000 0.0000000
## Cumulative Proportion 1.0000000 1.0000000 1.0000000 1.000000 1.0000000
## PC691 PC692 PC693 PC694 PC695
## Standard deviation 0.0007197 0.0006262 0.0006113 0.0005513 0.0004487
## Proportion of Variance 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
## Cumulative Proportion 1.0000000 1.0000000 1.0000000 1.0000000 1.0000000
## PC696 PC697 PC698 PC699 PC700
## Standard deviation 0.0004181 0.000326 0.0003132 0.000214 9.258e-05
## Proportion of Variance 0.0000000 0.000000 0.0000000 0.000000 0.000e+00
## Cumulative Proportion 1.0000000 1.000000 1.0000000 1.000000 1.000e+00
## PC701 PC702 PC703 PC704 PC705
## Standard deviation 7.497e-05 6.844e-05 3.047e-05 2.908e-05 2.922e-15
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
## PC706 PC707 PC708 PC709 PC710
## Standard deviation 2.543e-15 2.423e-15 2.228e-15 1.211e-15 1.164e-15
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
## PC711 PC712 PC713 PC714 PC715
## Standard deviation 1.142e-15 1.022e-15 9.269e-16 9.024e-16 8.321e-16
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
## PC716 PC717 PC718 PC719 PC720
## Standard deviation 8.048e-16 7.471e-16 6.999e-16 6.595e-16 6.125e-16
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
## PC721 PC722 PC723 PC724 PC725
## Standard deviation 5.681e-16 5.346e-16 5.069e-16 4.816e-16 4.135e-16
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
## PC726 PC727 PC728 PC729 PC730
## Standard deviation 3.895e-16 3.698e-16 3.2e-16 3.143e-16 2.898e-16
## Proportion of Variance 0.000e+00 0.000e+00 0.0e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.0e+00 1.000e+00 1.000e+00
## PC731 PC732 PC733 PC734 PC735
## Standard deviation 2.639e-16 2.605e-16 2.479e-16 2.153e-16 1.893e-16
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
## PC736 PC737 PC738 PC739 PC740
## Standard deviation 1.456e-16 1.456e-16 1.456e-16 1.456e-16 1.456e-16
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
## PC741 PC742 PC743 PC744 PC745
## Standard deviation 1.456e-16 1.456e-16 1.456e-16 1.456e-16 1.456e-16
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
## PC746 PC747 PC748 PC749 PC750
## Standard deviation 1.456e-16 1.456e-16 1.456e-16 1.456e-16 1.456e-16
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
## PC751 PC752 PC753 PC754 PC755
## Standard deviation 1.456e-16 1.456e-16 1.456e-16 1.456e-16 1.456e-16
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
## PC756 PC757 PC758 PC759 PC760
## Standard deviation 1.456e-16 1.456e-16 1.456e-16 1.456e-16 1.456e-16
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
## PC761 PC762 PC763 PC764 PC765
## Standard deviation 1.456e-16 1.456e-16 1.456e-16 1.456e-16 1.456e-16
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
## PC766 PC767 PC768 PC769 PC770
## Standard deviation 1.456e-16 1.456e-16 1.456e-16 1.456e-16 1.456e-16
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
## PC771 PC772 PC773 PC774 PC775
## Standard deviation 1.456e-16 1.456e-16 1.456e-16 1.456e-16 1.456e-16
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
## PC776 PC777 PC778 PC779 PC780
## Standard deviation 1.456e-16 1.456e-16 1.456e-16 1.45e-16 1.382e-16
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.00e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.00e+00 1.000e+00
## PC781 PC782 PC783 PC784 PC785
## Standard deviation 1.152e-16 8.929e-17 8.528e-17 4.319e-17 1.827e-17
## Proportion of Variance 0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
## Cumulative Proportion 1.000e+00 1.000e+00 1.000e+00 1.000e+00 1.000e+00
# Store the first ten coordinates and the label in a data frame
pca_plot <- data.frame(pca_x = pca_output$x[, 1], pca_y = pca_output$x[, 10],
label = as.factor(trainpic$label))
# Plot the first ten principal components using the true labels as color and shape
ggplot(pca_plot, aes(x = pca_x, y = pca_y, color = label)) +
ggtitle("PCA of MNIST sample") +
geom_text(aes(label = label)) +
theme(legend.position = "none")

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble 3.1.4 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.1 v forcats 0.5.1
## v purrr 0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::combine() masks gridExtra::combine()
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(dplyr)
eightpic <- trainpic %>% filter(label == 8)
For 8
# Get the principal components from PCA for 8
pca_outpute <- prcomp(eightpic[, -1])
# Observe a summary of the output
#summary(pca_outpute)
# Store the first ten coordinates and the label in a data frame for 8
pca_plot <- data.frame(pca_xe = pca_outpute$x[, 1], pca_ye = pca_outpute$x[, 10],
label = as.factor(eightpic$label))
# Plot the first ten principal components using the true labels as color and shape
ggplot(pca_plot, aes(x = pca_xe, y = pca_ye, color = label)) +
ggtitle("PCA of eight MNIST sample") +
geom_text(aes(label = label)) +
theme(legend.position = "none")
