Q.
[ R ] 화면설계 예제 TEST (10)
yul_S2
2022. 12. 25. 14:45
반응형
1. iris 데이터를 이용하여 인공신경망 기법 또는 xgboost 기법을 이용하여 분류분석 하시오.
(1) 데이터 가져오기 및 샘플링 하시오. (샘플링 시 species별 데이터가 같게 하시오)
library(xgboost) data(iris) head(iris) iris_label <- ifelse(iris$Species == 'setosa', 0, ifelse(iris$Species == 'versicolor', 1, 2)) table(iris_label) iris$label <- iris_label |
(2) 분류모델을 생성하시오.
2-1)
idx <- sample(nrow(iris), 0.7 * nrow(iris)) train <- iris[idx, ] test <- iris[-idx, ] # class(train) "data.frame" |
2-2) data.frame -> matrix 변환 (x변수는 matrix객체로 변환) / (y변수는 label이용)
head(train) train_mat <- as.matrix(train[-c(5:6)]) #Species, label제외 train_mat #x변수는 matrix객체로 변환 train_label <- train$label train_label # y변수는 label이용 |
2-3)
xgbtrain <- xgb.DMatrix(data = train_mat, label = train_lab) |
2-4) 모델 생성 xgboost 이용
xgb_model <- xgboost(data = xgbtrain, max_depth = 2, eta = 1, nthread = 2, nrounds = 2, objective = "multi:softmax", num_class = 3, verbose = 0) xgb_model |
(3) 테스트 데이터를 이용하여 분류하시오
test_mat <- as.matrix(test[-c(5:6)]) test_label <- test$label |
(4)예측정확도를 산출하시오
pred_iris <- predict(xgb_model, test_mat) table(pred_iris, test_lab) sum(pred_iris == test$label)/length(pred_iris) |
table 나온 결과값 보고 숫자 입력 해서 예측도 계산하지 말것!
2. iris 데이터를 대상으로 다음 조건에 맞게 시각화 하시오.
(1) 1번 컬럼을 x축으로 하고 3번 컬럼을 y축으로 하고 5번 컬럼을 색상 지정하시오.
data(iris) head(iris) plot(iris[,1], iris[,3], col=iris[,5]) |
(2) 차트 제목을 “Scatter plot for iris data”로 설정하시오
title(main="Scatter plot for iris data") |
(3) 작성한 차트를 파일명이 “iris_(본인 영문이니셜).jpg”인 파일에 저장하고 제출하시오
setwd("C:/Rwork/output") jpeg("iris_(본인 영문이니셜).jpg", width=720, height=480) plot(iris[,1], iris[,3], col=iris[,5]) title(main="Scatter plot for iris data") dev.off() |
3. diamonds 데이터 셋을 대상으로
(1) x축에 carat변수, y축에 price변수를 지정하고, clarity변수를 선 색으로 지정하여 미적 요소 맵핑 객체를 생성하시오.
library(ggplot2) data(diamonds) head(diamonds) p <- ggplot(diamonds, aes(carat, price, color = factor(clarity))) |
(2) 산점도 그래프 주변에 부드러운 곡선이 추가되도록 레이아웃을 추가하시오.
p+geom_point()+geom_smooth() |
(3) 작성한 차트를 파일명이 “diamonds_(본인 영문이니셜).jpg”인 파일에 저장하고 제출하시오.
setwd("C:/Rwork/output") jpeg("diamonds_(본인 영문이니셜).jpg", width=720, height=480) p+geom_point()+geom_smooth() dev.off() |
반응형