R 7장 신경망과 서포트 벡터 머신
- 머신러닝의 3가지 종류
- 1.지도학습 :
뇌 |
인공 신경망 |
신경세포(뉴런) |
노드 |
신경세포의 연결 |
연결 가중치 |
바이어스: -> 가중치와 함께 신경망의 정보를 저장하는데 관여하는 변수
v = w1 * x1 + w2 * x2 + w3 * x3 +b
v = wx + b
w = [w1 w2 w3]
x = [x1 x2 x3] ^T
y = f(v)
활성함수
1. 입력신호의 가중의 합을 구한다
2. 활성함수에 가중합을 입력해 얻은 값을 외부로 출력한다.
|
|
- 표준정규분포: scale()
- 최대최소변환: normalize() 함수를 만들었음 : ( 0 ~ 1 ) 사이의 값
##### 7장 : 신경망(Neural Network)과 서포트 벡터 머신(Support Vector Machines) -------------------
##### Part 1: 신경망 -------------------
## 예제 : 콘크리트의 강도 모델링 ----
## 2단계 : 데이터 준비와 살펴보기 ----
# 데이터 읽기와 구조 확인
concrete <- read.csv("concrete.csv")
str(concrete)
# 정규화 함수
normalize <- function(x) {
return((x - min(x)) / (max(x) - min(x)))
}
# 전체 데이터 프레임에 정규화 적용
concrete_norm <- as.data.frame(lapply(concrete, normalize))
# 0과1 사이에 범위 확인
summary(concrete_norm$strength)
# 본래 데이터의 최소값, 최대값 비교
summary(concrete$strength)
# 훈련과 테스트 데이터 생성
concrete_train <- concrete_norm[1:773, ]
concrete_test <- concrete_norm[774:1030, ]
## 3단계 : 데이터로 모델 훈련 ----
# neuralnet 모델 훈련
install.packages("neuralnet")
library(neuralnet)
# 하나의 은닉 뉴런에 대한 단순한 ANN
concrete_model <- neuralnet(formula = strength ~ cement + slag +
ash + water + superplastic +
coarseagg + fineagg + age,
data = concrete_train)
# 망(network) 시각화
plot(concrete_model)
## 4단계 : 모델 성능 평가 ----
# 모델 결과
model_results <- compute(concrete_model, concrete_test[1:8])
# 강도값 예측
predicted_strength <- model_results$net.result
# 예측값과 실제값간의 상관 관계 확인
cor(predicted_strength, concrete_test$strength)
## 5단계 : 모델 성능 향상 ----
# 5개 은닉 뉴런인 복잡한 뉴런망
concrete_model2 <- neuralnet(strength ~ cement + slag +
ash + water + superplastic +
coarseagg + fineagg + age,
data = concrete_train, hidden = 5)
# 망(network) 시각화
plot(concrete_model2)
# 결과 평가
model_results2 <- compute(concrete_model2, concrete_test[1:8])
predicted_strength2 <- model_results2$net.result
cor(predicted_strength2, concrete_test$strength)
- R 기본 수업
- R 을 활용한 기계학습
- svm 으로 보통사람들(중산층)을 분류
- 이모델이 얼마나 정확한지 기존 훈련 데이터로 평가를 해본다.
- 예측한 결과와 실제와의 차이
m1 <- svm(CAT..MEDV~ ., type ="C-classification", data= boston_norm )
library(caret)
confusionMatrix(table(boston_norm $CAT..MEDV , predict(m1,boston_norm )))
|
- 중산층 분류 실습
- 필기체 분류 숫자
- 필기체 분류 영어
install.packages("caret")
install.packages("doParallel")
install.packages("kernlab")
install.packages("ggplot2")
install.packages("lattice")
library(ggplot2)
library(lattice)
library(kernlab)
library(caret)
library(doParallel)
# Enable parallel processing.
cl <- makeCluster(detectCores())
registerDoParallel(cl)
# Load the MNIST digit recognition dataset into R
# http://yann.lecun.com/exdb/mnist/
# assume you have all 4 files and gunzip'd them
# creates train$n, train$x, train$y and test$n, test$x, test$y
# e.g. train$x is a 60000 x 784 matrix, each row is one digit (28x28)
# call: show_digit(train$x[5,]) to see a digit.
# brendan o'connor - gist.github.com/39760 - anyall.org
load_mnist <- function() {
load_image_file <- function(filename) {
ret = list()
f = file(filename,'rb')
readBin(f,'integer',n=1,size=4,endian='big')
ret$n = readBin(f,'integer',n=1,size=4,endian='big')
nrow = readBin(f,'integer',n=1,size=4,endian='big')
ncol = readBin(f,'integer',n=1,size=4,endian='big')
x = readBin(f,'integer',n=ret$n*nrow*ncol,size=1,signed=F)
ret$x = matrix(x, ncol=nrow*ncol, byrow=T)
close(f)
ret
}
load_label_file <- function(filename) {
f = file(filename,'rb')
readBin(f,'integer',n=1,size=4,endian='big')
n = readBin(f,'integer',n=1,size=4,endian='big')
y = readBin(f,'integer',n=n,size=1,signed=F)
close(f)
y
}
train <<- load_image_file('train-images.idx3-ubyte')
test <<- load_image_file('t10k-images.idx3-ubyte')
train$y <<- load_label_file('train-labels.idx1-ubyte')
test$y <<- load_label_file('t10k-labels.idx1-ubyte')
}
show_digit <- function(arr784, col=gray(12:1/12), ...) {
image(matrix(arr784, nrow=28)[,28:1], col=col, ...)
}
train <- data.frame()
test <- data.frame()
# Load data.
load_mnist()
train$x <- train$x / 255
# Setup training data with digit and pixel values with 60/40 split for train/cv.
inTrain = data.frame(y=train$y, train$x)
inTrain$y <- as.factor(inTrain$y)
trainIndex = createDataPartition(inTrain$y, p = 0.60,list=FALSE)
training = inTrain[trainIndex,]
# SVM. 95/94.
fit <- train(y ~ ., data = head(training, 1000), method = 'svmRadial', tuneGrid = data.frame(sigma=0.0107249, C=1))
results
confusionMatrix(results, head(cv$y, 1000))
show_digit(as.matrix(training[5,2:785]))
# Predict the digit.
predict(fit, newdata = training[5,])
# Check the actual answer for the digit.
training[5,1]
'R(알) ' 카테고리의 다른 글
R 7장 신경망과 서포트 벡터 머신(발표자료) (0) | 2017.09.23 |
---|---|
R 9장. k-means 알고리즘 (0) | 2017.09.09 |
R 5장. 결정트리 (0) | 2017.09.08 |
R 4장. 나이브 베이즈의 이해활용한 기계학습4 (0) | 2017.09.08 |
R 3장. knn (최근접 이웃을 사용한 분류의 이해) (0) | 2017.09.08 |