-
-
Save JoeUnsung/34d5268e3765fbca378699aba1186fa9 to your computer and use it in GitHub Desktop.
MACHINE_LEARNING_IN_R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## MACHINE LEARNING IN R | |
## INDEX | |
## 1. KNN | |
## 2. 나이브 베이즈 | |
## 3. 의사결정 트리 | |
## 4. 서포트 벡터 머신 | |
## 5. 회귀분석 | |
################################################################################################################ | |
## 전체 인덱스 | |
machine_learning <- function() { | |
ml_cho<-menu(c("knn 알고리즘", | |
"나이브 베이즈", | |
"의사 결정트리", | |
"서포트 벡터 머신", | |
"회귀 분석"), title="Do you want this?") | |
if (ml_cho == 1){ | |
knn_fun() | |
} | |
if (ml_cho == 2){ | |
##Naive_fun() | |
} | |
if (ml_cho == 3){ | |
ml_cho2 <- menu(c("정보획득량 출력하면 1번을", | |
"의사결정 트리로 시각화 하려면 2번", | |
"규칙기반의 리퍼 알고리즘을 사용하려면 3번")) | |
if (ml_cho2 == 1){ | |
#information_fun() | |
} | |
else if ( ml_cho2 == 2){ | |
#decision_fun() | |
} | |
else if (ml_cho2 == 3){ | |
#riper_ | |
} | |
} | |
} | |
################################################################################################################## | |
################################ 1. KNN ########################################################################## | |
knn_fun <- function(){ | |
library(data.table) | |
library(class) | |
input_table <- readline('csv파일을 입력하세요. ex) emp.csv : ') | |
input_header <- readline('컬럼명이 있습니까? ex)T OR F : ') | |
input_label_num <- readline('라벨이 위치한 번호을 입력하세요. ex)N (N>=1) : ') | |
input_rm_num <- readline('제거할 컬럼 위치의 번호를 입력하세요. ex) n,n,n ... (n>=1) ,없으면 0: ') | |
### 헤더 유무 검사 | |
if (input_header == 'T'){ | |
table_name <- read.csv(input_table, stringsAsFactors = F, header=T) | |
} | |
else { | |
table_name <- read.csv(input_table, stringsAsFactors = F, header=F) | |
} | |
### shuffle 기능 추가 | |
table_name <- table_name[sample(nrow(table_name)), ] | |
### na값 제거 | |
table_name <- na.omit(table_name) | |
### 라벨 위치를 정수형으로 변경(추후 사용을 위해) | |
input_label_num <- as.integer(input_label_num) | |
### 제거할 컬럼 제거 | |
split_num <- as.integer(unlist(strsplit(input_rm_num, ','))) | |
split_num <- sort(split_num, decreasing = T) | |
cnt <- 0 | |
if ( as.integer(split_num)== 0 ) | |
{ | |
table2 <- table_name | |
} | |
else | |
{ | |
for(i in split_num){ | |
table_name <- table_name[,-as.integer(i)] | |
cnt <- sum(ifelse(as.integer(split_num) < input_label_num, 1, 0)) | |
} | |
} | |
### 컬럼을 제거하고 실제 라벨 위치 | |
label_loc <- input_label_num - cnt | |
### 라벨을 factor로 변경 | |
label_tmp <- factor(table_name[,label_loc]) | |
### 라벨을 가장 마지막 컬럼으로 이동 | |
table2 <- table_name[,-label_loc] | |
table2$label <- label_tmp | |
### 정규화 함수 | |
normalize <- function(x){ | |
return((x-min(x))/(max(x)-min(x))) | |
} | |
### 정규화 데이터 삽입 | |
table2[,1:(ncol(table2)-1)] <- as.data.frame(lapply(table2[,1:(ncol(table2)-1)],normalize)) | |
### 데이터 나누기 | |
mm <- round(nrow(table2)*2/3) | |
### 데이터 설정 | |
train_data <- table2[1:mm,1:(ncol(table2)-1)] | |
test_data <- table2[(mm+1):nrow(table2), 1:(ncol(table2)-1)] | |
train_label <- table2[1:mm,'label'] | |
test_label <- table2[(mm+1):nrow(table2),'label'] | |
## k값을 훈련데이터의 건수의 제곱근으로 취하는 방법 | |
k_n <- round( sqrt( nrow(train_data) ) ) | |
###knn 결과 확인 | |
result <- knn(train = train_data, test = test_data, cl = train_label, k=k_n) | |
round(prop.table(table(ifelse(test_label==result,'o','x')))*100,1) | |
} | |
##################################################################################################################### | |
##################################################################################################################### | |
############### 3. DECISION TREE #################################################################################### | |
## ■ 규칙 기반 결정트리 실습 | |
##1. 버섯 데이터를 로드 한다. | |
mushroom <- read.csv("mushroom.csv", header=F, | |
stringsAsFactors=F) | |
mushroom[which(mushroom$V12=='?'), 12] <- NA | |
for (i in (15:23)) { | |
mushroom[which(mushroom[, i] ==''), i] <- NA | |
} | |
for ( i in 1:23) { | |
mushroom[,i] <- factor(mushroom[,i]) | |
} | |
## 5. mushroom 데이터를 훈련 데이터와 테스트 데이터로 | |
## 나눈다 ( 75% 는 훈련 데이터, 25% 는 테스트 데이터) | |
set.seed(1) | |
dim(mushroom) | |
train_cnt <- round(0.75*dim(mushroom)[1]) | |
train_index <- sample(1:dim(mushroom)[1],train_cnt, | |
replace=F) | |
mushroom_train <- mushroom[train_index, ] | |
mushroom_test <- mushroom[-train_index, ] | |
## 6. 분류한다 | |
model1 <- JRip(V1~., data=mushroom_train, na.action = na.pass) | |
model1 | |
summary(model1) | |
## 7. 결과를 확인한다. | |
result1 <- predict(model1, mushroom_test[ ,-1] ) | |
library(gmodels) | |
CrossTable(result1, mushroom_test[ , 1] ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment