# # # # # # # # # # # # # # # # # # # # # # # # ######################### 국가 깃발 정보 로 종교 분류 분석 ## source : https : / / archive . ics . uci . edu / ml / datasets / Flags # # Attribute 설명 # 1 . name : 국명 # 2 . landmass : 1 = 북미 , 2 = 남미 , 3 = 유럽 , 4 = 아프리카 , 5 = 아시아 , 6 = 오세 아니 아 # 3 . zone 구역 : 그리 니치 와 적도 에 근거 한 지리 적 사분면 ; 1 = NE , 2 = SE , 3 = SW , 4 = NW # 4 . area 면적 : 수천 평방 킬로미터 # 5 . population 인구 : 둥근 수백 만 # 6 . language 언어 : 1 = 영어 , 2 = 스페인어 , 3 = 프랑스어 , 4 = 독일어 , 5 = 슬라브 어 , 6 = 그 외 인도 - 유럽 어 , # 7 = 중국어 , 8 = 아랍어 , 9 = 일본어 / 터키어 / 핀란드어 / 마야어 , 10 = 기타 # 7 . religion 종교 : 0 = 가톨릭 , 1 = 개신교 , 2 = 무슬림 , 3 = 불교 , 4 = 힌두교 , 5 = 민족 신앙 , 6 = 마르크스 , # 7 = 기타 # 8 . bars 막대 : 수직 막대 수 # 9 . stripes 줄무늬 : 수평 줄무늬 수 # 10 . colours 색깔 : 다른 색깔 의 수 # 11 . red 빨간색 : 빨간색 이 없 으면 0 , 플래그 에 빨간색 이 있 으면 1 # 12 . green 녹색 : 색 유무 # 13 . blue 파랑 : 색 유무 # 14 . gold 골드 : 색 유무 # 15 . white 흰색 : 색 유무 # 16 . black 블랙 : 색 유무 # 17 . orange 주황색 : 색 유무 # 18 . mainhue : 깃발 의 주요 색상 # 19 . circles 서클 : 플래그 의 서클 수 # 20 . crosses 십자가 : 십자가 의 수 # 21 . saltires : 대각선 십자가 의 수 # 22 . quarters 분기 : 분기 섹션 수 # 23 . sunstars : 태양 또는 별 기호 수 # 24 . crescent 초승달 : 초승달 기호 가 있 으면 1 , 그렇 지 않 으면 0 # 25 . triangle 삼각형 : 삼각형 이 있 으면 1 , 그렇 지 않 으면 0 # 26 . icon : 무생물 이미지 ( 예 : 보트 ) 가 있 는 경우 1 , 그렇 지 않 은 경우 0 # 27 . animate 애니메이션 : 1 애니메이션 이미지 ( 예 : 독수리 , 나무 , 인간 손 ) 가 있 으면 0 , 그렇 지 않 으면 0 # 28 . text 텍스트 : 1 깃발 에 글자 나 글씨 가 있 으면 ( 예 : 좌우명 또는 슬로건 ) , 그렇 지 않 으면 0 # 29 . topleft 왼쪽 상단 : 왼쪽 상단 의 색상 # 30 . botright 오른쪽 하단 : 왼쪽 하단 의 색상 rm ( list = ls ( ) ) # data 불러오 기 -----------------------# setwd ( "< file _ path >")# getwd ( ) flag . data . csv # ? read . csvd <- read . csv ( " https : / / archive . ics . uci . edu / ml / machine - learning - databases / flags / flag . data ", header = F ) colnames ( d ) <- c ( " name ", " landmass ", " zone ", " area ", " population ", " language ", " religion ", " bars ", " stripes ", " colours ", " red ", " green ", " blue ", " gold ", " white ", " black ", " orange ", " mainhue ", " circles ", " crosses ", " saltires ", " quarters ", " sunstars ", " crescent ", " triangle ", " icon ", " animate ", " text ", " topleft ", " botright ") d . backup <- d # data 살펴보 기 ---------------------- str ( d ) # View ( d ) # colSums ( is . na ( d ) ) # colnames ( d ) # nrow ( d ) summary ( d ) # 한국 데이터 만들 기 # d . backup [ d . backup $ name == " Japan ", ]# S . Korea <- data . frame ( # name = " South Korea ", # landmass = 5 , # zone = 1 , # area = 223 , # population = 77 , # language = 9 , # religion = 7 , # bars = 0 , # stripes = 0 , # colours = 4 , # red = 1 , # green = 0 , # blue = 1 , # gold = 0 , # white = 1 , # black = 1 , # orange = 0 , # mainhue = " white ", # circles = 1 , # crosses = 0 , # saltires = 0 , # quarters = 0 , # sunstars = 0 , # crescent = 0 , # triangle = 0 , # icon = 0 , # animate = 0 , # text = 0 , # topleft = " white ", # botright = " white ")# str ( S . Korea ) # ? rbind # rbind ( d , S . Korea ) # d . backup [ d . backup $ name == " South Korea ", ]# 종교 분포 확인 # library ( ggplot 2 ) # library ( Hmisc ) # describe ( d $ religion ) # ggplot ( d , aes ( x = religion ) ) + geom _ histogram ( ) # 상관관계 확인 # d $ mainhue <- as . numeric ( d $ mainhue ) # d $ topleft <- as . numeric ( d $ topleft ) # d $ botright <- as . numeric ( d $ botright ) # library ( corrplot ) # str ( d 1 ) # d 1 <- d [ , - 1 ] # cor ( d 1 ) # corrplot . mixed ( cor ( d 1 ) , upper = " ellipse " , lower =" number ", tl . pos =" lt ", bg =" gray ")# data 전처리 -----------------# 항목 변수 처리 colnames ( d ) d $ name <- as . character ( d $ name ) d $ landmass <- as . factor ( d $ landmass ) levels ( d $ landmass ) <- c ( " N . America ", " S . America ", " Europe ", " Africa ", " Asia ", " Oceania ") d $ zone <- as . factor ( d $ zone ) levels ( d $ zone ) <- c ( " NE ", " SE ", " SW ", " NW ") d $ language <- as . factor ( d $ language ) levels ( d $ language ) <- c ( " English ", " Spanish ", " French ", " German ", " Slavic ", " Other . Indo - European ", " Chinese ", " Arabic ", " Japanese / Turkish / Finnish / Magyar ", " Others ") d $ religion <- as . factor ( d $ religion ) levels ( d $ religion ) <- c ( " Catholic ", " Other . Christian ", " Muslim ", " Buddhist ", " Hindu ", " Ethnic ", " Marxist ", " Others ") d $ red <- as . factor ( d $ red ) levels ( d $ red ) <- c ( " absent ", " present ") d $ green <- as . factor ( d $ green ) levels ( d $ green ) <- c ( " absent ", " present ") d $ blue <- as . factor ( d $ blue ) levels ( d $ blue ) <- c ( " absent ", " present ") d $ gold <- as . factor ( d $ gold ) levels ( d $ gold ) <- c ( " absent ", " present ") d $ white <- as . factor ( d $ white ) levels ( d $ white ) <- c ( " absent ", " present ") d $ black <- as . factor ( d $ black ) levels ( d $ black ) <- c ( " absent ", " present ") d $ orange <- as . factor ( d $ orange ) levels ( d $ orange ) <- c ( " absent ", " present ") d $ crescent <- as . factor ( d $ crescent ) levels ( d $ crescent ) <- c ( " absent ", " present ") d $ triangle <- as . factor ( d $ triangle ) levels ( d $ triangle ) <- c ( " absent ", " present ") d $ icon <- as . factor ( d $ icon ) levels ( d $ icon ) <- c ( " absent ", " present ") d $ animate <- as . factor ( d $ animate ) levels ( d $ animate ) <- c ( " absent ", " present ") d $ text <- as . factor ( d $ text ) levels ( d $ text ) <- c ( " absent ", " present ")# str ( d ) # head ( d ) # summary ( d ) # d [ ( # ( ( d . backup $ landmass == 1 ) | ( d . backup $ landmass == 2 ) # | ( d . backup $ landmass == 3 ) | ( d . backup $ landmass == 6 ) ) # & ( ( d . backup $ language ! = 2 ) & ( d . backup $ language ! = 3 ) ) # & ( ( d . backup $ landmass ! = 1 ) & ( d . backup $ landmass ! = 6 ) ) # & ( d . backup $ bars < 1 ) # ) , c ( 1 , 2 , 6 , 7 , 8 ) ] # d [ ( # ( d . backup $ crosses < 0 . 5 ) # & ( d . backup $ green == 1 ) # & ( d . backup $ gold == 0 ) # ) , c ( 1 , 7 , 12 , 14 , 20 ) ] # train , test - - - ------------------------- d $ name <- NULL # 깃발 외 정보 삭제 d $ landmass <- NULLd $ language <- NULLd $ population <- NULLd $ area <- NULLd $ zone <- NULL # win . graph ( ) # plot ( d ) # library ( psych ) # pairs . panels ( d ) # 종교 갯 수 줄이 기 ( 8 -> 5 ) # 7 . religion 종교 : 1 = 가톨릭 , 2 = 다른 기독교 , 3 = 무슬림 , 4 = 불교 , 5 = 힌두교 , 6 = 민족 , 7 = 맑스 , 8 = 기타 # summary ( d $ religion ) # d $ religion 2 <- as . numeric ( d $ religion ) # d $ religion 2 <- ifelse ( d $ religion 2 == 4 , 8 , d $ religion 2 ) # d $ religion 2 <- ifelse ( d $ religion 2 == 5 , 8 , d $ religion 2 ) # d $ religion 2 <- ifelse ( d $ religion 2 == 7 , 8 , d $ religion 2 ) # d $ religion 2 <- as . factor ( d $ religion 2 ) # levels ( d $ religion 2 ) <- c ( " Catholic ", " Other . Christian ", " Muslim ", " Ethnic ", " Others ")# summary ( d $ religion ) # summary ( d $ religion 2 ) # # d $ religion <- d $ religion 2 # d $ religion 2 <- NULL # 종교 갯 수 줄이 기 ( 8 -> 2 ) # 7 . religion 종교 : 1 = 가톨릭 , 2 = 다른 기독교 , 3 = 무슬림 , 4 = 불교 , 5 = 힌두교 , 6 = 민족 , 7 = 맑스 , 8 = 기타 # summary ( d $ religion ) # d $ religion 2 <- as . numeric ( d $ religion ) # d $ religion 2 <- ifelse ( d $ religion 2 == 2 , 1 , d $ religion 2 ) # d $ religion 2 <- ifelse ( d $ religion 2 == 3 , 8 , d $ religion 2 ) # d $ religion 2 <- ifelse ( d $ religion 2 == 4 , 8 , d $ religion 2 ) # d $ religion 2 <- ifelse ( d $ religion 2 == 5 , 8 , d $ religion 2 ) # d $ religion 2 <- ifelse ( d $ religion 2 == 6 , 8 , d $ religion 2 ) # d $ religion 2 <- ifelse ( d $ religion 2 == 7 , 8 , d $ religion 2 ) # d $ religion 2 <- as . factor ( d $ religion 2 ) # levels ( d $ religion 2 ) <- c ( " Christian ", " Others ")# summary ( d $ religion ) # summary ( d $ religion 2 ) # # d $ religion <- d $ religion 2 # d $ religion 2 <- NULLset . seed ( 1000 ) index <- sample ( 1 : nrow ( d ) , nrow ( d ) * 0 . 8 ) train <- d [ index , ] test <- d [ - index , ]# nrow ( train ) ; nrow ( test ) # svm model ------------------------ library ( e 1071 ) # ? svmsvm . fit <- svm ( religion ~ . , train ) svm . pr <- predict ( svm . fit , test ) library ( caret ) # ? c o n f u sionMatrixconfusionMatrix ( svm . pr , test $ religion ) # Accuracy : 0 . 6154 ( 8 ) # Accuracy : 0 . 6667 ( 5 ) # Accuracy : 0 . 9231 ( 2 ) # random forest model - - ------------------------- library ( randomForest ) randfore . fit <- randomForest ( religion ~ . , train , ntree = 100 ) randfore . pr <- predict ( randfore . fit , test ) confusionMatrix ( randfore . pr , test $ religion ) # Accuracy : 0 . 641 ( 8 ) # Accuracy : 0 . 6923 ( 5 ) # Accuracy : 0 . 8974 ( 2 ) # rpart model - - - - - - - - - - - - - - - - - - - - - - - ------------------------- library ( rpart ) # ? rpartrpart . fit <- rpart ( religion ~ . , train ) summary ( rpart . fit ) # plot ( rpart . fit ) # text ( rpart . fit ) # prune # ? printcp # printcp ( rpart . fit ) # plotcp ( rpart . fit ) # ? prune # str ( rpart . fit ) rpart . prune <- prune ( rpart . fit , cp = rpart . fit $ cptable [ which . min ( rpart . fit $ cptable [ , " xerror "])," CP "])# plot ( rpart . prune ) # text ( rpart . prune ) # install . packages ( c ( " rpart . plot ", " rattle ", " RColorBrewer ")) library ( " rpart . plot ") library ( " rattle ") library ( " RColorBrewer ")# win . graph ( ) # rpart . plot ( rpart . prune , main = " Religion ")# fancyRpartPlot ( rpart . fit ) # predict , estimaterpart . pr <- predict ( rpart . prune , test , type = " class ")# plot ( rpart . pr ) confusionMatrix ( rpart . pr , test $ religion ) # Accuracy : 0 . 641 ( 8 ) # Accuracy : 0 . 7179 ( 5 ) # Accuracy : 0 . 9231 ( 2 ) # ctree model - - - - - - - - - - - - - ------------------------- library ( party ) # ? ctreectree . fit <- ctree ( religion ~ . , train ) # plot ( ctree . fit ) # predict , estimate # ? predictctree . pr <- predict ( ctree . fit , test ) # newdata = table ( ctree . pr , test $ religion ) # plot ( ctree . pr ) confusionMatrix ( ctree . pr , test $ religion ) # Accuracy : 0 . 5385 ( 8 ) # Accuracy : 0 . 5641 ( 5 ) # Accuracy : 0 . 9231 ( 2 ) # tree model - - - - - - - - ------------------------- library ( tree ) tree . fit <- tree ( religion ~ . , data = train ) # plot ( tree . fit ) # text ( tree . fit ) # prune # ? cv . tree # ? prune . misclasstree . prune <- prune . misclass ( tree . fit , best = 6 ) # for regression decision tree , use prune . tree function # plot ( tree . prune ) # text ( tree . prune , pretty = 0 ) # predict , estimatetree . pr <- predict ( tree . fit , test , type = " class ")# plot ( tree . pr ) confusionMatrix ( tree . pr , test $ religion ) # Accuracy : 0 . 641 ( 8 ) # Accuracy : 0 . 7179 ( 5 ) # Accuracy : 0 . 8462 ( 2 ) # KNN model - - - - - - - - - - -----------------------## the normalization function is creatednor <- function ( x ) { ( x - min ( x ) ) / ( max ( x ) - min ( x ) ) } d . n <- d . backupstr ( d . n ) d . n $ name <- NULLd . n $ mainhue <- as . numeric ( d . n $ mainhue ) d . n $ topleft <- as . numeric ( d . n $ topleft ) d . n $ botright <- as . numeric ( d . n $ botright ) d . n <- data . frame ( lapply ( d . n , nor ) ) str ( d . n ) summary ( d . n ) # train / test , train . answer / test . answerd . n . train <- d . n [ index , - 6 ] d . n . test <- d . n [ - index , - 6 ] d . n . train . answer <- as . numeric ( d [ index , 6 ] ) d . n . test . answer <- as . numeric ( d [ - index , 6 ] ) library ( FNN ) knn . pr <- knn ( d . n . train , d . n . test , cl = d . n . train . answer , k = 1 ) summary ( knn . pr ) # length ( d . n . test . answer ) levels ( knn . pr ) <- c ( " Catholic ", " Other . Christian ", " Muslim ", " Buddhist ", " Hindu ", " Ethnic ", " Marxist ", " Others ")# levels ( knn . pr ) <- c ( " Catholic ", " Other . Christian ", " Muslim ", " Ethnic ", " Others ")# levels ( knn . pr ) <- c ( " Christian ", " Others ") d . n . test . answer <- as . factor ( d . n . test . answer ) levels ( d . n . test . answer ) <- levels ( knn . pr ) confusionMatrix ( knn . pr , d . n . test . answer ) # Accuracy : 0 . 3846 ( 8 ) # Accuracy : 0 . 4359 ( 5 ) # Accuracy : 0 . 7179 ( 2 ) # model comparision - - - - - - ------------------------- cm . summary <- function ( model . pr , answer ) { confusionmatrix <- confusionMatrix ( model . pr , answer ) cm _ overall <- confusionmatrix $ overall acc <- cm _ overall [ attributes ( cm _ overall ) $ names == " Accuracy "][[ 1 ] ] # cm _ byclass <- confusionmatrix $ byClass # sens <- cm _ byclass [ attributes ( cm _ byclass ) $ names == " Sensitivity "][[ 1 ] ] # spec <- cm _ byclass [ attributes ( cm _ byclass ) $ names == " Specificity "][[ 1 ] ] # cbind . data . frame ( acc , sens , spec ) data . frame ( acc ) # c ( acc , sens , spec ) }# cm . summary ( rpart . pr , test $ religion ) # attributes ( confusionMatrix ( rpart . pr , test $ religion ) $ overall ) models . pr <- c ( " svm . pr ", " rpart . pr ", " ctree . pr ", " tree . pr ", " randfore . pr ", " knn . pr ") cm . summary . total <- function ( ) { s <- NULL for ( i in 1 : length ( models . pr ) ) { n <- cm . summary ( get ( models . pr [ i ] ) , test $ religion ) dimnames ( n ) [ [ 1 ] ] <- models . pr [ i ] s <- rbind ( s , n ) } s } result <- cm . summary . total ( ) result $ model <- dimnames ( result ) [ [ 1 ] ] library ( plyr ) arrange ( result , desc ( result $ acc ) ) # 결과 ( 종교 8 개 ) , set . seed ( 1000 ) # acc model # 1 0 . 6410256 rpart . pr # 2 0 . 6410256 tree . pr # 3 0 . 6410256 randfore . pr # 4 0 . 6153846 svm . pr # 5 0 . 5384615 ctree . pr # 6 0 . 3589744 knn . pr # 결과 ( 종교 5 개 ) , set . seed ( 1000 ) # acc model # 1 0 . 7179487 rpart . pr # 2 0 . 7179487 tree . pr # 3 0 . 6923077 randfore . pr # 4 0 . 6666667 svm . pr # 5 0 . 5641026 ctree . pr # 6 0 . 4358974 knn . pr # 결과 ( 종교 2 개 , 크리스찬 - 비 크리스찬 ) , set . seed ( 1000 ) # acc sens spec model # 1 0 . 9230769 1 . 0 0 . 8750000 svm . pr # 2 0 . 9230769 1 . 0 0 . 8750000 rpart . pr # 3 0 . 9230769 1 . 0 0 . 8750000 ctree . pr # 4 0 . 8974359 1 . 0 0 . 8333333 randfore . pr # 5 0 . 8461538 1 . 0 0 . 7500000 tree . pr # 6 0 . 7179487 0 . 8 0 . 6666667 knn . pr 국기 정보 중 분류 에 가장 영향 을 준 변수 국가 정보 : landmass ( 대륙 ) , language ( 언어 ) 국기 정보 : bar ( 수직 막대 수 ) 국가 정보 를 제외 한 국기 정보 로 만 분류 했 을 때 는 정확도 가 떨어지 지만 , 십자가 존재 여부 , 녹색 , 금색 존재 여부 가 크리스찬 이 냐 이슬람 이 냐 구분 할 정도 는 나온다 해당 자료 는 1990 년 대 의 빈약 한 데이터 를 기반 으로 한 것 이 어서 ( 대한민국 이 없 다니 . ..) 새로운 데이터 셋 을 찾아보 다가 이런 사이트 를 발견 했 다 . https : / / flagstories . co /