본문 바로가기
자격증 공부/빅데이터분석기사

빅데이터 분석기사 실기 예제 - 작업형 2유형(2)

by 해모해모 2023. 6. 18.
728x90
반응형

문제

코드
### EDA ###

# 라이브러리 불러오기
import pandas as pd

# 데이터 불러오기
train = pd.read_csv("../input/big-data-analytics-certification-kr-2022/train.csv")
test = pd.read_csv("../input/big-data-analytics-certification-kr-2022/test.csv")

# 데이터 크기 확인
train.shape, test.shape

# train 샘플 확인
train.head()

# test 샘플 확인 
test.head()

# target 확인
train['Segmentation'].value_counts()

# 결측치 확인(train)
train.isnull().sum()

# 결측치 확인(test)
test.isnull().sum()

# type 확인
train.info()

---------------------------------------------------------------------------------------------
### 전처리 ###

# target(y, label) 값 복사
target = train.pop('Segmentation')
target

# test데이터 ID 복사
test_ID = test.pop('ID')

# 수치형 컬럼(train)
# ['ID', 'Age', 'Work_Experience', 'Family_Size', 'Segmentation']
num_cols = ['Age', 'Work_Experience', 'Family_Size']
train = train[num_cols]
train.head(2)

# 수치형 컬럼(test)
test = test[num_cols]
test.head(2)

---------------------------------------------------------------------------------------------
### model 학습 및 예측 ###

# 모델 선택 및 학습
from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(random_state=0)
rf.fit(train, target)
pred = rf.predict(test)
pred

# 예측 결과 -> 데이터 프레임
# pd.DataFrame({'cust_id': X_test.cust_id, 'gender': pred}).to_csv('003000000.csv', index=False)

submit = pd.DataFrame({
    'ID': test_ID,
    'Segmentation': pred
})
submit

### 결과 csv 파일 제출 ###
submit.to_csv("submission.csv", index=False)

출처_퇴근후딴짓(캐글) : https://www.kaggle.com/datasets/agileteam/bigdatacertificationkr

 

Big Data Certification KR

빅데이터 분석기사 실기 (Python, R tutorial code)

www.kaggle.com

출처_퇴근후딴짓(유튜브) : https://www.youtube.com/@ai-study

 

퇴근후딴짓

퇴근 후, 함께 재미있게 성장하는 딴짓 커뮤니티 비 전공, 다른 직무라도 Re-skilling이 필요한 시대입니다. 함께 AI, 머신러닝, 딥러닝을 재미있게 Study 시작해보아요 :) 새롭게 알게 된 것, 공부한

www.youtube.com

728x90
반응형

댓글