Tilburg Thesis_Malawi School Dropout/src/thesis_utils_general.py
def evaluate_model(y_test,
                   y_pred,
                   y_prob=None,
                   sample_weights=None,
                   show=True,
                   compare_models=None,
                   store_model=False,
                   model_name=None,
                   prefix=None,
                   model=None,
                   features=None):
    """Evaluate model performance. Options to display results and store model"""

    metrics = calculate_metrics(y_test, y_pred, y_prob, sample_weights)

    # Provide an output name if none given
    if model_name is None:
        model_name = 'score'
    if prefix is not None:
        model_name = prefix + "_" + model_name
    metrics['name'] = model_name

    # Display results
    if show is True:

        # Load models to compare
        comp_models = [metrics]
        if compare_models is not None:
            for comp_model in np.ravel(compare_models):
                filepath = os.path.join('../models', comp_model + '.pkl')
                with open(filepath, "rb") as f:
                    m = pickle.load(f)
                    m_metrics = calculate_metrics(m['y_true'],
                                                  m['y_pred'],
                                                  m['y_prob'],
                                                  m['sample_weights'])
                    m_metrics['name'] = m['name']
                    comp_models.append(m_metrics)
        visualize.display_model_comparison(comp_models, show_roc=(y_prob is not None))

    # Store model
    if (store_model is True) & (model_name is not None):
        
        _dir = os.path.join('../models')

        if not os.path.exists(_dir):
            os.makedirs(_dir)

        filepath = os.path.join(_dir, model_name + '.pkl')
        with open(filepath, 'wb') as f:
            if type(model) == KerasClassifier:
                model_path = os.path.join(MODELS_DIR, model_name + '.h5')
                model.model.save(model_path)
                model = model_path
            output = {'model': model,
                      'y_true': y_test,
                      'y_pred': y_pred,
                      'y_prob': y_prob,
                      'sample_weights': sample_weights,
                      'features': features,
                      'timestamp': pd.Timestamp.utcnow(),
                      'name': model_name}
            pickle.dump(output, f)

    return metrics
Evaluate model performance. Options to display results and store model
Evaluate model performance. Options to display results and store model
Evaluate model performance . Options to display results and store model
def evaluate_model(y_test,
                   y_pred,
                   y_prob=None,
                   sample_weights=None,
                   show=True,
                   compare_models=None,
                   store_model=False,
                   model_name=None,
                   prefix=None,
                   model=None,
                   features=None):
    metrics = calculate_metrics(y_test, y_pred, y_prob, sample_weights)
    if model_name is None:
        model_name = 'score'
    if prefix is not None:
        model_name = prefix + "_" + model_name
    metrics['name'] = model_name
    if show is True:
        comp_models = [metrics]
        if compare_models is not None:
            for comp_model in np.ravel(compare_models):
                filepath = os.path.join('../models', comp_model + '.pkl')
                with open(filepath, "rb") as f:
                    m = pickle.load(f)
                    m_metrics = calculate_metrics(m['y_true'],
                                                  m['y_pred'],
                                                  m['y_prob'],
                                                  m['sample_weights'])
                    m_metrics['name'] = m['name']
                    comp_models.append(m_metrics)
        visualize.display_model_comparison(comp_models, show_roc=(y_prob is not None))
    if (store_model is True) & (model_name is not None):
        _dir = os.path.join('../models')
        if not os.path.exists(_dir):
            os.makedirs(_dir)
        filepath = os.path.join(_dir, model_name + '.pkl')
        with open(filepath, 'wb') as f:
            if type(model) == KerasClassifier:
                model_path = os.path.join(MODELS_DIR, model_name + '.h5')
                model.model.save(model_path)
                model = model_path
            output = {'model': model,
                      'y_true': y_test,
                      'y_pred': y_pred,
                      'y_prob': y_prob,
                      'sample_weights': sample_weights,
                      'features': features,
                      'timestamp': pd.Timestamp.utcnow(),
                      'name': model_name}
            pickle.dump(output, f)
    return metrics
def evaluate_model ( y_test , y_pred , y_prob = None , sample_weights = None , show = True , compare_models = None , store_model = False , model_name = None , prefix = None , model = None , features = None ) : metrics = calculate_metrics ( y_test , y_pred , y_prob , sample_weights ) if model_name is None : model_name = 'score' if prefix is not None : model_name = prefix + "_" + model_name metrics [ 'name' ] = model_name if show is True : comp_models = [ metrics ] if compare_models is not None : for comp_model in np . ravel ( compare_models ) : filepath = os . path . join ( '../models' , comp_model + '.pkl' ) with open ( filepath , "rb" ) as f : m = pickle . load ( f ) m_metrics = calculate_metrics ( m [ 'y_true' ] , m [ 'y_pred' ] , m [ 'y_prob' ] , m [ 'sample_weights' ] ) m_metrics [ 'name' ] = m [ 'name' ] comp_models . append ( m_metrics ) visualize . display_model_comparison ( comp_models , show_roc = ( y_prob is not None ) ) if ( store_model is True ) & ( model_name is not None ) : _dir = os . path . join ( '../models' ) if not os . path . exists ( _dir ) : os . makedirs ( _dir ) filepath = os . path . join ( _dir , model_name + '.pkl' ) with open ( filepath , 'wb' ) as f : if type ( model ) == KerasClassifier : model_path = os . path . join ( MODELS_DIR , model_name + '.h5' ) model . model . save ( model_path ) model = model_path output = { 'model' : model , 'y_true' : y_test , 'y_pred' : y_pred , 'y_prob' : y_prob , 'sample_weights' : sample_weights , 'features' : features , 'timestamp' : pd . Timestamp . utcnow ( ) , 'name' : model_name } pickle . dump ( output , f ) return metrics
Evaluate model performance.
Evaluate model performance .
"""Evaluate model performance. Options to display results and store model""" # Provide an output name if none given # Display results # Load models to compare # Store model
y_test
y_pred
y_prob
sample_weights
show
compare_models
store_model
model_name
prefix
model
features