Préparation pour la modélisation

Machine Learning pour le marketing en Python

Karolis Urbonas

Head of Analytics & Science, Amazon

Échantillon de données

telco_raw.head()

En-tête des données télécom

Machine Learning pour le marketing en Python

Types de données

telco_raw.dtypes
customerID           object
gender               object
SeniorCitizen        object
Partner              object
Dependents           object
tenure                int64
PhoneService         object
MultipleLines        object
InternetService      object

OnlineSecurity       object
OnlineBackup         object
DeviceProtection     object
TechSupport          object
StreamingTV          object
StreamingMovies      object
Contract             object
PaperlessBilling     object
PaymentMethod        object
MonthlyCharges      float64
TotalCharges        float64
Churn                object
Machine Learning pour le marketing en Python

Séparer les colonnes catégorielles et numériques

Séparez les noms de l'identifiant et de la cible en listes

custid = ['customerID']
target = ['Churn']

Séparez les noms de colonnes catégorielles et numériques en listes

categorical = telco_raw.nunique()[telcom.nunique()<10].keys().tolist()

categorical.remove(target[0])
numerical = [col for col in telco_raw.columns if col not in custid+target+categorical]
Machine Learning pour le marketing en Python

Encodage one-hot

Voici une colonne typique de type catégoriel

Couleur
Rouge
Blanc
Bleu
Rouge
Machine Learning pour le marketing en Python

Résultat de l'encodage one-hot

Voici le résultat après transformation par encodage one-hot.

Couleur Rouge Blanc Bleu
Rouge ----------> 1 0 0
Blanc ----------> 0 1 0
Bleu ----------> 0 0 1
Rouge ----------> 1 0 0
Machine Learning pour le marketing en Python

Encodage one-hot des variables catégorielles

Encodage one-hot des variables catégorielles

telco_raw = pd.get_dummies(data=telco_raw, columns=categorical, drop_first=True)
Machine Learning pour le marketing en Python

Mise à l'échelle des variables numériques

# Importer la bibliothèque StandardScaler
from sklearn.preprocessing import StandardScaler

# Initialiser une instance de StandardScaler scaler = StandardScaler()
# Ajuster le scaler aux colonnes numériques scaled_numerical = scaler.fit_transform(telco_raw[numerical])
# Construire un DataFrame scaled_numerical = pd.DataFrame(scaled_numerical, columns=numerical)
Machine Learning pour le marketing en Python

Réunir le tout

# Supprimer les colonnes numériques non mises à l'échelle 
telco_raw = telco_raw.drop(columns=numerical, axis=1)

# Fusionner les données non numériques avec les numériques mises à l'échelle telco = telco_raw.merge(right=scaled_numerical, how='left', left_index=True, right_index=True )
Machine Learning pour le marketing en Python

Passons à la pratique du prétraitement des données !

Machine Learning pour le marketing en Python

Preparing Video For Download...