Cỡ chữ 10 cỡ chữ 1 chữ màu xanh chữ màu đỏ chữ màu vàng
letter.recognition <- read.csv("/cloud/project/Data/letter-recognition.data", header=FALSE)
letters <- letter.recognition
str(letters)
## 'data.frame': 20000 obs. of 17 variables:
## $ V1 : chr "T" "I" "D" "N" ...
## $ V2 : int 2 5 4 7 2 4 4 1 2 11 ...
## $ V3 : int 8 12 11 11 1 11 2 1 2 15 ...
## $ V4 : int 3 3 6 6 3 5 5 3 4 13 ...
## $ V5 : int 5 7 8 6 1 8 4 2 4 9 ...
## $ V6 : int 1 2 6 3 1 3 4 1 2 7 ...
## $ V7 : int 8 10 10 5 8 8 8 8 10 13 ...
## $ V8 : int 13 5 6 9 6 8 7 2 6 2 ...
## $ V9 : int 0 5 2 4 6 6 6 2 2 6 ...
## $ V10: int 6 4 6 6 6 9 6 2 6 2 ...
## $ V11: int 6 13 10 4 6 5 7 8 12 12 ...
## $ V12: int 10 3 3 4 5 6 6 2 4 1 ...
## $ V13: int 8 9 7 10 9 6 6 8 8 9 ...
## $ V14: int 0 2 3 6 1 0 2 1 1 8 ...
## $ V15: int 8 8 7 10 7 8 8 6 6 1 ...
## $ V16: int 0 4 3 2 5 9 7 2 1 1 ...
## $ V17: int 8 10 9 8 10 7 10 7 7 8 ...
installing package
#install.packages("kernlab")
library("kernlab")
colnames(letters) <- c(
V1 = "letter",
V2 = "xbox",
V3 = "ybox",
V4 = "width",
V5 = "height",
V6 = "onpix",
V7= "xbar",
V8 = "ybar",
V9 = "x2bar",
V10 = "y2bar",
V11 = "xybar",
V12 = "x2ybar",
V13 = "xy2bar",
V14 = "xedge",
V15 = "xedgeycor",
V16 = "yedge",
V17 = "yedgexcor"
)
letters$letter <- as.factor(letters$letter)
str(letters)
## 'data.frame': 20000 obs. of 17 variables:
## $ letter : Factor w/ 26 levels "A","B","C","D",..: 20 9 4 14 7 19 2 1 10 13 ...
## $ xbox : int 2 5 4 7 2 4 4 1 2 11 ...
## $ ybox : int 8 12 11 11 1 11 2 1 2 15 ...
## $ width : int 3 3 6 6 3 5 5 3 4 13 ...
## $ height : int 5 7 8 6 1 8 4 2 4 9 ...
## $ onpix : int 1 2 6 3 1 3 4 1 2 7 ...
## $ xbar : int 8 10 10 5 8 8 8 8 10 13 ...
## $ ybar : int 13 5 6 9 6 8 7 2 6 2 ...
## $ x2bar : int 0 5 2 4 6 6 6 2 2 6 ...
## $ y2bar : int 6 4 6 6 6 9 6 2 6 2 ...
## $ xybar : int 6 13 10 4 6 5 7 8 12 12 ...
## $ x2ybar : int 10 3 3 4 5 6 6 2 4 1 ...
## $ xy2bar : int 8 9 7 10 9 6 6 8 8 9 ...
## $ xedge : int 0 2 3 6 1 0 2 1 1 8 ...
## $ xedgeycor: int 8 8 7 10 7 8 8 6 6 1 ...
## $ yedge : int 0 4 3 2 5 9 7 2 1 1 ...
## $ yedgexcor: int 8 10 9 8 10 7 10 7 7 8 ...
letters_train <- letters[1:16000,]
letters_test <- letters[16001:20000,]
letter_classifier <- ksvm(letter ~ . , data = letters_train, kernel = "vanilladot") # kernel = "vanilladot" is shape of kernel in this case is linear
## Setting default kernel parameters
#letter_classifier ##dài quá
letter_predictions <- predict(letter_classifier, letters_test)
letter_predictions[1:100]
## [1] U N V X N H E Y G E N B G L G W M D Y R P D E W D S R G V R D J C I T C N
## [38] M B U R A A Q Q H K G S A J G W N H L B E Y J N S Z Q D T L G P K W H W A
## [75] H T I F I D T R Z I D F C S C B B N E D U L P C N F
## Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
#table(letter_predictions, letters_test$letter)
agreement <- letter_predictions == letters_test$letter
table(agreement)
## agreement
## FALSE TRUE
## 643 3357
prop.table(table(agreement))
## agreement
## FALSE TRUE
## 0.16075 0.83925
letter_classifier_rbf <- ksvm(letter ~ ., data = letters_train, kernel = "rbfdot") # thay đổi kernel = cách khác
letter_predictions_rbf <- predict(letter_classifier_rbf,
letters_test) # check thôi
agreement_rbf <- letter_predictions_rbf == letters_test$letter
table(agreement_rbf) # so sánh
## agreement_rbf
## FALSE TRUE
## 278 3722
prop.table(table(agreement_rbf)) # tính % của bảng
## agreement_rbf
## FALSE TRUE
## 0.0695 0.9305