titanic 知識點總結

1,缺失值

填補缺失值可以根據與其相關性較高的屬性進行分組填充

df_all['Age'] = df_all.groupby(['Sex', 'Pclass'])['Age'].apply(lambda x: x.fillna(x.median()))

如果對某個特征補的缺失值過多,可以將其特征中的分布相似的值進行合并,已達到減少偏度,使填補的值不對最后預測產生很大的影響

2,觀察目標變量(分類)

survived = df_train['Survived'].value_counts()[1]

not_survived = df_train['Survived'].value_counts()[0]

survived_per = survived / df_train.shape[0] * 100

not_survived_per = not_survived / df_train.shape[0] * 100

print('{} of {} passengers survived and it is the {:.2f}% of the training set.'.format(survived, df_train.shape[0], survived_per))

print('{} of {} passengers didnt survive and it is the {:.2f}% of the training set.'.format(not_survived, df_train.shape[0], not_survived_per))

plt.figure(figsize=(10, 8))

sns.countplot(df_train['Survived'])

plt.xlabel('Survival', size=15, labelpad=15)

plt.ylabel('Passenger Count', size=15, labelpad=15)

plt.xticks((0, 1), ['Not Survived ({0:.2f}%)'.format(not_survived_per), 'Survived ({0:.2f}%)'.format(survived_per)])

plt.tick_params(axis='x', labelsize=13)

plt.tick_params(axis='y', labelsize=13)

plt.title('Training Set Survival Distribution', size=15, y=1.05)

plt.show()

3,相關性做圖

fig, axs = plt.subplots(nrows=2, figsize=(20, 20))

sns.heatmap(df_train.drop(['PassengerId'], axis=1).corr(), ax=axs[0], annot=True, square=True, cmap='coolwarm', annot_kws={'size': 14})

sns.heatmap(df_test.drop(['PassengerId'], axis=1).corr(), ax=axs[1], annot=True, square=True, cmap='coolwarm', annot_kws={'size': 14})

for i in range(2):? ?

? ? axs[i].tick_params(axis='x', labelsize=14)

? ? axs[i].tick_params(axis='y', labelsize=14)


axs[0].set_title('Training Set Correlations', size=15)

axs[1].set_title('Test Set Correlations', size=15)

plt.show()

4,觀察目標變量與特征(連續)分布圖

cont_features = ['Age', 'Fare']

surv = df_train['Survived'] == 1

fig, axs = plt.subplots(ncols=2, nrows=2, figsize=(20, 20))

plt.subplots_adjust(right=1.5)

for i, feature in enumerate(cont_features):? ?

? ? # Distribution of survival in feature

? ? #ax 表示左圖還是右圖

? ? sns.distplot(df_train[~surv][feature], label='Not Survived', hist=True, color='#e74c3c', ax=axs[0][i])

? ? sns.distplot(df_train[surv][feature], label='Survived', hist=True, color='#2ecc71', ax=axs[0][i])


? ? # Distribution of feature in dataset

? ? sns.distplot(df_train[feature], label='Training Set', hist=False, color='#e74c3c', ax=axs[1][i])

? ? sns.distplot(df_test[feature], label='Test Set', hist=False, color='#2ecc71', ax=axs[1][i])


? ? axs[0][i].set_xlabel('')

? ? axs[1][i].set_xlabel('')


? ? for j in range(2):? ? ? ?

? ? ? ? axs[i][j].tick_params(axis='x', labelsize=20)

? ? ? ? axs[i][j].tick_params(axis='y', labelsize=20)


? ? axs[0][i].legend(loc='upper right', prop={'size': 20})

? ? axs[1][i].legend(loc='upper right', prop={'size': 20})

? ? axs[0][i].set_title('Distribution of Survival in {}'.format(feature), size=20, y=1.05)

axs[1][0].set_title('Distribution of {} Feature'.format('Age'), size=20, y=1.05)

axs[1][1].set_title('Distribution of {} Feature'.format('Fare'), size=20, y=1.05)


plt.show()

離散

cat_features = ['Embarked', 'Parch', 'Pclass', 'Sex', 'SibSp', 'Deck']

fig, axs = plt.subplots(ncols=2, nrows=3, figsize=(20, 20))

plt.subplots_adjust(right=1.5, top=1.25)

for i, feature in enumerate(cat_features, 1):? ?

? ? plt.subplot(2, 3, i)

? ? sns.countplot(x=feature, hue='Survived', data=df_train)


? ? plt.xlabel('{}'.format(feature), size=20, labelpad=15)

? ? plt.ylabel('Passenger Count', size=20, labelpad=15)? ?

? ? plt.tick_params(axis='x', labelsize=20)

? ? plt.tick_params(axis='y', labelsize=20)


? ? plt.legend(['Not Survived', 'Survived'], loc='upper center', prop={'size': 18})

? ? plt.title('Count of Survival in {} Feature'.format(feature), size=20, y=1.05)

plt.show()

5,對連續特征進行分箱

df_all['Fare'] = pd.qcut(df_all['Fare'], 13)

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。