3. 오늘의 과정
1. Kaggle 제출 예제
– 전부 죽이기
2. Manually fitted model
– 수동으로 노가다
3. Deicision tree
4. Feature Engineering
5. Random Forest
4. Kaggle
• 머신러닝 문제 풀이 온라인 플랫폼
• 포럼
• 예제
• 등등 좋은 정보가 엄청 많음.
– 단, 영어.
5. 오늘의 문제 : 타이타닉
• Titanic : Machine Learning from Disaster
• https://www.kaggle.com/c/titanic/
• Home -> Data 에서 데이터 다운로드 가능
• Home -> Make a submission 으로 제출 가능
6. 일단 자료를 볼까요
• 다운로드
• train.csv
• test.csv
• test파일에는 한 컬럼이 없음.
• 걔를 답으로 맞춰서 제출 하는 것!
15. 3. Decision tree
• Greedy 한 decision tree 생성기
fit <- rpart(Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked,
data=train,
method="class")
19. 4. Featuring Engineering
• 두 테이블을 합친다.
– 새로운 Feature를 만들어야 하기 때문에
– 한 테이블에서 작업해야 Factor가 같이 생성됨
> test$Survived <- NA
> combi <- rbind(train, test)
21. 4. Featuring Engineering
• 전체 테이블에 적용해 새로운 Feature 생성
combi$Title <- sapply(combi$Name, FUN=function(x) {strsplit(x,
split='[,.]')[[1]][2]})
combi$Title <- sub(' ', '', combi$Title)
table(combi$Title)
Capt Col Don Dona Dr Jonkheer Lady
1 4 1 1 8 1 1
Major Master Miss Mlle Mme Mr Mrs
2 61 260 2 1 757 197
Ms Rev Sir the Countess
2 8 1 1
28. 4. Featuring Engineering
• 여기서 2명 이하인 성씨를 삭제
• 원래 자료에서 famIDs에 속하는 성과 안
속하는 성으로 나눈다.
famIDs <- famIDs[famIDs$Freq <= 2,]
combi$FamilyID[combi$FamilyID %in% famIDs$Var1] <- 'Small’
combi$FamilyID <- factor(combi$FamilyID)
29. 4. Featuring Engineering
• 다시 train, test 분리후
• 디시전 트리 생성
train <- combi[1:891,]
test <- combi[892:1309,]
fit <- rpart(
Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked + Title
+ FamilySize + FamilyID,
data=train,
method="class")
38. 5. Random Forest
• Random Forest 라이브러리 설치 후 실행!!
> install.packages('randomForest')
> library(randomForest)
> set.seed(415)
> fit <- randomForest(as.factor(Survived) ~ Pclass + Sex + Age + SibSp
+ Parch + Fare + Embarked + Title + FamilySize + FamilyID2,
data=train,
importance=TRUE,
ntree=2000)