Applicazioni pratiche di machine learning/Previsioni su direct mail: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
mNessun oggetto della modifica
m colorazione codice R
Riga 1:
==Caricamento librerie ==
 
<syntaxhighlight lang="rsplus">
library(data.table)
library(readr)
Line 9 ⟶ 10:
library(caTools)
library(caret)
</syntaxhighlight>
 
 
==Parte 1: Dati==
Line 24 ⟶ 25:
 
'''Caricamento dei dati:'''
<syntaxhighlight lang="rsplus">
 
cat("sampling train to get around 8GB memory limitations\n")
 
train <- read_csv("train.csv", n_max=40000)
test <- read_csv("test.csv")
</syntaxhighlight>
 
'''Panoramica dei dati:'''
<syntaxhighlight lang="rsplus">
 
# 1. numero di righe e colonne in train and test datasets
 
dim(train)
dim(test)
</syntaxhighlight>
 
[1] 40000 1934
Line 42 ⟶ 45:
# 2. Do un'occhiata alla cima e alla coda dei dati di training
 
<syntaxhighlight lang="rsplus">
head(train)
tail(train)
</syntaxhighlight>
 
ID VAR_0001 VAR_0002 VAR_0003 VAR_0004 VAR_0005 VAR_0006 VAR_0007 VAR_0008
Line 57 ⟶ 62:
'''Pulizia dei dati:'''
 
<syntaxhighlight lang="rsplus">
cat("sostituisco valori mancanti con -1\n")
train[is.na(train)] <- -1
Line 62 ⟶ 68:
test[which(is.na(test[,i])),i]<- -1
}
</syntaxhighlight>
 
'''Rimuovo le variabili costanti:'''
<syntaxhighlight lang="rsplus">
 
v<-c()
for (i in 1:ncol(train)) {
Line 75 ⟶ 82:
train = train[, !names(train) %in% v]
test = test[, !names(test) %in% v]
</syntaxhighlight>
 
'''Rimuovo le variabili duplicate:'''
<syntaxhighlight lang="rsplus">
 
map <-hashmap("0","0")
cols <- names(train)
Line 98 ⟶ 106:
train = train[, !names(train) %in% map$keys()]
test = test[, !names(test) %in% dup_cols]
</syntaxhighlight>
 
==Parte 2: Domanda di ricerca ==
Line 107 ⟶ 116:
 
==Parte 3: Modellizzazione ==
 
<syntaxhighlight lang="rsplus">
feature.names <- names(train)[2:ncol(train)-1]
cat("trasformo le variabile categoriche in numeriche\n")
Line 116 ⟶ 127:
}
}
 
 
 
cat("Per creare il modello si utilizza l'algoritmo di Boosting xgboost\n")
Line 125 ⟶ 134:
objective = "binary:logistic",
eval_metric = "auc")
</syntaxhighlight>
 
==Parte 4: Previsione ==
Line 131 ⟶ 141:
Vengono considerati clienti validi, quelli che risponderanno alla direct mail oltrepassando il 70% di probabilità.
 
<syntaxhighlight lang="rsplus">
p1 = predict(clf, data.matrix(train[,feature.names]))
p <- rep(0,nrow(train))
Line 138 ⟶ 149:
auc <-colAUC(p1, train$target)
cat('Boosting AUC in training set: ', auc, '\n', sep='')
</syntaxhighlight>
 
[[File:Confusion Matrix in RStudio.png|frame|centro|]]
Line 143 ⟶ 155:
Ora è possibile sapere quale sarà la probabilità di rispondere alle direct mail da parte di clienti contenuti nel testing set
 
<syntaxhighlight lang="rsplus">
cat("making predictions in batches due to 8GB memory limitation\n")
submission <- data.frame(ID=test$ID)
Line 149 ⟶ 162:
submission[rows, "target"] <- predict(clf, data.matrix(test[rows,feature.names]))
}
 
 
 
df <-submission %>%
Line 157 ⟶ 168:
 
head(df)
 
 
df %>%
ggplot(aes(ID,target, colour= ValuedCustomer)) + geom_point() +
ggtitle("Valued Customers with a probability of more ", subtitle = "than 70% to respond to direct mail")
</syntaxhighlight>
 
[[File:Validi clienti 2.png|frame|centro]]