1. EDA & ์ ์ฒ๋ฆฌ
๋ค์ด๊ฐ๋ฉฐ
์ฒ์ฒด ์ ํ ๋ถ๋ฅ ๋ํ
๋ฐฐ๊ฒฝ
์๋
ํ์ธ์ ์ฌ๋ฌ๋ถ! ์ฒ์ฒด ์ ํ ๋ถ๋ฅ ๋ํ์ ์ค์ ๊ฒ์ ํ์ํฉ๋๋ค.
์ต๊ทผ ์ธ๋ฅ์๊ฒ ๋ค๊ฐ์จ ๋น
๋ฐ์ดํฐ๋ผ๋ ๋จ์ด๋ ์ฐ์ฃผ์ ์ฒ๋ฌธํ์๊ฒ ๋ฏ์ค์ง ์์ต๋๋ค. ์ฐฐ๋์ ์๊ฐ์๋ ์ฐ์ฃผ๋ ์ฒ๋ฌธํ์ ์ธ ์์ ๋ฐ์ดํฐ๋ฅผ ์์ฐํด์๊ณ , ์ค๋ ์ ๋ถํฐ ์ฒ๋ฌธํ์๋ค์ ์ฐ์ฃผ๋ฅผ ๊ด์ธกํ์ผ๋ฉฐ ๊ทธ ๋ฐฉ๋ํจ์ ๋น๋กํ๋ ๋ฐ์ดํฐ๋ฅผ ์์ง ๋ฐ ๋ถ์ํ๊ธฐ ๋๋ฌธ์
๋๋ค.
์ฌ๋ก ๋์งํธ ์ฒ์ฒด ๊ด์ธก(Sloan Digital Sky Survey: ์ดํ SDSS)๋ ์ธ๊ณ์ ์ฒ์ฒด ๊ด์ธก ํ๋ก์ ํธ๋ก, ์ฐ์ฃผ์ ๋ํ ์ฒ๋ฌธํ์ ์ธ ๊ท๋ชจ์ ๋ฐ์ดํฐ๋ฅผ ์์งํ๊ณ ์์ต๋๋ค. ์ด๊ณณ์์ ์์งํ ๋ฐ์ดํฐ๋ ์ฝ 6,000๊ฐ ๋
ผ๋ฌธ์ ์ฌ์ฉ๋์๊ณ , 25๋ง ํ ์ด์ ์ธ์ฉ๋์์ ์ ๋๋ก ์ฒ๋ฌธํ์ ํฐ ๊ธฐ์ฌ๋ฅผ ํ์ต๋๋ค. ์ ์ ๊ฑฐ๋ํด์ง๋ ๊ท๋ชจ์ ๋ฐ๋ผ ๋ฐ์ดํฐ ์ฒ๋ฆฌ์๋ ๋จธ์ ๋ฌ๋๊ณผ ๋ฅ๋ฌ๋ ๊ธฐ๋ฒ์ด ํ์ฉ๋๊ธฐ ์์ํ์ต๋๋ค.
์ฌ์ ํ ์ฐ์ฃผ์๋ ๋ค์ํ ๋ฏธ์ง์ ์ด์ผ๊ธฐ๊ฐ ๋จ์์๊ณ , ์ค๋๋ ์ธ๊ฐ์ ํ๋์์ ๋ง์ ๋ฐ์ดํฐ๋ฅผ ์ป์ด๋ผ ์ ๋๋ก ๋ฐ์ ํ์ต๋๋ค. ์ด ๋ฐ์ดํฐ๋ฅผ ๋ถ์ํ์ฌ ์ด์ฉ๋ฉด ๋๋ฌ๋์ง ์์ ๊ท์น์ด ์ฌ๋ฌ๋ถ์ ์๋์์ ๋ฐํ์ง ์ ์์ต๋๋ค. ์๋ก์ด ์๊ณ ๋ฆฌ์ฆ์ ํตํด ์ฐ์ฃผ์ ๋น๋ฐ์ ์ฐพ์์ฃผ์ธ์!
๋ชฉ์
์ฒ์ฒด ์ ํ ๋ถ๋ฅ ์๊ณ ๋ฆฌ์ฆ ๊ฐ๋ฐ
EDA
#ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํธ์ถ
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
#ํ๋ จ ๋ฐ ํ
์คํธ๋ฅผ ์ํ ๋ฐ์ดํฐ
train = pd.read_csv("./dataset_dsob/train.csv")
test = pd.read_csv('./dataset_dsob/test.csv')
sample_submission = pd.read_csv('./dataset_dsob/sample_submission.csv')
data info
#๋ฐ์ดํฐ ํฌ๊ธฐ ํ์ธ
print('Size of train data', train.shape)
print('Size of test data', test.shape)

