Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 3 - Classification/Logistic Regression/logistic_regression.R
1009 views
1
# Logistic Regression
2
3
# Importing the dataset
4
dataset = read.csv('Social_Network_Ads.csv')
5
dataset = dataset[3:5]
6
7
# Encoding the target feature as factor
8
dataset$Purchased = factor(dataset$Purchased, levels = c(0, 1))
9
10
# Splitting the dataset into the Training set and Test set
11
# install.packages('caTools')
12
library(caTools)
13
set.seed(123)
14
split = sample.split(dataset$Purchased, SplitRatio = 0.75)
15
training_set = subset(dataset, split == TRUE)
16
test_set = subset(dataset, split == FALSE)
17
18
# Feature Scaling
19
training_set[-3] = scale(training_set[-3])
20
test_set[-3] = scale(test_set[-3])
21
22
# Fitting Logistic Regression to the Training set
23
classifier = glm(formula = Purchased ~ .,
24
family = binomial,
25
data = training_set)
26
27
# Predicting the Test set results
28
prob_pred = predict(classifier, type = 'response', newdata = test_set[-3])
29
y_pred = ifelse(prob_pred > 0.5, 1, 0)
30
31
# Making the Confusion Matrix
32
cm = table(test_set[, 3], y_pred > 0.5)
33
34
# Visualising the Training set results
35
library(ElemStatLearn)
36
set = training_set
37
X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)
38
X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)
39
grid_set = expand.grid(X1, X2)
40
colnames(grid_set) = c('Age', 'EstimatedSalary')
41
prob_set = predict(classifier, type = 'response', newdata = grid_set)
42
y_grid = ifelse(prob_set > 0.5, 1, 0)
43
plot(set[, -3],
44
main = 'Logistic Regression (Training set)',
45
xlab = 'Age', ylab = 'Estimated Salary',
46
xlim = range(X1), ylim = range(X2))
47
contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
48
points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'springgreen3', 'tomato'))
49
points(set, pch = 21, bg = ifelse(set[, 3] == 1, 'green4', 'red3'))
50
51
# Visualising the Test set results
52
library(ElemStatLearn)
53
set = test_set
54
X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01)
55
X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01)
56
grid_set = expand.grid(X1, X2)
57
colnames(grid_set) = c('Age', 'EstimatedSalary')
58
prob_set = predict(classifier, type = 'response', newdata = grid_set)
59
y_grid = ifelse(prob_set > 0.5, 1, 0)
60
plot(set[, -3],
61
main = 'Logistic Regression (Test set)',
62
xlab = 'Age', ylab = 'Estimated Salary',
63
xlim = range(X1), ylim = range(X2))
64
contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE)
65
points(grid_set, pch = '.', col = ifelse(y_grid == 1, 'springgreen3', 'tomato'))
66
points(set, pch = 21, bg = ifelse(set[, 3] == 1, 'green4', 'red3'))
67