建模前的準備

Python 的行銷機器學習

Karolis Urbonas

Head of Analytics & Science, Amazon

資料範例

telco_raw.head()

電信資料表頭

Python 的行銷機器學習

資料型別

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
Python 的行銷機器學習

分開類別與數值欄位

將識別欄與目標變數名稱分成清單

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

將類別與數值欄位名稱分成清單

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]
Python 的行銷機器學習

One-hot encoding

這是一個典型的類別型欄位

顏色
Python 的行銷機器學習

One-hot encoding 結果

這是使用 one-hot encoding 轉換後的樣子。

顏色
----------> 1 0 0
----------> 0 1 0
----------> 0 0 1
----------> 1 0 0
Python 的行銷機器學習

對類別變數做 one-hot encoding

對類別變數做 one-hot encoding

telco_raw = pd.get_dummies(data=telco_raw, columns=categorical, drop_first=True)
Python 的行銷機器學習

縮放數值特徵

# 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)
Python 的行銷機器學習

整合全部處理

# 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 )
Python 的行銷機器學習

一起來練習資料前處理!

Python 的行銷機器學習

Preparing Video For Download...