Datayı Okuma

Bu satırla datayı csv dosyasından okuyoruz

csgo_data <- read.csv('csgo/cs-go.csv')

Daha sonra datayı train ve test olmak üzere ikiye ayırıyoruz

split_index <- 20000
csgo_train_indices <- sample(seq_len(nrow(csgo_data)), size=split_index)
csgo_train <- csgo_data[-csgo_train_indices, ]
csgo_test <- csgo_data[csgo_train_indices, ]

Datayı train ve teste böldükten sonra bu parçaları X ve y olmak üzere ayrı kümelere ayırıyoruz. X örneğn değerlerini y ise maç sonucunu tutuyor. Bunları X ve y ye ayırdıktan sonra aynı zamanda bazı değer dönüşümleri yapmamız gerekiyor çünkü bu şekilde modelleri eğitirken hata almaktan kaçınmış oluyoruz. y için ‘CT’ ve ‘T’ değerleri yerine 1 ve 0 getiriyoruz. Aynı zamanda X’teki “bomb_planted” kolonu için ‘True’ ve ‘False’ yerine aynı şekilde 0 ve 1 getiriyoruz. X’te çok fazla parametre olduğu için parametrelerden daha öncelikli olanları seçiyoruz. Aksi taktirde model boyutları çok yüksek olduğundan çoğu bilgisayarda çalıştırmak imkansız oluyor.

ytrain <- csgo_train$round_winner
ytrain <- ifelse(ytrain == 'CT', 1, 0)
xtrain <- csgo_train[c('ct_score', 't_score', 'bomb_planted', 'ct_health', 't_health', 'ct_armor', 't_armor', 'ct_players_alive', 't_players_alive')]
xtrain$bomb_planted <- ifelse(xtrain$bomb_planted == 'True', 1, 0)

ytest <- csgo_test$round_winner
ytest <- ifelse(ytest == 'CT', 1, 0)
xtest <- csgo_test[c('ct_score', 't_score', 'bomb_planted', 'ct_health', 't_health', 'ct_armor', 't_armor', 'ct_players_alive', 't_players_alive')]
xtest$bomb_planted <- ifelse(xtest$bomb_planted == 'True', 1, 0)

Modellerin Eğitimi

Şu şekilde sırasıyla 5 model eğittik:

Decision Tree

library(rpart)
tree_model <- rpart(ytrain ~ ., data=xtrain, method='class')
tree_model_preds <- predict(tree_model, xtest, type='class')
plot(tree_model)
text(tree_model, use.n=TRUE)

Naive Bayes

library(e1071)
nb_model <- naiveBayes(ytrain ~ ., data=xtrain)
nb_model_preds <- predict(nb_model, xtest)

Lineer Discriminant Analiz

library(MASS)
lda_model <- lda(ytrain ~ ., data=xtrain)
lda_model_preds <- predict(lda_model, xtest)$class
plot(lda_model)

Gradient Boosting Machine

library(gbm)
## Loaded gbm 2.1.9
## This version of gbm is no longer under development. Consider transitioning to gbm3, https://github.com/gbm-developers/gbm3
gbm_model <- gbm(ytrain ~ ., data=xtrain, distribution='bernoulli', n.trees=100)
gbm_model_preds <- predict(gbm_model, xtest, n.trees=100, type='response')
gbm_model_preds <- ifelse(gbm_model_preds > 0.5, 1, 0)

gbm_stats <- gbm.perf(gbm_model, method = "OOB")
## OOB generally underestimates the optimal number of iterations although predictive performance is reasonably competitive. Using cv_folds>1 when calling gbm usually results in improved predictive performance.

plot(gbm_model, which=1)

XGBoost

