Example #1
0
def feature_importance(boost_obj, args):
    plt.figure(figsize=[10, 10])

    model = args.model
    path = args.out

    if model.lower() == 'gboost':
        import xgboost as xgb
        xgb.plot_importance(boost_obj.model, color='green')
        plt.rcParams['figure.figsize'] = [10, 10]
        '''
        xgb.plot_tree(boost_obj.model, num_trees=0, rankdir='LR')
        fig = matplotlib.pyplot.gcf()
        fig.set_size_inches(150, 100)
        fig.savefig('tree.png')
        '''
    elif model.lower() in ['lr', 'svml']:
        pd.Series(abs(boost_obj.model.coef_[0]),
                  index=boost_obj.X_data.columns).plot(kind='barh')
    elif model.lower() in ['ngboost', 'svm_rbf']:
        return 0
    else:
        importances_rf = pd.Series(boost_obj.model.feature_importances_,
                                   index=boost_obj.X_data.columns)
        sorted_importance_rf = importances_rf.sort_values()
        sorted_importance_rf.plot(kind='barh', color='lightgreen')

    plt.title('Feature Importance')
    if args.snps:
        name = 'feature_importance_' + args.predictor + '_snps.pdf'
    elif args.indels:
        name = 'feature_importance_' + args.predictor + '_indels.pdf'
    elif args.all:
        name = 'feature_importance_' + args.predictor + '.pdf'
    plt.savefig(os.path.join(path, name), bbox_inches='tight')
def feature_importance(boost_obj, model, path):
    plt.figure(figsize=[10, 10])

    if model.lower() == 'gboost':
        import xgboost as xgb
        xgb.plot_importance(boost_obj.model)
        plt.rcParams['figure.figsize'] = [10, 10]
    else:
        importances_rf = pd.Series(boost_obj.model.feature_importances_,
                                   index=boost_obj.X_data.columns)
        sorted_importance_rf = importances_rf.sort_values()
        sorted_importance_rf.plot(kind='barh', color='lightgreen')
    plt.title('Feature Importance')
    plt.savefig(os.path.join(path, 'feature_importance.png'))