Spaceship Titanic

Yolcuların veya mürettebatın çoğunun çok az tanıdığı Uzay Gemisi Titanic yıldızlararası yolcu gemisi, en son Sonsuz Olasılıksızlık Sürüşü ile donatıldı. Ancak görünen o ki beklenmedik bir uzay-zaman anomalisiyle karşılaşmak pek de iyi bir tepki vermedi ve kimin alternatif bir boyuta taşınıp kimin taşınmadığından tam olarak emin değiliz.

Eğer kabul etmeyi seçerseniz göreviniz test.csvdataset’teki her bir yolcunun ulaşım olasılığını hesaplamak olacaktır.

Bu yarışma, ünlü kaggle Uzay Gemisi Titanik yarışmasının tüm aynı verileri kullanan olasılıksal bir çeşididir.

library(readr)
train <- read_csv("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.
library(readr)
test <- read_csv("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.
library(DataExplorer)
library(explore)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ purrr     1.0.2
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ── 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
describe_all(train)
## # A tibble: 14 × 8
##    variable     type     na na_pct unique   min   mean   max
##    <chr>        <chr> <int>  <dbl>  <int> <dbl>  <dbl> <dbl>
##  1 PassengerId  chr       0    0     8693    NA  NA       NA
##  2 HomePlanet   chr     201    2.3      4    NA  NA       NA
##  3 CryoSleep    lgl     217    2.5      3     0   0.36     1
##  4 Cabin        chr     199    2.3   6561    NA  NA       NA
##  5 Destination  chr     182    2.1      4    NA  NA       NA
##  6 Age          dbl     179    2.1     81     0  28.8     79
##  7 VIP          lgl     203    2.3      3     0   0.02     1
##  8 RoomService  dbl     181    2.1   1274     0 225.   14327
##  9 FoodCourt    dbl     183    2.1   1508     0 458.   29813
## 10 ShoppingMall dbl     208    2.4   1116     0 174.   23492
## 11 Spa          dbl     183    2.1   1328     0 311.   22408
## 12 VRDeck       dbl     188    2.2   1307     0 305.   24133
## 13 Name         chr     200    2.3   8474    NA  NA       NA
## 14 Transported  lgl       0    0        2     0   0.5      1
describe_all(test)
## # A tibble: 13 × 8
##    variable     type     na na_pct unique   min   mean   max
##    <chr>        <chr> <int>  <dbl>  <int> <dbl>  <dbl> <dbl>
##  1 PassengerId  chr       0    0     4277    NA  NA       NA
##  2 HomePlanet   chr      87    2        4    NA  NA       NA
##  3 CryoSleep    lgl      93    2.2      3     0   0.37     1
##  4 Cabin        chr     100    2.3   3266    NA  NA       NA
##  5 Destination  chr      92    2.2      4    NA  NA       NA
##  6 Age          dbl      91    2.1     80     0  28.7     79
##  7 VIP          lgl      93    2.2      3     0   0.02     1
##  8 RoomService  dbl      82    1.9    843     0 219.   11567
##  9 FoodCourt    dbl     106    2.5    903     0 439.   25273
## 10 ShoppingMall dbl      98    2.3    716     0 177.    8292
## 11 Spa          dbl     101    2.4    834     0 303.   19844
## 12 VRDeck       dbl      80    1.9    797     0 311.   22272
## 13 Name         chr      94    2.2   4177    NA  NA       NA

train[c('ailenum', 'ailesira')]<-str_split_fixed(train$PassengerId, "_", 2)
test[c('ailenum', 'ailesira')]<-str_split_fixed(test$PassengerId, "_", 2)
train[c('dock', 'number', 'side')]<-str_split_fixed(train$Cabin, "/", 3)
test[c('dock', 'number', 'side')]<-str_split_fixed(test$Cabin, "/", 3)
train[train=='']<-NA
test[test=='']<-NA
train<-train%>%select(-Cabin)
test<-test%>%select(-Cabin)
unique(train$HomePlanet)
## [1] "Europa" "Earth"  "Mars"   NA
levels(train$HomePlanet)
## NULL
train$HomePlanet<-addNA(train$HomePlanet)
test$HomePlanet<-addNA(test$HomePlanet)
unique(train$HomePlanet)
## [1] Europa Earth  Mars   <NA>  
## Levels: Earth Europa Mars <NA>
levels(test$HomePlanet)[is.na(levels(test$HomePlanet))]<-"NA"
levels(train$HomePlanet)[is.na(levels(train$HomePlanet))]<-"NA"
levels(train$HomePlanet)
## [1] "Earth"  "Europa" "Mars"   "NA"
train<-train%>%select(-Name)
test<-test%>%select(-Name)
train$CryoSleep<-addNA(train$CryoSleep)
test$CryoSleep<-addNA(test$CryoSleep)
train$VRDeck<-addNA(train$VRDeck)
test$VRDeck<-addNA(test$VRDeck)
train$dock<-addNA(train$dock)
test$dock<-addNA(test$dock)
train$Destination<-addNA(train$Destination)
test$Destination<-addNA(test$Destination)
train$VIP<-addNA(train$VIP)
test$VIP<-addNA(test$VIP)
train$side<-addNA(train$side)
test$side<-addNA(test$side)
train <- train %>% group_by(HomePlanet) %>% mutate(across(Age, ~replace_na(., mean(., na.rm = -TRUE))))
test <- test %>% group_by(HomePlanet) %>% mutate(across(Age, ~replace_na(., mean(., na.rm = -TRUE))))
test <- test %>% group_by(HomePlanet) %>% mutate(across(RoomService, ~replace_na(., mean(., na.rm = -TRUE))))
train <- train %>% group_by(HomePlanet) %>% mutate(across(RoomService, ~replace_na(., mean(., na.rm = -TRUE))))
train <- train %>% group_by(HomePlanet) %>% mutate(across(FoodCourt, ~replace_na(., mean(., na.rm = -TRUE))))
test <- test %>% group_by(HomePlanet) %>% mutate(across(FoodCourt, ~replace_na(., mean(., na.rm = -TRUE))))
train <- train %>% group_by(HomePlanet) %>% mutate(across(ShoppingMall, ~replace_na(., mean(., na.rm = -TRUE))))
test <- test %>% group_by(HomePlanet) %>% mutate(across(ShoppingMall, ~replace_na(., mean(., na.rm = -TRUE))))
train <- train %>% group_by(HomePlanet) %>% mutate(across(Spa, ~replace_na(., mean(., na.rm = -TRUE))))
test <- test %>% group_by(HomePlanet) %>% mutate(across(Spa, ~replace_na(., mean(., na.rm = -TRUE))))
train <- train %>% mutate(FoodCourt = coalesce (FoodCourt, 0))
test <- test %>% mutate(FoodCourt = coalesce (FoodCourt, 0))
train <- train %>% mutate(ShoppingMall = coalesce (ShoppingMall, 0))
test <- test %>% mutate(ShoppingMall = coalesce (ShoppingMall, 0))
train <- train %>% mutate(Age = coalesce (Age, 0))
test <- test %>% mutate(Age = coalesce (Age, 0))
train <- train %>% mutate(RoomService = coalesce (RoomService, 0))
test <- test %>% mutate(RoomService = coalesce (RoomService, 0))
train <- train %>% mutate(Spa = coalesce (Spa, 0))
test <- test %>% mutate(Spa = coalesce (Spa, 0))
train$aile<- ifelse (duplicated(train$ailenum) |  duplicated(train$ailenum,fromLast=TRUE),1,0)
test$aile<- ifelse (duplicated(test$ailenum) |  duplicated(test$ailenum,fromLast=TRUE),1,0)
train<-train%>%select(-number)
test<-test%>%select(-number)
train<-train%>%select(-ailenum,-ailesira)
test<-test%>%select(-ailenum,-ailesira)
describe_all(train)
## # A tibble: 15 × 8
##    variable     type     na na_pct unique   min   mean   max
##    <chr>        <chr> <int>  <dbl>  <int> <dbl>  <dbl> <dbl>
##  1 PassengerId  chr       0      0   8693    NA  NA       NA
##  2 HomePlanet   fct       0      0      4    NA  NA       NA
##  3 CryoSleep    fct       0      0      3    NA  NA       NA
##  4 Destination  fct       0      0      4    NA  NA       NA
##  5 Age          dbl       0      0     80     0  28.2     79
##  6 VIP          fct       0      0      3    NA  NA       NA
##  7 RoomService  dbl       0      0   1273     0 220.   14327
##  8 FoodCourt    dbl       0      0   1507     0 448.   29813
##  9 ShoppingMall dbl       0      0   1115     0 170.   23492
## 10 Spa          dbl       0      0   1327     0 305.   22408
## 11 VRDeck       fct       0      0   1307    NA  NA       NA
## 12 Transported  lgl       0      0      2     0   0.5      1
## 13 dock         fct       0      0      9    NA  NA       NA
## 14 side         fct       0      0      3    NA  NA       NA
## 15 aile         dbl       0      0      2     0   0.45     1
describe_all(test)
## # A tibble: 14 × 8
##    variable     type     na na_pct unique   min   mean   max
##    <chr>        <chr> <int>  <dbl>  <int> <dbl>  <dbl> <dbl>
##  1 PassengerId  chr       0      0   4277    NA  NA       NA
##  2 HomePlanet   fct       0      0      4    NA  NA       NA
##  3 CryoSleep    fct       0      0      3    NA  NA       NA
##  4 Destination  fct       0      0      4    NA  NA       NA
##  5 Age          dbl       0      0     79     0  28.0     79
##  6 VIP          fct       0      0      3    NA  NA       NA
##  7 RoomService  dbl       0      0    842     0 215.   11567
##  8 FoodCourt    dbl       0      0    902     0 429.   25273
##  9 ShoppingMall dbl       0      0    715     0 173.    8292
## 10 Spa          dbl       0      0    833     0 296.   19844
## 11 VRDeck       fct       0      0    797    NA  NA       NA
## 12 dock         fct       0      0      9    NA  NA       NA
## 13 side         fct       0      0      3    NA  NA       NA
## 14 aile         dbl       0      0      2     0   0.45     1
library(rsample)
set.seed(1234)
library(simEd)
## Loading required package: rstream
## 
## Attaching package: 'simEd'
## The following objects are masked from 'package:base':
## 
##     sample, set.seed
ayristirmaörneklemi <- initial_split(train, prop = 0.80)
training <- training(ayristirmaörneklemi)
testing <- testing(ayristirmaörneklemi)

##naive bayes tahmini

library(e1071)
## 
## Attaching package: 'e1071'
## The following object is masked from 'package:rsample':
## 
##     permutations
train_set<-train[2:15]
test_set<-test[2:14]
library(caTools)
set.seed(123)
split=sample.split(train_set$Transported,SplitRatio = 0.80)
training_set=subset(train_set,split==TRUE)
testing_set=subset(train_set,split==FALSE)
fit_nb<-naiveBayes(Transported~ . ,data=training_set)
preds<-predict(fit_nb,newdata=testing_set,type= "raw") %>%
  data.frame()
y_pred=ifelse(preds$TRUE.>0.5,1,0)
y_true<-ifelse(testing_set[11]==TRUE,1,0)
cm=table(y_true,y_pred)
cm
##       y_pred
## y_true   0   1
##      0 412 451
##      1  76 800
(412+800)/(412+800+76+451)
## [1] 0.6969523
nb_son=naiveBayes(Transported~ . ,data=training_set)
preds<-predict(nb_son,newdata=test_set,type= "raw") %>%
  data.frame()
y_pred=ifelse(preds$TRUE.>0.5,TRUE,FALSE)
Transported<-as.character(y_pred)
PassengerId<-test$PassengerId
Transported<-as.vector(Transported)
submission<-cbind(PassengerId,Transported)
submission<-as.data.frame(submission)
submission$Transported<-str_to_title(submission$Transported)
write.csv(submission,"subb_nb.csv",row.names = FALSE,quote = FALSE)

##naive bayes tahmini

Naive Bayes, istatistiksel sınıflandırma problemlerinde kullanılan bir makine öğrenimi algoritmasıdır. Bu algoritma, Bayes teoremi temelinde çalışır ve “naive” (saf) olarak adlandırılır, çünkü sınıflandırma sürecindeki bazı temel varsayımları basitleştirir.

Algoritmanın temel amacı, verilen bir örneğin belirli bir sınıfa ait olma olasılığını tahmin etmektir. Varsayımlar arasında, özelliklerin birbirinden bağımsız olduğu ve her bir özelliğin sınıf belirlenirken aynı öneme sahip olduğu yer alır.

Naive Bayes, özellikle metin sınıflandırma gibi uygulamalarda başarılı olabilir. Örneğin, bir e-postanın spam olup olmadığını belirlemek veya bir belgenin belirli bir kategoriye ait olup olmadığını tahmin etmek için kullanılabilir.