-
Notifications
You must be signed in to change notification settings - Fork 1
/
decisionTree.R
224 lines (157 loc) · 7.23 KB
/
decisionTree.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
###############################################################
# #
# Decision Tree for Regression #
# #
###############################################################
# #
# Credit: Dr. Prashant Singh Rana #
# Email : psrana@gmail.com #
# Web : www.psrana.com #
# #
###############################################################
# #
# Train and Test Decision Tree model for Regression #
# #
# This script do the following: #
# 1. Load the Data #
# 2. Partition the data into Train/Test set #
# 3. Train the Decision Tree Model #
# 4. Test #
# 5. Evaluate on : Correlation, Regression, RMSE, Accuracy. #
# 6. Finally Saving the results. #
# #
###############################################################
#--------------------------------------------------------------
# Step 0: Start; Getting the starting time
#--------------------------------------------------------------
cat("\nSTART\n")
startTime = proc.time()[3]
startTime
#--------------------------------------------------------------
# Step 1: Include Library
#--------------------------------------------------------------
cat("\nStep 1: Library Inclusion")
library(rpart)
#--------------------------------------------------------------
# Step 2: Variable Declaration
#--------------------------------------------------------------
cat("\nStep 2: Variable Declaration")
modelName <- "decisionTree"
modelName
InputDataFileName="regressionDataSet.csv"
InputDataFileName
training = 50 # Defining Training Percentage; Testing = 100 - Training
#--------------------------------------------------------------
# Step 3: Data Loading
#--------------------------------------------------------------
cat("\nStep 3: Data Loading")
dataset <- read.csv(InputDataFileName) # Read the datafile
dataset <- dataset[sample(nrow(dataset)),] # Shuffle the data row wise.
head(dataset) # Show Top 6 records
nrow(dataset) # Show number of records
names(dataset) # Show fields names or columns names
#--------------------------------------------------------------
# Step 4: Count total number of observations/rows.
#--------------------------------------------------------------
cat("\nStep 4: Counting dataset")
totalDataset <- nrow(dataset)
totalDataset
#--------------------------------------------------------------
# Step 5: Choose Target variable
#--------------------------------------------------------------
cat("\nStep 5: Choose Target Variable")
target <- names(dataset)[1] # i.e. RMSD
target
#--------------------------------------------------------------
# Step 6: Choose inputs Variables
#--------------------------------------------------------------
cat("\nStep 6: Choose Inputs Variable")
inputs <- setdiff(names(dataset),target)
inputs
length(inputs)
#Feature Selection
#n=4
#inputs <-sample(inputs, n)
#--------------------------------------------------------------
# Step 7: Select Training Data Set
#--------------------------------------------------------------
cat("\nStep 7: Select training dataset")
trainDataset <- dataset[1:(totalDataset * training/100),c(inputs, target)]
head(trainDataset) # Show Top 6 records
nrow(trainDataset) # Show number of train Dataset
#--------------------------------------------------------------
# Step 8: Select Testing Data Set
#--------------------------------------------------------------
cat("\nStep 8: Select testing dataset")
testDataset <- dataset[(totalDataset * training/100):totalDataset,c(inputs, target)]
head(testDataset)
nrow(testDataset)
#--------------------------------------------------------------
# Step 9: Model Building (Training)
#--------------------------------------------------------------
cat("\nStep 9: Model Building -> ", modelName)
formula <- as.formula(paste(target, "~", paste(c(inputs), collapse = "+")))
formula
model <- rpart(formula, trainDataset, parms=list(split="information"), control=rpart.control(usesurrogate=0, maxsurrogate=0))
model
#--------------------------------------------------------------
# Step 10: Prediction (Testing)
#--------------------------------------------------------------
cat("\nStep 10: Prediction using -> ", modelName)
Predicted <- predict(model, testDataset)
head(Predicted)
#--------------------------------------------------------------
# Step 11: Extracting Actual
#--------------------------------------------------------------
cat("\nStep 11: Extracting Actual")
Actual <- as.double(unlist(testDataset[target]))
head(Actual)
#--------------------------------------------------------------
# Step 12: Model Evaluation
#--------------------------------------------------------------
cat("\nStep 12: Model Evaluation")
# Step 12.1: Correlation
r <- cor(Actual,Predicted )
r <- round(r,2)
r
# Step 12.2: RSquare
R <- r * r
R <- round(R,2)
R
# Step 12.3: RMSE
rmse <- mean(abs(Actual-Predicted))
rmse <- round(rmse,2)
rmse
# Step 12.4: Accuracy
accuracy <- mean(abs(Actual-Predicted) <=1)
accuracy <- round(accuracy,4) *100
accuracy
# Step 12.5: Total Time
totalTime = proc.time()[3] - startTime
totalTime
# Step 12.6: Scatter Plot
png(filename=paste(modelName,"-ScatterPlot.png",sep=''))
plot(Actual,Predicted,main=paste("Actual Vs Predicted\n",modelName),xlab="Predicted", ylab="Actual")#, pch=19)
abline(lm(Actual ~ Predicted,),col="White")
dev.off()
# Step 12.7: Save evaluation resut
result <- data.frame(modelName,r,R,rmse,accuracy, totalTime)[1:1,]
result
#--------------------------------------------------------------
# Step 13: Writing to file
#--------------------------------------------------------------
cat("\nStep 13: Writing to file")
# Step 13.1: Writing to file (evaluation result)
write.csv(result, file=paste(modelName,"-Evaluation-Result.csv",sep=''), row.names=FALSE)
# Step 13.2: Writing to file (Actual and Predicted)
write.csv(data.frame(Actual,Predicted), file=paste(modelName,"-ActualPredicted-Result.csv",sep=''), row.names=FALSE)
#--------------------------------------------------------------
# Step 14: Saving the Model
#--------------------------------------------------------------
cat("\nStep 14: Saving the Model ->",modelName)
save.image(file=paste(modelName,"-Model.RData",sep=''))
cat("\nDone")
cat("\nTotal Time Taken: ", totalTime," sec")
#--------------------------------------------------------------
# END
#--------------------------------------------------------------