-
Notifications
You must be signed in to change notification settings - Fork 2
/
Neural Networks 50_Startups.R
87 lines (61 loc) · 2.15 KB
/
Neural Networks 50_Startups.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
library(neuralnet)
library(nnet)
library(NeuralNetTools)
library(plyr)
Startups <- read.csv(file.choose())
View(Startups)
class(Startups)
Startups$State <- as.numeric(revalue(Startups$State, c("New York"="0", "California"="1","Florida"="2")))
str(Startups)
Startups <- as.data.frame(Startups)
attach(Startups)
plot(R.D.Spend, Profit)
plot(Administration, Profit)
plot(Marketing.Spend, Profit)
plot(State, Profit)
#windows()
pairs(Startups)
cor(Startups)
summary(Startups)
normalize<-function(x){
return ( (x-min(x))/(max(x)-min(x)))
}
Startups_norm<-as.data.frame(lapply(Startups,FUN=normalize))
summary(Startups_norm$Profit) # Normalized form of profit
summary(Startups$profit)
set.seed(123)
ind <- sample(2, nrow(Startups_norm), replace = TRUE, prob = c(0.7,0.3))
Startups_train <- Startups_norm[ind==1,]
startups_test <- Startups_norm[ind==2,]
# training model
startups_model <- neuralnet(Profit~R.D.Spend+Administration
+Marketing.Spend+State,data = Startups_train)
str(startups_model)
plot(startups_model, rep = "best")
summary(startups_model)
par(mar = numeric(4), family = 'serif')
plotnet(startups_model, alpha = 0.6)
set.seed(12323)
model_results <- compute(startups_model,startups_test[1:4])
predicted_profit <- model_results$net.result
# test model
cor(predicted_profit,startups_test$Profit)
str_max <- max(Startups$Profit)
str_min <- min(Startups$Profit)
unnormalize <- function(x, min, max) {
return( (max - min)*x + min )
}
ActualProfit_pred <- unnormalize(predicted_profit,str_min,str_max)
head(ActualProfit_pred)
set.seed(12345)
Startups_model2 <- neuralnet(Profit~R.D.Spend+Administration
+Marketing.Spend+State,data = Startups_train,
hidden = 2)
plot(Startups_model2 ,rep = "best")
summary(Startups_model2)
model_results2<-compute(Startups_model2,startups_test[1:4])
predicted_Profit2<-model_results2$net.result
cor(predicted_Profit2,startups_test$Profit)
plot(predicted_Profit2,startups_test$Profit)
par(mar = numeric(4), family = 'serif')
plotnet(Startups_model2, alpha = 0.6)