#๋ฐ์ดํฐ ์์ฝ์ ๋ณด ํ์ธ
train.info()
train.describe()


๊ฐ๋ณ ๋ณ์ ํ์
Type
#๊ฐ๋ณ๋ณ์ ํ์-type
#type๋ณ ๊ฐ์ ํ์ธ - ์ ์ฒด ๋ถํฌ
plt.figure(figsize=(15,6))
plt.title('Type count')
plt.ylabel('count')
plt.xlabel('Type')
plt.bar(train.groupby('type')['fiberID'].count().index, train.groupby('type')['fiberID'].count().values)
plt.xticks(rotation=90)
plt.show()

#type๋ณ ๊ฐ์ ํ์ธ - ์ค์ ๊ฐ์
train['type'].value_counts()

-> ํด๋์ค ๋ถ๊ท ํ ์ฌํ ํธ์ => ๋ณ๋ค๋ฅธ ์กฐ์น๊ฐ ํ์ํ๋ค
fiberId
#fiberId
sns.barplot(train['fiberID'].value_counts().index,train['fiberID'].value_counts())
plt.xticks(rotation=90);

-> ์ ์ฒด์ ์ธ ๋ถํฌ๋ฅผ ์ดํผ๊ณ ์, FiberId ํ๋์ ๊ฐ๋น ๊ฐ์๋ฅผ ์๋ ค๊ณ ํ๋๋ฐ ์คํจ.. => ๋ง๋๊ทธ๋ํ ๋ง๊ณ ์ฐ์ ๋๋ฅผ ๊ทธ๋ ค๋ณด์!
sns.set_style(style='whitegrid')
sns.scatterplot(
data=train['fiberID'],
x=train['fiberID'],
y=train['fiberID'].value_counts(),
palette='Paired_r'
)
plt.title('fiberID\'s counts')
plt.xlabel('fiberID')
plt.ylabel('counts')
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)

