Advertisement
encoree1996

Untitled

Jun 7th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. library(MASS)
  2. painters
  3. painters_sg<-painters[, 1:4]
  4.  
  5. print("Skladowe glowne: ")
  6. sklad_glowne=prcomp(painters_sg)
  7. sklad_glowne
  8. plot(sklad_glowne)
  9. summary(sklad_glowne)
  10. biplot(sklad_glowne)
  11.  
  12. print("Wartosci srednie: ")
  13.  
  14. print("Composition: ")
  15. mean(painters[,1])
  16.  
  17. print("Drawings: ")
  18. mean(painters[,2])
  19.  
  20. print("Colors: ")
  21. mean(painters[,3])
  22.  
  23. print("Expressions: ")
  24. mean(painters[,4])
  25.  
  26. print("Odchylenia standardowe: ")
  27.  
  28. print("Composition: ")
  29. sd(painters[,1])
  30.  
  31. print("Drawings: ")
  32. sd(painters[,2])
  33.  
  34. print("Color: ")
  35. sd(painters[,3])
  36.  
  37. print("Expression: ")
  38. sd(painters[,4])
  39.  
  40. print("Histogram: ")
  41. summary(painters)
  42.  
  43. print("Composition: ")
  44. hist_comp<-subset(painters, painters$Composition<20)
  45. summary(hist_comp$Composition)
  46. attach(hist_comp)
  47. summary(Composition)
  48. bin_comp=seq(min(Composition),max(Composition)+2,2)
  49. hist(Composition, main="Histogram composition", ylab="Czestotl composition", xlab="Composition", col="grey", breaks=54)
  50.  
  51. print("Drawing: ")
  52. hist_draw<-subset(painters, painters$Drawing<20)
  53. summary(hist_draw$Drawing)
  54. attach(hist_draw)
  55. summary(Drawing)
  56. bin_draw=seq(min(Drawing),max(Drawing)+2,2)
  57. hist(Drawing, main="Histogram drawing", ylab="Czestotl drawing", xlab="Drawing", col="blue", breaks=54)
  58.  
  59. print("Color: ")
  60. hist_col<-subset(painters, painters$Colour<20)
  61. summary(hist_col$Colour)
  62. attach(hist_col)
  63. summary(Colour)
  64. bin_col=seq(min(Colour),max(Colour)+2,2)
  65. hist(Colour, main="Histogram color", ylab="Czestotl color", xlab="Colour", col="red", breaks=54)
  66.  
  67. print("Expression: ")
  68. hist_exp<-subset(painters, painters$Expression<20)
  69. summary(hist_exp$Expression)
  70. attach(hist_exp)
  71. summary(Expression)
  72. bin_exp=seq(min(Expression),max(Expression)+2,2)
  73. hist(Expression, main="Histogram of 'Expression'", ylab="Czestotl expression", xlab="Expression", col="yellow", breaks=54)
  74.  
  75. print("Macierz korelacji: ")
  76. pairs(painters[,1:4])
  77. cor(painters[,1:4])
  78. cor(painters[,1:4], method="spearman")
  79. cor(painters[,1:4], method="kendall")
  80.  
  81. print("Klasyfikator LDA")
  82. painters.lda=lda(School~., data=painters)
  83. painters.pred=predict(painters.lda, newdata=painters)
  84.  
  85. print(table_lda<-table(painters$School, painters.pred$class))
  86.  
  87. print(procent<-100*sum(diag(table_lda))/sum(table_lda))
  88.  
  89. print("Klasyfikator QDA")
  90. painters.qda1=qda(School~Composition+Drawing, data=painters)
  91. painters.pred_qda1=predict(painters.qda1, newdata=painters)
  92.  
  93. print(table_qda1<-table(painters$School, painters.pred_qda1$class))
  94.  
  95. print(procent<-100*sum(diag(table_qda1))/sum(table_qda1))
  96. painters.qda2=qda(School~Colour+Expression, data=painters)
  97. painters.pred_qda2=predict(painters.qda2, newdata=painters)
  98.  
  99. print(table_qda2<-table(painters$School, painters.pred_qda2$class))
  100.  
  101. print(procent<-100*sum(diag(table_qda2))/sum(table_qda2))
  102.  
  103. print("Metoda krokowa")
  104. set.seed(4578)
  105. painters.step_forw=stepclass(School~., data=painters, method="lda", direction="forward", improvement=0.0001)
  106. painters.step_back=stepclass(School~., data=painters, method="lda", direction="backward", improvement=0.0001)
  107.  
  108. print("Metoda Bayesa")
  109. painters.bayes<-NaiveBayes(School~., data=painters, userkernel=TRUE)
  110. painters.bayes_pred<-predict(painters.bayes, painters)
  111.  
  112. print(table_bayes<-table(painters$School, painters.bayes_pred$class))
  113.  
  114. print(procent<-100.0*sum(diag(table_bayes))/sum(table_bayes))
  115.  
  116. print("Metoda najblizszych sasiadow")
  117. data(painters)
  118. str(painters)
  119. table(painters$School)
  120. head(painters)
  121. set.seed(9850)
  122. painters.grp<-runif(nrow(painters))
  123. painters<-painters[order(painters.grp),]
  124. str(painters)
  125. summary(painters[,c(1,2,3,4)])
  126. normalizacja<-function(x){ return( (x-min(x))/(max(x)-min(x)) ) }
  127. paintersNorm<-as.data.frame(lapply(painters[,c(1,2,3,4)], normalizacja))
  128. str(paintersNorm)
  129. summary(paintersNorm)
  130. paintersTrn<-paintersNorm[1:33,]
  131. painters_test<-paintersNorm[34:54,]
  132. paintersTrn_cel<-painters[1:33,5]
  133. painters_test_cel<-painters[34:54,5]
  134. require(class)
  135. model1<-knn(train=paintersTrn, test=painters_test, cl=paintersTrn_cel, k=8)
  136. model1
  137. knnTable=table(painters_test_cel, model1)
  138. knnTable
  139.  
  140. print(procent<-100*sum(diag(knnTable))/sum(knnTable))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement