# 불필요한 경고창 끄기 # warning message : deprecated import warnings warnings.filterwarnings('ignore')
# 데이터 훑어보기 df.info() # 숫자형 데이터의 경우에만 유의미 df.describe() # 데이터의 처음 5개의 표본을 출력 df.head()
# plt를 이용해서 그래프 df.plot()
EDA : 어떤 요소가 결과에 영향을 미치느냐?
# 바차트 df.groupby(['sex', 'survived']).count()['name'].plot.bar() # 성별에 영향이 있을수도 있다
# 히스토그램 df['age'].plot.hist(bins=20) # 연령대에 따른 -> 생존 사망 구분 age_cat = pd.cut(df['age'], bins=[0, 10, 20, 50, 100], labels=['baby','teens','adult','elder'])
import seaborn as sns sns.barplot('age_cat', 'survived', data=df) # 연령대 기준 : 영유아의 경우 다른 연령대에 비해서 생존율이 높다 sns.barplot('pclass', 'survived', data=df) # 객실 등급이 생존에 영향을 준다
sns.barplot('sex', 'survived', data=df) # 성별에도 영향이 있다
# 사망자의 연령대 분포 sns.kdeplot(df['age'][(df['survived']==0) & (df['age'].notnull())], shade=True) # 생존자의 연령대 분포 sns.kdeplot(df['age'][(df['survived']==1) & (df['age'].notnull())], shade=True)
fig = plt.figure(figsize=(12,6)) ax1 = fig.add_subplot(2,2,1) ax2 = fig.add_subplot(2,2,2) ax3 = fig.add_subplot(2,2,3) ax4 = fig.add_subplot(2,2,4) sns.countplot(x = 'sex', data=df, ax=ax1) sns.countplot(x = 'sex', hue='survived', data=df, ax=ax2) sns.countplot(x = 'sex', hue='pclass', data=df, ax=ax3) sns.countplot(x = 'sex', hue='age_cat', data=df, ax=ax4) fig
plt.bar(df.groupby(['pclass'])['survived'].mean().index, df.groupby(['pclass'])['survived'].mean(), color = ['blue','red','green']) plt.xticks([1,2,3])
fig = plt.figure(figsize=(12,6)) ax1 = fig.add_subplot(1,3,1) ax2 = fig.add_subplot(1,3,2) ax3 = fig.add_subplot(1,3,3) sns.barplot(x='pclass', y='survived', data=df, ax=ax1) sns.barplot(x='age_cat', y='survived', data=df, ax=ax2) sns.barplot(x='sex', y='survived', data=df, ax=ax3)
import missingno as msno df.isnull().sum() msno.matrix(df) plt.show() msno.bar(df) plt.show()
#df.fillna() #df['age'].fillna() df['age'].interpolate(method='values') msno.matrix(df)
# 보트에 탑승한 승객 is_boat = df['boat'].notnull() fig = plt.figure(figsize=(12,6)) ax = fig.add_subplot(1,1,1) # 보트에 탑승해서 생존한 승객 df[is_boat]['survived'].value_counts().plot.pie( explode=[0,0.3], autopct="%0.2f%%", labels = ['Survived', 'Not Survived'], ax=ax ) ax.set_ylabel('')