🤍 전 강의 25% 할인 중 🤍

2024년 상반기를 돌아보고 하반기에도 함께 성장해요!
인프런이 준비한 25% 할인 받으러 가기 >>

  • 카테고리

    질문 & 답변
  • 세부 분야

    자격증 (데이터 사이언스)

  • 해결 여부

    해결됨

예시문제 작업형2 코드 질문

23.06.15 22:11 작성 23.06.15 22:11 수정 조회수 289

0

import pandas as pd
test = pd.read_csv("data/X_test.csv")
train = pd.read_csv("data/X_train.csv")
y_train = pd.read_csv("data/y_train.csv")
pd.set_option('display.max_columns', None)
# 사용자 코딩

test['환불금액'] = test['환불금액'].fillna(0)
train['환불금액'] = train['환불금액'].fillna(0)

from sklearn.preprocessing import LabelEncoder
cols = train.select_dtypes(include='object').columns
# print(cols)
le = LabelEncoder()
for col in cols:
	train[col] = le.fit_transform(train[col])
	test[col] = le.transform(test[col])

train = train.drop('cust_id', axis = 1)
test_id = test.pop('cust_id')
# print(train.head())
	
# print(train.head(3))
from sklearn.model_selection import train_test_split
X_tr, X_val, y_tr, y_val = train_test_split(train, y_train['gender'], test_size=0.2, random_state = 0)

from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(random_state = 0)
rf.fit(X_tr, y_tr)
pred = rf.predict_proba(X_val)

# print(pred)
from sklearn.metrics import roc_auc_score
# print(roc_auc_score(y_val, pred[:, 1]))

pred_res = rf.predict_proba(test)

pd.DataFrame({'custid': test_id, 'gender': pred_res[:,1]}).to_csv('950326.csv', index=False)
# print(pd.read_csv('950326.csv'))

안녕하세요, 딴짓님

제가 강의 안 보고 혼자 작성한 내용인데요..

혹시 해당 내용처럼 작성하면 올바르게 작성한 건지 혼자서 판단이 안 돼서요ㅠ

RandomForest 모델 + 소개해주신 lightGBM정도만 준비해가면 될는지요..

답변 1

답변을 작성해보세요.

0

네 큰 문제 없어 보입니다. 💪💪💪💪💪
6회를 준비할 땐 마지막 csv는 아래와 같은 형태가 될 예정이에요

pd.DataFrame({'pred': pred_res[:,1]}).to_csv('result.csv', index=False)

그리고 데이터셋이 2개가 주어진 적이 많으니

train = pd.concat([train, y_train['gender']], axis=1) 이렇게 합쳐서도 연습해주세요 :)

아 네넵 감사합니다.

데이터셋이 2개가 주어진 경우 강의 해주신 것 같은데, 일단 3개는 이정도로 공부하면서 유지해 나가며 2개인 경우 공부해보겠습니다. 감사합니다!

채널톡 아이콘