Q. [마케팅] 자동차 시장 세분화
- 자동차 회사는 새로운 전략을 수립하기 위해 4개의 시장으로 세분화했습니다.
- 기존 고객 분류 자료를 바탕으로 신규 고객이 어떤 분류에 속할지 예측해주세요!
- 예측할 값(y): "Segmentation" (1,2,3,4)
- 평가: Macro f1-score
- data: train.csv, test.csv
- 제출 형식:
ID,Segmentation
458989,1
458994,2
459000,3
459003,4
답안 제출 참고
- 아래 코드 예측변수와 수험번호를 개인별로 변경하여 활용
- pd.DataFrame({'ID': test.ID, 'Segmentation': pred}).to_csv('003000000.csv', index=False)
노트북 구분
- basic: 수치형 데이터만 활용 -> 학습 및 test데이터 예측
- intermediate: 범주형 데이터도 활용 -> 학습 및 test데이터 예측
- advanced: 학습 및 교차 검증(모델 평가) -> 하이퍼파라미터 튜닝 -> test데이터 예측
import pandas as pd
train = pd.read_csv()
test = pd.read_csv()
print(train.shape, test.shape)
train.info()
train.head()
test.describe(include = 'O')
train['Segmentation'].value_counts()
from sklearn.preprocessing import LabelEncoder
cols = train.select_dtypes(include = 'object').columns
le = LabelEncoder()
for col in cols:
train[col] = le.fit_transform(train[col])
test[col] = le.transform(test[col])
print(train.shape, test.shape)
target = train.pop('Segmentation')
train.drop('ID', axis = 1 , inplace = True)
test_id = test.pop('ID')
print(train.shape, test.shape)
from sklearn.model_selection import train_test_split
X_tr, X_val, y_tr, y_val = train_test_split(train, target, test_size = 0.2, random_state = 2022)
print(X_tr.shape, X_val.shape, y_tr.shape, y_val.shape)
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(random_state = 2022, max_depth = 7, n_estimators = 1000)
rf.fit(X_tr, y_tr)
rf_pred = rf.predict(X_val)
print(accuracy_score(y_val, rf_pred))
from sklearn.linear_model import LogisticRegression
lo = LogisticRegression()
lo.fit(X_tr, y_tr)
lo_pred = lo.predict(X_val)
print(accuracy_score(y_val, lo_pred))
pred= rf.predict(test)
answer = pd.DataFrame({'ID' : test_id,
'Segmentation' : pred}).to_csv("result.csv", index = False)