Uzay Gemisi Titanic Projesi Giriş Bu R Markdown projesi olan “Uzay Gemisi Titanic,” veri analizi ve makine öğrenimi yöntemlerinin kullanımını içeren bir çalışmadır. Projenin amacı, üç farklı makine öğrenimi algoritması olan karar ağacı, lojistik regresyon ve naive Bayes kullanarak, Uzay Gemisi Titanic veri kümesini analiz etmek ve modellemektir.
Kütüphane ve Çalışma Ortamının Kurulumu Projenin ilk aşaması olarak, gerekli R kütüphanelerinin ve çalışma ortamının kurulumu gerçekleştirilmiştir. Bu aşama, veri analizi ve makine öğrenimi için temel araçların sağlanmasını içermektedir. readr, caret, randomForest, dplyr, tidyr, stringr, e1071, ve xgboost gibi kütüphaneler projenin temel altyapısını oluşturmaktadır.
Veri Ön İşleme İkinci aşama, Uzay Gemisi Titanic veri kümesinin ön işlenmesini içermektedir. Bu adım, veri setindeki eksik değerlerin doldurulması, kategorik değişkenlerin faktörler haline getirilmesi ve gerekli dönüşümlerin yapılmasını içermektedir. Ayrıca, veri setindeki özelliklerin daha iyi anlaşılması için görselleştirmeler ve özet istatistikler de bu aşamada ele alınmıştır.
Modelleme ve Eğitim Projenin son aşamasında, karar ağacı, lojistik regresyon ve naive Bayes olmak üzere üç farklı makine öğrenimi algoritması kullanılarak modeller eğitilmiştir. Bu modeller, eğitim veri seti üzerinde öğrenilmiş ve ardından test veri seti üzerinde değerlendirilmiştir. Model performansı, karar ağacı ve lojistik regresyon için karmaşıklık matrisi ile değerlendirilirken, naive Bayes için ilgili metrikler kullanılmıştır.
Bu proje, Uzay Gemisi Titanic veri kümesi üzerinde uygulanan analiz ve modelleme adımlarıyla, makine öğrenimi yöntemlerinin pratik uygulamalarına odaklanmaktadır.
library("readr", quietly = TRUE)
library('caret', quietly = TRUE)
library('randomForest', quietly = TRUE)## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
##
## Attaching package: 'randomForest'
## The following object is masked from 'package:ggplot2':
##
## margin
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:randomForest':
##
## combine
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library('tidyr', quietly = TRUE)
library('stringr', quietly = TRUE)
library("e1071", quietly = TRUE)
library("xgboost", quietly = TRUE)##
## Attaching package: 'xgboost'
## The following object is masked from 'package:dplyr':
##
## slice
## 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.
## # 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>
## PassengerId HomePlanet CryoSleep Cabin
## Length:8693 Length:8693 Mode :logical Length:8693
## Class :character Class :character FALSE:5439 Class :character
## Mode :character Mode :character TRUE :3037 Mode :character
## NA's :217
##
##
##
## Destination Age VIP RoomService
## Length:8693 Min. : 0.00 Mode :logical Min. : 0.0
## Class :character 1st Qu.:19.00 FALSE:8291 1st Qu.: 0.0
## Mode :character Median :27.00 TRUE :199 Median : 0.0
## Mean :28.83 NA's :203 Mean : 224.7
## 3rd Qu.:38.00 3rd Qu.: 47.0
## Max. :79.00 Max. :14327.0
## NA's :179 NA's :181
## FoodCourt ShoppingMall Spa VRDeck
## Min. : 0.0 Min. : 0.0 Min. : 0.0 Min. : 0.0
## 1st Qu.: 0.0 1st Qu.: 0.0 1st Qu.: 0.0 1st Qu.: 0.0
## Median : 0.0 Median : 0.0 Median : 0.0 Median : 0.0
## Mean : 458.1 Mean : 173.7 Mean : 311.1 Mean : 304.9
## 3rd Qu.: 76.0 3rd Qu.: 27.0 3rd Qu.: 59.0 3rd Qu.: 46.0
## Max. :29813.0 Max. :23492.0 Max. :22408.0 Max. :24133.0
## NA's :183 NA's :208 NA's :183 NA's :188
## Name Transported
## Length:8693 Mode :logical
## Class :character FALSE:4315
## Mode :character TRUE :4378
##
##
##
##
## 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.
## # A tibble: 6 × 13
## PassengerId HomePlanet CryoSleep Cabin Destination Age VIP RoomService
## <chr> <chr> <lgl> <chr> <chr> <dbl> <lgl> <dbl>
## 1 0013_01 Earth TRUE G/3/S TRAPPIST-1e 27 FALSE 0
## 2 0018_01 Earth FALSE F/4/S TRAPPIST-1e 19 FALSE 0
## 3 0019_01 Europa TRUE C/0/S 55 Cancri e 31 FALSE 0
## 4 0021_01 Europa FALSE C/1/S TRAPPIST-1e 38 FALSE 0
## 5 0023_01 Earth FALSE F/5/S TRAPPIST-1e 20 FALSE 10
## 6 0027_01 Earth FALSE F/7/P TRAPPIST-1e 31 FALSE 0
## # ℹ 5 more variables: FoodCourt <dbl>, ShoppingMall <dbl>, Spa <dbl>,
## # VRDeck <dbl>, Name <chr>
## PassengerId HomePlanet CryoSleep Cabin
## Length:4277 Length:4277 Mode :logical Length:4277
## Class :character Class :character FALSE:2640 Class :character
## Mode :character Mode :character TRUE :1544 Mode :character
## NA's :93
##
##
##
## Destination Age VIP RoomService
## Length:4277 Min. : 0.00 Mode :logical Min. : 0.0
## Class :character 1st Qu.:19.00 FALSE:4110 1st Qu.: 0.0
## Mode :character Median :26.00 TRUE :74 Median : 0.0
## Mean :28.66 NA's :93 Mean : 219.3
## 3rd Qu.:37.00 3rd Qu.: 53.0
## Max. :79.00 Max. :11567.0
## NA's :91 NA's :82
## FoodCourt ShoppingMall Spa VRDeck
## Min. : 0.0 Min. : 0.0 Min. : 0.0 Min. : 0.0
## 1st Qu.: 0.0 1st Qu.: 0.0 1st Qu.: 0.0 1st Qu.: 0.0
## Median : 0.0 Median : 0.0 Median : 0.0 Median : 0.0
## Mean : 439.5 Mean : 177.3 Mean : 303.1 Mean : 310.7
## 3rd Qu.: 78.0 3rd Qu.: 33.0 3rd Qu.: 50.0 3rd Qu.: 36.0
## Max. :25273.0 Max. :8292.0 Max. :19844.0 Max. :22272.0
## NA's :106 NA's :98 NA's :101 NA's :80
## Name
## Length:4277
## Class :character
## Mode :character
##
##
##
##
## [1] "0013_01" "0018_01" "0019_01" "0021_01" "0023_01" "0027_01"
## Length Class Mode
## 4277 character character
Bu kod, “train” adlı veri çerçevesindeki “PassengerId” sütununu “_” karakteriyle ayrıştırarak “groups” ve “num” adlı iki yeni sütun oluşturur. Ardından, “Cabin” sütununu “/” karakteriyle ayrıştırarak “c_deck”, “C_num”, ve “C_side” adlı üç yeni sütun ekler. Bu işlemler, veri setindeki özellikleri daha ayrıntılı bir şekilde organize etmek ve analiz etmek için kullanılır. Ayrıca, veri setindeki belirli bilgilerin daha anlamlı bir şekilde temsil edilmesine olanak tanır.
train <- separate(train, PassengerId, into = c("groups", "num"), sep = "_")
# Separating the 'Cabin' column in the 'train' dataframe into 'c_deck', 'C_num', and 'C_side' columns using '/' as the separator
train <- separate(train, Cabin, into = c("c_deck", "C_num", "C_side"), sep = "/")# Filling missing values in certain columns with default values
train$HomePlanet <- coalesce(train$HomePlanet, "Earth")
train$Destination <- coalesce(train$Destination, "TRAPPIST-1e")
train$RoomService <- coalesce(train$RoomService, 0)
train$FoodCourt <- coalesce(train$FoodCourt, 0)
train$ShoppingMall <- coalesce(train$ShoppingMall, 0)
train$Spa <- coalesce(train$Spa, 0)
train$VRDeck <- coalesce(train$VRDeck, 0)
train$CryoSleep <- coalesce(train$CryoSleep, as.logical("FALSE"))
# Displaying the first few rows of the modified 'train' dataframe
head(train)## # A tibble: 6 × 17
## groups num HomePlanet CryoSleep c_deck C_num C_side Destination Age VIP
## <chr> <chr> <chr> <lgl> <chr> <chr> <chr> <chr> <dbl> <lgl>
## 1 0001 01 Europa FALSE B 0 P TRAPPIST-1e 39 FALSE
## 2 0002 01 Earth FALSE F 0 S TRAPPIST-1e 24 FALSE
## 3 0003 01 Europa FALSE A 0 S TRAPPIST-1e 58 TRUE
## 4 0003 02 Europa FALSE A 0 S TRAPPIST-1e 33 FALSE
## 5 0004 01 Earth FALSE F 1 S TRAPPIST-1e 16 FALSE
## 6 0005 01 Earth FALSE F 0 P PSO J318.5-… 44 FALSE
## # ℹ 7 more variables: RoomService <dbl>, FoodCourt <dbl>, ShoppingMall <dbl>,
## # Spa <dbl>, VRDeck <dbl>, Name <chr>, Transported <lgl>
Bu kod bloğu, "train" veri çerçevesindeki "VIP", "c_deck", ve "C_side" sütunlarını kategorik kodlamaya dönüştürür. Eğer bu sütunlarda eksik değer varsa, "FALSE" veya "N" ile doldurulur. Ayrıca, "RoomService", "FoodCourt", "ShoppingMall", "Spa", ve "VRDeck" sütunlarının toplamını içeren "bills" adlı yeni bir sütun oluşturulur. Son olarak, değişikliklerin etkisini görmek için modifiye edilmiş "train" veri çerçevesinin ilk birkaç satırı baştan yazılarak görüntülenir.
# Converting certain columns to factors for categorical encoding
train$VIP <- coalesce(train$VIP, as.logical("FALSE"))
train$c_deck <- coalesce(train$c_deck, "N")
train$C_side <- coalesce(train$C_side, "N")
# Calculating the sum of 'RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', and 'VRDeck' columns and storing the result in 'bills' column
train$bills <- train$RoomService + train$FoodCourt + train$ShoppingMall + train$Spa + train$VRDeck
# Displaying the first few rows of the modified 'train' dataframe
head(train)## # A tibble: 6 × 18
## groups num HomePlanet CryoSleep c_deck C_num C_side Destination Age VIP
## <chr> <chr> <chr> <lgl> <chr> <chr> <chr> <chr> <dbl> <lgl>
## 1 0001 01 Europa FALSE B 0 P TRAPPIST-1e 39 FALSE
## 2 0002 01 Earth FALSE F 0 S TRAPPIST-1e 24 FALSE
## 3 0003 01 Europa FALSE A 0 S TRAPPIST-1e 58 TRUE
## 4 0003 02 Europa FALSE A 0 S TRAPPIST-1e 33 FALSE
## 5 0004 01 Earth FALSE F 1 S TRAPPIST-1e 16 FALSE
## 6 0005 01 Earth FALSE F 0 P PSO J318.5-… 44 FALSE
## # ℹ 8 more variables: RoomService <dbl>, FoodCourt <dbl>, ShoppingMall <dbl>,
## # Spa <dbl>, VRDeck <dbl>, Name <chr>, Transported <lgl>, bills <dbl>
ggplot(train, aes(x = Age, y = bills, color = Transported)) +
geom_point() +
labs(title = "Scatter Plot of Age vs Bills (Colored by Transported Status)", x = "Age", y = "Bills")## Warning: Removed 179 rows containing missing values (`geom_point()`).
# Categorizing passengers into age groups based on the 'Age' column and storing the result in the 'age_group' column
train$age_group <- case_when(
train$Age <= 10 ~ 0,
train$Age <= 20 ~ 1,
train$Age <= 30 ~ 2,
train$Age <= 40 ~ 3,
train$Age <= 50 ~ 4,
train$Age <= 60 ~ 5,
train$Age <= 70 ~ 6,
train$Age <= 80 ~ 7,
TRUE ~ 99
)Bu kod bloğu, “train” veri çerçevesindeki belirli sütunları kategorik kodlamaya dönüştürür. “VIP”, “Destination”, “HomePlanet”, “Transported”, “CryoSleep”, “c_deck”, ve “C_num” sütunları faktör değişkenlere çevrilir. Bu dönüşümler, bu sütunlardaki değerleri kategorik olarak işleyebilmek ve makine öğrenimi modelleri için uygun formata getirebilmek amacıyla yapılır. Bu işlem sonucunda, veri setindeki kategorik değişkenlerin analiz ve modelleme süreçlerinde kullanılabilir hale gelmesi sağlanır.
# Converting certain columns to factors for categorical encoding
train$VIP <- as.factor(train$VIP)
train$Destination <- as.factor(train$Destination)
train$HomePlanet <- as.factor(train$HomePlanet)
train$Transported <- as.factor(train$Transported)
train$CryoSleep <- as.factor(train$CryoSleep)
train$c_deck <- as.factor(train$c_deck)
train$C_num <- as.factor(train$C_num)# Creating a new dataframe 'df' by selecting specific columns from 'train' for modeling
df <- train %>% select(-c(Name, groups, C_num, Age))
# Displaying the first few rows of the new dataframe 'df'
head(df)## # A tibble: 6 × 15
## num HomePlanet CryoSleep c_deck C_side Destination VIP RoomService
## <chr> <fct> <fct> <fct> <chr> <fct> <fct> <dbl>
## 1 01 Europa FALSE B P TRAPPIST-1e FALSE 0
## 2 01 Earth FALSE F S TRAPPIST-1e FALSE 109
## 3 01 Europa FALSE A S TRAPPIST-1e TRUE 43
## 4 02 Europa FALSE A S TRAPPIST-1e FALSE 0
## 5 01 Earth FALSE F S TRAPPIST-1e FALSE 303
## 6 01 Earth FALSE F P PSO J318.5-22 FALSE 0
## # ℹ 7 more variables: FoodCourt <dbl>, ShoppingMall <dbl>, Spa <dbl>,
## # VRDeck <dbl>, Transported <fct>, bills <dbl>, age_group <dbl>
# Load the randomForest package
library(randomForest)
# Train the random forest model
model <- randomForest(Transported ~ ., data = df)
# Making predictions on the training data using the trained model
prediction <- predict(model, df)
# Displaying the first few rows of the predictions
head(prediction)## 1 2 3 4 5 6
## TRUE TRUE FALSE FALSE TRUE TRUE
## Levels: FALSE TRUE
# Creating a confusion matrix to evaluate model performance
conMat <- confusionMatrix(df$Transported, prediction)
# Displaying the confusion matrix
print(conMat)## Confusion Matrix and Statistics
##
## Reference
## Prediction FALSE TRUE
## FALSE 3596 719
## TRUE 25 4353
##
## Accuracy : 0.9144
## 95% CI : (0.9083, 0.9202)
## No Information Rate : 0.5835
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.8286
##
## Mcnemar's Test P-Value : < 2.2e-16
##
## Sensitivity : 0.9931
## Specificity : 0.8582
## Pos Pred Value : 0.8334
## Neg Pred Value : 0.9943
## Prevalence : 0.4165
## Detection Rate : 0.4137
## Detection Prevalence : 0.4964
## Balanced Accuracy : 0.9257
##
## 'Positive' Class : FALSE
##
# Processing the 'PassengerId' and 'Cabin' columns in the test data
test <- separate(test, PassengerId, into = c("groups", "num"), sep = "_")
test <- separate(test, Cabin, into = c("c_deck", "C_num", "C_side"), sep = "/")
# Filling missing values in certain columns with default values
test$HomePlanet <- coalesce(test$HomePlanet, "Earth")
test$Destination <- coalesce(test$Destination, "TRAPPIST-1e")
test$RoomService <- coalesce(test$RoomService, 0)
test$FoodCourt <- coalesce(test$FoodCourt, 0)
test$ShoppingMall <- coalesce(test$ShoppingMall, 0)
test$Spa <- coalesce(test$Spa, 0)
test$VRDeck <- coalesce(test$VRDeck, 0)
test$CryoSleep <- coalesce(test$CryoSleep, as.logical("FALSE"))
summary(test)## groups num HomePlanet CryoSleep
## Length:4277 Length:4277 Length:4277 Mode :logical
## Class :character Class :character Class :character FALSE:2733
## Mode :character Mode :character Mode :character TRUE :1544
##
##
##
##
## c_deck C_num C_side Destination
## Length:4277 Length:4277 Length:4277 Length:4277
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
##
## Age VIP RoomService FoodCourt
## Min. : 0.00 Mode :logical Min. : 0.0 Min. : 0.0
## 1st Qu.:19.00 FALSE:4110 1st Qu.: 0.0 1st Qu.: 0.0
## Median :26.00 TRUE :74 Median : 0.0 Median : 0.0
## Mean :28.66 NA's :93 Mean : 215.1 Mean : 428.6
## 3rd Qu.:37.00 3rd Qu.: 48.0 3rd Qu.: 66.0
## Max. :79.00 Max. :11567.0 Max. :25273.0
## NA's :91
## ShoppingMall Spa VRDeck Name
## Min. : 0.0 Min. : 0.0 Min. : 0.0 Length:4277
## 1st Qu.: 0.0 1st Qu.: 0.0 1st Qu.: 0.0 Class :character
## Median : 0.0 Median : 0.0 Median : 0.0 Mode :character
## Mean : 173.2 Mean : 295.9 Mean : 304.9
## 3rd Qu.: 27.0 3rd Qu.: 43.0 3rd Qu.: 31.0
## Max. :8292.0 Max. :19844.0 Max. :22272.0
##
# Converting certain columns to factors for categorical encoding
test$VIP <- coalesce(test$VIP, as.logical("FALSE"))
test$c_deck <- coalesce(test$c_deck, "N")
test$C_side <- coalesce(test$C_side, "N")
# Calculating the sum of 'RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', and 'VRDeck' columns and storing the result in 'bills' column
test$bills <- test$RoomService + test$FoodCourt + test$ShoppingMall + test$Spa + test$VRDeck
# Categorizing passengers into age groups based on the 'Age' column and storing the result in the 'age_group' column
test$age_group <- case_when(
test$Age <= 10 ~ 0,
test$Age <= 20 ~ 1,
test$Age <= 30 ~ 2,
test$Age <= 40 ~ 3,
test$Age <= 50 ~ 4,
test$Age <= 60 ~ 5,
test$Age <= 70 ~ 6,
test$Age <= 80 ~ 7,
TRUE ~ 99
)
# Converting certain columns to factors for categorical encoding
test$VIP <- as.factor(test$VIP)
test$Destination <- as.factor(test$Destination)
test$HomePlanet <- as.factor(test$HomePlanet)
test$CryoSleep <- as.factor(test$CryoSleep)
test$c_deck <- as.factor(test$c_deck)
test$C_num <- as.factor(test$C_num)
# Creating a new dataframe 'df_test' by selecting specific columns from 'test' for prediction
df_test <- test %>% select(-c(Name, groups, C_num, Age))
summary(df_test)## num HomePlanet CryoSleep c_deck
## Length:4277 Earth :2350 FALSE:2733 F :1445
## Class :character Europa:1002 TRUE :1544 G :1222
## Mode :character Mars : 925 E : 447
## B : 362
## C : 355
## D : 242
## (Other): 204
## C_side Destination VIP RoomService
## Length:4277 55 Cancri e : 841 FALSE:4203 Min. : 0.0
## Class :character PSO J318.5-22: 388 TRUE : 74 1st Qu.: 0.0
## Mode :character TRAPPIST-1e :3048 Median : 0.0
## Mean : 215.1
## 3rd Qu.: 48.0
## Max. :11567.0
##
## FoodCourt ShoppingMall Spa VRDeck
## Min. : 0.0 Min. : 0.0 Min. : 0.0 Min. : 0.0
## 1st Qu.: 0.0 1st Qu.: 0.0 1st Qu.: 0.0 1st Qu.: 0.0
## Median : 0.0 Median : 0.0 Median : 0.0 Median : 0.0
## Mean : 428.6 Mean : 173.2 Mean : 295.9 Mean : 304.9
## 3rd Qu.: 66.0 3rd Qu.: 27.0 3rd Qu.: 43.0 3rd Qu.: 31.0
## Max. :25273.0 Max. :8292.0 Max. :19844.0 Max. :22272.0
##
## bills age_group
## Min. : 0 Min. : 0.000
## 1st Qu.: 0 1st Qu.: 1.000
## Median : 714 Median : 2.000
## Mean : 1418 Mean : 4.389
## 3rd Qu.: 1444 3rd Qu.: 3.000
## Max. :33666 Max. :99.000
##
Bu kod bloğu, “rpart” paketi kullanılarak “Transported” hedef değişkenine dayalı bir Decision Trees (Karar Ağaçları) modeli oluşturur. Bu model daha sonra eğitim veri seti üzerinde uygulanır ve test veri seti için tahminler yapılır. Elde edilen tahminler, bir veri çerçevesine dönüştürülerek “PassengerId” ve “Transported” sütunları içerir. Bu tahminler, bir CSV dosyasına yazılarak kaydedilir ve başlık olarak “submission_decision_trees.csv” verilir. Ayrıca, oluşturulan karışıklık matrisi tablosu “kable” fonksiyonu kullanılarak bir tablo olarak görüntülenir. Son olarak, başlık ve sonuç mesajları yazdırılarak işlemin durumu kullanıcıya bildirilir.
dt_model <- rpart(Transported ~ ., data = df, method = "class")
# Making predictions on the test data using the trained Decision Trees model
dt_prediction <- predict(dt_model, df_test)
dt_submit <- data.frame(PassengerId = passid, Transported = str_to_title(dt_prediction))
colnames(dt_submit) <- c("PassengerId", "Transported") # Set column names
head(dt_submit)## PassengerId Transported
## 1 0013_01 0.213523131672598
## 2 0018_01 0.840932117527862
## 3 0019_01 0.213523131672598
## 4 0021_01 0.340023612750885
## 5 0023_01 0.380622837370242
## 6 0027_01 0.340023612750885
write_csv(dt_submit, "C:/Users/acer/Desktop/final project/submission_decision_trees.csv", col_names = TRUE) # Ensure col_names is TRUE
cat("Decision Trees results saved to 'submission_decision_trees.csv'.\n")## Decision Trees results saved to 'submission_decision_trees.csv'.
Bu kod bloğu, eğitilmiş Naive Bayes modeli kullanılarak test veri seti üzerinde tahminler yapar. Elde edilen tahminler, “PassengerId” ve “Transported” sütunları içeren bir veri çerçevesine dönüştürülür. Bu veri çerçevesinin sütun isimleri belirlenir ve özet istatistiklerini içeren bir özet tablo görüntülenir. Ardından, tahminler bir CSV dosyasına yazılarak kaydedilir ve başlık olarak “submission_naive_bayes.csv” belirlenir. Son olarak, başlık ve sonuç mesajları yazdırılarak işlemin durumu kullanıcıya bildirilir.
# Making predictions on the test data using the trained Naive Bayes model
# Assuming you have trained the Naive Bayes model using train function from the caret package
library(caret)
# Training the Naive Bayes model
nb_model <- train(Transported ~ ., data = df, method = "naive_bayes")
# Making predictions on the test data using the trained Naive Bayes model
nb_prediction <- predict(nb_model, df_test)
nb_submit <- data.frame(PassengerId = passid, Transported = str_to_title(nb_prediction))
colnames(nb_submit) <- c("PassengerId", "Transported") # Set column names
summary(nb_submit)## PassengerId Transported
## Length:4277 Length:4277
## Class :character Class :character
## Mode :character Mode :character
write_csv(nb_submit, "C:/Users/acer/Desktop/final project/submission_naive_bayes.csv", col_names = TRUE)
cat("Naive Bayes results saved to 'submission_naive_bayes.csv'.\n")## Naive Bayes results saved to 'submission_naive_bayes.csv'.
# Train logistic regression model with glmnet
logistic_model <- train(Transported ~ ., data = df, method = "glmnet")
# Make predictions on the test data
logistic_prediction <- predict(logistic_model, df_test)
# Create submission data frame
logistic_submit <- data.frame(PassengerId = passid, Transported = str_to_title(logistic_prediction))
summary(logistic_submit)## PassengerId Transported
## Length:4277 Length:4277
## Class :character Class :character
## Mode :character Mode :character
colnames(logistic_submit) <- c("PassengerId", "Transported") # Set column names
write_csv(logistic_submit, "C:/Users/acer/Desktop/final project/submission_logistic.csv", col_names = TRUE) # Ensure col_names is TRUE
cat("Logistic Regression results saved to 'submission_logistic.csv'.\n")## Logistic Regression results saved to 'submission_logistic.csv'.
# Plot the distribution of Age
ggplot(train, aes(x = Age)) +
geom_histogram(binwidth = 5, fill = "skyblue", color = "black") +
labs(title = "Distribution of Age", x = "Age", y = "Frequency")## Warning: Removed 179 rows containing non-finite values (`stat_bin()`).