Exemple #1
0
 def test_standard_fit_regression_stacker_no_bagging_free_weights(self):
     """Test regression stacking with metric mean absolute error, no bagging"""
     stacker = LinearPredictorStacker(
         metric=metric_mae,
         algorithm=LinearPredictorStacker.STANDARD,
         normed_weights=False,
         max_iter=200,
         n_bags=1,
         colsample=1.,
         subsample=1.,
         verbose=2,
         eps=1e-3,
         seed=0)
     stacker.add_predictors_by_filename(files=[
         get_path('noid_OOF_predictions_2.csv'),
         get_path('noid_OOF_predictions_3.csv'),
         get_path('noid_OOF_predictions_4.csv')
     ])
     self.assertEqual(len(stacker.target), 1000)
     self.assertEqual(len(stacker.predictors), 1000)
     self.assertEqual(stacker.predictors.shape[1], 22)
     stacker.fit()
     print(stacker.get_weights())
     self.assertAlmostEqual(stacker.score, 1185.0745095110819, places=4)
     self.assertAlmostEqual(stacker.mean_score,
                            1188.5725272161117,
                            places=4)
Exemple #2
0
 def test_fit_with_predictors_and_target(self):
     data = pd.read_csv(get_path('noid_OOF_predictions_2.csv'))
     target = data.loss
     data.drop(['loss'], axis=1, inplace=True)
     stacker = LinearPredictorStacker(
         metric=metric_rmse,
         algorithm=LinearPredictorStacker.SWAPPING,
         max_iter=200,
         n_bags=1,
         colsample=1.,
         subsample=1.,
         verbose=0)
     stacker.fit(predictors=data, target=target)
     self.assertAlmostEqual(stacker.score, 1969.3946377360355, places=4)
     self.assertAlmostEqual(stacker.mean_score,
                            2033.0404650725116,
                            places=4)
     # test weights
     self.assertTupleEqual(
         stacker.get_weights(),
         (-0.75000000000000011, 0.050000000000000003, -0.44999999999999996,
          2.4499999999999993, 0.050000000000000003, -1.2500000000000004,
          0.050000000000000003, -1.7500000000000009, 0.050000000000000003,
          0.44999999999999996, 0.25, 3.099999999999997,
          -0.75000000000000011, -0.39999999999999997, 2.5499999999999989,
          -2.3999999999999995, -1.850000000000001, 0.050000000000000003,
          1.5000000000000007, 0.050000000000000003))
Exemple #3
0
 def test_fit_swapping_regression_stacker_no_bagging_normed_weights(self):
     """Test regression stacking with metric mean absolute error, no bagging"""
     stacker = LinearPredictorStacker(
         metric=metric_mae,
         algorithm=LinearPredictorStacker.SWAPPING,
         normed_weights=True,
         max_iter=2000,
         n_bags=1,
         colsample=1.,
         subsample=1.,
         verbose=2,
         eps=1e-3,
         seed=11)
     stacker.add_predictors_by_filename(files=[
         get_path('noid_OOF_predictions_2.csv'),
         get_path('noid_OOF_predictions_3.csv'),
         get_path('noid_OOF_predictions_4.csv')
     ])
     self.assertEqual(len(stacker.target), 1000)
     self.assertEqual(len(stacker.predictors), 1000)
     self.assertEqual(stacker.predictors.shape[1], 22)
     stacker.fit()
     self.assertAlmostEqual(stacker.score, 1156.3066, places=4)
     self.assertAlmostEqual(stacker.mean_score,
                            1188.5725272161117,
                            places=4)
Exemple #4
0
 def test_fit_regression_stacker_mae_ten_bags(self):
     """Test regression stacking with metric mean absolute error and 20 bags"""
     stacker = LinearPredictorStacker(metric=metric_mae,
                                      max_iter=10,
                                      n_bags=20,
                                      colsample=.8,
                                      subsample=.8,
                                      seed=24698537)
     stacker.add_predictors_by_filename(files=[
         get_path('noid_OOF_predictions_2.csv'),
         get_path('noid_OOF_predictions_3.csv'),
         get_path('noid_OOF_predictions_4.csv')
     ])
     self.assertEqual(len(stacker.target), 1000)
     self.assertEqual(len(stacker.predictors), 1000)
     self.assertEqual(stacker.predictors.shape[1], 22)
     stacker.fit()
     self.assertAlmostEqual(stacker.score, 1179.2406088734808, places=4)
     # Old version self.assertAlmostEqual(stacker.score, 1187.6537373418842, places=4)
     self.assertAlmostEqual(stacker.mean_score,
                            1188.5725272161117,
                            places=4)
