การเตรียมข้อมูลสำหรับโมเดล

Machine Learning สำหรับการตลาดด้วย Python

Karolis Urbonas

Head of Analytics & Science, Amazon

ตัวอย่างข้อมูล

telco_raw.head()

ส่วนหัวของข้อมูลโทรคมนาคม

Machine Learning สำหรับการตลาดด้วย 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
Machine Learning สำหรับการตลาดด้วย Python

แยกคอลัมน์ categorical และ numerical

แยกชื่อตัวระบุและตัวแปรเป้าหมายเป็นลิสต์

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

แยกชื่อคอลัมน์ categorical และ numeric เป็นลิสต์

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 สำหรับการตลาดด้วย Python

One-hot encoding

นี่คือตัวอย่างคอลัมน์ข้อมูลประเภท categorical ทั่วไป

Color
Red
White
Blue
Red
Machine Learning สำหรับการตลาดด้วย Python

ผลลัพธ์ของ one-hot encoding

และนี่คือผลลัพธ์หลังแปลงด้วย one-hot encoding

Color Red White Blue
Red ----------> 1 0 0
White ----------> 0 1 0
Blue ----------> 0 0 1
Red ----------> 1 0 0
Machine Learning สำหรับการตลาดด้วย Python

ทำ one-hot encoding กับตัวแปร categorical

ทำ one-hot encoding กับตัวแปร categorical

telco_raw = pd.get_dummies(data=telco_raw, columns=categorical, drop_first=True)
Machine Learning สำหรับการตลาดด้วย 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)
Machine Learning สำหรับการตลาดด้วย 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 )
Machine Learning สำหรับการตลาดด้วย Python

มาฝึกการเตรียมข้อมูลกันเถอะ!

Machine Learning สำหรับการตลาดด้วย Python

Preparing Video For Download...