归一化
normalize <- function(x) {
return((x - min(x)) / (max(x) - min(x)))
}
lapply应用到数据框每一列
concrete_norm <- as.data.frame(lapply(concrete, normalize))
library(neuralnet)
用法
concrete_model <- neuralnet(formula = strength ~ cement + slag +
ash + water + superplastic +
coarseagg + fineagg + age,
data = concrete_train)
neuralnet(target~predictors,data=mydata,hidden=1) #hidden隐藏神经元个数
hidden隐藏神经元个数threshold停止阈值stepmax最大步数rep重复次数startweights起始权重
learningrate.limit学习率最高最低向量learningrate学习率algorithm选择传播算法err.fct计算误差函数
act.fct隐藏神经元的传递函数linear.output输出函数是否为线性
neuralnet(formula, data, hidden = 1, threshold = 0.01,
stepmax = 1e+05, rep = 1, startweights = NULL,
learningrate.limit = NULL,
learningrate.factor = list(minus = 0.5, plus = 1.2),
learningrate=NULL, lifesign = "none",
lifesign.step = 1000, algorithm = "rprop+",
err.fct = "sse", act.fct = "logistic",
linear.output = TRUE, exclude = NULL,
constant.weights = NULL, likelihood = FALSE)
预测
normalize <- function(x) {
return((x - min(x)) / (max(x) - min(x)))
}
lapply应用到数据框每一列
concrete_norm <- as.data.frame(lapply(concrete, normalize))
library(neuralnet)
用法
concrete_model <- neuralnet(formula = strength ~ cement + slag +
ash + water + superplastic +
coarseagg + fineagg + age,
data = concrete_train)
neuralnet(target~predictors,data=mydata,hidden=1) #hidden隐藏神经元个数
hidden隐藏神经元个数threshold停止阈值stepmax最大步数rep重复次数startweights起始权重
learningrate.limit学习率最高最低向量learningrate学习率algorithm选择传播算法err.fct计算误差函数
act.fct隐藏神经元的传递函数linear.output输出函数是否为线性
neuralnet(formula, data, hidden = 1, threshold = 0.01,
stepmax = 1e+05, rep = 1, startweights = NULL,
learningrate.limit = NULL,
learningrate.factor = list(minus = 0.5, plus = 1.2),
learningrate=NULL, lifesign = "none",
lifesign.step = 1000, algorithm = "rprop+",
err.fct = "sse", act.fct = "logistic",
linear.output = TRUE, exclude = NULL,
constant.weights = NULL, likelihood = FALSE)
预测
p<-compute(concrete_model, concrete_test[1:8])
concrete <- read.csv("concrete.csv")
str(concrete)
# custom normalization function
normalize <- function(x) {
return((x - min(x)) / (max(x) - min(x)))
}
# apply normalization to entire data frame
concrete_norm <- as.data.frame(lapply(concrete, normalize))
# confirm that the range is now between zero and one
summary(concrete_norm$strength)
# compared to the original minimum and maximum
summary(concrete$strength)
# create training and test data
concrete_train <- concrete_norm[1:773, ]
concrete_test <- concrete_norm[774:1030, ]
## Step 3: Training a model on the data ----
# train the neuralnet model
library(neuralnet)
# simple ANN with only a single hidden neuron
set.seed(12345) # to guarantee repeatable results
concrete_model <- neuralnet(formula = strength ~ cement + slag +
ash + water + superplastic +
coarseagg + fineagg + age,
data = concrete_train)
# visualize the network topology
plot(concrete_model)
## Step 4: Evaluating model performance ----
# obtain model results
model_results <- compute(concrete_model, concrete_test[1:8])
# obtain predicted strength values
predicted_strength <- model_results$net.result
# examine the correlation between predicted and actual values
cor(predicted_strength, concrete_test$strength)
## Step 5: Improving model performance ----
# a more complex neural network topology with 5 hidden neurons
set.seed(12345) # to guarantee repeatable results
concrete_model2 <- neuralnet(strength ~ cement + slag +
ash + water + superplastic +
coarseagg + fineagg + age,
data = concrete_train, hidden = 5)
# plot the network
plot(concrete_model2)
# evaluate the results as we did before
model_results2 <- compute(concrete_model2, concrete_test[1:8])
predicted_strength2 <- model_results2$net.result
cor(predicted_strength2, concrete_test$strength)