Esempio n. 1
0
def test_save_pfs():
    # Regression
    xgboost_regressor_cls = get_xgboost_learner('xgboost.XGBRegressor')
    dataset = Abalone()
    target_column = dataset.y_column
    df = dataset.df

    # Features generation
    features_df = df.kxy.generate_features(entity=None,
                                           max_lag=None,
                                           entity_name='*',
                                           exclude=[target_column])

    # Model building
    path = 'Abalone'
    results = features_df.kxy.fit(target_column, xgboost_regressor_cls, \
     problem_type='regression', feature_selection_method='pfs', \
     path=path)
    loaded_predictor = PFSPredictor().load(path, xgboost_regressor_cls)
    feature_directions = loaded_predictor.feature_directions
    assert feature_directions.shape[1] == features_df.shape[1] - 1
    predictions = loaded_predictor.predict(features_df)
    assert len(predictions.columns) == 1
    assert target_column in predictions.columns
    assert set(features_df.index).difference(set(predictions.index)) == set()
    assert set(predictions.index).difference(set(features_df.index)) == set()
Esempio n. 2
0
def test_lean_boosted_xgboost_regressor():
    for clz in ['xgboost.XGBRegressor']:
        # Regression
        xgboost_regressor_cls = get_xgboost_learner(clz)
        dataset = Abalone()
        target_column = dataset.y_column
        df = dataset.df

        # Features generation
        features_df = df.kxy.generate_features(entity=None,
                                               max_lag=None,
                                               entity_name='*',
                                               exclude=[target_column])

        # Model building
        results = features_df.kxy.fit(target_column, xgboost_regressor_cls, \
         problem_type='regression', additive_learning=True, return_scores=True, \
         n_down_perf_before_stop=1)
        model = results['predictor'].models[0]
        feature_columns = results['Selected Variables']
        x = features_df[feature_columns].values
        predictions = model.predict(x)
        path = '../kxy/misc/cache/%s-%s.sav' % (dataset.name, clz)
        model.save(path)

        loaded_model = xgboost_regressor_cls(path=path)
        loaded_predictions = loaded_model.predict(x)

        assert np.allclose(predictions, loaded_predictions)
Esempio n. 3
0
def test_rfe_predictor_xgboost():
    for clz in ['xgboost.XGBRegressor']:
        # Regression
        xgboost_regressor_cls = get_xgboost_learner(clz)
        dataset = Abalone()
        target_column = dataset.y_column
        df = dataset.df

        # Features generation
        features_df = df.kxy.generate_features(entity=None,
                                               max_lag=None,
                                               entity_name='*',
                                               exclude=[target_column])

        # Model building
        results = features_df.kxy.fit(target_column, xgboost_regressor_cls, \
         problem_type='regression', feature_selection_method='rfe', rfe_n_features=5)
        predictor = results['predictor']
        predictions = predictor.predict(features_df)
        path = '../kxy/misc/cache/%s-%s.sav' % (dataset.name, clz)
        predictor.save(path)

        loaded_predictor = RFEPredictor.load(path, xgboost_regressor_cls)
        loaded_predictions = loaded_predictor.predict(features_df)

        assert np.allclose(predictions, loaded_predictions)
Esempio n. 4
0
def test_xgboost_regression():
    regressor_cls = get_xgboost_learner('xgboost.XGBRegressor', random_state=0)

    fs = RFE(regressor_cls)

    dataset = Abalone()
    target_column = dataset.y_column
    df = dataset.df

    # Features generation
    features_df = df.kxy.generate_features(entity=None,
                                           max_lag=None,
                                           entity_name='*',
                                           exclude=[target_column])

    # Feature selection
    x_columns = [_ for _ in features_df.columns if _ != target_column]
    x_df = features_df[x_columns]
    y_df = features_df[[target_column]]
    n_vars = max(x_df.shape[1] - 5, 1)
    m = fs.fit(x_df, y_df, n_vars)

    # Assertions
    assert len(fs.selected_variables) == n_vars
    assert fs.selected_variables == ['Shell weight', 'Sex_I', 'Shucked weight.ABS(* - Q25(*))', 'Shucked weight', 'Shucked weight.ABS(* - MEDIAN(*))', \
     'Shucked weight.ABS(* - MEAN(*))', 'Diameter.ABS(* - Q75(*))', 'Height.ABS(* - Q75(*))', 'Diameter.ABS(* - MEAN(*))', 'Diameter.ABS(* - Q25(*))', \
     'Whole weight.ABS(* - MEDIAN(*))', 'Viscera weight.ABS(* - Q75(*))', 'Sex_M', 'Height.ABS(* - MEAN(*))', 'Shucked weight.ABS(* - Q75(*))', \
     'Viscera weight.ABS(* - MEAN(*))', 'Height.ABS(* - Q25(*))', 'Whole weight.ABS(* - MEAN(*))', 'Shell weight.ABS(* - Q25(*))', 'Whole weight.ABS(* - Q25(*))', \
     'Length.ABS(* - MEAN(*))', 'Length.ABS(* - Q75(*))', 'Whole weight.ABS(* - Q75(*))', 'Diameter.ABS(* - MEDIAN(*))', 'Shell weight.ABS(* - Q75(*))', \
     'Shell weight.ABS(* - MEAN(*))', 'Shell weight.ABS(* - MEDIAN(*))', 'Length.ABS(* - MEDIAN(*))', 'Sex_F', 'Viscera weight', 'Whole weight', \
     'Length.ABS(* - Q25(*))', 'Viscera weight.ABS(* - MEDIAN(*))']
