def plot_mean_feature_importances(feature_importances, max_num=50, importance_type='gain', path=None):
    mean_gain = feature_importances[[importance_type, 'feature']].groupby('feature').mean()
    feature_importances[('mean_' + importance_type)] = feature_importances['feature'].map(mean_gain[importance_type])
    if (path is not None):
        data = feature_importances.sort_values(('mean_' + importance_type), ascending=False).iloc[:max_num, :]
        plt.clf()
        plt.figure(figsize=(16, 8))
        sns.barplot(x=importance_type, y='feature', data=data)
        plt.tight_layout()
        plt.savefig(path)
    return feature_importances
def plot_mean_feature_importances(feature_importances, max_num=50, importance_type='gain', path=None):
    mean_gain = feature_importances[[importance_type, 'feature']].groupby('feature').mean()
    feature_importances[('mean_' + importance_type)] = feature_importances['feature'].map(mean_gain[importance_type])
    if (path is not None):
        data = feature_importances.sort_values(('mean_' + importance_type), ascending=False).iloc[:max_num, :]
        plt.clf()
        plt.figure(figsize=(16, 8))
        sns.barplot(x=importance_type, y='feature', data=data)
        plt.tight_layout()
        plt.savefig(path)
    return feature_importances