


library(data.table)

library(caret)

library(gbm)
train<-fread("../input/train_numeric.csv",nrows=400000,select=c( "L3_S32_F3850","L3_S33_F3855","L1_S24_F1846","L3_S33_F3859","L1_S24_F1723","L3_S34_F3882","L1_S24_F1842","L3_S29_F3351","L3_S29_F3407","L3_S30_F3544"  ,
"Response"))




test<-fread("../input/test_numeric.csv",select=c(  "L3_S32_F3850","L3_S33_F3855","L1_S24_F1846","L3_S33_F3859","L1_S24_F1723","L3_S34_F3882","L1_S24_F1842","L3_S29_F3351","L3_S29_F3407","L3_S30_F3544" ))




testId<-fread("../input/test_numeric.csv",select="Id")






                           

LogLossBinary = function(actual, predicted, eps = 1e-15) {  
    predicted = pmin(pmax(predicted, eps), 1-eps)  
    - (sum(actual * log(predicted) + (1 - actual) * log(1 - predicted))) / length(actual)
}







#splitIndex <- createDataPartition(train1[,outcomeName,with=F], p = .75, list = FALSE, times = 1)
#train2 <- train1[ splitIndex,]
#test1  <- train1[-splitIndex,]



gbmModel = gbm(formula = Response ~.,
             
               data = train,
               n.trees = 2500,
               shrinkage = .01,
               n.minobsinnode = 20)


gbmTrainPredictions= predict(object = gbmModel,
                             newdata = train,
                             n.trees = 1000,
                             type = "response")
                             
labelst_m<-train$Response


mcc <- function(y_true, y_prob) {
  DT <- data.table(y_true = y_true, y_prob = y_prob, key="y_prob")
  
  nump <- sum(y_true)
  numn <- length(y_true)- nump
  
  DT[, tn_v:= cumsum(as.numeric(y_true == 0))]
  DT[, fp_v:= cumsum(as.numeric(y_true == 1))]
  DT[, fn_v:= numn - tn_v]
  DT[, tp_v:= nump - fp_v]
  DT[, tp_v:= nump - fp_v]
  DT[, mcc_v:= (tp_v * tn_v - fp_v * fn_v) / sqrt((tp_v + fp_v) * (tp_v + fn_v) * (tn_v + fp_v) * (tn_v + fn_v))]
  DT[, mcc_v:= ifelse(!is.finite(mcc_v), 0, mcc_v)]
  
  return(max(DT[['mcc_v']]))
}
mcc_eval <- function(y_prob, dtrain) {
  y_true <- getinfo(dtrain, "label")
  best_mcc <- mcc(y_true, y_prob)
  return(list(metric="MCC", value=best_mcc))
}



matt <- data.table(thresh = seq(0.99, 0.9999, by = 0.0001))
matt$scores <- sapply(matt$thresh, FUN = function(x) mcc(labelst_m, (gbmTrainPredictions > quantile(gbmTrainPredictions, x)) * 1))
threshold <- matt$thresh[which(matt$scores == max(matt$scores))]
threshold







gbmTestPredictions = predict(object = gbmModel,
                             newdata = test,
                             n.trees = 1000,
                             type = "response")
                             
 gbmTestPredictions<- (gbmTestPredictions> quantile(gbmTestPredictions, threshold)) * 1                           
outputDataSet = data.frame("Id" = testId$Id,
                           "Response" = gbmTestPredictions)
                           
                           
write.csv(outputDataSet,"sample.csv",row.names=F)