library(xgboost)
xgb_model <- xgboost(data=model.matrix(~., data=xtrain), label=ytrain, nround=100)
## [1]  train-rmse:0.453123 
## [2]  train-rmse:0.427159 
## [3]  train-rmse:0.412852 
## [4]  train-rmse:0.405146 
## [5]  train-rmse:0.400211 
## [6]  train-rmse:0.397756 
## [7]  train-rmse:0.396035 
## [8]  train-rmse:0.394792 
## [9]  train-rmse:0.393742 
## [10] train-rmse:0.393155 
## [11] train-rmse:0.392325 
## [12] train-rmse:0.391777 
## [13] train-rmse:0.391197 
## [14] train-rmse:0.390387 
## [15] train-rmse:0.389751 
## [16] train-rmse:0.389021 
## [17] train-rmse:0.388753 
## [18] train-rmse:0.388578 
## [19] train-rmse:0.388258 
## [20] train-rmse:0.387654 
## [21] train-rmse:0.386986 
## [22] train-rmse:0.386856 
## [23] train-rmse:0.386391 
## [24] train-rmse:0.386005 
## [25] train-rmse:0.385597 
## [26] train-rmse:0.385194 
## [27] train-rmse:0.384826 
## [28] train-rmse:0.384349 
## [29] train-rmse:0.383954 
## [30] train-rmse:0.383552 
## [31] train-rmse:0.383299 
## [32] train-rmse:0.383081 
## [33] train-rmse:0.382977 
## [34] train-rmse:0.382652 
## [35] train-rmse:0.382191 
## [36] train-rmse:0.381740 
## [37] train-rmse:0.381209 
## [38] train-rmse:0.380947 
## [39] train-rmse:0.380745 
## [40] train-rmse:0.380404 
## [41] train-rmse:0.380314 
## [42] train-rmse:0.380263 
## [43] train-rmse:0.380025 
## [44] train-rmse:0.379867 
## [45] train-rmse:0.379631 
## [46] train-rmse:0.379444 
## [47] train-rmse:0.379018 
## [48] train-rmse:0.378524 
## [49] train-rmse:0.378233 
## [50] train-rmse:0.377965 
## [51] train-rmse:0.377573 
## [52] train-rmse:0.377092 
## [53] train-rmse:0.376787 
## [54] train-rmse:0.376552 
## [55] train-rmse:0.376152 
## [56] train-rmse:0.375946 
## [57] train-rmse:0.375584 
## [58] train-rmse:0.375285 
## [59] train-rmse:0.375225 
## [60] train-rmse:0.375202 
## [61] train-rmse:0.374821 
## [62] train-rmse:0.374618 
## [63] train-rmse:0.374342 
## [64] train-rmse:0.373990 
## [65] train-rmse:0.373739 
## [66] train-rmse:0.373451 
## [67] train-rmse:0.373223 
## [68] train-rmse:0.372869 
## [69] train-rmse:0.372671 
## [70] train-rmse:0.372333 
## [71] train-rmse:0.372119 
## [72] train-rmse:0.371933 
## [73] train-rmse:0.371669 
## [74] train-rmse:0.371396 
## [75] train-rmse:0.371160 
## [76] train-rmse:0.370907 
## [77] train-rmse:0.370774 
## [78] train-rmse:0.370437 
## [79] train-rmse:0.370152 
## [80] train-rmse:0.369945 
## [81] train-rmse:0.369871 
## [82] train-rmse:0.369707 
## [83] train-rmse:0.369667 
## [84] train-rmse:0.369571 
## [85] train-rmse:0.369506 
## [86] train-rmse:0.369485 
## [87] train-rmse:0.369375 
## [88] train-rmse:0.368991 
## [89] train-rmse:0.368642 
## [90] train-rmse:0.368336 
## [91] train-rmse:0.368127 
## [92] train-rmse:0.367773 
## [93] train-rmse:0.367736 
## [94] train-rmse:0.367502 
## [95] train-rmse:0.367297 
## [96] train-rmse:0.367176 
## [97] train-rmse:0.366894 
## [98] train-rmse:0.366803 
## [99] train-rmse:0.366686 
## [100]    train-rmse:0.366373
xgb_model_preds <- predict(xgb_model, model.matrix(~., data=xtest))
xgb_model_preds <- ifelse(xgb_model_preds > 0.5, 1, 0)

importance_matrix <- xgb.importance(feature_names = colnames(model.matrix(~., xtrain)), model = xgb_model)

xgb.plot.importance(importance_matrix)

Test Sonuçları

Modellerin eğitimi tamamlandıktan sonra hepsinin test sonuçlarını değerlendiriyoruz.

library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
ytest <- factor(ytest, levels = c('0', '1'))
gbm_model_preds <- factor(gbm_model_preds, levels = c('0', '1'))
xgb_model_preds <- factor(xgb_model_preds, levels = c('0', '1'))
cm_tree <- confusionMatrix(tree_model_preds, ytest)
cm_nb <- confusionMatrix(nb_model_preds, ytest)
cm_lda <- confusionMatrix(lda_model_preds, ytest)
cm_gbm <- confusionMatrix(gbm_model_preds, ytest)
cm_xgb <- confusionMatrix(xgb_model_preds, ytest)

Daha sonra bu değerlerin hepsini yan yana bir grafiğe bastıralım

accuracy_tree <- cm_tree$overall['Accuracy']
accuracy_nb <- cm_nb$overall['Accuracy']
accuracy_lda <- cm_lda$overall['Accuracy']
accuracy_gbm <- cm_gbm$overall['Accuracy']
accuracy_xgb <- cm_xgb$overall['Accuracy']

accuracy_df <- data.frame(
  Model = c("Tree", "NB", "LDA", "GBM", "XGB"),
  Accuracy = c(accuracy_tree, accuracy_nb, accuracy_lda, accuracy_gbm, accuracy_xgb)
)

