rm(list=ls())
ls()
## character(0)
getwd()
## [1] "C:/data"
library(dplyr)
## Warning: 패키지 'dplyr'는 R 버전 4.2.2에서 작성되었습니다
##
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(caret)
## Warning: 패키지 'caret'는 R 버전 4.2.2에서 작성되었습니다
## 필요한 패키지를 로딩중입니다: ggplot2
## 필요한 패키지를 로딩중입니다: lattice
library(recipes)
## Warning: 패키지 'recipes'는 R 버전 4.2.2에서 작성되었습니다
##
## 다음의 패키지를 부착합니다: 'recipes'
## The following object is masked from 'package:stats':
##
## step
library(pROC)
## Warning: 패키지 'pROC'는 R 버전 4.2.2에서 작성되었습니다
## Type 'citation("pROC")' for a citation.
##
## 다음의 패키지를 부착합니다: 'pROC'
## The following objects are masked from 'package:stats':
##
## cov, smooth, var
x_test<-read.csv('X_test.csv', fileEncoding="euc-kr")
x_train<-read.csv('X_train.csv', fileEncoding="euc-kr")
y_train<-read.csv('y_train.csv', fileEncoding="euc-kr")
x_train %>% glimpse
## Rows: 3,500
## Columns: 10
## $ cust_id <int> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1…
## $ 총구매액 <dbl> 68282840, 2136000, 3197000, 16077620, 29050000, 1137900…
## $ 최대구매액 <int> 11264000, 2136000, 1639000, 4935000, 24000000, 9552000,…
## $ 환불금액 <int> 6860000, 300000, NA, NA, NA, 462000, 4582000, 29524000,…
## $ 주구매상품 <chr> "기타", "스포츠", "남성 캐주얼", "기타", "보석", "디자…
## $ 주구매지점 <chr> "강남점", "잠실점", "관악점", "광주점", "본 점", "일산…
## $ 내점일수 <int> 19, 2, 2, 18, 2, 3, 5, 63, 18, 1, 25, 3, 2, 27, 84, 152…
## $ 내점당구매건수 <dbl> 3.894737, 1.500000, 2.000000, 2.444444, 1.500000, 1.666…
## $ 주말방문비율 <dbl> 0.52702703, 0.00000000, 0.00000000, 0.31818182, 0.00000…
## $ 구매주기 <int> 17, 1, 1, 16, 85, 42, 42, 5, 15, 0, 13, 89, 16, 10, 4, …
y_train%>% glimpse
## Rows: 3,500
## Columns: 2
## $ cust_id <int> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
## $ gender <int> 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1,…
left_join(x_train,y_train,by='cust_id') %>% mutate(index='train')->train
glimpse(train)
## Rows: 3,500
## Columns: 12
## $ cust_id <int> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1…
## $ 총구매액 <dbl> 68282840, 2136000, 3197000, 16077620, 29050000, 1137900…
## $ 최대구매액 <int> 11264000, 2136000, 1639000, 4935000, 24000000, 9552000,…
## $ 환불금액 <int> 6860000, 300000, NA, NA, NA, 462000, 4582000, 29524000,…
## $ 주구매상품 <chr> "기타", "스포츠", "남성 캐주얼", "기타", "보석", "디자…
## $ 주구매지점 <chr> "강남점", "잠실점", "관악점", "광주점", "본 점", "일산…
## $ 내점일수 <int> 19, 2, 2, 18, 2, 3, 5, 63, 18, 1, 25, 3, 2, 27, 84, 152…
## $ 내점당구매건수 <dbl> 3.894737, 1.500000, 2.000000, 2.444444, 1.500000, 1.666…
## $ 주말방문비율 <dbl> 0.52702703, 0.00000000, 0.00000000, 0.31818182, 0.00000…
## $ 구매주기 <int> 17, 1, 1, 16, 85, 42, 42, 5, 15, 0, 13, 89, 16, 10, 4, …
## $ gender <int> 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0…
## $ index <chr> "train", "train", "train", "train", "train", "train", "…
x_test%>%mutate(index='test')->test
colSums(is.na(test))
## cust_id 총구매액 최대구매액 환불금액 주구매상품
## 0 0 0 1611 0
## 주구매지점 내점일수 내점당구매건수 주말방문비율 구매주기
## 0 0 0 0 0
## index
## 0
glimpse(test) # 평가용 데이터에는 gender 함수가 빠져있음
## Rows: 2,482
## Columns: 11
## $ cust_id <int> 3500, 3501, 3502, 3503, 3504, 3505, 3506, 3507, 3508, 3…
## $ 총구매액 <dbl> 70900400, 310533100, 305264140, 7594080, 1795790, 13000…
## $ 최대구매액 <int> 22000000, 38558000, 14825000, 5225000, 1411200, 2160000…
## $ 환불금액 <int> 4050000, 48034700, 30521000, NA, NA, NA, 39566000, NA, …
## $ 주구매상품 <chr> "골프", "농산물", "가공식품", "주방용품", "수산품", "화…
## $ 주구매지점 <chr> "부산본점", "잠실점", "본 점", "부산본점", "청량리점",…
## $ 내점일수 <int> 13, 90, 101, 5, 3, 5, 144, 1, 1, 28, 21, 3, 23, 30, 3, …
## $ 내점당구매건수 <dbl> 1.461538, 2.433333, 14.623762, 2.000000, 2.666667, 2.20…
## $ 주말방문비율 <dbl> 0.78947368, 0.36986301, 0.08327691, 0.00000000, 0.12500…
## $ 구매주기 <int> 26, 3, 3, 47, 8, 61, 2, 0, 0, 12, 14, 2, 15, 11, 112, 2…
## $ index <chr> "test", "test", "test", "test", "test", "test", "test",…
bind_rows(train,test) ->full #train과 test 데이터를 한번에 결합하여 full이라는 객체에 저장
glimpse(full) # 분류모형은 무조건 factor로 되어 있어야함 따라서 chr을 factor로 바꿔줘야 함
## Rows: 5,982
## Columns: 12
## $ cust_id <int> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1…
## $ 총구매액 <dbl> 68282840, 2136000, 3197000, 16077620, 29050000, 1137900…
## $ 최대구매액 <int> 11264000, 2136000, 1639000, 4935000, 24000000, 9552000,…
## $ 환불금액 <int> 6860000, 300000, NA, NA, NA, 462000, 4582000, 29524000,…
## $ 주구매상품 <chr> "기타", "스포츠", "남성 캐주얼", "기타", "보석", "디자…
## $ 주구매지점 <chr> "강남점", "잠실점", "관악점", "광주점", "본 점", "일산…
## $ 내점일수 <int> 19, 2, 2, 18, 2, 3, 5, 63, 18, 1, 25, 3, 2, 27, 84, 152…
## $ 내점당구매건수 <dbl> 3.894737, 1.500000, 2.000000, 2.444444, 1.500000, 1.666…
## $ 주말방문비율 <dbl> 0.52702703, 0.00000000, 0.00000000, 0.31818182, 0.00000…
## $ 구매주기 <int> 17, 1, 1, 16, 85, 42, 42, 5, 15, 0, 13, 89, 16, 10, 4, …
## $ gender <int> 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0…
## $ index <chr> "train", "train", "train", "train", "train", "train", "…
full$gender<-ifelse(full$gender==0, "남성", "여성")
full$gender<-as.factor(full$gender)
full$gender<-as.factor(full$index)
names(full)
## [1] "cust_id" "총구매액" "최대구매액" "환불금액"
## [5] "주구매상품" "주구매지점" "내점일수" "내점당구매건수"
## [9] "주말방문비율" "구매주기" "gender" "index"
data<-full%>%rename(total="총구매액",
max="최대구매액",
refund="환불금액",
product="주구매상품",
store="주구매지점",
day="내점일수",
count="내점당구매건수",
week="주말방문비율",
cycle="구매주기")%>% select(cust_id,index,gender,total,max,refund, product, store,day,count,week,cycle)
glimpse(data)
## Rows: 5,982
## Columns: 12
## $ cust_id <int> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
## $ index <chr> "train", "train", "train", "train", "train", "train", "train",…
## $ gender <fct> train, train, train, train, train, train, train, train, train,…
## $ total <dbl> 68282840, 2136000, 3197000, 16077620, 29050000, 11379000, 1005…
## $ max <int> 11264000, 2136000, 1639000, 4935000, 24000000, 9552000, 761200…
## $ refund <int> 6860000, 300000, NA, NA, NA, 462000, 4582000, 29524000, NA, NA…
## $ product <chr> "기타", "스포츠", "남성 캐주얼", "기타", "보석", "디자이너", "…
## $ store <chr> "강남점", "잠실점", "관악점", "광주점", "본 점", "일산점", "…
## $ day <int> 19, 2, 2, 18, 2, 3, 5, 63, 18, 1, 25, 3, 2, 27, 84, 152, 26, 2…
## $ count <dbl> 3.894737, 1.500000, 2.000000, 2.444444, 1.500000, 1.666667, 2.…
## $ week <dbl> 0.52702703, 0.00000000, 0.00000000, 0.31818182, 0.00000000, 0.…
## $ cycle <int> 17, 1, 1, 16, 85, 42, 42, 5, 15, 0, 13, 89, 16, 10, 4, 2, 13, …
colSums(is.na(data))
## cust_id index gender total max refund product store day count
## 0 0 0 0 0 3906 0 0 0 0
## week cycle
## 0 0
data$refund<-ifelse(is.na(data$refund), 0, data$refund)
colSums(is.na(data))
## cust_id index gender total max refund product store day count
## 0 0 0 0 0 0 0 0 0 0
## week cycle
## 0 0
library(recipes)
recipe(gender~.,data=data) %>%
step_YeoJohnson(total,max,refund,day,count,week,cycle) %>%
step_scale(total, max,refund,day,count,week,cycle) %>%
step_center(total,max,refund,day,count,week,cycle) %>%
prep() %>% juice()->data1
data1 %>% glimpse
## Rows: 5,982
## Columns: 12
## $ cust_id <int> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
## $ index <fct> train, train, train, train, train, train, train, train, train,…
## $ total <dbl> -0.1109616, -0.5964776, -0.5864340, -0.4794213, -0.3820895, -0…
## $ max <dbl> -0.25879992, -0.58005471, -0.59931645, -0.47681619, 0.15253819…
## $ refund <dbl> 1.3776676, 1.2130535, -0.7281455, -0.7281455, -0.7281455, 1.23…
## $ product <fct> 기타, 스포츠, 남성 캐주얼, 기타, 보석, 디자이너, 시티웨어, 명…
## $ store <fct> 강남점, 잠실점, 관악점, 광주점, 본 점, 일산점, 강남점, 본 점…
## $ day <dbl> 0.6267964, -0.9872986, -0.9872986, 0.5877041, -0.9872986, -0.7…
## $ count <dbl> 0.92059492, -0.89611526, -0.32407144, 0.06726813, -0.89611526,…
## $ week <dbl> 0.96145636, -1.31805060, -1.31805060, 0.32838074, -1.31805060,…
## $ cycle <dbl> 0.28905563, -1.19528222, -1.19528222, 0.24219137, 1.78728765, …
## $ gender <fct> train, train, train, train, train, train, train, train, train,…
data1 %>% filter(index=="train") %>% select(-index)->train
data1 %>% filter(index=="test") %>% select(-index)->test
glimpse(train)
## Rows: 3,500
## Columns: 11
## $ cust_id <int> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
## $ total <dbl> -0.1109616, -0.5964776, -0.5864340, -0.4794213, -0.3820895, -0…
## $ max <dbl> -0.25879992, -0.58005471, -0.59931645, -0.47681619, 0.15253819…
## $ refund <dbl> 1.3776676, 1.2130535, -0.7281455, -0.7281455, -0.7281455, 1.23…
## $ product <fct> 기타, 스포츠, 남성 캐주얼, 기타, 보석, 디자이너, 시티웨어, 명…
## $ store <fct> 강남점, 잠실점, 관악점, 광주점, 본 점, 일산점, 강남점, 본 점…
## $ day <dbl> 0.6267964, -0.9872986, -0.9872986, 0.5877041, -0.9872986, -0.7…
## $ count <dbl> 0.92059492, -0.89611526, -0.32407144, 0.06726813, -0.89611526,…
## $ week <dbl> 0.96145636, -1.31805060, -1.31805060, 0.32838074, -1.31805060,…
## $ cycle <dbl> 0.28905563, -1.19528222, -1.19528222, 0.24219137, 1.78728765, …
## $ gender <fct> train, train, train, train, train, train, train, train, train,…
library(caret)
ctrl<-trainControl(method='cv', number=10,
summaryFunction = twoClassSummary,
classProbs = TRUE)
# 이후 에러뜸(gender 값이 도출되지 않음)
names(getModelInfo())
## [1] "ada" "AdaBag" "AdaBoost.M1"
## [4] "adaboost" "amdai" "ANFIS"
## [7] "avNNet" "awnb" "awtan"
## [10] "bag" "bagEarth" "bagEarthGCV"
## [13] "bagFDA" "bagFDAGCV" "bam"
## [16] "bartMachine" "bayesglm" "binda"
## [19] "blackboost" "blasso" "blassoAveraged"
## [22] "bridge" "brnn" "BstLm"
## [25] "bstSm" "bstTree" "C5.0"
## [28] "C5.0Cost" "C5.0Rules" "C5.0Tree"
## [31] "cforest" "chaid" "CSimca"
## [34] "ctree" "ctree2" "cubist"
## [37] "dda" "deepboost" "DENFIS"
## [40] "dnn" "dwdLinear" "dwdPoly"
## [43] "dwdRadial" "earth" "elm"
## [46] "enet" "evtree" "extraTrees"
## [49] "fda" "FH.GBML" "FIR.DM"
## [52] "foba" "FRBCS.CHI" "FRBCS.W"
## [55] "FS.HGD" "gam" "gamboost"
## [58] "gamLoess" "gamSpline" "gaussprLinear"
## [61] "gaussprPoly" "gaussprRadial" "gbm_h2o"
## [64] "gbm" "gcvEarth" "GFS.FR.MOGUL"
## [67] "GFS.LT.RS" "GFS.THRIFT" "glm.nb"
## [70] "glm" "glmboost" "glmnet_h2o"
## [73] "glmnet" "glmStepAIC" "gpls"
## [76] "hda" "hdda" "hdrda"
## [79] "HYFIS" "icr" "J48"
## [82] "JRip" "kernelpls" "kknn"
## [85] "knn" "krlsPoly" "krlsRadial"
## [88] "lars" "lars2" "lasso"
## [91] "lda" "lda2" "leapBackward"
## [94] "leapForward" "leapSeq" "Linda"
## [97] "lm" "lmStepAIC" "LMT"
## [100] "loclda" "logicBag" "LogitBoost"
## [103] "logreg" "lssvmLinear" "lssvmPoly"
## [106] "lssvmRadial" "lvq" "M5"
## [109] "M5Rules" "manb" "mda"
## [112] "Mlda" "mlp" "mlpKerasDecay"
## [115] "mlpKerasDecayCost" "mlpKerasDropout" "mlpKerasDropoutCost"
## [118] "mlpML" "mlpSGD" "mlpWeightDecay"
## [121] "mlpWeightDecayML" "monmlp" "msaenet"
## [124] "multinom" "mxnet" "mxnetAdam"
## [127] "naive_bayes" "nb" "nbDiscrete"
## [130] "nbSearch" "neuralnet" "nnet"
## [133] "nnls" "nodeHarvest" "null"
## [136] "OneR" "ordinalNet" "ordinalRF"
## [139] "ORFlog" "ORFpls" "ORFridge"
## [142] "ORFsvm" "ownn" "pam"
## [145] "parRF" "PART" "partDSA"
## [148] "pcaNNet" "pcr" "pda"
## [151] "pda2" "penalized" "PenalizedLDA"
## [154] "plr" "pls" "plsRglm"
## [157] "polr" "ppr" "pre"
## [160] "PRIM" "protoclass" "qda"
## [163] "QdaCov" "qrf" "qrnn"
## [166] "randomGLM" "ranger" "rbf"
## [169] "rbfDDA" "Rborist" "rda"
## [172] "regLogistic" "relaxo" "rf"
## [175] "rFerns" "RFlda" "rfRules"
## [178] "ridge" "rlda" "rlm"
## [181] "rmda" "rocc" "rotationForest"
## [184] "rotationForestCp" "rpart" "rpart1SE"
## [187] "rpart2" "rpartCost" "rpartScore"
## [190] "rqlasso" "rqnc" "RRF"
## [193] "RRFglobal" "rrlda" "RSimca"
## [196] "rvmLinear" "rvmPoly" "rvmRadial"
## [199] "SBC" "sda" "sdwd"
## [202] "simpls" "SLAVE" "slda"
## [205] "smda" "snn" "sparseLDA"
## [208] "spikeslab" "spls" "stepLDA"
## [211] "stepQDA" "superpc" "svmBoundrangeString"
## [214] "svmExpoString" "svmLinear" "svmLinear2"
## [217] "svmLinear3" "svmLinearWeights" "svmLinearWeights2"
## [220] "svmPoly" "svmRadial" "svmRadialCost"
## [223] "svmRadialSigma" "svmRadialWeights" "svmSpectrumString"
## [226] "tan" "tanSearch" "treebag"
## [229] "vbmpRadial" "vglmAdjCat" "vglmContRatio"
## [232] "vglmCumulative" "widekernelpls" "WM"
## [235] "wsrf" "xgbDART" "xgbLinear"
## [238] "xgbTree" "xyf"
# part4 예제 1번
rm(list=ls())
train<-read.csv("insurance_train_10.csv")
test<-read.csv("insurance_test_10.csv")
glimpse(train)
## Rows: 6,969
## Columns: 9
## $ Gender <chr> "Male", "Female", "Male", "Male", "Male", "Female", "F…
## $ Ever_Married <chr> "No", "Yes", "Yes", "Yes", "No", "No", "Yes", "Yes", "…
## $ Age <int> 22, 67, 67, 56, 32, 33, 61, 55, 26, 19, 58, 41, 32, 31…
## $ Graduated <chr> "No", "Yes", "Yes", "No", "Yes", "Yes", "Yes", "Yes", …
## $ Profession <chr> "Healthcare", "Engineer", "Lawyer", "Artist", "Healthc…
## $ Work_Experience <int> 1, 1, 0, 0, 1, 1, 0, 1, 1, 4, 0, 1, 9, 1, 1, 0, 12, 3,…
## $ Spending_Score <chr> "Low", "Low", "High", "Average", "Low", "Low", "Low", …
## $ Family_Size <int> 4, 1, 2, 2, 3, 3, 3, 4, 3, 4, 1, 2, 5, 6, 4, 1, 1, 4, …
## $ Segmentation <int> 4, 2, 2, 3, 3, 4, 4, 3, 1, 4, 2, 3, 4, 2, 2, 3, 1, 4, …
colSums(is.na(train))
## Gender Ever_Married Age Graduated Profession
## 0 0 0 0 0
## Work_Experience Spending_Score Family_Size Segmentation
## 0 0 0 0
train$Segmentation<-as.factor(train$Segmentation)
library(caret)
ctrl<-trainControl(method='cv', number=10)
train(Segmentation~., data=train,
method='knn',trControl=ctrl,
preProcess=c("center","scale"))->knn_fit
knn_fit
## k-Nearest Neighbors
##
## 6969 samples
## 8 predictor
## 4 classes: '1', '2', '3', '4'
##
## Pre-processing: centered (19), scaled (19)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 6272, 6272, 6272, 6272, 6272, 6273, ...
## Resampling results across tuning parameters:
##
## k Accuracy Kappa
## 5 0.4841441 0.3108678
## 7 0.4940408 0.3240489
## 9 0.4969166 0.3277060
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was k = 9.
predict(knn_fit, test)->pred_fit
head(pred_fit)
## [1] 2 1 2 3 3 1
## Levels: 1 2 3 4
NROW(pred_fit)
## [1] 2267
test %>% glimpse
## Rows: 2,267
## Columns: 9
## $ X <int> 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17…
## $ Gender <chr> "Female", "Male", "Female", "Male", "Male", "Male", "F…
## $ Ever_Married <chr> "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"…
## $ Age <int> 36, 37, 69, 59, 47, 61, 47, 50, 19, 22, 22, 50, 27, 18…
## $ Graduated <chr> "Yes", "Yes", "No", "No", "Yes", "Yes", "Yes", "Yes", …
## $ Profession <chr> "Engineer", "Healthcare", "", "Executive", "Doctor", "…
## $ Work_Experience <int> 0, 8, 0, 11, 0, 5, 1, 2, 0, 0, 0, 1, 8, 0, 0, 1, 1, 8,…
## $ Spending_Score <chr> "Low", "Average", "Low", "High", "High", "Low", "Avera…
## $ Family_Size <int> 1, 4, 1, 2, 5, 3, 3, 4, 4, 3, 6, 5, 3, 3, 1, 3, 2, 1, …
bind_cols(test,pred_fit)->df
## New names:
## • `` -> `...10`
names(df)[9]<-"Segmentation_pred"
df %>% select(9)->df1
write.csv(df1,"수험번호.csv",row.names=FALSE)
set.seed(12345) #이 함수 때문에 결과값이 책이랑 다를 수 있음
IDX<-createDataPartition(train$Segmentation,p=0.7,list=FALSE)
train_t<-train[IDX,]
test_v<-train[-IDX,]
train_t$Segmentation<-as.factor(train_t$Segmentation)
test_v$Segmentation<-as.factor(test_v$Segmentation)
ctrl<-trainControl(method='cv', number=10)
train(Segmentation~.,data=train_t,
method='knn', trControl=ctrl, preProcess=c("center","scale"))->knn_fit1
predict(knn_fit1,newdata=test_v)->test_pred
confusionMatrix(test_pred, test_v$Segmentation, mode="prec_recall")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 1 2 3 4
## 1 207 114 62 115
## 2 120 152 123 53
## 3 71 163 284 33
## 4 109 60 64 358
##
## Overall Statistics
##
## Accuracy : 0.4794
## 95% CI : (0.4578, 0.5011)
## No Information Rate : 0.2677
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.3047
##
## Mcnemar's Test P-Value : 0.009816
##
## Statistics by Class:
##
## Class: 1 Class: 2 Class: 3 Class: 4
## Precision 0.41566 0.3393 0.5154 0.6058
## Recall 0.40828 0.3108 0.5328 0.6404
## F1 0.41194 0.3244 0.5240 0.6226
## Prevalence 0.24282 0.2342 0.2553 0.2677
## Detection Rate 0.09914 0.0728 0.1360 0.1715
## Detection Prevalence 0.23851 0.2146 0.2639 0.2830
## Balanced Accuracy 0.61211 0.5629 0.6806 0.7440