인류의 복지와 편익을 위한 인프라 건설을 주도하는토목공학과
제목
토목빅데이터 마이닝 딥러닝 코딩(R)(박영훈 교수)
작성일
2021.11.22
작성자
부천대학교 토목과
'R'을 활용한 딥러닝 기초 코딩입니다 install.packages("RSNNS") history(Inf) install.packages("neuralnet") fit=neuralnet(response~attribute, data=data, hidden = c(3,3), threshold=0.01) #fit2=neuralnet(response~attribute, data=data, hidden = c(4,3), threshold=0.01) #fit3=neuralnet(response~attribute, data=data, hidden = c(4,3,2,1), threshold=0.01) testdata=as.matrix(sample(seq(-2,2,length=10), 10, replace=FALSE), ncol=1) pred=compute(fit, testdata) result=cbind(testdata, pred$net.result, testdata^2) ----------------------------------------------------------------------------------------- install.packages("neuralnet") apply(data,2, function(x) sum(is.na(x))) # 결측치 확인 -> 결측 치 없다. f=medv~crim+indus+nox+rm+age+dis+tax+ptratio+lstat fit=neuralnet(f,data=data[train,], hidden=c(10,12,20), algorithm="rprop+", err.fct ="sse", act.fct="logistic", threshold=0.1, linear.output=TRUE) pred=compute(fit, data[-train, 1:9]) Response=data[-train,10] ---------------------------------------------------------------------------------- install.packages("deepnet") data("Boston", package="MASS") X=data[train, 1:9] fitB=nn.train(x=X, y=Y, initW=NULL, initB=NULL, hidden=c(10,12,20), learningrate=0.58, momentum=0.74, learningrate_scale=1, activationfun="sigm", output="linear", numepochs=970, batchsize=60, hidden_dropout=0, visible_dropout=0) Xtest=data[-train, 1:9] install.packages("Metrics") ------------------------------------ sapply(PimaIndiansDiabetes2, function(x) sum(is.na(x))) temp=PimaIndiansDiabetes2 temp=na.omit(temp) nrow(temp) y=temp$diabetes n=nrow(temp) train=sample(1:n, n_train, FALSE) install.packages("RSNNS") fitMLP=mlp(x=X, y=Y, size=c(12,8), maxit=1000, initFunc = "Randomize_Weights", initFuncParams = c(-0.3,0.3), learnFunc = "Std_Backpropagation", learnFuncParams = c(0.2,0), updateFunc = "Topological_Order", updateFuncParams = c(0), hiddenActFunc = "Act_Logistic", shufflePatterns = TRUE, linOut = TRUE) table(predMLP, sign(temp[-train,7]), dnn=c("Predicted", "Observed")) ------------------------------------------------
install.packages("installr")
installr::updateR()
require(RSNNS)
library(neuralnet)
set.seed(2016)
attribute=as.data.frame(sample(seq(-2,2,length=50),50, replace=FALSE), ncol=1)
attribute
response=attribute^2
response
data=cbind(attribute, response)
colnames(data)=c("attribute", "response")
data
plot(data$attribute,data$response )
plot(fit)
#plot(fit2)
#plot(fit3)
testdata
pred
colnames(result)=c("Attribute", "Prediction", "Actual")
result
round(result,4)
plot(pred$net.result~testdata, xlab="Attribute", ylab="DNN Prediction")
library(neuralnet)
install.packages("Metrics")
library(Metrics)
data("Boston", package="MASS")
data=Boston
head(data)
data=scale(data)
head(data)
keeps=c("crim", "indus", "nox", "rm", "age", "dis", "tax", "ptratio", "lstat", "medv")
data=data[ ,keeps]
head(data)
set.seed(2016)
n=nrow(data)
n
train=sample(1:n, 400, FALSE)
head(train)
plot(fit)
round(cor(pred$net.result, data[-train, 10])**2,6)
mse(data[-train,10], pred$net.result)
rmse(data[-train,10], pred$net.result)
Predicted_Value=pred$net.result
plot(Response~Predicted_Value)
library(deepnet)
data=Boston
head(data)
data=scale(data)
head(data)
keeps=c("crim", "indus", "nox", "rm", "age", "dis", "tax", "ptratio", "lstat", "medv")
data=data[ ,keeps]
head(data)
n=nrow(data)
n
train=sample(1:n, 400, FALSE)
head(train)
Y=data[train, 10]
preB=nn.predict(fitB, Xtest)
round(cor(preB, data[-train,10])^2,6)
library(Metrics)
mse(data[-train,10], preB)
rmse(data[-train,10], preB)
data("PimaIndiansDiabetes2", package="mlbench")
data("PimaIndiansDiabetes2", package="mlbench")
head(PimaIndiansDiabetes2)
ncol(PimaIndiansDiabetes2)
nrow(PimaIndiansDiabetes2)
str(PimaIndiansDiabetes2)
head(temp)
temp$insulin=NULL
temp$triceps=NULL
head(temp)
str(temp)
str(temp)
ncol(temp)
temp$diabetes=NULL
head(temp)
temp=scale(temp)
temp=cbind(as.factor(y), temp)
head(temp)
str(temp)
class(temp)
summary(temp)
head(n)
n_train=600
n_test=n-n_train
n_test
head(train)
library(RSNNS)
set.seed(2016)
X=temp[train, 1:6]
Y=temp[train,7]
head(X)
head(Y)
error_rate=(1-sum(predMLP==sign(temp[-train,7]))/124)
round(error_rate,3)
detach("package:RSNNS", unload=TRUE)
install.packages("AMORE")
library(AMORE)
net=newff(n.neurons = c(6,12,8,1), learning.rate.global = 0.01, momentum.global = 0.5, error.criterium = "LMLS", Stao=NA, hidden.layer = "sigmoid", output.layer = "purelin", method="ADAPTgdwm")
X=temp[train,]
Y=temp[train,7]
head(x)
head(Y)
fit=train(net, P=X, T=Y, error.criterium="LMLS", report=TRUE, show.step=100, n.shows=5)
pred=sign(sim(fit$net, temp[-train,]))
table(pred, sign(temp[-train,7]), dnn=c("Predicted", "Observed"))
error_rate=(1-sum(pred==sign(temp[-train,7]))/124)
round(error_rate, 3)