Exemple #5
0
 def test_fit_regression_stacker_rmse_no_bagging_step_decrease(self):
     """Test regression stacking with metric root mean squared error, no bagging"""
     stacker = LinearPredictorStacker(metric=metric_rmse,
                                      max_iter=250,
                                      n_bags=1,
                                      colsample=1.,
                                      subsample=1.,
                                      verbose=0)
     stacker.add_predictors_by_filename(files=[
         get_path('noid_OOF_predictions_2.csv'),
         get_path('noid_OOF_predictions_3.csv'),
         get_path('noid_OOF_predictions_4.csv')
     ])
     self.assertEqual(len(stacker.target), 1000)
     self.assertEqual(len(stacker.predictors), 1000)
     self.assertEqual(stacker.predictors.shape[1], 22)
     stacker.fit()
     self.assertAlmostEqual(stacker.score, 1972.574584232116, places=4)
     # Old version self.assertAlmostEqual(stacker.score, 2030.5021340510675, places=4)
     self.assertAlmostEqual(stacker.mean_score,
                            2032.2110846499691,
                            places=4)
Exemple #6
0
 def test_fit_regression_stacker_mae_no_bagging(self):
     """Test regression stacking with metric mean absolute error, no bagging"""
     stacker = LinearPredictorStacker(metric=metric_mae,
                                      max_iter=20,
                                      n_bags=1,
                                      colsample=1.,
                                      subsample=1.,
                                      verbose=0)
     stacker.add_predictors_by_filename(files=[
         get_path('noid_OOF_predictions_2.csv'),
         get_path('noid_OOF_predictions_3.csv'),
         get_path('noid_OOF_predictions_4.csv')
     ])
     self.assertEqual(len(stacker.target), 1000)
     self.assertEqual(len(stacker.predictors), 1000)
     self.assertEqual(stacker.predictors.shape[1], 22)
     stacker.fit()
     self.assertAlmostEqual(stacker.score, 1176.295406, places=4)
     # Old version self.assertAlmostEqual(stacker.score, 1187.1916616561432, places=4)
     self.assertAlmostEqual(stacker.mean_score,
                            1188.5725272161117,
                            places=4)
Exemple #7
0
 def test_swapping_fit_regression_stacker_rmse_no_bagging(self):
     """Test regression stacking with metric root mean squared error, no bagging"""
     stacker = LinearPredictorStacker(
         metric=metric_rmse,
         algorithm=LinearPredictorStacker.SWAPPING,
         max_iter=20,
         n_bags=1,
         colsample=1.,
         subsample=1.,
         verbose=0)
     stacker.add_predictors_by_filename(files=[
         get_path('noid_OOF_predictions_2.csv'),
         get_path('noid_OOF_predictions_3.csv'),
         get_path('noid_OOF_predictions_4.csv')
     ])
     self.assertEqual(len(stacker.target), 1000)
     self.assertEqual(len(stacker.predictors), 1000)
     self.assertEqual(stacker.predictors.shape[1], 22)
     stacker.fit()
     self.assertAlmostEqual(stacker.score, 2008.0897782026507, places=4)
     self.assertAlmostEqual(stacker.mean_score,
                            2032.2110846499691,
                            places=4)
Exemple #8
0
 def test_regression_r2_score_maximization(self):
     stacker = LinearPredictorStacker(
         metric=r2_score,
         maximize=True,
         algorithm=LinearPredictorStacker.STANDARD,
         normed_weights=False,
         max_iter=200,
         n_bags=1,
         colsample=1.,
         subsample=1.,
         verbose=2,
         eps=1e-4,
         seed=0)
     stacker.add_predictors_by_filename(files=[
         get_path('noid_OOF_predictions_2.csv'),
         get_path('noid_OOF_predictions_3.csv'),
         get_path('noid_OOF_predictions_4.csv')
     ])
     self.assertEqual(len(stacker.target), 1000)
     self.assertEqual(len(stacker.predictors), 1000)
     self.assertEqual(stacker.predictors.shape[1], 22)
     stacker.fit()
     self.assertAlmostEqual(stacker.score, 0.5478882, places=4)
     self.assertAlmostEqual(stacker.mean_score, 0.537271112149033, places=4)