df <- read.csv("G:\\RStudio\\udemy\\ml\\Machine Learning AZ\\Part 3 - Classification\\Section 20 - Random Forest Classification\\Random_Forest_Classification\\Social_Network_Ads.csv")
head(df)
df <- df[,3:5]
head(df)
# econding the target feature as factor
df$Purchased <- factor(df$Purchased, levels = c(0,1))
library(caTools)
set.seed(1234)
split <- sample.split(df$Purchased, SplitRatio = 0.75)
training_set <- subset(df, split == TRUE)
test_set <- subset(df, split == FALSE)
# Feature Scaling 1 age, 2 is salary
training_set[,1:2] <- scale(training_set[,1:2])
test_set[,1:2] <- scale(test_set[,1:2])
# Create the classifier here
# install.packages("randomForest")
library(randomForest)
classifier <- randomForest(x = training_set[-3],
y = training_set$Purchased,
ntree = 500)
summary(classifier)
Length Class Mode
call 4 -none- call
type 1 -none- character
predicted 300 factor numeric
err.rate 1500 -none- numeric
confusion 6 -none- numeric
votes 600 matrix numeric
oob.times 300 -none- numeric
classes 2 -none- character
importance 2 -none- numeric
importanceSD 0 -none- NULL
localImportance 0 -none- NULL
proximity 0 -none- NULL
ntree 1 -none- numeric
mtry 1 -none- numeric
forest 14 -none- list
y 300 factor numeric
test 0 -none- NULL
inbag 0 -none- NULL
y_pred <- predict(classifier, newdata = test_set[-3])
y_pred
5 15 19 25 29 40 42 43 49 51 54 55 66 74 76 77 78 90 92 100
0 0 1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 1 0
104 106 110 112 120 133 136 137 141 142 143 144 145 153 157 159 164 166 172 174
1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0
179 181 183 184 195 197 202 203 216 218 221 223 228 229 235 238 239 240 242 244
0 0 1 0 0 0 0 1 1 0 0 1 1 0 1 0 1 1 0 1
245 249 250 254 256 258 271 273 275 286 287 290 291 294 301 303 305 309 318 319
0 0 0 1 1 0 1 1 1 0 0 0 1 0 1 1 0 1 0 1
327 330 332 333 338 341 343 351 353 354 357 360 361 363 368 369 373 386 387 394
0 1 1 0 0 1 0 1 0 0 1 0 1 1 1 0 0 1 1 1
Levels: 0 1
# Making the confusion matrix
# [3] refers to the outcome
cm <- table(test_set[,3], y_pred)
cm
y_pred
0 1
0 56 8
1 4 32
# install.packages("ElemStatLearn")
library(ElemStatLearn)
set <- training_set
X1 <- seq(min(set[,1]) - 1, max(set[,1]) + 1, by = 0.01)
X2 <- seq(min(set[,2]) - 1, max(set[,2]) + 1, by = 0.01)
grid_set <- expand.grid(X1, X2)
colnames(grid_set) = c('Age','EstimatedSalary')
y_grid = predict(classifier, newdata = grid_set)
plot(set[,-3],
main = 'Classifier Model (Training Set)',
xlab = 'Age', ylab = 'Estimated Salary',
xlim = range(X1), ylim = range(X2))
contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'springgreen3', 'tomato'))
points(set, pch = 21, bg= ifelse(set[,3] == 1,'green4', 'red3'))
The red region is predicted by the classifier as “Dont buy” The green region is predicted by the classifier as “Buy” The red dots are those people that actually did not buy The green dots are those people that actually bought. The line is the prediction boundary.
set <- test_set
X1 <- seq(min(set[,1]) - 1, max(set[,1]) + 1, by = 0.01)
X2 <- seq(min(set[,2]) - 1, max(set[,2]) + 1, by = 0.01)
grid_set <- expand.grid(X1, X2)
colnames(grid_set) = c('Age','EstimatedSalary')
y_grid = predict(classifier, newdata = grid_set)
plot(set[,-3],
main = 'Classifier Model (Test Set)',
xlab = 'Age', ylab = 'Estimated Salary',
xlim = range(X1), ylim = range(X2))
contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'springgreen3', 'tomato'))
points(set, pch = 21, bg= ifelse(set[,3] == 1,'green4', 'red3'))