[빅데이터분석기사] 실기기출 4회(작업형 2)

동훈·2025년 6월 24일

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()

#target = 'Segmentation'
print(train.shape, test.shape)
train.info()

train.head()

test.describe(include = 'O')

train['Segmentation'].value_counts()

# 전처리 및 인코딩 및 스케일링
#one-hot

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)

# 범주형 수치형 분할
# train_oh = train.select_dtypes(include = 'object')
# train_sc = train.select_dtypes(exclude = 'object')
# test_oh = test.select_dtypes(include = 'object')
# test_sc = test.select_dtypes(exclude = 'object')


#train.select_dtypes(exclude = 'object').columns
#스케일링 minmax
# cols =['Age', 'Work_Experience', 'Family_Size']
# from sklearn.preprocessing import MinMaxScaler
# scaler = MinMaxScaler()
# train_sc[cols] = scaler.fit_transform(train_sc[cols])
# test_sc[cols] = scaler.transform(test_sc[cols])

# train_oh = pd.get_dummies(train_oh)
# test_oh = pd.get_dummies(test_oh)

# train = pd.concat([train_oh, train_sc], axis = 1)
# test = pd.concat([test_oh, test_sc], axis = 1)
# 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))
# 0.5393848462115529

#로지스틱회귀
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)
profile
성실함 한스쿱

0개의 댓글