Applicazioni pratiche di machine learning/Approvazione carte di credito: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Nuova pagina: == Caricamento librerie == <syntaxhighlight lang="rsplus"> library(dplyr) library(caret) library(h2o) </syntaxhighlight> == Parte 1 : Dati == Una banca per decidere se emetter...
 
Nessun oggetto della modifica
Riga 90:
colSums(is.na(df))
</syntaxhighlight>
 
== Modellizzazione ==
 
A partire dal dataset df si creano 2 datasets training e testing, il primo contenente il 70% dei records in modo casuale su cui verrà addestrato il modello e il secondo contenente il restante 30% su cui verrà testato il modello :
 
 
<syntaxhighlight lang="rsplus">
trainIndex <- createDataPartition(df$target,p=0.7, list = FALSE)
training <- df[trainIndex,]
testing <- df[-trainIndex,]
</syntaxhighlight>
 
 
Si utilizza la libreria h2o per cercare automaticamente per 300 secondi un algoritmo di machine learning idoneo per prevedere con una buona accuratezza la variabile target:
 
<syntaxhighlight lang="rsplus">
h2o.init()
</syntaxhighlight>
 
 
H2O is not running yet, starting it now...
 
openjdk version "11.0.8" 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)
 
Starting H2O JVM and connecting: .... Connection successful!
 
R is connected to the H2O cluster:
H2O cluster uptime: 5 seconds 862 milliseconds
H2O cluster timezone: Europe/Rome
H2O data parsing timezone: UTC
H2O cluster version: 3.28.0.2
H2O cluster version age: 8 months and 13 days !!!
H2O cluster name: H2O_started_from_R_gian_kyb058
H2O cluster total nodes: 1
H2O cluster total memory: 1.92 GB
H2O cluster total cores: 2
H2O cluster allowed cores: 2
H2O cluster healthy: TRUE
H2O Connection ip: localhost
H2O Connection port: 54321
H2O Connection proxy: NA
H2O Internal Security: FALSE
H2O API Extensions: Amazon S3, XGBoost, Algos, AutoML, Core V3, TargetEncoder, Core V4
R Version: R version 3.6.3 (2020-02-29)
 
 
 
<syntaxhighlight lang="rsplus">
df_hf <- as.h2o(training)
y <- "target"
x <- names(training)[2:18]
 
aml <- h2o.automl(x = x, y = y,
training_frame = df_hf,
max_runtime_secs = 300)
</syntaxhighlight>
 
Come algoritmo viene trovato XGBoost
 
 
<syntaxhighlight lang="rsplus">
lb <- aml@leaderboard
lb
</syntaxhighlight>
1 XGBoost_1_AutoML_20201003_190717
 
 
Il modello trovato si prova sul dataset di testing...
 
 
<syntaxhighlight lang="rsplus">
test <- as.h2o(testing)
model <- aml@leader
p1 = h2o.predict(model, newdata=test)
 
</syntaxhighlight>
 
Si calcola l'accuratezza che è del 99,53% sul testing set:
 
<syntaxhighlight lang="rsplus">
df2 <- as.data.frame(p1$predict)
 
mean(df2$predict==testing$target)
</syntaxhighlight>
 
[1] 0.9953368
 
Si costruisce la matrice di confusione e si trova l'accuratezza del 99,53% con una specificity del 34,55% che indica la possibilità di trovare molti falsi positivi sebbene il modello sia molto accurato:
 
<syntaxhighlight lang="rsplus">
confusionMatrix(df2$predict,testing$target)
</syntaxhighlight>
 
Confusion Matrix and Statistics
 
Reference
Prediction 0 1
0 231933 555
1 533 293
Accuracy : 0.9953
95% CI : (0.9951, 0.9956)
No Information Rate : 0.9964
P-Value [Acc > NIR] : 1.0000
Kappa : 0.3477
Mcnemar's Test P-Value : 0.5243
Sensitivity : 0.9977
Specificity : 0.3455
Pos Pred Value : 0.9976
Neg Pred Value : 0.3547
Prevalence : 0.9964
Detection Rate : 0.9941
Detection Prevalence : 0.9965
Balanced Accuracy : 0.6716
'Positive' Class : 0
 
 
{{avanzamento|75%|4 ottobre 2020}}
[[Categoria:Applicazioni pratiche di machine learning|/Approvazione carte di credito]]