Příprava pro modelování

Machine Learning for Marketing in Python

Karolis Urbonas

Head of Analytics & Science, Amazon

Ukázka dat

telco_raw.head()

Záhlaví dat telekomunikační společnosti

Machine Learning for Marketing in Python

Datové typy

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 for Marketing in Python

Oddělení kategorických a numerických sloupců

Identifikátor a název cílové proměnné uložte jako seznamy

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

Názvy kategorických a numerických sloupců uložte jako seznamy

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 for Marketing in Python

One-hot encoding

Typický příklad kategorického sloupce

Barva
Červená
Bílá
Modrá
Červená
Machine Learning for Marketing in Python

Výsledek one-hot encoding

Takto vypadá výsledek po transformaci pomocí one-hot encoding.

Barva Červená Bílá Modrá
Červená ----------> 1 0 0
Bílá ----------> 0 1 0
Modrá ----------> 0 0 1
Červená ----------> 1 0 0
Machine Learning for Marketing in Python

One-hot encoding kategorických proměnných

One-hot encoding kategorických proměnných

telco_raw = pd.get_dummies(data=telco_raw, columns=categorical, drop_first=True)
Machine Learning for Marketing in Python

Škálování numerických příznaků

# Import StandardScaler library
from sklearn.preprocessing import StandardScaler

# Initialize StandardScaler instance scaler = StandardScaler()
# Fit the scaler to numerical columns scaled_numerical = scaler.fit_transform(telco_raw[numerical])
# Build a DataFrame scaled_numerical = pd.DataFrame(scaled_numerical, columns=numerical)
Machine Learning for Marketing in Python

Složení výsledku

# Drop non-scaled numerical columns 
telco_raw = telco_raw.drop(columns=numerical, axis=1)

# Merge the non-numerical with the scaled numerical data telco = telco_raw.merge(right=scaled_numerical, how='left', left_index=True, right_index=True )
Machine Learning for Marketing in Python

Pojďme procvičit předzpracování dat!

Machine Learning for Marketing in Python

Preparing Video For Download...