Kaggle’da bulunan TitanicSpaceship yarışmasına katılınacaktır.
Ad: Samir Heydarov
Soyad: 2017710504196
Kütüphanelerin yüklenmesi:
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(rpart)
library(rpart.plot)
library(caret)
## Loading required package: lattice
##
## Attaching package: 'caret'
##
## The following object is masked from 'package:purrr':
##
## lift
Verinin yüklenmesi:
df = read_csv("C:\\Project\\train.csv")
## Rows: 8693 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): PassengerId, HomePlanet, Cabin, Destination, Name
## dbl (6): Age, RoomService, FoodCourt, ShoppingMall, Spa, VRDeck
## lgl (3): CryoSleep, VIP, Transported
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Çekilen verinin kontrol edilmesi:
head(df)
## # A tibble: 6 × 14
## PassengerId HomePlanet CryoSleep Cabin Destination Age VIP RoomService
## <chr> <chr> <lgl> <chr> <chr> <dbl> <lgl> <dbl>
## 1 0001_01 Europa FALSE B/0/P TRAPPIST-1e 39 FALSE 0
## 2 0002_01 Earth FALSE F/0/S TRAPPIST-1e 24 FALSE 109
## 3 0003_01 Europa FALSE A/0/S TRAPPIST-1e 58 TRUE 43
## 4 0003_02 Europa FALSE A/0/S TRAPPIST-1e 33 FALSE 0
## 5 0004_01 Earth FALSE F/1/S TRAPPIST-1e 16 FALSE 303
## 6 0005_01 Earth FALSE F/0/P PSO J318.5-22 44 FALSE 0
## # ℹ 6 more variables: FoodCourt <dbl>, ShoppingMall <dbl>, Spa <dbl>,
## # VRDeck <dbl>, Name <chr>, Transported <lgl>
BoÅŸluk Doldurma
Eksik olan bazı verilerin boş kalmaması için en çok tekrar eden verilerle doldurulması
mode <- function(x) { u <- unique(x)
tab <- tabulate(match(x, u))
u[tab == max(tab)]
}
df2 = df %>%
mutate(HomePlanet = ifelse(is.na(HomePlanet), mode(df$HomePlanet), HomePlanet),
CryoSleep = ifelse(is.na(CryoSleep), mode(df$CryoSleep), CryoSleep),
Destination = ifelse(is.na(Destination), mode(df$Destination), Destination),
VIP = ifelse(is.na(VIP), mode(df$VIP), VIP),
Age = ifelse(is.na(Age), mode(df$Age), Age)
)
df2 = df2 %>%
mutate(RoomService = ifelse(is.na(RoomService), 0, RoomService),
FoodCourt = ifelse(is.na(FoodCourt), 0, FoodCourt),
ShoppingMall = ifelse(is.na(ShoppingMall), 0, ShoppingMall),
Spa = ifelse(is.na(Spa), 0, Spa),
VRDeck = ifelse(is.na(VRDeck), 0, VRDeck),
Transported = as.factor(as.numeric(Transported))
)
df2 = df2 %>%
select(-c( PassengerId, Cabin, Name))
head(df2)
## # A tibble: 6 × 11
## HomePlanet CryoSleep Destination Age VIP RoomService FoodCourt
## <chr> <lgl> <chr> <dbl> <lgl> <dbl> <dbl>
## 1 Europa FALSE TRAPPIST-1e 39 FALSE 0 0
## 2 Earth FALSE TRAPPIST-1e 24 FALSE 109 9
## 3 Europa FALSE TRAPPIST-1e 58 TRUE 43 3576
## 4 Europa FALSE TRAPPIST-1e 33 FALSE 0 1283
## 5 Earth FALSE TRAPPIST-1e 16 FALSE 303 70
## 6 Earth FALSE PSO J318.5-22 44 FALSE 0 483
## # ℹ 4 more variables: ShoppingMall <dbl>, Spa <dbl>, VRDeck <dbl>,
## # Transported <fct>
Verilerin Ayrılması:
tr = df2[sample(1:nrow(df2), 0.8 * nrow(df2)),]
test = df2[-sample(1:nrow(df2), 0.8 * nrow(df2)),]
LINEAR REGRESSION
Linear Regression modelinin eÄŸitilmesi:
tr = tr %>%
mutate(Transported = as.numeric(Transported),)
lm <- lm(Transported~., data = tr)
summary(lm)
##
## Call:
## lm(formula = Transported ~ ., data = tr)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.41990 -0.30098 -0.01303 0.28381 1.76719
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.437e+00 1.626e-02 88.378 < 2e-16 ***
## HomePlanetEuropa 2.582e-01 1.449e-02 17.821 < 2e-16 ***
## HomePlanetMars 1.021e-01 1.347e-02 7.577 4.00e-14 ***
## CryoSleepTRUE 3.798e-01 1.191e-02 31.884 < 2e-16 ***
## DestinationPSO J318.5-22 -4.099e-02 2.047e-02 -2.003 0.0453 *
## DestinationTRAPPIST-1e -5.426e-02 1.276e-02 -4.252 2.15e-05 ***
## Age -2.401e-03 3.520e-04 -6.821 9.83e-12 ***
## VIPTRUE -5.895e-02 3.256e-02 -1.810 0.0703 .
## RoomService -1.127e-04 7.784e-06 -14.480 < 2e-16 ***
## FoodCourt 4.209e-05 3.303e-06 12.742 < 2e-16 ***
## ShoppingMall 7.811e-05 8.296e-06 9.415 < 2e-16 ***
## Spa -8.648e-05 4.648e-06 -18.606 < 2e-16 ***
## VRDeck -8.359e-05 4.622e-06 -18.083 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4053 on 6941 degrees of freedom
## Multiple R-squared: 0.3443, Adjusted R-squared: 0.3431
## F-statistic: 303.7 on 12 and 6941 DF, p-value: < 2.2e-16
plot(lm)
LOGISTIC REGRESSION:
Logistic Regression modelinin eÄŸitilmesi
tr = tr %>%
mutate(Transported = as.factor(as.numeric(Transported)),)
lrm = train(form = Transported ~ ., data = tr, method = "glm", family = 'binomial',
trControl = trainControl(method = "cv", number = 5)
)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(lrm)
##
## Call:
## NULL
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 2.590e-01 1.039e-01 2.494 0.012642 *
## HomePlanetEuropa 2.184e+00 1.296e-01 16.844 < 2e-16 ***
## HomePlanetMars 6.457e-01 8.385e-02 7.700 1.36e-14 ***
## CryoSleepTRUE 1.243e+00 7.943e-02 15.642 < 2e-16 ***
## `DestinationPSO J318.5-22` -3.603e-01 1.249e-01 -2.884 0.003922 **
## `DestinationTRAPPIST-1e` -4.449e-01 8.787e-02 -5.064 4.11e-07 ***
## Age -8.603e-03 2.236e-03 -3.847 0.000119 ***
## VIPTRUE -4.491e-01 2.608e-01 -1.722 0.085119 .
## RoomService -1.430e-03 9.848e-05 -14.525 < 2e-16 ***
## FoodCourt 4.757e-04 4.173e-05 11.400 < 2e-16 ***
## ShoppingMall 5.398e-04 7.547e-05 7.153 8.48e-13 ***
## Spa -1.914e-03 1.145e-04 -16.716 < 2e-16 ***
## VRDeck -1.785e-03 1.098e-04 -16.251 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 9640.0 on 6953 degrees of freedom
## Residual deviance: 6214.1 on 6941 degrees of freedom
## AIC: 6240.1
##
## Number of Fisher Scoring iterations: 7
Decision Tree modelinin eÄŸitilmesi:
dt <- rpart(Transported~., data = tr)
dt_prediction <- rpart.predict(dt,test, type = 'vector')
summary(dt)
## Call:
## rpart(formula = Transported ~ ., data = tr)
## n= 6954
##
## CP nsplit rel error xerror xstd
## 1 0.43589001 0 1.0000000 1.0000000 0.01206787
## 2 0.03444284 1 0.5641100 0.5641100 0.01084033
## 3 0.01000000 4 0.4607815 0.4752533 0.01025062
##
## Variable importance
## CryoSleep VRDeck Spa RoomService FoodCourt ShoppingMall
## 45 16 14 13 6 4
## HomePlanet
## 2
##
## Node number 1: 6954 observations, complexity param=0.43589
## predicted class=1 expected loss=0.4968364 P(node) =1
## class counts: 3499 3455
## probabilities: 0.503 0.497
## left son=2 (4536 obs) right son=3 (2418 obs)
## Primary splits:
## CryoSleep < 0.5 to the left, improve=733.6767, (0 missing)
## RoomService < 0.5 to the right, improve=414.2836, (0 missing)
## Spa < 0.5 to the right, improve=413.9996, (0 missing)
## VRDeck < 0.5 to the right, improve=379.5874, (0 missing)
## ShoppingMall < 0.5 to the right, improve=231.4551, (0 missing)
## Surrogate splits:
## Spa < 0.5 to the right, agree=0.718, adj=0.189, (0 split)
## FoodCourt < 0.5 to the right, agree=0.699, adj=0.134, (0 split)
## VRDeck < 0.5 to the right, agree=0.697, adj=0.129, (0 split)
## RoomService < 0.5 to the right, agree=0.688, adj=0.102, (0 split)
## ShoppingMall < 0.5 to the right, agree=0.683, adj=0.089, (0 split)
##
## Node number 2: 4536 observations, complexity param=0.03444284
## predicted class=1 expected loss=0.3291446 P(node) =0.6522865
## class counts: 3043 1493
## probabilities: 0.671 0.329
## left son=4 (1257 obs) right son=5 (3279 obs)
## Primary splits:
## Spa < 257.5 to the right, improve=94.98277, (0 missing)
## RoomService < 343 to the right, improve=91.56613, (0 missing)
## Age < 12.5 to the right, improve=88.64602, (0 missing)
## FoodCourt < 1407 to the left, improve=77.96928, (0 missing)
## VRDeck < 570.5 to the right, improve=65.21905, (0 missing)
## Surrogate splits:
## HomePlanet splits as RLR, agree=0.731, adj=0.029, (0 split)
## VRDeck < 9449 to the right, agree=0.724, adj=0.005, (0 split)
## FoodCourt < 5736 to the right, agree=0.724, adj=0.004, (0 split)
## Age < 75.5 to the right, agree=0.724, adj=0.002, (0 split)
## ShoppingMall < 7126 to the right, agree=0.724, adj=0.002, (0 split)
##
## Node number 3: 2418 observations
## predicted class=2 expected loss=0.1885856 P(node) =0.3477135
## class counts: 456 1962
## probabilities: 0.189 0.811
##
## Node number 4: 1257 observations
## predicted class=1 expected loss=0.1638823 P(node) =0.1807593
## class counts: 1051 206
## probabilities: 0.836 0.164
##
## Node number 5: 3279 observations, complexity param=0.03444284
## predicted class=1 expected loss=0.3924977 P(node) =0.4715272
## class counts: 1992 1287
## probabilities: 0.608 0.392
## left son=10 (1041 obs) right son=11 (2238 obs)
## Primary splits:
## RoomService < 278 to the right, improve=134.49980, (0 missing)
## FoodCourt < 1659.5 to the left, improve= 92.70302, (0 missing)
## VRDeck < 349.5 to the right, improve= 78.21670, (0 missing)
## Age < 12.5 to the right, improve= 61.22586, (0 missing)
## HomePlanet splits as LRL, improve= 48.42343, (0 missing)
## Surrogate splits:
## HomePlanet splits as RRL, agree=0.742, adj=0.187, (0 split)
##
## Node number 10: 1041 observations
## predicted class=1 expected loss=0.1825168 P(node) =0.149698
## class counts: 851 190
## probabilities: 0.817 0.183
##
## Node number 11: 2238 observations, complexity param=0.03444284
## predicted class=1 expected loss=0.4901698 P(node) =0.3218292
## class counts: 1141 1097
## probabilities: 0.510 0.490
## left son=22 (675 obs) right son=23 (1563 obs)
## Primary splits:
## VRDeck < 355 to the right, improve=159.45000, (0 missing)
## FoodCourt < 2069.5 to the left, improve= 53.50494, (0 missing)
## HomePlanet splits as LRR, improve= 42.56408, (0 missing)
## Age < 9.5 to the right, improve= 29.39214, (0 missing)
## ShoppingMall < 1205 to the left, improve= 29.29708, (0 missing)
## Surrogate splits:
## VIP < 0.5 to the right, agree=0.701, adj=0.009, (0 split)
## FoodCourt < 12128.5 to the right, agree=0.701, adj=0.007, (0 split)
## Age < 68.5 to the right, agree=0.699, adj=0.001, (0 split)
##
## Node number 22: 675 observations
## predicted class=1 expected loss=0.202963 P(node) =0.09706644
## class counts: 538 137
## probabilities: 0.797 0.203
##
## Node number 23: 1563 observations
## predicted class=2 expected loss=0.3857965 P(node) =0.2247627
## class counts: 603 960
## probabilities: 0.386 0.614
LINEAR REGRESSION BAÅžARIM ORANI:
lm_prediction = predict(lm, test)
lm_conf_matrix = table(lm_prediction, test$Transported)
accuracy = sum(diag(lm_conf_matrix))/sum(lm_conf_matrix)
accuracy * 100
## [1] 0.05750431
LOGISTIC REGRESSION MODELİ BAŞARIM ORANI:
lrm_prediction = predict(lrm, test, type="raw")
lrm_conf_matrix = table(lrm_prediction, test$Transported)
accuracy = sum(diag(lrm_conf_matrix))/sum(lrm_conf_matrix)
accuracy * 100
## [1] 79.24094
DECISION REGRESSION MODELİ BAŞARIM ORANI:
dt_conf_matrix = table(dt_prediction, test$Transported)
accuracy = sum(diag(dt_conf_matrix))/sum(dt_conf_matrix)
accuracy * 100
## [1] 77.91834
Yarışmaya gönderilecek dosya oluşturulması için önce test dosyasında aynı işlemler yapılacak. Ve logistic regresyon modeli kullanılarak yarışmaya katılım sağlanacak.
ftest = read_csv("C:\\Project\\test.csv")
## Rows: 4277 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): PassengerId, HomePlanet, Cabin, Destination, Name
## dbl (6): Age, RoomService, FoodCourt, ShoppingMall, Spa, VRDeck
## lgl (2): CryoSleep, VIP
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
ftest = ftest %>%
mutate(HomePlanet = ifelse(is.na(HomePlanet), mode(df$HomePlanet), HomePlanet),
CryoSleep = ifelse(is.na(CryoSleep), mode(df$CryoSleep), CryoSleep),
Destination = ifelse(is.na(Destination), mode(df$Destination), Destination),
VIP = ifelse(is.na(VIP), mode(df$VIP), VIP),
Age = ifelse(is.na(Age), mode(df$Age), Age)
)
ftest = ftest %>%
mutate(RoomService = ifelse(is.na(RoomService), 0, RoomService),
FoodCourt = ifelse(is.na(FoodCourt), 0, FoodCourt),
ShoppingMall = ifelse(is.na(ShoppingMall), 0, ShoppingMall),
Spa = ifelse(is.na(Spa), 0, Spa),
VRDeck = ifelse(is.na(VRDeck), 0, VRDeck),
)
predictions = data.frame(PassengerId = ftest$PassengerId, Transported = str_to_title(as.logical(as.numeric(predict(lrm, ftest, type="raw"))-1)))
write.csv(predictions, "submission.csv", row.names = FALSE)
Yarışma puan tablosundan alınan ekran görüntüsü aşağıdadır: