Previsão de occorências de cancer de mama utilizando o dataset do UCI: http://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+%28Diagnostic%29
setwd("/home/sandro/Documents/4.projetos_DS/machine_learning/prevendo_cancer_de_mama")
getwd()
## [1] "/home/sandro/Documents/4.projetos_DS/machine_learning/prevendo_cancer_de_mama"
df <- read.csv("dataset.csv", stringsAsFactors = FALSE)
Utilizamos StringsAsFactors para que não sejam identificadas nenhuma variável como tipo fator, por tratar-se de um problema de classificaçao.
View(df)
str(df)
## 'data.frame': 569 obs. of 32 variables:
## $ id : int 87139402 8910251 905520 868871 9012568 906539 925291 87880 862989 89827 ...
## $ diagnosis : chr "B" "B" "B" "B" ...
## $ radius_mean : num 12.3 10.6 11 11.3 15.2 ...
## $ texture_mean : num 12.4 18.9 16.8 13.4 13.2 ...
## $ perimeter_mean : num 78.8 69.3 70.9 73 97.7 ...
## $ area_mean : num 464 346 373 385 712 ...
## $ smoothness_mean : num 0.1028 0.0969 0.1077 0.1164 0.0796 ...
## $ compactness_mean : num 0.0698 0.1147 0.078 0.1136 0.0693 ...
## $ concavity_mean : num 0.0399 0.0639 0.0305 0.0464 0.0339 ...
## $ points_mean : num 0.037 0.0264 0.0248 0.048 0.0266 ...
## $ symmetry_mean : num 0.196 0.192 0.171 0.177 0.172 ...
## $ dimension_mean : num 0.0595 0.0649 0.0634 0.0607 0.0554 ...
## $ radius_se : num 0.236 0.451 0.197 0.338 0.178 ...
## $ texture_se : num 0.666 1.197 1.387 1.343 0.412 ...
## $ perimeter_se : num 1.67 3.43 1.34 1.85 1.34 ...
## $ area_se : num 17.4 27.1 13.5 26.3 17.7 ...
## $ smoothness_se : num 0.00805 0.00747 0.00516 0.01127 0.00501 ...
## $ compactness_se : num 0.0118 0.03581 0.00936 0.03498 0.01485 ...
## $ concavity_se : num 0.0168 0.0335 0.0106 0.0219 0.0155 ...
## $ points_se : num 0.01241 0.01365 0.00748 0.01965 0.00915 ...
## $ symmetry_se : num 0.0192 0.035 0.0172 0.0158 0.0165 ...
## $ dimension_se : num 0.00225 0.00332 0.0022 0.00344 0.00177 ...
## $ radius_worst : num 13.5 11.9 12.4 11.9 16.2 ...
## $ texture_worst : num 15.6 22.9 26.4 15.8 15.7 ...
## $ perimeter_worst : num 87 78.3 79.9 76.5 104.5 ...
## $ area_worst : num 549 425 471 434 819 ...
## $ smoothness_worst : num 0.139 0.121 0.137 0.137 0.113 ...
## $ compactness_worst: num 0.127 0.252 0.148 0.182 0.174 ...
## $ concavity_worst : num 0.1242 0.1916 0.1067 0.0867 0.1362 ...
## $ points_worst : num 0.0939 0.0793 0.0743 0.0861 0.0818 ...
## $ symmetry_worst : num 0.283 0.294 0.3 0.21 0.249 ...
## $ dimension_worst : num 0.0677 0.0759 0.0788 0.0678 0.0677 ...
head(df)
## id diagnosis radius_mean texture_mean perimeter_mean area_mean
## 1 87139402 B 12.32 12.39 78.85 464.1
## 2 8910251 B 10.60 18.95 69.28 346.4
## 3 905520 B 11.04 16.83 70.92 373.2
## 4 868871 B 11.28 13.39 73.00 384.8
## 5 9012568 B 15.19 13.21 97.65 711.8
## 6 906539 B 11.57 19.04 74.20 409.7
## smoothness_mean compactness_mean concavity_mean points_mean symmetry_mean
## 1 0.10280 0.06981 0.03987 0.03700 0.1959
## 2 0.09688 0.11470 0.06387 0.02642 0.1922
## 3 0.10770 0.07804 0.03046 0.02480 0.1714
## 4 0.11640 0.11360 0.04635 0.04796 0.1771
## 5 0.07963 0.06934 0.03393 0.02657 0.1721
## 6 0.08546 0.07722 0.05485 0.01428 0.2031
## dimension_mean radius_se texture_se perimeter_se area_se smoothness_se
## 1 0.05955 0.2360 0.6656 1.670 17.43 0.008045
## 2 0.06491 0.4505 1.1970 3.430 27.10 0.007470
## 3 0.06340 0.1967 1.3870 1.342 13.54 0.005158
## 4 0.06072 0.3384 1.3430 1.851 26.33 0.011270
## 5 0.05544 0.1783 0.4125 1.338 17.72 0.005012
## 6 0.06267 0.2864 1.4400 2.206 20.30 0.007278
## compactness_se concavity_se points_se symmetry_se dimension_se radius_worst
## 1 0.011800 0.01683 0.012410 0.01924 0.002248 13.50
## 2 0.035810 0.03354 0.013650 0.03504 0.003318 11.88
## 3 0.009355 0.01056 0.007483 0.01718 0.002198 12.41
## 4 0.034980 0.02187 0.019650 0.01580 0.003442 11.92
## 5 0.014850 0.01551 0.009155 0.01647 0.001767 16.20
## 6 0.020470 0.04447 0.008799 0.01868 0.003339 13.07
## texture_worst perimeter_worst area_worst smoothness_worst compactness_worst
## 1 15.64 86.97 549.1 0.1385 0.1266
## 2 22.94 78.28 424.8 0.1213 0.2515
## 3 26.44 79.93 471.4 0.1369 0.1482
## 4 15.77 76.53 434.0 0.1367 0.1822
## 5 15.73 104.50 819.1 0.1126 0.1737
## 6 26.98 86.43 520.5 0.1249 0.1937
## concavity_worst points_worst symmetry_worst dimension_worst
## 1 0.12420 0.09391 0.2827 0.06771
## 2 0.19160 0.07926 0.2940 0.07587
## 3 0.10670 0.07431 0.2998 0.07881
## 4 0.08669 0.08611 0.2102 0.06784
## 5 0.13620 0.08178 0.2487 0.06766
## 6 0.25600 0.06664 0.3035 0.08284
summary(df)
## id diagnosis radius_mean texture_mean
## Min. : 8670 Length:569 Min. : 6.981 Min. : 9.71
## 1st Qu.: 869218 Class :character 1st Qu.:11.700 1st Qu.:16.17
## Median : 906024 Mode :character Median :13.370 Median :18.84
## Mean : 30371831 Mean :14.127 Mean :19.29
## 3rd Qu.: 8813129 3rd Qu.:15.780 3rd Qu.:21.80
## Max. :911320502 Max. :28.110 Max. :39.28
## perimeter_mean area_mean smoothness_mean compactness_mean
## Min. : 43.79 Min. : 143.5 Min. :0.05263 Min. :0.01938
## 1st Qu.: 75.17 1st Qu.: 420.3 1st Qu.:0.08637 1st Qu.:0.06492
## Median : 86.24 Median : 551.1 Median :0.09587 Median :0.09263
## Mean : 91.97 Mean : 654.9 Mean :0.09636 Mean :0.10434
## 3rd Qu.:104.10 3rd Qu.: 782.7 3rd Qu.:0.10530 3rd Qu.:0.13040
## Max. :188.50 Max. :2501.0 Max. :0.16340 Max. :0.34540
## concavity_mean points_mean symmetry_mean dimension_mean
## Min. :0.00000 Min. :0.00000 Min. :0.1060 Min. :0.04996
## 1st Qu.:0.02956 1st Qu.:0.02031 1st Qu.:0.1619 1st Qu.:0.05770
## Median :0.06154 Median :0.03350 Median :0.1792 Median :0.06154
## Mean :0.08880 Mean :0.04892 Mean :0.1812 Mean :0.06280
## 3rd Qu.:0.13070 3rd Qu.:0.07400 3rd Qu.:0.1957 3rd Qu.:0.06612
## Max. :0.42680 Max. :0.20120 Max. :0.3040 Max. :0.09744
## radius_se texture_se perimeter_se area_se
## Min. :0.1115 Min. :0.3602 Min. : 0.757 Min. : 6.802
## 1st Qu.:0.2324 1st Qu.:0.8339 1st Qu.: 1.606 1st Qu.: 17.850
## Median :0.3242 Median :1.1080 Median : 2.287 Median : 24.530
## Mean :0.4052 Mean :1.2169 Mean : 2.866 Mean : 40.337
## 3rd Qu.:0.4789 3rd Qu.:1.4740 3rd Qu.: 3.357 3rd Qu.: 45.190
## Max. :2.8730 Max. :4.8850 Max. :21.980 Max. :542.200
## smoothness_se compactness_se concavity_se points_se
## Min. :0.001713 Min. :0.002252 Min. :0.00000 Min. :0.000000
## 1st Qu.:0.005169 1st Qu.:0.013080 1st Qu.:0.01509 1st Qu.:0.007638
## Median :0.006380 Median :0.020450 Median :0.02589 Median :0.010930
## Mean :0.007041 Mean :0.025478 Mean :0.03189 Mean :0.011796
## 3rd Qu.:0.008146 3rd Qu.:0.032450 3rd Qu.:0.04205 3rd Qu.:0.014710
## Max. :0.031130 Max. :0.135400 Max. :0.39600 Max. :0.052790
## symmetry_se dimension_se radius_worst texture_worst
## Min. :0.007882 Min. :0.0008948 Min. : 7.93 Min. :12.02
## 1st Qu.:0.015160 1st Qu.:0.0022480 1st Qu.:13.01 1st Qu.:21.08
## Median :0.018730 Median :0.0031870 Median :14.97 Median :25.41
## Mean :0.020542 Mean :0.0037949 Mean :16.27 Mean :25.68
## 3rd Qu.:0.023480 3rd Qu.:0.0045580 3rd Qu.:18.79 3rd Qu.:29.72
## Max. :0.078950 Max. :0.0298400 Max. :36.04 Max. :49.54
## perimeter_worst area_worst smoothness_worst compactness_worst
## Min. : 50.41 Min. : 185.2 Min. :0.07117 Min. :0.02729
## 1st Qu.: 84.11 1st Qu.: 515.3 1st Qu.:0.11660 1st Qu.:0.14720
## Median : 97.66 Median : 686.5 Median :0.13130 Median :0.21190
## Mean :107.26 Mean : 880.6 Mean :0.13237 Mean :0.25427
## 3rd Qu.:125.40 3rd Qu.:1084.0 3rd Qu.:0.14600 3rd Qu.:0.33910
## Max. :251.20 Max. :4254.0 Max. :0.22260 Max. :1.05800
## concavity_worst points_worst symmetry_worst dimension_worst
## Min. :0.0000 Min. :0.00000 Min. :0.1565 Min. :0.05504
## 1st Qu.:0.1145 1st Qu.:0.06493 1st Qu.:0.2504 1st Qu.:0.07146
## Median :0.2267 Median :0.09993 Median :0.2822 Median :0.08004
## Mean :0.2722 Mean :0.11461 Mean :0.2901 Mean :0.08395
## 3rd Qu.:0.3829 3rd Qu.:0.16140 3rd Qu.:0.3179 3rd Qu.:0.09208
## Max. :1.2520 Max. :0.29100 Max. :0.6638 Max. :0.20750
Vamos dropar a coluna id, e em seguida faremos a normalização:
df$id <- NULL
df$diagnosis <- as.factor(df$diagnosis)
normalizar <- function(x){
return((x-min(x))/ (max(x)-min(x)))
}
Normalização:
df2 <- as.data.frame(lapply(df[-1], normalizar))
df3 <- as.data.frame(scale(df[-1]))
round(prop.table(table(df$diagnosis))*100, digits =2 )
##
## B M
## 62.74 37.26
treino <- df2[1:469, ]
teste <- df2[470:569, ]
Criando o Labels e criando o modelo de KNN
treino_labels <- df[1:469, 1]
teste_labels <- df[470:569, 1]
length(treino_labels)
## [1] 469
length(teste_labels)
## [1] 100
modelo1 <- knn(train = treino,
test = teste,
cl = treino_labels,
k = 21)
summary(modelo1)
## B M
## 63 37
CrossTable(x = teste_labels, y = modelo1, prop.chisq = FALSE)
##
##
## Cell Contents
## |-------------------------|
## | N |
## | N / Row Total |
## | N / Col Total |
## | N / Table Total |
## |-------------------------|
##
##
## Total Observations in Table: 100
##
##
## | modelo1
## teste_labels | B | M | Row Total |
## -------------|-----------|-----------|-----------|
## B | 61 | 0 | 61 |
## | 1.000 | 0.000 | 0.610 |
## | 0.968 | 0.000 | |
## | 0.610 | 0.000 | |
## -------------|-----------|-----------|-----------|
## M | 2 | 37 | 39 |
## | 0.051 | 0.949 | 0.390 |
## | 0.032 | 1.000 | |
## | 0.020 | 0.370 | |
## -------------|-----------|-----------|-----------|
## Column Total | 63 | 37 | 100 |
## | 0.630 | 0.370 | |
## -------------|-----------|-----------|-----------|
##
##
Podemos identificar que, para todos os dados onde foram observados “B”, nosso modelo errou 0.
Já para os dados onde foram observados “M”, nosso modelo teve 2 erros.
Taxa de acerto do modelo1: 98%
treino2 <- df3[1:469, ]
teste2 <- df3[470:569, ]
treino_labels <- df[1:469, 1]
teste_labels <- df[470:569, 1]
length(treino_labels)
## [1] 469
length(teste_labels)
## [1] 100
modelo2 <- knn(train = treino2,
test = teste2,
cl = treino_labels,
k = 21)
summary(modelo2)
## B M
## 66 34
CrossTable(x = teste_labels, y = modelo2, prop.chisq = FALSE)
##
##
## Cell Contents
## |-------------------------|
## | N |
## | N / Row Total |
## | N / Col Total |
## | N / Table Total |
## |-------------------------|
##
##
## Total Observations in Table: 100
##
##
## | modelo2
## teste_labels | B | M | Row Total |
## -------------|-----------|-----------|-----------|
## B | 61 | 0 | 61 |
## | 1.000 | 0.000 | 0.610 |
## | 0.924 | 0.000 | |
## | 0.610 | 0.000 | |
## -------------|-----------|-----------|-----------|
## M | 5 | 34 | 39 |
## | 0.128 | 0.872 | 0.390 |
## | 0.076 | 1.000 | |
## | 0.050 | 0.340 | |
## -------------|-----------|-----------|-----------|
## Column Total | 66 | 34 | 100 |
## | 0.660 | 0.340 | |
## -------------|-----------|-----------|-----------|
##
##
Podemos identificar que, para todos os dados foram observador “B”, nosso modelo errou 0.
Para os dados observados “M”, nosso modelo errou 5.
Taxa de acerto do modelo 2: 95%.