-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassification.R
More file actions
226 lines (189 loc) · 7.11 KB
/
Classification.R
File metadata and controls
226 lines (189 loc) · 7.11 KB
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
225
226
'
Building, Testing, and examining Machine Learning Classification Models
'
'
=== === ===
Data Preprocessing
=== === ===
'
#Import Data
library('readr')
dataset <- read_csv('medexpense_data.csv')
#Change Smoke and Gender into binary values
SMOKER<-ifelse(dataset$smoker=="yes", 1, 0)
GENDER<-ifelse(dataset$gender=="male", 1, 0)
dataset$smoker <- SMOKER
dataset$gender <- GENDER
#Determine features with largest correlation
install.packages("corrplot")
library(corrplot)
dataset.cor = cor(dataset)
corrplot(dataset.cor)
df = data.frame(dataset$medical_expenses, dataset$bmi, dataset$smoker)
#Encoding Target Feature
df$dataset.smoker = factor(df$dataset.smoker, levels = c(0,1))
'
=== === ===
Splitting Data and Feature Scaling
=== === ===
'
#Splitting Data into training and test data (25/75)
install.packages('caTools')
library(caTools)
set.seed(123)
split= sample.split(df$dataset.smoker, SplitRatio = 0.80)
training_set = subset(df, split==TRUE)
test_set = subset(df, split==FALSE)
# Feature Scaling
training_set[, 1:2] = scale(training_set[, 1:2])
test_set[, 1:2] = scale(test_set[, 1:2])
'
=== === ===
Training
=== === ===
'
install.packages('e1071')
install.packages('rpart')
install.packages('randomForest')
library(e1071)
library(rpart)
library(randomForest)
library(class)
#Issues with e1071: Download in Anaconda Through Environments r-e1071
#Logistic Regression
#Fitting Logistic Regression to training set and predicting results
Logistic_Classifier = glm(formula = training_set$dataset.smoker ~., family = binomial, data = training_set)
#KNN
#Fitting KNN to training set and predicting results
KNN_Classifier = knn(train = training_set[,-3], test = test_set[, -3], cl = training_set[,3], k = 10)
#SVM
#Fitting SVM to Training Set
SVM_Classifier = svm(formula = training_set$dataset.smoker ~ ., data = training_set, type = 'C-classification', kernel = 'linear')
#Kernel SVM
#Fitting Kernel SVM to Training Set
KSVM_Classifier= svm(formula = training_set$dataset.smoker ~ ., data = training_set, type = 'C-classification', kernel = 'radial')
#Naive Bayes
#Fitting Naive Bayes to Training Set
NB_Classifier= naiveBayes(x = training_set[-3], y = training_set$dataset.smoker)
#Decision Tree
#Fitting Decision Tree to Training Set
DT_Classifier = rpart(formula = training_set$dataset.smoker ~ ., data = training_set)
#Random Forest
#Fitting Decision Tree to Training Set
RF_Classifier = randomForest(x = training_set[-3], y = training_set$dataset.smoker, ntree = 100)
'
=== === ===
Model Testing
=== === ===
'
#Logistic Regression
prob_pred = predict(Logistic_Classifier, type = 'response', newdata = test_set[, -3])
y_pred_LR = ifelse(prob_pred > 0.5, 1, 0)
conf_matrix = table(test_set[,3], y_pred_LR)
conf_matrix
fourfoldplot(conf_matrix)
#KNN
y_pred_KNN = KNN_Classifier
conf_matrix = table(test_set[,3], y_pred_KNN)
conf_matrix
fourfoldplot(conf_matrix)
#SVM
y_pred_SVM = predict(SVM_Classifier, newdata = test_set[, -3])
conf_matrix = table(test_set[,3], y_pred_SVM)
conf_matrix
fourfoldplot(conf_matrix)
#K SVM
y_pred_KSVM = predict(KSVM_Classifier, newdata = test_set[, -3])
conf_matrix = table(test_set[,3], y_pred_KSVM)
conf_matrix
fourfoldplot(conf_matrix)
#Naive Bayes
y_pred_NB = predict(NB_Classifier, newdata = test_set[, -3])
conf_matrix = table(test_set[,3], y_pred_NB)
conf_matrix
fourfoldplot(conf_matrix)
#Decision Tree
y_pred_DT = predict(DT_Classifier, newdata = test_set[, -3], type = 'class')
conf_matrix = table(test_set[,3], y_pred_DT)
conf_matrix
fourfoldplot(conf_matrix)
#Random Forest
y_pred_RF = predict(RF_Classifier, newdata = test_set[, -3])
conf_matrix = table(test_set[,3], y_pred_RF)
conf_matrix
fourfoldplot(conf_matrix)
'
=== === ===
Data Visualization
=== === ===
'
install.packages('ElemStatLearn')
library(ElemStatLearn)
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('dataset.medical_expenses', 'dataset.bmi')
#Logistic Regression
classifier = Logistic_Classifier
prob_set = predict(classifier, type = 'response', newdata = grid_set)
y_grid = ifelse(prob_set > 0.5, 1, 0)
plot(set[, -3], main = 'Logistic Regression (Test set)',
xlab = 'Medical Expenses', ylab = 'BMI',
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'))
#KNN
y_grid = knn(train = training_set[,-3], test = grid_set, cl = training_set[,3], k = 10)
plot(set[, -3], main = 'KNN (Test set)',
xlab = 'Medical Expenses', ylab = 'BMI',
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'))
#SVM
classifier = SVM_Classifier
y_grid = predict(classifier, newdata = grid_set)
plot(set[, -3], main = 'SVM (Test set)',
xlab = 'Medical Expenses', ylab = 'BMI',
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'))
#K SVM
classifier = KSVM_Classifier
y_grid = predict(classifier, newdata = grid_set)
plot(set[, -3], main = 'SVM (Test set)',
xlab = 'Medical Expenses', ylab = 'BMI',
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'))
#Naive Bayes
classifier = NB_Classifier
y_grid = predict(classifier, newdata = grid_set)
plot(set[, -3], main = 'Naive Bayes',
xlab = 'Medical Expenses', ylab = 'BMI',
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'))
#Decision Tree
classifier = DT_Classifier
y_grid = predict(classifier, newdata = grid_set, type = 'class')
plot(set[, -3], main = 'Decision Tree (Test set)',
xlab = 'Medical Expenses', ylab = 'BMI',
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'))
#Random Forest
classifier = RF_Classifier
y_grid = predict(classifier, newdata = grid_set)
plot(set[, -3], main = 'Random Forest (Test set)',
xlab = 'Medical Expenses', ylab = 'BMI',
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'))