library(ggplot2)

ggplot(accuracy_df, aes(x = Model, y = Accuracy, fill = Model)) +
  geom_bar(stat = "identity") +
  labs(title = "Model Başarı Karşılaştırılması", x = "Model", y = "Accuracy") +
  theme_minimal()

Aynı zamanda accuracy değernin yanında diğer değerleri de görmek istersek diye onları da yazı olarak bastırıyorum.

confusionMatrix(tree_model_preds, ytest)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    0    1
##          0 6815 2054
##          1 3303 7828
##                                          
##                Accuracy : 0.7322         
##                  95% CI : (0.726, 0.7383)
##     No Information Rate : 0.5059         
##     P-Value [Acc > NIR] : < 2.2e-16      
##                                          
##                   Kappa : 0.465          
##                                          
##  Mcnemar's Test P-Value : < 2.2e-16      
##                                          
##             Sensitivity : 0.6736         
##             Specificity : 0.7921         
##          Pos Pred Value : 0.7684         
##          Neg Pred Value : 0.7033         
##              Prevalence : 0.5059         
##          Detection Rate : 0.3407         
##    Detection Prevalence : 0.4435         
##       Balanced Accuracy : 0.7328         
##                                          
##        'Positive' Class : 0              
## 
confusionMatrix(nb_model_preds, ytest)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    0    1
##          0 5157 1068
##          1 4961 8814
##                                           
##                Accuracy : 0.6986          
##                  95% CI : (0.6921, 0.7049)
##     No Information Rate : 0.5059          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.3998          
##                                           
##  Mcnemar's Test P-Value : < 2.2e-16       
##                                           
##             Sensitivity : 0.5097          
##             Specificity : 0.8919          
##          Pos Pred Value : 0.8284          
##          Neg Pred Value : 0.6399          
##              Prevalence : 0.5059          
##          Detection Rate : 0.2579          
##    Detection Prevalence : 0.3113          
##       Balanced Accuracy : 0.7008          
##                                           
##        'Positive' Class : 0               
## 
confusionMatrix(lda_model_preds, ytest)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    0    1
##          0 7432 2600
##          1 2686 7282
##                                           
##                Accuracy : 0.7357          
##                  95% CI : (0.7295, 0.7418)
##     No Information Rate : 0.5059          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.4714          
##                                           
##  Mcnemar's Test P-Value : 0.2424          
##                                           
##             Sensitivity : 0.7345          
##             Specificity : 0.7369          
##          Pos Pred Value : 0.7408          
##          Neg Pred Value : 0.7305          
##              Prevalence : 0.5059          
##          Detection Rate : 0.3716          
##    Detection Prevalence : 0.5016          
##       Balanced Accuracy : 0.7357          
##                                           
##        'Positive' Class : 0               
## 
confusionMatrix(gbm_model_preds, ytest)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    0    1
##          0 6690 1750
##          1 3428 8132
##                                          
##                Accuracy : 0.7411         
##                  95% CI : (0.735, 0.7472)
##     No Information Rate : 0.5059         
##     P-Value [Acc > NIR] : < 2.2e-16      
##                                          
##                   Kappa : 0.4832         
##                                          
##  Mcnemar's Test P-Value : < 2.2e-16      
##                                          
##             Sensitivity : 0.6612         
##             Specificity : 0.8229         
##          Pos Pred Value : 0.7927         
##          Neg Pred Value : 0.7035         
##              Prevalence : 0.5059         
##          Detection Rate : 0.3345         
##    Detection Prevalence : 0.4220         
##       Balanced Accuracy : 0.7421         
##                                          
##        'Positive' Class : 0              
## 
confusionMatrix(xgb_model_preds, ytest)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction    0    1
##          0 7587 2120
##          1 2531 7762
##                                           
##                Accuracy : 0.7674          
##                  95% CI : (0.7615, 0.7733)
##     No Information Rate : 0.5059          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.5351          
##                                           
##  Mcnemar's Test P-Value : 1.834e-09       
##                                           
##             Sensitivity : 0.7499          
##             Specificity : 0.7855          
##          Pos Pred Value : 0.7816          
##          Neg Pred Value : 0.7541          
##              Prevalence : 0.5059          
##          Detection Rate : 0.3794          
##    Detection Prevalence : 0.4854          
##       Balanced Accuracy : 0.7677          
##                                           
##        'Positive' Class : 0               
## 

Örnek Tahminleme

Bu kısımda da örnek bir sample seçip üzerinde lda modeli ile tahmin uyguluyoruz

prediction <- predict(lda_model, xtest[10, ])$class
prediction
## [1] 0
## Levels: 0 1