R 神经网络的预测功能给出了奇怪的结果
我对R中的机器学习技术和编程都比较陌生,目前我正在尝试将神经网络与我掌握的一些数据相匹配。然而,由此产生的神经网络预测对我来说没有意义。我已经查看了StackOverflow,但找不到解决此问题的方法 我的数据(这是测试集的一部分,训练集的格式相同) 我的代码R 神经网络的预测功能给出了奇怪的结果,r,machine-learning,neural-network,R,Machine Learning,Neural Network,我对R中的机器学习技术和编程都比较陌生,目前我正在尝试将神经网络与我掌握的一些数据相匹配。然而,由此产生的神经网络预测对我来说没有意义。我已经查看了StackOverflow,但找不到解决此问题的方法 我的数据(这是测试集的一部分,训练集的格式相同) 我的代码 resultsnn <- neuralnet(target~monday+tuesday+wednesday+thursday+friday+saturday+independent,data=training,hidden=3,t
resultsnn <- neuralnet(target~monday+tuesday+wednesday+thursday+friday+saturday+independent,data=training,hidden=3,threshold=0.01,linear.output = TRUE)
compute(resultsnn,test[,2:8])$net.result
我还试过什么?
我尝试过没有假人的版本(只包括自变量,这不会改变结果的类型)
我已经创建了一些合成数据并将其用作输入,对于相同的代码,这确实可以正常工作:
#building training set
input_train <- as.data.frame(c(1:100))
output_train <- as.data.frame(c(sqrt((1:100)+1)))
train <- cbind.data.frame(output_train,input_train)
colnames(train) <- c("output","input")
#building test set
input_test <- as.data.frame(c(101:150))
output_test <- as.data.frame(c(sqrt((101:150)+1)))
test <- cbind.data.frame(output_test,input_test)
colnames(test) <- c("output","input")
#NEURALNET PACKAGE
#neural network 3 neurons
res.train <- neuralnet(output~input,data=train,hidden=3,threshold=0.01) #train nn
compute(res.train,test[,2])$net.result #predict using nn on test set
如果您需要任何其他信息,请告诉我!谢谢大家的帮助(:看起来目标和独立之间没有信号。现在忽略工作日,如果你适合线性模型,有梯度和没有梯度:
# a linear model looking at response with indepedent (with intercept)
lm1 <- lm(target ~ indepedent, data = training)
lm1
#
# Call:
# lm(formula = target ~ indepedent, data = training)
#
# Coefficients:
# (Intercept) indepedent
# 206.37312594 0.04853823
# intercept only
lm0 <- lm(target ~ 1, data = training)
lm0
#
# Call:
# lm(formula = target ~ 1, data = training)
#
# Coefficients:
# (Intercept)
# 310.0769
# two models of the data equivalent to possible outcomes
plot(target ~ indepedent, data = training)
lines(fitted(lm1) ~ indepedent, data = training, lty = 2)
lines(fitted(lm0) ~ indepedent, data = training, col = 2)
这也是机器学习方法告诉你的。简单模型预测每个独立值的目标值。添加工作日变量显然不能改善这一点
您看到了玩具示例的预测,因为响应中有一个强信号。当我添加更多的协变量时,神经网络是否可能工作(换句话说:我唯一的问题是当前的协变量集对目标没有任何解释力)?是的,有可能存在解释目标和独立性之间关系的协变量,除非你没有包括它们。如果你对这些变量之间的关系有任何机械上的理解,那可能建议尝试一个变量。如果你只是希望存在一种关系,那么就尝试很多变量;一旦你有了candidate解释变量,查看测试数据中是否存在该变量。在查看测试之前,尝试完成对训练数据的探索!
#building training set
input_train <- as.data.frame(c(1:100))
output_train <- as.data.frame(c(sqrt((1:100)+1)))
train <- cbind.data.frame(output_train,input_train)
colnames(train) <- c("output","input")
#building test set
input_test <- as.data.frame(c(101:150))
output_test <- as.data.frame(c(sqrt((101:150)+1)))
test <- cbind.data.frame(output_test,input_test)
colnames(test) <- c("output","input")
#NEURALNET PACKAGE
#neural network 3 neurons
res.train <- neuralnet(output~input,data=train,hidden=3,threshold=0.01) #train nn
compute(res.train,test[,2])$net.result #predict using nn on test set
str(test)
'data.frame': 82 obs. of 8 variables:
$ target : int 277 204 309 487 289 411 182 296 212 308 ...
$ monday : int 1 0 0 0 0 0 0 1 0 0 ...
$ tuesday : int 0 1 0 0 0 0 0 0 1 0 ...
$ wednesday : int 0 0 1 0 0 0 0 0 0 1 ...
$ thursday : int 0 0 0 1 0 0 0 0 0 0 ...
$ friday : int 0 0 0 0 1 0 0 0 0 0 ...
$ saturday : int 0 0 0 0 0 1 0 0 0 0 ...
$ independent: int 3317 1942 2346 2394 2023 1886 1750 1749 1810 2021 ...
str(training)
'data.frame': 397 obs. of 8 variables:
$ target : int 1079 1164 1069 1038 629 412 873 790 904 898 ...
$ monday : int 0 0 0 0 0 0 1 0 0 0 ...
$ tuesday : int 1 0 0 0 0 0 0 1 0 0 ...
$ wednesday : int 0 1 0 0 0 0 0 0 1 0 ...
$ thursday : int 0 0 1 0 0 0 0 0 0 1 ...
$ friday : int 0 0 0 1 0 0 0 0 0 0 ...
$ saturday : int 0 0 0 0 1 0 0 0 0 0 ...
$ independent: int 2249 2381 4185 2899 2387 2145 2933 2617 2378 3569 ...
# a linear model looking at response with indepedent (with intercept)
lm1 <- lm(target ~ indepedent, data = training)
lm1
#
# Call:
# lm(formula = target ~ indepedent, data = training)
#
# Coefficients:
# (Intercept) indepedent
# 206.37312594 0.04853823
# intercept only
lm0 <- lm(target ~ 1, data = training)
lm0
#
# Call:
# lm(formula = target ~ 1, data = training)
#
# Coefficients:
# (Intercept)
# 310.0769
# two models of the data equivalent to possible outcomes
plot(target ~ indepedent, data = training)
lines(fitted(lm1) ~ indepedent, data = training, lty = 2)
lines(fitted(lm0) ~ indepedent, data = training, col = 2)
# test which model is better
# large p-value suggests we're happy accepting the simple model
anova(lm0, lm1)
# Analysis of Variance Table
#
# Model 1: target ~ 1
# Model 2: target ~ indepedent
# Res.Df RSS Df Sum of Sq F Pr(>F)
# 1 12 86990.923
# 2 11 81792.165 1 5198.7582 0.69917 0.42086
head(fitted(lm0))
# 428 429 430 431 432 433
# 310.0769231 310.0769231 310.0769231 310.0769231 310.0769231 310.0769231