FINAL PROJET
##1910504512
SPASECHIP TITANIC
Kaggle recently launched a fun competition called Spaceship Titanic. It is designed to be an update of the popular Titanic competition which helps people new to data science learn the basics of machine learning, get acquainted with Kaggle’s platform, and meet others in the community. This article is a beginner-friendly analysis of the Spaceship Titanic Kaggle Competition. It covers steps to obtain any meaningful insights from the data and to predict the “ground truth” for the test set with an accuracy of ~80% using RandomForestClassifier.
It seems like you’ve provided R code using the backticks and curly braces, but the actual content inside the curly braces appears to be missing. If you have a specific question or if you’d like assistance with something related to the code, please provide more details or the missing part of the code, and I’ll do my best to help you.
## spc_tbl_ [8,693 × 14] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ PassengerId : chr [1:8693] "0001_01" "0002_01" "0003_01" "0003_02" ...
## $ HomePlanet : chr [1:8693] "Europa" "Earth" "Europa" "Europa" ...
## $ CryoSleep : logi [1:8693] FALSE FALSE FALSE FALSE FALSE FALSE ...
## $ Cabin : chr [1:8693] "B/0/P" "F/0/S" "A/0/S" "A/0/S" ...
## $ Destination : chr [1:8693] "TRAPPIST-1e" "TRAPPIST-1e" "TRAPPIST-1e" "TRAPPIST-1e" ...
## $ Age : num [1:8693] 39 24 58 33 16 44 26 28 35 14 ...
## $ VIP : logi [1:8693] FALSE FALSE TRUE FALSE FALSE FALSE ...
## $ RoomService : num [1:8693] 0 109 43 0 303 0 42 0 0 0 ...
## $ FoodCourt : num [1:8693] 0 9 3576 1283 70 ...
## $ ShoppingMall: num [1:8693] 0 25 0 371 151 0 3 0 17 0 ...
## $ Spa : num [1:8693] 0 549 6715 3329 565 ...
## $ VRDeck : num [1:8693] 0 44 49 193 2 0 0 NA 0 0 ...
## $ Name : chr [1:8693] "Maham Ofracculy" "Juanna Vines" "Altark Susent" "Solam Susent" ...
## $ Transported : logi [1:8693] FALSE TRUE FALSE FALSE TRUE TRUE ...
## - attr(*, "spec")=
## .. cols(
## .. PassengerId = col_character(),
## .. HomePlanet = col_character(),
## .. CryoSleep = col_logical(),
## .. Cabin = col_character(),
## .. Destination = col_character(),
## .. Age = col_double(),
## .. VIP = col_logical(),
## .. RoomService = col_double(),
## .. FoodCourt = col_double(),
## .. ShoppingMall = col_double(),
## .. Spa = col_double(),
## .. VRDeck = col_double(),
## .. Name = col_character(),
## .. Transported = col_logical()
## .. )
## - attr(*, "problems")=<externalptr>
str() function in R to display the structure of the train object, which is presumably a data frame read from a CSV file using the readr library. However, you’ve added the word “expression” after the code block, which might be unintentional.
## spc_tbl_ [4,277 × 13] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ PassengerId : chr [1:4277] "0013_01" "0018_01" "0019_01" "0021_01" ...
## $ HomePlanet : chr [1:4277] "Earth" "Earth" "Europa" "Europa" ...
## $ CryoSleep : logi [1:4277] TRUE FALSE TRUE FALSE FALSE FALSE ...
## $ Cabin : chr [1:4277] "G/3/S" "F/4/S" "C/0/S" "C/1/S" ...
## $ Destination : chr [1:4277] "TRAPPIST-1e" "TRAPPIST-1e" "55 Cancri e" "TRAPPIST-1e" ...
## $ Age : num [1:4277] 27 19 31 38 20 31 21 20 23 24 ...
## $ VIP : logi [1:4277] FALSE FALSE FALSE FALSE FALSE FALSE ...
## $ RoomService : num [1:4277] 0 0 0 0 10 0 0 0 0 0 ...
## $ FoodCourt : num [1:4277] 0 9 0 6652 0 ...
## $ ShoppingMall: num [1:4277] 0 0 0 0 635 263 0 0 0 0 ...
## $ Spa : num [1:4277] 0 2823 0 181 0 ...
## $ VRDeck : num [1:4277] 0 0 0 585 0 60 0 0 0 0 ...
## $ Name : chr [1:4277] "Nelly Carsoning" "Lerome Peckers" "Sabih Unhearfus" "Meratz Caltilter" ...
## - attr(*, "spec")=
## .. cols(
## .. PassengerId = col_character(),
## .. HomePlanet = col_character(),
## .. CryoSleep = col_logical(),
## .. Cabin = col_character(),
## .. Destination = col_character(),
## .. Age = col_double(),
## .. VIP = col_logical(),
## .. RoomService = col_double(),
## .. FoodCourt = col_double(),
## .. ShoppingMall = col_double(),
## .. Spa = col_double(),
## .. VRDeck = col_double(),
## .. Name = col_character()
## .. )
## - attr(*, "problems")=<externalptr>
str() function in R to display the structure of the test object, which is presumably a data frame read from a CSV file using the readr library. However, you’ve added the word “expression” after the code block, which might be unintentional.
## [1] "Europa" "Earth" "Mars" NA
“expression” after the code block again. If you want to execute the R code and display the unique values of the “HomePlanet” column in your train data frame, you should remove the “expression” text.
## [1] "Earth" "Europa" "Mars" NA
“expression” after the code block again. If you want to execute the R code and display the unique values of the “HomePlanet” column in your test data frame, you should remove the “expression” text.
## [1] FALSE TRUE NA
“expression” again. If you want to execute the R code and display the unique values of the “CryoSleep” column in your train data frame, you should remove the “expression” text.
## [1] TRUE FALSE NA
“expression” again. If you want to execute the R code and display the unique values of the “CryoSleep” column in your test data frame, you should remove the “expression” text.
## [1] "TRAPPIST-1e" "PSO J318.5-22" "55 Cancri e" NA
“expression” again. If you want to execute the R code and display the unique values of the “Destination” column in your train data frame, you should remove the “expression” text.
## [1] "TRAPPIST-1e" "55 Cancri e" "PSO J318.5-22" NA
“expression” again. If you want to execute the R code and display the unique values of the “Destination” column in your test data frame, you should remove the “expression” text.
addNA() function to add an “NA” level to the “HomePlanet” column in your train data frame. This function is typically used to add a missing value level to factor variables in R.
addNA() function to add an “NA” level to the “HomePlanet” column in your test data frame. This function is typically used to add a missing value level to factor variables in R.
“expression” again. If you want to execute the R code and add an “NA” level to the “Destination” column in your train data frame, you should remove the “expression” text.
“expression” again. If you want to execute the R code and add an “NA” level to the “Destination” column in your test data frame, you should remove the “expression” text.
library(dplyr)
library(tidyr)
train <- train %>%
group_by(HomePlanet) %>%
mutate_at(vars(Age), ~replace_na(., mean(., na.rm = TRUE)))dplyr and tidyr packages to group the train data frame by the “HomePlanet” variable and then replace missing values in the “Age” variable within each group with the mean of that group. The mutate_at function is used to apply the replacement operation to the specified variable(s) within each group.
test <- test %>%
group_by(HomePlanet) %>%
mutate_at(vars(Age), ~replace_na(., mean(., na.rm = TRUE)))dplyr and tidyr packages to group the test data frame by the “HomePlanet” variable and then replace missing values in the “Age” variable within each group with the mean of that group. The mutate_at function is used to apply the replacement operation to the specified variable(s) within each group.
addNA() function to add an “NA” level to the “CryoSleep” column in the train data frame. This function is often used with factor variables to explicitly represent missing values.
addNA() function to add an “NA” level to the “CryoSleep” column in the test data frame. This function is often used with factor variables to explicitly represent missing values.
addNA() function to add an “NA” level to the “VIP” column in the train data frame. Similar to previous examples, this function is commonly used with factor variables to represent missing values explicitly.
addNA() function to add an “NA” level to the “VIP” column in the test data frame. Similar to previous examples, this function is commonly used with factor variables to represent missing values explicitly.
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ purrr 1.0.2
## ✔ ggplot2 3.4.4 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ── 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
tidyverse in R, it’s not clear what aspect of the tidyverse you’re referring to. Exploratory Data Analysis (EDA): EDA is the process of visually and statistically exploring datasets to summarize their main characteristics, often with the help of graphical representations. In R, packages such as ggplot2 (part of the tidyverse) are commonly used for creating informative and customizable plots.
## # 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 fct 0 0 4 NA NA NA
## 3 CryoSleep fct 0 0 3 NA NA NA
## 4 Cabin chr 199 2.3 6561 NA NA NA
## 5 Destination fct 0 0 4 NA NA NA
## 6 Age dbl 0 0 84 0 28.8 79
## 7 VIP fct 0 0 3 NA NA NA
## 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 function with the pipe operator from the tidyverse to describe all variables in the train dataset. However, it seems there is a syntax error in your code.
## # 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 fct 0 0 4 NA NA NA
## 3 CryoSleep fct 0 0 3 NA NA NA
## 4 Cabin chr 100 2.3 3266 NA NA NA
## 5 Destination fct 0 0 4 NA NA NA
## 6 Age dbl 0 0 83 0 28.7 79
## 7 VIP fct 0 0 3 NA NA NA
## 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
describe_all function with the pipe operator from the tidyverse to describe all variables in the test dataset. However, it seems there is a syntax error in your code.
train <- train %>% mutate(FoodCourt = coalesce(FoodCourt, 0),RoomService = coalesce(RoomService, 0),ShoppingMall = coalesce(ShoppingMall,0),Spa = coalesce(Spa ,0), VRDeck = coalesce(VRDeck, 0))mutate function from the dplyr package to replace any missing values in specific columns (FoodCourt, RoomService, ShoppingMall, Spa, and VRDeck) with zeros using the coalesce function. The coalesce function returns the first non-missing value in a set of columns.
test <- test %>% mutate(FoodCourt = coalesce(FoodCourt, 0),RoomService = coalesce(RoomService, 0),ShoppingMall = coalesce(ShoppingMall,0),Spa = coalesce(Spa ,0), VRDeck = coalesce(VRDeck, 0))mutate function from the dplyr package to replace any missing values in specific columns (FoodCourt, RoomService, ShoppingMall, Spa, and VRDeck) with zeros using the coalesce function. The coalesce function returns the first non-missing value in a set of columns.
## # 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 fct 0 0 4 NA NA NA
## 3 CryoSleep fct 0 0 3 NA NA NA
## 4 Cabin chr 199 2.3 6561 NA NA NA
## 5 Destination fct 0 0 4 NA NA NA
## 6 Age dbl 0 0 84 0 28.8 79
## 7 VIP fct 0 0 3 NA NA NA
## 8 RoomService dbl 0 0 1273 0 220. 14327
## 9 FoodCourt dbl 0 0 1507 0 448. 29813
## 10 ShoppingMall dbl 0 0 1115 0 170. 23492
## 11 Spa dbl 0 0 1327 0 305. 22408
## 12 VRDeck dbl 0 0 1306 0 298. 24133
## 13 Name chr 200 2.3 8474 NA NA NA
## 14 Transported lgl 0 0 2 0 0.5 1
describe_all function, but as of my knowledge cutoff date in January 2022, the describe_all function is not part of the tidyverse or dplyr package.
## # 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 fct 0 0 4 NA NA NA
## 3 CryoSleep fct 0 0 3 NA NA NA
## 4 Cabin chr 100 2.3 3266 NA NA NA
## 5 Destination fct 0 0 4 NA NA NA
## 6 Age dbl 0 0 83 0 28.7 79
## 7 VIP fct 0 0 3 NA NA NA
## 8 RoomService dbl 0 0 842 0 215. 11567
## 9 FoodCourt dbl 0 0 902 0 429. 25273
## 10 ShoppingMall dbl 0 0 715 0 173. 8292
## 11 Spa dbl 0 0 833 0 296. 19844
## 12 VRDeck dbl 0 0 796 0 305. 22272
## 13 Name chr 94 2.2 4177 NA NA NA
describe_all function, but as of my knowledge cutoff date in January 2022, the describe_all function is not part of the tidyverse or dplyr package.
train dataset, but as of my knowledge cutoff date in January 2022, the describe_all function is not a standard function in the tidyverse or dplyr package.
test dataset, but as of my knowledge cutoff date in January 2022, the describe_all function is not a standard function in the tidyverse or dplyr package.
str_split_fixed function from the stringr package to split the values in the “Cabin” column of the train data frame into three separate columns (“Deck,” “Num,” and “Side”).
str_split_fixed function from the stringr package to split the values in the “Cabin” column of the test data frame into three separate columns (“Deck,” “Num,” and “Side”).
with NA in the train data frame. However, this approach may not work as expected because when comparing an entire data frame to a single value (’’), it may not handle the replacement correctly.
with NA in the test data frame. However, this approach may not work as expected because when comparing an entire data frame to a single value (’’), it may not handle the replacement correctly.
duplicated function to identify duplicated values in the “ailenum” column of the train data frame. It then uses the ifelse function to create a new column “aile” where the value is set to 1 if the corresponding “ailenum” value is duplicated (either earlier or later in the sequence), and 0 otherwise.
duplicated function to identify duplicated values in the “ailenum” column of the test data frame. It then uses the ifelse function to create a new column “aile” where the value is set to 1 if the corresponding “ailenum” value is duplicated (either earlier or later in the sequence), and 0 otherwise.
## # A tibble: 10 × 4
## PassengerId ailenum ailesira aile
## <chr> <chr> <chr> <dbl>
## 1 0001_01 0001 01 0
## 2 0002_01 0002 01 0
## 3 0003_01 0003 01 1
## 4 0003_02 0003 02 1
## 5 0004_01 0004 01 0
## 6 0005_01 0005 01 0
## 7 0006_01 0006 01 1
## 8 0006_02 0006 02 1
## 9 0007_01 0007 01 0
## 10 0008_01 0008 01 1
head function to display the first 10 rows of specific columns (“PassengerId,” “ailenum,” “ailesira,” and “aile”) from the train data frame
## # A tibble: 10 × 4
## PassengerId ailenum ailesira aile
## <chr> <chr> <chr> <dbl>
## 1 0013_01 0013 01 0
## 2 0018_01 0018 01 0
## 3 0019_01 0019 01 0
## 4 0021_01 0021 01 0
## 5 0023_01 0023 01 0
## 6 0027_01 0027 01 0
## 7 0029_01 0029 01 0
## 8 0032_01 0032 01 1
## 9 0032_02 0032 02 1
## 10 0033_01 0033 01 0
head function to display the first 10 rows of specific columns (“PassengerId,” “ailenum,” “ailesira,” and “aile”) from the test data frame
select function from the dplyr package to remove specific columns (ailenum, ailesira, Num, Cabin, Name) from the train data frame.
select function from the dplyr package to remove specific columns (ailenum, ailesira, Num, Cabin, Name) from the test data frame.
## # 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 84 0 28.8 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 dbl 0 0 1306 0 298. 24133
## 12 Transported lgl 0 0 2 0 0.5 1
## 13 Deck chr 199 2.3 9 NA NA NA
## 14 Side chr 199 2.3 3 NA NA NA
## 15 aile dbl 0 0 2 0 0.45 1
train dataset, but as of my knowledge cutoff in January 2022, there isn’t a built-in describe_all function in the tidyverse or dplyr package.
## # 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 83 0 28.7 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 dbl 0 0 796 0 305. 22272
## 12 Deck chr 100 2.3 9 NA NA NA
## 13 Side chr 100 2.3 3 NA NA NA
## 14 aile dbl 0 0 2 0 0.45 1
train dataset, but as of my knowledge cutoff in January 2022, there isn’t a built-in describe_all function in the tidyverse or dplyr package.
“Age” variable in the train data frame using the hist function. Here’s the corrected code without the “expression” text:
“Age” variable in the test data frame using the hist function. Here’s the corrected code without the “expression” text:
“FoodCourt” variable in the train data frame using the hist function. However, creating a histogram for a variable like “FoodCourt” might not be appropriate if “FoodCourt” is not a numeric variable.
“FoodCourt” variable in the test data frame using the hist function. However, creating a histogram for a variable like “FoodCourt” might not be appropriate if “FoodCourt” is not a numeric variable.
“RoomService” variable in the train data frame using the hist function. Here’s the corrected code without the “expression” text:
“RoomService” variable in the test data frame using the hist function. Here’s the corrected code without the “expression” text:
LOGİSTİC REGRESYON
Logistic regression is a statistical modeling technique used to model how a dependent variable (usually categorical) is related to an independent variable or variables. Usually, the goal of logistic regression is to estimate the probability distribution between two categories of the dependent variable.
train_set that includes columns 2 through 15 (all columns from the 2nd to the 15th) of the original train data frame.
test_set that includes columns 2 through 15 (all columns from the 2nd to the 15th) of the original train data frame.
library(caTools)
set.seed(123)
split = sample.split(train_set$Transported, SplitRatio = 0.75)
training_set = subset(train_set, split == TRUE)
testing_set = subset(train_set, split == FALSE)sample.split function from the caTools package to split the train_set data frame into a training set (training_set) and a testing set (testing_set) based on the “Transported” variable. It randomly assigns 75% of the observations to the training set and the remaining 25% to the testing set.
## y_pred
## y_true 0 1
## 0 803 253
## 1 182 882
## [1] 0.7948113
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Decision trees
Decision Trees are a machine learning algorithm used in classification and regression problems. This tree structure consists of a set of decision nodes and edges connecting these nodes. Each node represents a decision point on a feature and these decisions are used to classify the dataset or predict a target variable.
training_set$Transported <- as.factor(training_set$Transported)
testing_set$Transported <- as.factor(testing_set$Transported)
train_set$Transported <- as.factor(train_set$Transported)## y_pred
## y_true 0 1
## 0 735 344
## 1 170 924
## [1] 0.7634607