建模前的准备

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 营销中的机器学习

独热编码

这是一个典型的分类列

颜色
Python 营销中的机器学习

独热编码结果

经过独热编码后如下所示。

颜色
----------> 1 0 0
----------> 0 1 0
----------> 0 0 1
----------> 1 0 0
Python 营销中的机器学习

对分类变量进行独热编码

对分类变量进行独热编码

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...