모델링 준비

Python으로 배우는 마케팅용 Machine Learning

Karolis Urbonas

Head of Analytics & Science, Amazon

데이터 샘플

telco_raw.head()

통신 데이터 헤더

Python으로 배우는 마케팅용 Machine Learning

데이터 타입

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으로 배우는 마케팅용 Machine Learning

범주형/수치형 컬럼 분리

식별자와 타깃 변수명을 리스트로 분리

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으로 배우는 마케팅용 Machine Learning

원-핫 인코딩

전형적인 범주형 열 예시입니다

색상
Red
White
Blue
Red
Python으로 배우는 마케팅용 Machine Learning

원-핫 인코딩 결과

원-핫 인코딩으로 변환하면 다음과 같습니다.

색상 Red White Blue
Red ----------> 1 0 0
White ----------> 0 1 0
Blue ----------> 0 0 1
Red ----------> 1 0 0
Python으로 배우는 마케팅용 Machine Learning

범주형 변수 원-핫 인코딩

범주형 변수 원-핫 인코딩

telco_raw = pd.get_dummies(data=telco_raw, columns=categorical, drop_first=True)
Python으로 배우는 마케팅용 Machine Learning

수치형 특성 스케일링

# 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으로 배우는 마케팅용 Machine Learning

모두 합치기

# 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으로 배우는 마케팅용 Machine Learning

데이터 전처리를 연습해 봅시다!

Python으로 배우는 마케팅용 Machine Learning

Preparing Video For Download...