-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project MarkDown.Rmd
292 lines (200 loc) · 8.01 KB
/
Project MarkDown.Rmd
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
---
title: "Breast Cancer Detection Using Classification Models"
author: "Sumanth Donthula(A20519856) and Ram Vaka (A20481446)"
date: "2022-12-01"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Project Description:
The project is a classifier problem. The Data set contains the different dependent features to predict the Breast Cancer whether it is Benign or Malignant. The Data set is taken from the study done by University of California at Irvine from there ML Data Set Repository.
Reading the Data File
```{r}
Data=read.table("data.csv", header = TRUE, sep = ",")
```
Checking if any null records are present in Data Set
```{r}
sum(is.na(Data))
```
The dataset is almost equally distributed for both Malignant and Benign cases
```{r}
table(Data$diagnosis)
```
Displaying the Data Set
```{r}
head(Data)
```
Displaying the classifier data with one of the feature. The graph displays radius_mean, a feature from our data set to visualize the classifier problem.
```{r}
library(ggplot2)
# Scatter plot by group
ggplot(Data, aes(x = diagnosis, y = radius_mean, color = diagnosis)) +
geom_point()
```
We can see tha there is a colummn called id in our dataset which we dont require for trainig our model. Droppinf the column id. From the data we can see that the records of dependent variable contains mainly mean, standard error and worst features.
Exploratory Data Analysis :
```{r}
summary(Data)
table(Data$diagnosis)
Data=subset(Data,select=(-1))
```
Grouping the dependent variables into the groups for easily analyzing them.
```{r}
meanIdx = grepl('mean', colnames(Data))
seIdx = grepl('se',colnames(Data))
worstIdx= grepl('worst',colnames(Data))
```
Plotting the Histograms and observing the distribution for mean group data. We can see that some of the features like symmetric mean, smoothness mean, texture mean are uniform. Othere features a little skewed distributions.
```{r}
meanData=Data[meanIdx]
par(mfrow=c(3,5))
for(i in 1:ncol(meanData)) { # for-loop over columns
set.seed(seed = 49078)
x <- meanData[ , i]
hist(main="Histogram", ylab="Distribution",xlab=colnames(meanData)[i],x = x, freq = FALSE)
lines(x = density(x = x), col = "blue")
}
```
Plotting the Histograms and observing the distribution for standard error group data. Almost all the distributions of this group is appearing skewed.
```{r}
seData=Data[seIdx]
par(mfrow=c(3,5))
for(i in 1:ncol(seData)) { # for-loop over columns
set.seed(seed = 49078)
x <- seData[ , i]
hist(main="Histogram", ylab="Distribution",xlab=colnames(seData)[i],x = x, freq = FALSE)
lines(x = density(x = x), col = "blue")
}
```
Plotting the Histograms and observing the distribution for worst group data. Almost all the distributions of this group is appearing skewed.
```{r}
worstData=Data[worstIdx]
par(mfrow=c(3,5))
for(i in 1:ncol(worstData)) { # for-loop over columns
set.seed(seed = 49078)
x <- worstData[ , i]
hist(main="Histogram", ylab="Distribution",xlab=colnames(worstData)[i],x = x, freq = FALSE)
lines(x = density(x = x), col = "blue")
}
```
Plotting boxplots to see the outliers in mean data. Most of the dependent variables has outliers.
```{r}
par(mfrow=c(3,5))
for(i in 1:ncol(meanData)) { # for-loop over columns
set.seed(seed = 49078)
x <- meanData[ , i]
boxplot(xlab=colnames(meanData)[i],x = x, freq = FALSE)
}
```
Plotting boxplots to see the outliers in se data. Most of the dependent variables has outliers.
```{r}
par(mfrow=c(3,5))
for(i in 1:ncol(seData)) { # for-loop over columns
set.seed(seed = 49078)
x <- seData[ , i]
boxplot(xlab=colnames(seData)[i],x = x, freq = FALSE)
}
```
Plotting boxplots to see the outliers in worst data. Most of the dependent variables has outliers.
```{r}
par(mfrow=c(3,5))
for(i in 1:ncol(worstData)) { # for-loop over columns
set.seed(seed = 49078)
x <- worstData[ , i]
boxplot(xlab=colnames(worstData)[i],x = x, freq = FALSE)
}
```
Checking the correlation of data set with the Dependent variable diagnosis we can see only few columns are correlated with diagnosis. We will use this columns to build our model in classification.
```{r}
Data$diagnosis <- ifelse(Data$diagnosis=='M', 1, 0)
library(reshape2)
df=abs(cor(Data[-1:-2],Data[2]))>0.7
df=melt(df)
df[df$value==TRUE,-2]
df$var2==TRUE
```
Dividing and splitting the data into train and test data sets
```{r}
#+ perimeter_mean+ area_worst+ radius_mean
library(caTools)
# Splitting dataset
split <- sample.split(Data, SplitRatio = 0.8)
split
train_reg <- subset(Data, split == "TRUE")
test_reg <- subset(Data, split == "FALSE")
```
Implementing SVM Classifier
```{r}
library(e1071)
classifier = svm(diagnosis~concave.points_worst+ perimeter_worst+ concave.points_mean+ radius_worst+ area_mean+ perimeter_mean+ area_worst+ radius_mean,data = train_reg,type = 'C-classification',kernel = 'linear')
Y_predicion = predict(classifier, newdata = test_reg)
ConMat=table(test_reg$diagnosis, Y_predicion)
print("confusionMatrix")
ConMat
missing_classerr <- mean(Y_predicion != test_reg$diagnosis)
print(paste('Accuracy =', 1-missing_classerr))
plot(classifier, train_reg,area_worst~radius_worst)
```
Implementing KNN Classifier
```{r}
library(class)
knnModel=knn(train=train_reg, test=test_reg, cl=train_reg$diagnosis, k=21)
# Notice that I am only getting 2 dimensions
plot_predictions=data.frame(test_reg$diagnosis
,test_reg$concave.points_worst
,test_reg$perimeter_worst
,test_reg$concave.points_mean
,test_reg$radius_worst
,test_reg$area_mean
,test_reg$perimeter_mean
,test_reg$area_worst
,test_reg$radius_mean,predicted=knnModel)
colnames(plot_predictions) <- c("diagnosis",
"concave.points_worst",
"perimeter_worst",
"concave.points_mean",
"radius_worst",
"area_mean",
"perimeter_mean",
"area_worst",
"radius_mean",
'predicted')
# Visualize the KNN algorithm results.
library(ggplot2)
ggplot(plot_predictions, aes(area_mean, radius_mean, color = predicted, fill = predicted)) +
geom_point(size = 5) +
geom_text(aes(label=diagnosis),hjust=1, vjust=2) +
ggtitle("Knn Visualization") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(legend.position = "none")
confMatrix=table(test_reg$diagnosis, knnModel)
print("confusionMatrix")
confMatrix
missing_classerr <- mean(test_reg$diagnosis != knnModel)
print(paste('Accuracy =', 1-missing_classerr))
```
Implementing logistic regression
```{r}
# Training model
logistic_model <- glm(diagnosis~concave.points_worst+ perimeter_worst+ concave.points_mean
+ radius_worst+ area_mean+ perimeter_mean+ area_worst+ radius_mean
,family=binomial("logit"),data = train_reg)
predictData=predict(logistic_model,data.frame(concave.points_worst=test_reg$concave.points_worst,perimeter_worst=test_reg$perimeter_worst,concave.points_mean=test_reg$concave.points_mean,radius_worst=test_reg$radius_worst,perimeter_mean=test_reg$perimeter_mean,area_worst=test_reg$area_worst,radius_mean=test_reg$radius_mean,area_mean=test_reg$area_mean),type="response")
library(InformationValue)
origTest=test_reg$diagnosis
#find optimal cutoff probability to use to maximize accuracy
optimal <- optimalCutoff(test_reg, predictData)[1]
optimal
predictDif <- ifelse(predictData>optimal, 0, 1)
library(ggplot2)
print("confusionMatrix")
table(origTest, predictDif)
missing_classerr <- mean(predictDif != origTest)
print(paste('Accuracy =', 1 - missing_classerr))
ggplot(Data, aes(x=radius_mean, y=diagnosis)) + geom_point() +
stat_smooth(method="glm", color="red", se=FALSE,
method.args = list(family=binomial))
```