Esempio n. 5
0
def test_xgboost_classifier():
    # Binary classification
    classifier_cls = get_xgboost_learner('xgboost.XGBClassifier',
                                         use_label_encoder=False,
                                         eval_metric='logloss',
                                         learning_rate=0.1,
                                         max_depth=10)
    fs = RFE(classifier_cls)

    dataset = BankNote()
    target_column = dataset.y_column
    df = dataset.df

    # Features generation
    features_df = df.kxy.generate_features(entity=None,
                                           max_lag=None,
                                           entity_name='*',
                                           exclude=[target_column])

    # Feature selection
    x_columns = [_ for _ in features_df.columns if _ != target_column]
    x_df = features_df[x_columns]
    y_df = features_df[[target_column]]
    n_vars = max(x_df.shape[1] - 5, 1)
    m = fs.fit(x_df, y_df, n_vars)

    # Assertions
    assert len(fs.selected_variables) == n_vars
    assert fs.selected_variables == ['Variance', 'Skewness', 'Kurtosis', 'Entropy', 'Skewness.ABS(* - MEDIAN(*))', \
     'Variance.ABS(* - MEAN(*))', 'Skewness.ABS(* - MEAN(*))', 'Kurtosis.ABS(* - MEDIAN(*))', 'Kurtosis.ABS(* - Q25(*))', \
     'Entropy.ABS(* - MEDIAN(*))', 'Skewness.ABS(* - Q25(*))', 'Entropy.ABS(* - MEAN(*))', 'Variance.ABS(* - Q25(*))', \
     'Kurtosis.ABS(* - MEAN(*))', 'Kurtosis.ABS(* - Q75(*))']
Esempio n. 6
0
def test_xgboost_regression():
    regressor_cls = get_xgboost_learner('xgboost.XGBRegressor', random_state=0)

    fs = Boruta(regressor_cls)

    dataset = Abalone()
    target_column = dataset.y_column
    df = dataset.df

    # Features generation
    features_df = df.kxy.generate_features(entity=None,
                                           max_lag=None,
                                           entity_name='*',
                                           exclude=[target_column])

    # Feature selection
    x_columns = [_ for _ in features_df.columns if _ != target_column]
    x_df = features_df[x_columns]
    y_df = features_df[[target_column]]
    m = fs.fit(x_df, y_df)

    # Assertions
    assert len(fs.selected_variables) == 5
    assert fs.selected_variables == [
        'Shucked weight', 'Shell weight', 'Sex_I',
        'Shucked weight.ABS(* - Q25(*))', 'Whole weight'
    ]
Esempio n. 7
0
def test_lean_boosted_xgboost_classifier():
    # Binary classification
    xgboost_classifier_cls = get_xgboost_learner('xgboost.XGBClassifier',
                                                 use_label_encoder=False,
                                                 eval_metric='logloss',
                                                 learning_rate=0.1,
                                                 max_depth=10)
    dataset = BankNote()
    target_column = dataset.y_column
    df = dataset.df

    # Features generation
    features_df = df.kxy.generate_features(entity=None,
                                           max_lag=None,
                                           entity_name='*',
                                           exclude=[target_column])

    # Model building
    results = features_df.kxy.fit(target_column, xgboost_classifier_cls, \
     problem_type='classification', additive_learning=True, return_scores=True, \
     n_down_perf_before_stop=1)

    assert results['Testing Accuracy'] == '0.974'
    assert results['Selected Variables'] == [
        'Variance', 'Skewness.ABS(* - Q25(*))', 'Kurtosis'
    ]
