๊ธ์ ์์ฑํ๊ธฐ์ ์์... ์ฌ์ค EDA๋ฅผ ์ด๋ ๊ฒ ์งํํ๋ ๊ฒ์ด ๋ง๋์ง ํ์คํ์ง๋ ์์ผ๋ ์ต๋ํ ์ด์ฌํ ๊ณต๋ถํด๋ณด์์ต๋๋ค.. ํํ
EDA(Exploratory Data Analysis, ํ์์ ๋ฐ์ดํฐ ๋ถ์)
๊ฐ๋
๋ฒจ์ฐ๊ตฌ์์ ์ํ์ ‘์กด ํํค’๊ฐ ๊ฐ๋ฐํ ๋ฐ์ดํฐ๋ถ์ ๊ณผ์ ์ ๋ํ ๊ฐ๋ ์ผ๋ก, ๋ฐ์ดํฐ๋ฅผ ๋ถ์ํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ด๋ ๊ณผ์ ์ ์์ด์ ์ง์์ ์ผ๋ก ํด๋น ๋ฐ์ดํฐ์ ๋ํ ‘ํ์๊ณผ ์ดํด’๋ฅผ ๊ธฐ๋ณธ์ผ๋ก ๊ฐ์ ธ์ผ ํ๋ค๋ ๊ฒ์ ์๋ฏธ
์ฒ์์ ๋ก์ฐ๋ฐ์ดํฐ(raw data)๋ฅผ ์ ํ ๋๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ ์ดํดํ๊ณ ํ์ ํ ๋ค์, ์ด๋ค ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค์ด๋ผ ์ง ‘์ด feature(column)๋ก ํํฐํด๋ณด๊ณ , ์ feature๋ก ํด๋ณด๊ณ ..’ ์ด๋ ๊ฒ ๋ฐ์ดํฐ๋ฅผ ์ฌ๋ฌ ์ธก๋ฉด์ผ๋ก ์ชผ๊ฐ๊ณ , ์ถ๋ ฅํด๋ณด๋ฉด์ ์ธ์ฌ์ดํธ๋ฅผ ์ป์ด๋ด๋ ๊ฒ. ๊ทธ๊ฒ์ด EDA์ ๋ฐ์ดํฐ๋ถ์
How?
1. raw data ์ description, dictionary ๋ฅผ ํตํด ๋ฐ์ดํฐ์ ๊ฐ column๋ค๊ณผ row์ ์๋ฏธ๋ฅผ ์ดํด
๋ฐ์ดํฐ๋ถ์ ์ฐ์ต ์์ ๋ง์ด ์ฌ์ฉ๋๋ ์ฌ์ดํธ์์ ๋ฐ์ดํฐ์ ๊ฐ ์นผ๋ผ๊ณผ ๋ก์ฐ(row)์ ๋ํ ์ค๋ช ์ด ์ ๋์์๋ค๊ณ ํจ.
2. ๊ฒฐ์ธก์น ์ฒ๋ฆฌ ๋ฐ ๋ฐ์ดํฐํํฐ๋ง ๊ธฐ์
๋ฐ์ดํฐ ๋ถ์์ ๋ณธ๊ฒฉ์ ์ผ๋ก ๋ค์ด๊ฐ๊ธฐ ์ , ๋ฐ๋์ ๋ฐ์ดํฐ์ ๊ฒฐ์ธก์น๊ฐ ์๋์ง ํ์ธํ๊ณ , ์๋ค๋ฉด ์ ๊ฑฐํด์ค์ผ ํจ.
๋ถ์ ์ ํ์ํ ๋ฐ์ดํฐ๊ฐ ์์นํ ๋ฐ์ดํฐ(numerical data)์ธ๋ฐ ๋ฒ์ฃผํ(categorical data)์ผ๋ก ๋์ด ์๋ค๋ฉด (๋ฐ์ดํฐ ํ์ ์ด ‘object’๋ก ๋ธ) ์์นํ์ผ๋ก ๋ณํ(ex. astype ํ์ฉ)ํด์ค์ผ ํจ.
3. ๋๊ตฌ๋ ์ดํดํ๊ธฐ ์ฌ์ด ์๊ฐํ๋ฅผ ํ๋ ๊ธฐ์
๋ฐ์ดํฐ ๋ถ์์ ํตํด ๋งํ๊ณ ์ ํ๋ ๋ฐ๋ฅผ ์ ๋๋ก ์ ๋ฌํ ์ ์๋ ๊ธฐ์ ์ด ๋ฐ์ดํฐ๋ถ์๊ฐ๋ก์ ๊ฐ์ฅ ์ ๊ฐ์ถฐ์ผ ํ๋ ๊ธฐ์
์๊ฐํ
์๋์ ๊ฐ์ ์ฝ๋๋ก train.csv ๋ฐ์ดํฐ์ ์ ์๊ฐํ ์์ผ๋ณด์๋ค.
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
# Random Forest ๋ชจ๋ธ ๊ฐ์ ธ์ค๊ธฐ
rf_clf = RandomForestClassifier()
bag_clf = BaggingClassifier(rf_clf)
# training dataset ๋ถ๋ฌ์ค๊ธฐ
train_data = pd.read_csv("drive/MyDrive/Colab Notebooks/train.csv")
# ๋ง์ถฐ์ผ ํ๋ ๊ฒ์ type์ด๊ธฐ ๋๋ฌธ์ type๊ณผ ๋๋จธ์ง ๋ฐ์ดํฐ๋ค์ ๋ถ๋ฆฌํด์ค (์ฌ๋ผ์ด์ฑ)
X = train_data.iloc[:, 2:]
y = train_data.iloc[:, 1] #type ์ด
# training dataset๊ณผ test dataset์ผ๋ก ์ชผ๊ฐ๊ธฐ
# training๊ณผ test์ ๋น์จ์ 0.3 [7:3์ผ๋ก ์ชผ๊ฐ ๋ค]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=11)
# ๋๋ค ํฌ๋ ์คํธ ํ์ต ๋ฐ ๋ณ๋์ ํ
์คํธ ์ธํธ๋ก ์์ธก ์ฑ๋ฅ ํ๊ฐ
rf_flf = RandomForestClassifier(criterion='entropy', bootstrap=True, random_state=42, max_depth=5)
rf_clf.fit(X_train, y_train)
pred = rf_clf.predict(X_test)
accuracy = accuracy_score(y_test, pred)
print('๋๋ค ํฌ๋ ์คํธ ์ ํ๋: {0:.4f}'.format(accuracy))
# ๋ง๋๊ทธ๋ํ ์๊ฐํ
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
ftr_importances_values = rf_clf.feature_importances_
ftr_importances = pd.Series(ftr_importances_values, index=X_train.columns)
ftr_top20 = ftr_importances.sort_values(ascending=False)[:20]
plt.figure(figsize=(8,6))
plt.title('Feature importances Top 20')
sns.barplot(x=ftr_top20, y=ftr_top20.index)
plt.show()
DACON ์ฒ์ฒด ์ ํ ๋ถ๋ฅ ๋ชจ๋ธ
๋ฐ์ดํฐ ํน์ฑ
๋ธ๋ก๊ทธ ์ฐธ๊ณ → ๐LINK : RandomForest ์ฌ์ฉ
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
- pd๋ผ๋ ์ด๋ฆ์ผ๋ก pandas ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ํฌํธํ๋ค.
- ์ฌ์ดํท๋ฐ์์ ์ ๊ณตํ๋ ์์๋ธ ๋ชจ๋์์ ๋๋คํฌ๋ ์คํธ ๋ถ๋ฅ ๋ชจ๋ธ์ ๋ถ๋ฌ์จ๋ค.
# matplotlib and seaborn for plotting
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
import matplotlib.patches as patches
- matplot library์์ ํจ์์ ๋ชจ์(pyplot)์ plt ์ด๋ฆ์ผ๋ก ์ํฌํธ.
- matplotlib.pypot
: ๋ช ๋ น์ด ์คํ์ผ๋ก ๋์ํ๋ ํจ์์ ๋ชจ์- ๋ชจ๋์ ๊ฐ๊ฐ์ ํจ์๋ฅผ ์ฌ์ฉํด์ ๊ฐํธํ๊ฒ ๊ทธ๋ํ๋ฅผ ๋ง๋ค๊ณ ๋ณํ๋ฅผ ์ค ์ ์์
- ex. ๊ทธ๋ํ ์์ญ์ ๋ง๋ค๊ธฐ, ์ ํํ, ๋ ์ด๋ธ ๊พธ๋ฏธ๊ธฐ ๋ฑ
- ๋ชจ๋์ ๊ฐ๊ฐ์ ํจ์๋ฅผ ์ฌ์ฉํด์ ๊ฐํธํ๊ฒ ๊ทธ๋ํ๋ฅผ ๋ง๋ค๊ณ ๋ณํ๋ฅผ ์ค ์ ์์
- seaborn ํจํค์ง ์ถ๊ฐ
- seaborn
: Matplotlib์ ๊ธฐ๋ฐ์ผ๋ก ๋ค์ํ ์์ ํ ๋ง์ ํต๊ณ์ฉ ์ฐจํธ ๋ฑ์ ๊ธฐ๋ฅ์ ์ถ๊ฐํ ์๊ฐํ ํจํค์ง - matplot library์์ patches ์ถ๊ฐ
- matplotlib.patches
: ๋ชจ๋ ์๊ฐํ ์์๋ค์ ๋ด๊ณ ์๋ Artist ์๋ธํด๋์ค ๋ฐ์ ์ํด ์๋ ํ๋ฉด์๊ณผ ํ ๋๋ฆฌ์์ ๋ํ๋ธ๋ค.
data_path = '/Users/hansung.dev/Github/kaggle_Data/แแ
ฅแซแแ
ฆแแ
ฒแแ
งแผแแ
ฎแซแ
แ
ฒ-แแ
ฏแฏแแ
กแซแแ
ฆแแ
ตแแ
ฉแซ2/'
train = pd.read_csv(data_path + 'data/train.csv', index_col=0)
test = pd.read_csv(data_path + 'data/test.csv', index_col=0)
sample_submission = pd.read_csv(data_path + 'data/sample_submission.csv', index_col=0)
- ๋ถ๋ฌ์ฌ ๋ฐ์ดํฐ์ ๊ฒฝ๋ก๋ฅผ ์ ์ด์ค.
- pd.read_csv - pandas library์์ ๋ถ์ํ ๋ฐ์ดํฐ ์๋ฃ(train.csv & test.csv & submission.csv)๋ฅผ ์ฝ์ด ๊ฐ ๋ณ์์ ํ ๋น. ํ ๋ ์ด๋ธ์ 0์ผ๋ก ์ง์ .
- index_col=None : int, str, sequence of int / str, or False, optional, default
- ๋ฐ์ดํฐํ๋ ์์์ ํ ๋ ์ด๋ธ๋ก ์ฌ์ฉํ๊ธฐ ์ํด ์ฐ์. ๋๋ ๋ฌธ์์ด ์ด๋ฆ์ด๋ column ์ธ๋ฑ์ค๋ก ์ฃผ์ด์ง.
EDA
print('Size of train data', train.shape)
print('Size of test data', test.shape)
#์ถ๋ ฅ๊ฒฐ๊ณผ
Size of train data (199991, 22)
Size of test data (10009, 21)
- train.shape์ ํตํด train.csv, tst.csv๋ฅผ ๋ค์ค์ธ๋ฑ์ฑ ํ ์๋ฃ์ shape์ ์ถ๋ ฅ (ํ ๊ฐ์, ์ด ๊ฐ์)
- pandas.Dataframe.shape
: Return a tuple representing the dimensionality of the DataFrame. ๋ฐ์ดํฐํ๋ ์์ ์ฐจ์์ ๋ํ๋ด๋ ํํ ๊ฐ์ ์๋ ค์ค.
train.info()
#์ถ๋ ฅ๊ฒฐ๊ณผ
<class 'pandas.core.frame.DataFrame'>
Int64Index: 199991 entries, 0 to 199990 #์ธ๋ฑ์ค ๋ฒํธ๋ 0~9๊น์ง (#๋ถ๋ถ)
Data columns (total 22 columns): #๋ฐ์ดํฐ ์ด์ ์ด 22๊ฐ
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 type 199991 non-null object
1 fiberID 199991 non-null int64
2 psfMag_u 199991 non-null float64
3 psfMag_g 199991 non-null float64
4 psfMag_r 199991 non-null float64
5 psfMag_i 199991 non-null float64
6 psfMag_z 199991 non-null float64
7 fiberMag_u 199991 non-null float64
8 fiberMag_g 199991 non-null float64
9 fiberMag_r 199991 non-null float64
10 fiberMag_i 199991 non-null float64
11 fiberMag_z 199991 non-null float64
12 petroMag_u 199991 non-null float64
13 petroMag_g 199991 non-null float64
14 petroMag_r 199991 non-null float64
15 petroMag_i 199991 non-null float64
16 petroMag_z 199991 non-null float64
17 modelMag_u 199991 non-null float64
18 modelMag_g 199991 non-null float64
19 modelMag_r 199991 non-null float64
20 modelMag_i 199991 non-null float64
21 modelMag_z 199991 non-null float64
dtypes: float64(20), int64(1), object(1) #๋ฐ์ดํฐ ํ์
์ float64, int64, object
memory usage: 35.1+ MB
train.describe()
#์คํ๊ฒฐ๊ณผ (๊ทธ๋ฆผ) 8 rows × 21 columns
train_desc_df = train.describe()
train_desc_df
train.head()
#์คํ๊ฒฐ๊ณผ(๊ทธ๋ฆผ) 5 rows × 22 columns
- Dataframe.describe()
: Generate descriptive statistics. ๋ฌ์ฌํ ํต๊ณ๋ฅผ ์์ฑํ๋ ํจ์- NaN ๊ฐ์ ์ ์ธํ ๋ฐ์ดํฐ ์ธํธ ๋ถํฌ์ ์ค์ฌ ๊ฒฝํฅ, ๋ถ์ฐ ๋ฐ ๋ชจ์์ ์์ฝํจ.
- ์์ฝ ํต๊ณ๋์ ๋ฐํํ๋ค.
- Dataframe.head()
: Return the first n rows. ์ฒซ ๋ฒ์งธ n๊ฐ์ ํ๋ค์ ๋ฐํํ๋ค.
โ๐ป ๋ฐ์ดํฐ ํ์ ์ ์ผ์น์์ผ์ผ ํ์ง ์์๊น?
- ๊ทธ ์ธ
Corr
# ์ ์ฒด ๋ฐ์ดํฐ์ ๋ํ ์๊ด๊ด๊ณ HeatMap ์๊ฐํ
corr = train.corr()
cmap = sns.color_palette("Blues")
f, ax = plt.subplots(figsize=(10, 7))
sns.heatmap(corr, cmap=cmap)
<matplotlib.axes._subplots.AxesSubplot at 0x1a31854390>
- Dataframe.corr()
- : ๋ฐ์ดํฐ์ ์ ๋ํ์ฌ ์๊ด๊ด๊ณ๋ฅผ ๋ํ๋
- palette์ ์์ ํ๋ ๊ณ์ด๋ก ์ ํํจ
- subplots ์ ์ค์ ์ ์ด๋ป๊ฒ ํ๋์ง ๋ชจ๋ฅด๊ฒ ๋ค…
type
# Log_ID
fig = plt.figure(figsize=(18,9))
plt.subplots_adjust(hspace=.5)
plt.subplot2grid((3,3), (0,0), colspan = 3)
train['type'].value_counts()[:100].plot(kind='bar', alpha=0.7)
plt.title('type Values in the Training Set - train ()')
Text(0.5, 1.0, 'type Values in the Training Set - train ()')
train['type'].value_counts()
QSO 49680
GALAXY 37347
SERENDIPITY_BLUE 21760
SPECTROPHOTO_STD 14630
REDDEN_STD 14618
STAR_RED_DWARF 13750
STAR_BHB 13500
SERENDIPITY_FIRST 7132
ROSAT_D 6580
STAR_CATY_VAR 6506
SERENDIPITY_DISTANT 4654
STAR_CARBON 3257
SERENDIPITY_RED 2562
STAR_WHITE_DWARF 2160
STAR_SUB_DWARF 1154
STAR_BROWN_DWARF 500
SKY 127
SERENDIPITY_MANUAL 61
STAR_PN 13
Name: type, dtype: int64
- ํน์ง
- QSO์ ๊ฐ์๊ฐ ์๋์ ์ผ๋ก ๋ง๋ค.
fiber ID
# Log_ID
fig = plt.figure(figsize=(18,9))
plt.subplots_adjust(hspace=.5)
plt.subplot2grid((3,3), (0,0), colspan = 3)
train['fiberID'].value_counts()[:100].plot(kind='bar', alpha=0.7)
plt.title('fiberID Values in the Training Set - train ()')
Text(0.5, 1.0, 'fiberID Values in the Training Set - train ()')
โ๐ป ์ฝ๋๋ฅผ ์ ๋ฐ์ ์ผ๋ก ๋ฏ์ด๋ณด์๋๋ฐ, ๋ฌธ์ ์ ์ด๋ ํด๊ฒฐ๋ฐฉ์์ ๋ถ์ํ๋ ๊ฑด ๋์ ์ ๋ณด์ด์ง ์๋๋ค.. ๋ค๋ฅธ ๋ถ๋ค์ ์๊ฒฌ์ ํจ๊ป ๋ค์ด๋ณด๊ณ ์ถ๋ค..!
'๐ก WIDA > DACON ๋ถ๋ฅ-ํ๊ท' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[DACON/๊น์ธ์ฐ] ํ์ด์ฌ์ ์ด์ฉํ EDA (0) | 2023.04.07 |
---|---|
[DACON/์กฐ์์] ํ์ด์ฌ์ ์ด์ฉํ EDA (0) | 2023.04.07 |
[DACON/๊น์ธ์ฐ] ๋ถ๋ฅ ๋ชจ๋ธ ๋ฏ์ด๋ณด๊ธฐ, ๋ถ๋ฅ ๋ชจ๋ธ ํ๊ฐ๋ฐฉ์ (0) | 2023.03.31 |
[DACON/๊น๊ฒฝ์] ๋ถ๋ฅ ๋ชจ๋ธ ๋ฏ์ด๋ณด๊ธฐ, ๋ถ๋ฅ ๋ชจ๋ธ ํ๊ฐ๋ฐฉ์ (0) | 2023.03.31 |
[DACON/๊น๊ท๋ฆฌ] ๋ถ๋ฅ ๋ชจ๋ธ ๋ฏ์ด๋ณด๊ธฐ, ๋ถ๋ฅ ๋ชจ๋ธ ํ๊ฐ๋ฐฉ์ (0) | 2023.03.31 |