해결된 질문
작성
·
227
1
# 데이터 및 라이브러리 로드하기
import pandas as pd
train = pd.read_csv('/content/train.csv')
test = pd.read_csv('/content/test.csv')
Train = train.copy()
X_train = Train.drop(['Attrition_Flag','CLIENTNUM'],axis=1)
y_train = Train['Attrition_Flag']
X_test = test.copy()
X_test_CL = X_test.pop('CLIENTNUM')
# print(X_train.head())
# print(y_train.head())
# print(X_test.head())
# EDA
# print(X_train.shape, X_test.shape, y_train.shape)
# print(X_train.isnull().sum())
# print(X_test.isnull().sum())
# print(X_train.describe())
# print(X_test.describe())
# print(X_train.describe(include = object))
# print(X_test.describe(include = object))
# print(y_train.value_counts())
# 데이터 전처리 - 결측치 없음, 이상치 없음
# 피처 엔지니어링
cols1 = list(X_train.columns[X_train.dtypes == object]) # 범주형
cols2 = list(X_train.columns[X_train.dtypes != object]) # 수치형
# print(cols2, X_train.info())
Xc_train = X_train[cols1] # 범주형
Xc_test = X_test[cols1]
Xn_train = X_train[cols2] # 수치형
Xn_test = X_test[cols2]
Xc_train = pd.get_dummies(X_train[cols1]) # 범주형 데이터 원핫인코딩
Xc_test = pd.get_dummies(X_test[cols1])
from sklearn.preprocessing import RobustScaler
Ro = RobustScaler()
X_train[cols2] = Ro.fit_transform(X_train[cols2])
X_test[cols2] = Ro.transform(X_test[cols2])
Xn_train = X_train[cols2] # 수치형
Xn_test = X_test[cols2]
X_train = pd.concat([Xc_train, Xn_train], axis = 1)
X_test= pd.concat([Xc_test, Xn_test], axis = 1)
# print(X_train.head())
# print(X_test.head())
# 데이터 분리
from sklearn.model_selection import train_test_split
X_tr, X_val, y_tr, y_val = train_test_split(X_train, y_train, test_size = 0.15, random_state=2023)
# print(X_tr.shape, X_val.shape, y_tr.shape, y_val.shape)
# 모델링
import lightgbm as lgb
lg = lgb.LGBMClassifier(random_state=2023)
lg.fit(X_tr, y_tr)
predict = lg.predict(X_val)
predictproba = lg.predict_proba(X_val)
# 평가 및 제출
# ROC-AUC, 정확도(Accuracy), F1, 정밀도(Precision), 재현율(Recall)
from sklearn.metrics import roc_auc_score, accuracy_score, f1_score, precision_score, recall_score
# print(roc_auc_score(y_val, predictproba[:,1]))
# print(accuracy_score(y_val, predict))
# print(f1_score(y_val, predict))
# print(precision_score(y_val, predict))
# print(recall_score(y_val, predict))
predictproba_final = lg.predict_proba(X_test)
submit = pd.DataFrame(
{
'CLIENTNUM':X_test_CL,
'Attrition_Flag':predictproba_final[:,1]
}
)
# submit.head()
submit.to_csv('1.csv', index = False)
# 제출 제대로 했나 확인
pd.read_csv('1.csv')
#
y_test = pd.read_csv('/content/y_test.csv')
print(roc_auc_score(y_test, predictproba_final[:,1]))
print(roc_auc_score(y_test, predictproba_final[:,1]))만 되고 print(roc_auc_score(y_test, predictproba[:,1]))는 안되는게 정상인가요?
답변 1
1
네 맞습니다.Xval은 yval로 평가를 비교해야 합니다 ;)
print(roc_auc_score(y_val, predictproba[:,1]))
시험에서는 ytest값은 제공하지 않으니 착오없으시길 바래요 💪💪