Esempio n. 8
0
def test_boruta():
	# Regression
	sklearn_regressor_cls = get_xgboost_learner('xgboost.XGBRegressor')
	dataset = Abalone()
	target_column = dataset.y_column
	df = dataset.df

	# Features generation
	features_df = df.kxy.generate_features(entity=None, max_lag=None, entity_name='*', exclude=[target_column])

	# Model building
	results = features_df.kxy.fit(target_column, sklearn_regressor_cls, \
		problem_type='regression', feature_selection_method='boruta', boruta_n_evaluations=100)
	assert results['Selected Variables'] == ['Shucked weight', 'Shell weight', 'Sex_I', \
		'Shucked weight.ABS(* - Q25(*))', 'Whole weight']
Esempio n. 9
0
def test_single_learner():
    # Regression
    xgboost_regressor_cls = get_xgboost_learner('xgboost.XGBRegressor')
    dataset = Abalone()
    target_column = dataset.y_column
    df = dataset.df

    # Features generation
    features_df = df.kxy.generate_features(entity=None,
                                           max_lag=None,
                                           entity_name='*',
                                           exclude=[target_column])

    # Model building
    results = features_df.kxy.fit(target_column, xgboost_regressor_cls, \
     problem_type='regression', start_n_features=2, min_n_features=2, max_n_features=2, \
     additive_learning=True, return_scores=True, n_down_perf_before_stop=1)
    assert results['Testing R-Squared'] == '0.493'
    assert results['Selected Variables'] == ['Shell weight', 'Shucked weight']
    assert len(features_df.kxy.predictor.models) == 1
Esempio n. 10
0
def test_lean_boosted_xgboost_regressor():
    # Regression
    xgboost_regressor_cls = get_xgboost_learner('xgboost.XGBRegressor')
    dataset = Abalone()
    target_column = dataset.y_column
    df = dataset.df

    # Features generation
    features_df = df.kxy.generate_features(entity=None,
                                           max_lag=None,
                                           entity_name='*',
                                           exclude=[target_column])

    # Model building
    results = features_df.kxy.fit(target_column, xgboost_regressor_cls, \
     problem_type='regression', additive_learning=True, return_scores=True, \
     n_down_perf_before_stop=1)
    assert results['Testing R-Squared'] == '0.496'
    assert results['Selected Variables'] == ['Shell weight', 'Shucked weight', 'Whole weight', \
     'Shell weight.ABS(* - Q25(*))', 'Viscera weight.ABS(* - MEDIAN(*))', 'Viscera weight.ABS(* - MEAN(*))', \
     'Height', 'Length', 'Diameter', 'Sex_I', 'Shucked weight.ABS(* - MEDIAN(*))', 'Diameter.ABS(* - MEDIAN(*))', \
     'Viscera weight.ABS(* - Q75(*))', 'Viscera weight.ABS(* - Q25(*))', 'Diameter.ABS(* - Q25(*))', 'Sex_M', 'Sex_F']
Esempio n. 11
0
def test_pfs_feature_selection():
    # Regression
    xgboost_regressor_cls = get_xgboost_learner('xgboost.XGBRegressor')
    dataset = Abalone()
    target_column = dataset.y_column
    df = dataset.df

    # Features generation
    features_df = df.kxy.generate_features(entity=None,
                                           max_lag=None,
                                           entity_name='*',
                                           exclude=[target_column])

    # Model building
    results = features_df.kxy.fit(target_column, xgboost_regressor_cls, \
     problem_type='regression', feature_selection_method='pfs')
    assert results['Feature Directions'].shape[1] == features_df.shape[1] - 1
    predictor = results['predictor']
    predictions = predictor.predict(features_df)
    assert len(predictions.columns) == 1
    assert target_column in predictions.columns
    assert set(features_df.index).difference(set(predictions.index)) == set()
    assert set(predictions.index).difference(set(features_df.index)) == set()
Esempio n. 12
0
def xgboost_classification_benchmark():
    # LeanML vs Boruta vs RFE
    xgboost_classifier_cls = get_xgboost_learner('xgboost.XGBClassifier')
    classification_benchmark(xgboost_classifier_cls, 'xgboost')
Esempio n. 13
0
def xgboost_regression_benchmark():
    # LeanML vs Boruta vs RFE
    xgboost_regressor_cls = get_xgboost_learner('xgboost.XGBRegressor')
    regression_benchmark(xgboost_regressor_cls, 'xgboost')