AI ์ค๋ฌด ๊ธฐ๋ณธ ๊ณผ์
02 ์ ๊ฐ ๋ฐ์ดํฐ๋ฅผ ํ์ฉํ ์ฃผ์ ์ ์์ฅ ๋ถ์
ํ๋ก์ ํธ ๋ชฉํ
<ํ๊ตญ์์ ๊ณต์ฌ ์ ํ๋ณ ์ฃผ์ ์ ํ๋งค๊ฐ๊ฒฉ> ๋ฐ์ดํฐ์ ๊ฐ๊ฒฉ/์ง์ญ/๋ธ๋๋/์ ํ์ฌ๋ถ๋ฅผ ๋ถ์ํ์ฌ ์ฃผ์ ์ ์์ฅ ๊ด๋ จ ์ธ์ฌ์ดํธ ๋์ถ
์ค์ ์ฐ๊ตฌ/์ค๋ฌด์์ ํ์ฉ๋๋ ํ์ ๋ถ์ ๊ณผ์ ๋ฐ ๊ธฐ๋ฒ์ ๋ํด ํ์ต
ํ๋ก์ ํธ ๋ชฉ์ฐจ
- ๋ฐ์ดํฐ Cleansing ๋ฐ Feature Engineering: ๋ถ์์ ์ํ ์ฌ์ ์ ๊ฒ ๋ฐ ๋ฐ์ดํฐ ๊ฐ๊ด ์ดํด
1.1. 2018๋ ๋ฐ์ดํฐ ๊ธฐ์ค ๋ฐ์ดํฐ Cleansing ๋ฐ Feature Engineering
1.2. Cleansing ๋ฐ Feature Engineering ํจ์ ์์ฑ ๋ฐ ์ ์ฒด ๋ ๋ ๋ฐ์ดํฐ ์ ์ฉ
1.3. ์ฐ๋๋ณ ๋ฐ์ดํฐ Outer Join - ์ฃผ์ ์ ๊ฐํ์
ํํฉ ๋ถ์: ์ฐ๋๋ณ ์ฃผ์ ์ ID ๋น๊ต๋ฅผ ํตํ ๊ฐํ์
ํํฉ ๋ถ์
2.1. ์ฐ๋๋ณ ๊ฐํ์ ์์น ๋ถ์ - ๋ธ๋๋ ๋ถ์: ๋ธ๋๋๋ณ ๊ฐ๊ฒฉ๊ฒฝ์๋ ฅ ๋ฐ ์์ฅ์ ์ ์จ ๋ถ์
3.1. ์ฃผ์ ๋ธ๋๋๋ณ ๊ฐ๊ฒฉ Line Plot ๋ถ์
3.2. ์ฃผ์ ๋ธ๋๋๋ณ ์ง๋ 4๋ ๊ฐ ์์ฅ ์ ์ ์จ Stacked Bar Plot ๋ฐ Heatmap ๋ถ์ - ๊ฐ๊ฒฉ ๋ถ์: ์ฃผ์ ์ ๋ฐ ์ง์ญ ๋ณ ๊ฐ๊ฒฉ ํธ์ฐจ ๋ถ์
4.1. ๊ฐ๊ฒฉ ๋ถํฌ Boxplot
4.2. ์ง์ญ๋ณ ๊ฐ๊ฒฉ ๋ถํฌ Boxplot (Multiple Columns)
๋ฐ์ดํฐ ์ถ์ฒ
- https://www.data.go.kr/data/15044628/fileData.do
- Opinet ์ ๊ฐ๋ด๋ ค๋ฐ๊ธฐ: 2018 ~ 2021๋ 4๊ฐ๋ ์ ๋ํด ๊ฐ๊ฐ 6์ 1์ผ~7์ผ ๋ฐ์ดํฐ ์ถ์ถ
- ํ๋ก์ ํธ์ ํ์ํ ์ปฌ๋ผ๋ง ์ถ์ถ
1. ๋ฐ์ดํฐ Cleansing ๋ฐ Feature Engineering
ํ์ํ ํจํค์ง๋ฅผ importํ ํ ๋ถ์์ ์ํ ์ฌ์ ์ ๊ฒ๊ณผ ํจ๊ป ๋ฐ์ดํฐ ๊ฐ๊ด์ ์ดํดํฉ๋๋ค.
1-1. 18๋ ๋ ๊ธฐ์ค ๋ฐ์ดํฐ ์ ๊ฒ ๋ฐ Cleansing
import numpy as np
import pandas as pd
import seaborn as sns
sns.set_style('darkgrid')
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
font_dirs = ['/usr/share/fonts/truetype/nanum', ]
font_files = fm.findSystemFonts(fontpaths=font_dirs)
for font_file in font_files:
fm.fontManager.addfont(font_file)
plt.rcParams['font.family'] = 'NanumBarunGothic'
plt.rcParams['axes.unicode_minus']=False
f18 = pd.read_csv(f'./data/แแ
ชแแ
ฅ_แแ
กแซแแ
ขแแ
กแแ
งแจ(แแ
ฎแแ
ฒแแ
ฉ)_2018.csv')
f18.head()
๊ธฐ์ค : ์ผ๊ฐ(20180601~20180607) | NaN | NaN | NaN | NaN | NaN |
A0011536 | 20180601.0 | ๊ฐ์ ๊ฐ๋ฆ์ | SK์๋์ง | ์ ํ | 1579.0 |
A0011536 | 20180602.0 | ๊ฐ์ ๊ฐ๋ฆ์ | SK์๋์ง | ์ ํ | 1579.0 |
A0011536 | 20180603.0 | ๊ฐ์ ๊ฐ๋ฆ์ | SK์๋์ง | ์ ํ | 1579.0 |
A0011536 | 20180604.0 | ๊ฐ์ ๊ฐ๋ฆ์ | SK์๋์ง | ์ ํ | 1579.0 |
# 0๋ฒ row ์ ๊ฑฐ
f18 = f18.drop(0)
# ๋ณ์๋ณ null๊ฐ ํ์ธ ๊ฒฐ๊ณผ null ์์
f18.isna().sum()
๋ฒํธ 0
๊ธฐ๊ฐ 0
์ง์ญ 0
์ํ 0
์
ํ์ฌ๋ถ 0
ํ๋ฐ์ 0
dtype: int64
# include='all': ์นดํ
๊ณ ๋ฆฌํ ๋ณ์๋ ์ ๋ณด ์ ๊ณต
f18.describe(include='all')
- unique ๋ฒํธ๊ฐ 11673๊ฐ์ด๋ฉฐ ์ต๋ 7๋ฒ๊น์ง ๊ธฐ๋ก๋์์
- ๊ธฐ๊ฐ์ด ์์น๋ก ์ธ์๋๊ณ ์์
- unique ์ง์ญ ๊ฐ์๊ฐ 229์ด์ด์ ๋๋ฌด ๋ง์
- unique ์ํ ๊ฐ์๊ฐ 9๊ฐ์ด๋ฏ๋ก ์ ์ ํจ
- unique ์ ํ์ฌ๋ถ ๊ฐ์๊ฐ 2๊ฐ์ด๋ฉฐ, ์ ํ์ฌ๋ถ๋ ๊ฐ๊ฐ ์ ๋ฐ์ ๋ ๋น์ค์ ์ฐจ์งํจ
- ํ๋ฐ์ min์ด 0์
# ๊ธฐ๊ฐ์ datetime ํํ๋ก ๋ณํ
f18['๊ธฐ๊ฐ'] = f18['๊ธฐ๊ฐ'].apply(lambda x:pd.to_datetime(str(int(x))))
# ์ง์ญ ๋ณ์ ์ค ์ฒซ ์ง์ญ ๊ตฌ๋ถ๋ง ์ปฌ๋ผ ํ์ฑ
region_len = f18['์ง์ญ'].apply(lambda x: len(x.split()))
print(f"min: {min(region_len)},max: {max(region_len)}")
f18['์ง์ญ2'] = f18['์ง์ญ'].apply(lambda x:x.split()[0])
import collections
collections.Counter(f18['์ง์ญ2'])
# ํ๋ฐ์ ๊ฐ 0์ธ ROW ํ์ธ
f18.loc[f18['ํ๋ฐ์ ']==0].head(10)
f18.loc[f18['๋ฒํธ']=='A0010629']
# ํ๋ฐ์ ๊ฐ 0์ธ ROW ์ ๊ฑฐ
f18 = f18.loc[f18['ํ๋ฐ์ ']!=0,:]
f18.describe(include='all',datetime_is_numeric=True)
#์ฃผ์ ์๋ณ ๋ฐ์ดํฐ ์ ํฉ์ฑ ํ์ธ(7์ผ๋์ ๋ณํ ์์๋ค๋ ์ ์ )
unique_count = f18.groupby('๋ฒํธ')[['์ง์ญ','์ํ','์
ํ์ฌ๋ถ']].nunique()
unique_count.head()
target = unique_count.loc[(unique_count!=1).sum(axis=1)!=0]
target
f18.loc[f18['๋ฒํธ'].isin(target.index)]
f18 = f18.loc[~f18['๋ฒํธ'].isin(target.index)]
# ์ฃผ์ ์๋ณ ๋ฐ์ดํฐ ํตํฉ
f18 = f18.groupby('๋ฒํธ')\
.agg({'์ง์ญ':'first','์ง์ญ2':'first','์ํ':'first','์
ํ์ฌ๋ถ':'first','ํ๋ฐ์ ':'mean'})\
.reset_index()
f18.describe(include='all')
1-2. Cleansing ๋ฐ Feature Engineering ํจ์ ์์ฑ ๋ฐ ์ ์ฒด ๋ ๋ ๋ฐ์ดํฐ ์ ์ฉ
def preprocess(df):
df_copy=df.copy() # ํํฐ๋ง ์
df = df.drop(0)
df['๊ธฐ๊ฐ'] = df['๊ธฐ๊ฐ'].apply(lambda x:pd.to_datetime(str(int(x))))
df['์ง์ญ2'] = df['์ง์ญ'].apply(lambda x:x.split()[0])
df = df.loc[df['ํ๋ฐ์ ']!=0,:]
unique_count = df.groupby('๋ฒํธ')[['๋ฒํธ','์ง์ญ','์ํ','์
ํ์ฌ๋ถ']].nunique()
target = unique_count.loc[(unique_count!=1).sum(axis=1)!=0,:]
df = df.loc[~df['๋ฒํธ'].isin(target.index),:]
df = df.groupby('๋ฒํธ')\
.agg({'์ง์ญ':'first','์ง์ญ2':'first','์ํ':'first','์
ํ์ฌ๋ถ':'first','ํ๋ฐ์ ':'mean'})\
.reset_index()
out = set(df_copy['๋ฒํธ']).difference(set(df['๋ฒํธ'])) # ํํฐ๋ง ํ
return(df,out)
f_dict = dict()
out_all = set() # ์ด์์น ๋ฐ๊ฒฌํ ์ฃผ์ ์ ๋ฒํธ ์ ์ฅ
for year in range(2018,2022):
df = pd.read_csv(f'./data/แแ
ชแแ
ฅ_แแ
กแซแแ
ขแแ
กแแ
งแจ(แแ
ฎแแ
ฒแแ
ฉ)_{year}.csv')
f_dict[year], out = preprocess(df)
out_all.update(out)
1.3. ์ฐ๋๋ณ ๋ฐ์ดํฐ Outer Join
key = list(f_dict[2018].columns)
key.remove('ํ๋ฐ์ ')
print(key)
m1 = pd.merge(f_dict[2018],f_dict[2019],on=key,how='outer',suffixes=('_2018', '_2019'))
m2 = pd.merge(f_dict[2020],f_dict[2021],on=key,how='outer',suffixes=('_2020', '_2021'))
m = pd.merge(m1,m2,on=key,how='outer')
m.head()
m.groupby('๋ฒํธ').size().sort_values(ascending=False).head()
m.loc[m['๋ฒํธ']=='A0019752']
(m.groupby('๋ฒํธ').size()>1).sum()
key.remove('์ํ')
key
m1 = pd.merge(f_dict[2018],f_dict[2019],on=key,how='outer',suffixes=('_2018', '_2019'))
m2 = pd.merge(f_dict[2020],f_dict[2021],on=key,how='outer',suffixes=('_2020', '_2021'))
m = pd.merge(m1,m2,on=key,how='outer')
m.head()
size = m.groupby('๋ฒํธ').size().sort_values(ascending=False)
size.head()
target = size[size>1].index
m.loc[m['๋ฒํธ'].isin(target)].sort_values('๋ฒํธ')
m = m.loc[~m['๋ฒํธ'].isin(target)]
m.groupby('๋ฒํธ').size().sort_values(ascending=False).head()
# ์ด์์น ๋ฐ๊ฒฌ๋์๋ ์ฃผ์ ์ ํํฐ๋ง
m = m.loc[[x not in out_all for x in m['๋ฒํธ']]]
m.head()
2. ์ฃผ์ ์ ๊ฐํ์ ํํฉ ๋ถ์: ์ฐ๋๋ณ ์ฃผ์ ์ ID ๋น๊ต๋ฅผ ํตํ ๊ฐํ์ ํํฉ ๋ถ์
2.1. ์ฐ๋๋ณ ๊ฐํ์ ์์น ๋ถ์
id_dict=dict()
for year in range(2018,2022):
id_dict[year] = set(m.loc[~m[f'์ํ_{year}'].isna()]['๋ฒํธ'].unique())
diff_dict=dict()
for year in range(2018,2021):
opened = len(id_dict[year+1].difference(id_dict[year]))
closed = len(id_dict[year].difference(id_dict[year+1]))
diff_dict[f'{year}_{year+1}']=[opened,closed]
diff_df = pd.DataFrame(diff_dict,index=['OPENED','CLOSED'])
diff_df
diff_df.plot()
diff_df.T.plot(color=['r','b'])
diff_df.plot()
diff_df.T.plot(color=['r','b'])
[๋ฌธ์ 1] 2020๋ ์ ์ ๊ท ๊ฐ์ ํ ์ ํ ์ฃผ์ ์์ ๊ฐ์๋ฅผ ๊ตฌํ์์ค.
id_dict=dict()
for year in range(2018,2022):
id_dict[year] = set(m.loc[(~m[f'์ํ_{year}'].isna())&(m['์
ํ์ฌ๋ถ']=='์
ํ')]['๋ฒํธ'].unique())
diff_dict=dict()
for year in range(2018,2021):
opened = len(id_dict[year+1].difference(id_dict[year]))
closed = len(id_dict[year].difference(id_dict[year+1]))
diff_dict[f'{year}_{year+1}']=[opened,closed]
diff_df = pd.DataFrame(diff_dict,index=['OPENED','CLOSED'])
diff_df
# 2020๋
์ ์ ๊ท ๊ฐ์
ํ ์
ํ ์ฃผ์ ์์ ๊ฐ์๋ฅผ ๊ตฌํ์ฌ quiz_1 ๋ณ์์ ์ ์ฅํฉ๋๋ค.
# ์ซ์ํ์ผ๋ก ์ ์ฅํฉ๋๋ค.
quiz_1 = diff_df['2019_2020']['OPENED']
3. ๋ธ๋๋ ๋ถ์: ๋ธ๋๋๋ณ ๊ฐ๊ฒฉ๊ฒฝ์๋ ฅ ๋ฐ ์์ฅ์ ์ ์จ ๋ถ์
3.1. ์ฃผ์ ๋ธ๋๋๋ณ ๊ฐ๊ฒฉ Line Plot ๋ถ์
brand_price_dict=dict()
for year in range(2018,2022):
brand_price_dict[str(year)]=m.groupby(f'์ํ_{year}')[f'ํ๋ฐ์ _{year}'].mean()
brand_price_df = pd.DataFrame(brand_price_dict)
brand_price_df
brand_price_df = brand_price_df.drop('SK๊ฐ์ค')
brand_price_df.T.plot(figsize=(10,5))
3.2. ์ฃผ์ ๋ธ๋๋๋ณ ์ง๋ 4๋ ๊ฐ ์์ฅ ์ ์ ์จ Stacked Bar Plot ๋ฐ Heatmap
brand_share_dict=dict()
for year in range(2018,2022):
brand_share_dict[str(year)]=m.groupby(f'์ํ_{year}').size()
brand_share_df = pd.DataFrame(brand_share_dict)
brand_share_df
brand_share_df = brand_share_df.drop('SK๊ฐ์ค')
brand_ratio_df = brand_share_df.apply(lambda x:x/brand_share_df.sum(),axis=1)
brand_ratio_df = brand_ratio_df.sort_values('2018',ascending=False)
brand_ratio_df
brand_ratio_df.T.plot(kind='bar',stacked=True,rot=0,figsize=(10,5))
plt.legend(bbox_to_anchor=(1, 1))
plt.figure(figsize=(10,5))
sns.heatmap(brand_ratio_df, cmap= 'RdBu_r', linewidths=1, linecolor='black',annot=True)
[๋ฌธ์ 2] 2019๋ ์ฃผ์ ์๋ฅผ ์ ํ ๋ฐ ์ผ๋ฐ ์ฃผ์ ์๋ก ๊ตฌ๋ถํ๊ณ ์ผ๋ฐ ์ฃผ์ ์๊ฐ ์ฐจ์งํ๋ ๋น์จ์ ๊ตฌํ์์ค
self_share_dict = m.loc[~m['์ํ_2019'].isna()].groupby('์
ํ์ฌ๋ถ').size()
self_ratio_dict = self_share_dict/self_share_dict.sum()
self_ratio_dic
# 2019๋
๊ธฐ์ค ์
ํ์ฃผ์ ์์ ์์ฅ ์ ์ ์จ์ quiz_2 ๋ณ์์ ์ ์ฅํฉ๋๋ค.
# ๋น์จ์ ์์ซ์ ๋์งธ์๋ฆฌ ๊น์ง ๋ฐ์ฌ๋ฆผํ์ฌ ์ซ์ํ์ผ๋ก ์ ์ถํฉ๋๋ค.
quiz_2 = round(self_ratio_dict['์ผ๋ฐ'],2)
4. ๊ฐ๊ฒฉ ๋ถ์: ์ฃผ์ ์ ๋ฐ ์ง์ญ ๋ณ ๊ฐ๊ฒฉ ํธ์ฐจ ๋ถ์
4.1. ๊ฐ๊ฒฉ ๋ถํฌ Boxplot
sns.boxplot(x=m['ํ๋ฐ์ _2021'])
4.2. ์ง์ญ๋ณ ๊ฐ๊ฒฉ ๋ถํฌ Boxplot (Multiple Columns)
boxplot_order = m.groupby('์ง์ญ2')['ํ๋ฐ์ _2021'].median().sort_values(ascending=False).index
plt.figure(figsize=(15,7))
sns.boxplot(x="์ง์ญ2", y="ํ๋ฐ์ _2021", data=m, orient="v", order=boxplot_order)
'๊ฐ์ธ๊ณต๋ถ > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
66. Python Statis Scipy ์ฐ์ต๋ฌธ์ (0) | 2021.11.30 |
---|---|
65. Python Numpy ์ฐ์ต๋ฌธ์ (2) (0) | 2021.11.29 |
63. NIPA AI์จ๋ผ์ธ ๊ต์ก AI ์ค๋ฌด ๊ธฐ๋ณธ ๊ณผ์ (3)๊ตญ๋ด ์ฝ๋ก๋ ํ์ ์ถ์ด ๋ถ์ (0) | 2021.11.27 |
62. Python Numpy ์ฐ์ต๋ฌธ์ (1) (0) | 2021.11.26 |
61. Python Group & Apply ์ฐ์ต๋ฌธ์ (0) | 2021.11.25 |