-> ๋๊ฐ 600๋ฒ๋ ์ด๋ด๋ก ๋ชฐ๋ ค์์์ ์ ์ ์๋ค
type์ ๋ถ์ํด๋ดค์ ๋ QSO์ ํด๋์ค๊ฐ ๋ชฐ๋ ค์๋ ๊ฒ์ฒ๋ผ fiberID๋ ์ข ๋ ์ธ๋ถ์ ์ผ๋ก ์ดํด๋ณด๊ณ ์ ํ์๋ค.
type-fiberID
*์ฝ๋ ์ฐธ๊ณ (https://github.com/Leewongi0731/DACON_CelestialClassification/blob/master/celestial_classifier.ipynb)
# type_num ์ด์ ์์ฑ
column_number = {}
for i, column in enumerate(sample_submission.columns):
column_number[column] = i
def to_number(x, dic):
return dic[x]
train['type_num'] = train['type'].apply(lambda x: to_number(x, column_number))
#fiberID์ type ๊ฐ์ ์๊ด๊ด๊ณ
#type, fiberID๊ด์ ๊ด๊ณ๋๋ฅผ ๋ณด๊ธฐ์ํ ์๊ฐํ
fig , ax = plt.subplots(nrows = len(set(train['type'])), ncols=1, figsize = (15,40))
for i in range(len(set(train['type']))):
ax[i].scatter(train.loc[train['type_num']==i, 'fiberID'],range(train.loc[train['type_num']==i, 'fiberID'].shape[0]))
ax[i].set_xlim(0,1000)
ax[i].set_ylabel('count')
ax[i].set_xlabel('fiberID')
ax[i].set_title(list(column_number.keys())[i])



-> qso์ ์ธ ๋๋จธ์ง ์ฒธ์ฒด์ fiberId๋ 0์์ 600๋ฒ๋ ์ฌ์ด์ ์กด์ฌ
์ ํํ fiberID ๊ตฌ๋ถ๊ธฐ์ค์ ์์๋ณด์
#fiberId ๊ตฌ๋ถ๊ธฐ์ค(์ง์ ) ์ฐพ๊ธฐ,์ต๋๊ฐ์ฐพ๊ธฐ ํ์ฉ
train.groupby('type')['fiberID'].max()

-> ์ต๋๊ฐ์ฐพ๊ธฐ ํ์ฉ => qso- 1000, ๋๋จธ์ง 640
=> ๋ถ๋ฅ๋๋ ์ง์ ์ด fiberId๊ฐ 640์ผ ๋์์ ์ ์ ์์
ํ ๋ฒ ๋ ํ์ธํ ๊ฒฐ๊ณผ FiberID > 640์ธ ์ฒ์ฒด๋ QSO๋ฟ์
train[ train['fiberID'] > 640 ]['type'].value_counts()

-> ํฅํ ๋ถ๋ฅํ ๋ ์ ๊ฒฝ์จ์ผ ํ ๋ถ๋ถ์ธ๋ฏํจ
์ ์ฒ๋ฆฌ
์ด์์น ํ์
fig, ax = plt.subplots(nrows = 20, ncols= 1 ,figsize = (20,70))
for i in range(20):
ax[i].scatter(x = train.index, y = train[train.columns[i+2]].values)
ax[i].set_title(train.columns[i+2])
plt.show()




-> ๋๊ฐ์ ๊ฐ์ด ํ ๊ฐ์ ๋ชฐ๋ ค์์ง๋ง, ๊ทน๋จ์ ์ผ๋ก ๋จ์ด์ ธ์๋ ์ด์์น๊ฐ ๊ฝค๋ ์กด์ฌํจ => ์ผ๋ถ๋ ์ ๊ฑฐ์ ํ์์ฑ ์์
type๋ณ ์ด์์น ํ์ - boxplotํ์ฉ
columns = train.columns
for s_type in columns[3:]:
plt.figure(figsize=(18,3))
sns.boxplot(x=train['type'],y=s_type,data=train)
plt.title(s_type)
plt.xticks(rotation=-90)

-> ์ด๋ ๊ฒ ํ ๋ ๊ฐ ์กด์ฌํ๋ ์ปฌ๋ผ์ ๊ฒฝ์ฐ ํฐ ๋ฌธ์ ๊ฐ ๋์ง ์์ง๋ง


์ด๋ ๊ฒ ๋ค์ ๊ฐ๊ฐ ์๋ ๊ฒฝ์ฐ๋ ์ ๊ฑฐํด์ค์ผ ํ๋ค๊ณ ํ๋จ๋จ
์ด์์น ๊ธฐ์ค ์ ์ ์ ์ํด ๊ฐ ์ปฌ๋ผ์ ์ต๋ , ์ต์๊ฐ ํ์ธ
for col in train.columns[3:]:
print(col,train[col].max())
print(col,train[col].min())

๊ทธ๋ฐ๋ฐ ์ต๋, ์ต์๋ง์ผ๋ก ์ด์์น ๊ธฐ์ค ์ ์ ํ๊ธฐ ์ด๋ ค์์ 100,200,300,400,500 ~ 1000๊น์ง ํ๋์ฉ ๋ฃ์ด๊ฐ๋ฉฐ ๊ทธ ๋์ ๊ฐ์๋ฅผ ํ์ ํด๋ด
for col in train.columns[3:]:
print(train[ abs(train[col]) > 500 ]['type'].value_counts())
print('===================================================')


100,200,300์ ํด๋น ๋ฒ์๊ฐ ๋๋ฌด ์ข์ ์ด์์น ๊ฐ์๊ฐ ๋ง์๊ณ , 1000์ด๋ 2000์ ๋ฒ์๊ฐ ๋๋ฌด ๋์ด ์ด์์น ๊ฐ์ ์ ์์
500~ 800์ ํด๋ดค์ ๋ ๋น์ทํ ์ ๋๋ก ๋์ 500์ ์ด์์น ๊ธฐ์ค ์์น๋ก ์ ์
๊ทธ๋ฆฌ๊ณ ์ ๊ฑฐํด์ค
remove_outlier_list = []
for i in range(len(train.index)):
cnt = 0
for j in train.columns[2:22]:
if(abs(train.loc[i,j]) > 500):
cnt += 1
if cnt > 5:
remove_outlier_list.append(i)
break
print(remove_outlier_list)

-> ๊ฐ ์ปฌ๋ผ๋ง๋ค 500 ๋๋ ๊ฐ์ด 5 ๊ฐ ์ด์์ธ ๊ฒฝ์ฐ ์ ๊ฑฐ ๋ฆฌ์คํธ์ ๋ด์์ค
#์ด์์น ๊ฐ ์ ๊ฑฐํ ์๋ก์ด ๋ฐ์ดํฐํ๋ ์ ์์ฑ
train_c = train.drop(remove_outlier_list)
#์ด์์น ์ ๊ฑฐ ํ ๋น๊ต๋ฅผ ์ํ ์๊ฐํ
fig, ax = plt.subplots(nrows = 20, ncols= 1 ,figsize = (20,70))
for i in range(20):
ax[i].scatter(x = train_c.index, y = train_c[train_c.columns[i+2]].values)
ax[i].set_title(train_c.columns[i+2])




-> y์ถ ๋ฒ์๋ฅผ ํ์ธํด๋ณด๋ฉด ๋๋ถ๋ถ์ด ์ด์์น๊ฐ ์ ๊ฑฐ๋ ๊ฒ์ ์ ์ ์์ง๋ง, ์ ์ด์ ์ด์์น ๊ฐ์๊ฐ ์ ์๋ 'psfMag_u'์ ๊ฐ์ ๊ฒฝ์ฐ ์ด์์น๊ฐ ๊ฑฐ์ ์ ๊ฑฐ๋์ง ์์์ด ๋ณด์. ๊ทธ๋ ์ง๋ง ํ์ ์ํฅ์ ํฌ๊ฒ ์ค ๊ฒ๋ค์ ์ ๊ฑฐํ์ผ๋ฏ๋ก ๋์ด๊ฐ
๋ชจ๋ธ๋ง
ํ์ต์ ์ํด train_c ๋ฐ์ดํฐ์ type์ ์ซ์ํ์ผ๋ก ๋ณํ
column_number = {}
for i, column in enumerate(sample_submission.columns):
column_number[column] = i
def to_number(x, dic):
return dic[x]
train_c['type_num'] = train_c['type'].apply(lambda x: to_number(x, column_number))
train_c['type_num']

์ฌ์ฉํ ๋ชจ๋ธ์ธ xgboost ๋ถ๋ฌ์์ค
#๋ชจ๋ธ๋ง
from xgboost import XGBClassifier
import xgboost as xgb
from xgboost import plot_importance
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix, roc_auc_score, f1_score
ํ์ต ๋ฐ์ดํฐ
x = train_c.drop(['type_num','type'],axis = 1) #column์ ๊ทผ
y = train_c['type_num'].values
test_x = test
RANDOM_SEED = 2000 #random_state์ ์ผ์น
x_train, x_valid, y_train, y_valid = train_test_split(x, y, \
test_size=0.2, random_state=RANDOM_SEED, stratify = y)
xgb_clf = XGBClassifier(booster='gbtree',
silent=True,
max_depth=5,
min_child_weight=8,
gamma=1,n_estimators=100,
colsample_bytree=0.6,
colsample_bylevel=0.6,
objective='multi:softprob',
random_state=RANDOM_SEED)
xgb_clf.fit(x_train,y_train, eval_set=[(x_train, y_train), (x_valid, y_valid)])
ํ๋ผ๋ฏธํฐ ์ค์ ํด์ค
- gbtree -> gblinear๋ณด๋ค ํธ๋ฆฌ๊ฐ ์ ํฉํ๋ค๊ณ ์๊ฐ๋์ด ์ ํ



์์ธก๊ฒฐ๊ณผ ํ์ธ
xgb_pred = xgb_clf.predict_proba(test_x)
print(np.round(xgb_pred[:5], 3))

์ฐ์ 5 ๊ฐ๋ง ๋ฝ์์ ๋ดค๋๋ฐ ์ ๋ ๊ฑด์ง ๋ชจ๋ฅด๊ฒ ๋ค
ํผ์ฒ ์ค์๋ ์๊ฐํ
%matplotlib inline
fig, ax = plt.subplots(figsize=(10, 12))
plot_importance(xgb_clf, ax=ax)

์ถ์ฒ
https://dacon.io/competitions/official/235573/codeshare/694?page=1&dtype=recent
https://dacon.io/competitions/official/235573/talkboard/400609
https://velog.io/@leo_kim/๋ถ์คํ
-๊ณ์ด-์์๋ธ-์๊ณ ๋ฆฌ์ฆ
https://youtu.be/8b1JEDvenQU
https://youtu.be/GrJP9FLV3FE
https://teddylee777.github.io/scikit-learn/scikit-learn-ensemble/
https://hwi-doc.tistory.com/entry/์ดํดํ๊ณ -์ฌ์ฉํ์-XGBoost
[ML] XGBoost ์ดํดํ๊ณ ์ฌ์ฉํ์
์์ ๊ฐ๋ ๊ธฐ๋ณธ ๊ตฌ์กฐ ํ๋ผ๋ฏธํฐ GridSearchCV 1. ๊ฐ๋ 'XGBoost (Extreme Gradient Boosting)' ๋ ์์๋ธ์ ๋ถ์คํ ๊ธฐ๋ฒ์ ํ ์ข ๋ฅ์ ๋๋ค. ์ด์ ๋ชจ๋ธ์ ์ค๋ฅ๋ฅผ ์์ฐจ์ ์ผ๋ก ๋ณด์ํด๋๊ฐ๋ ๋ฐฉ์์ผ๋ก ๋ชจ๋ธ์ ํ์ฑํ
hwi-doc.tistory.com
https://github.com/Leewongi0731/DACON_CelestialClassification/blob/master/celestial_classifier.ipynb
'๐ก WIDA > DACON ๋ถ๋ฅ-ํ๊ท' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[DACON/๊น๊ฒฝ์] ํ๋ก์ ํธ ์์ธ์ด (4) | 2023.05.05 |
---|---|
[DACON/๊น์ธ์ฐ] ํ๋ก์ ํธ ์์ธ์ด (4) | 2023.05.05 |
[DACON/์ฐธ๊ณ ์๋ฃ] ์์๋ธ ๋ชจ๋ธ (0) | 2023.05.01 |
[DACON/์ฐธ๊ณ ์๋ฃ] SVM ์ฐธ๊ณ ์๋ฃ (0) | 2023.04.26 |
[DACON/์ต๋ค์] ํ์ด์ฌ์ ์ด์ฉํ EDA (0) | 2023.04.07 |