def test_set_params(self): # Initialize estimator_class = LinearRegression amount_of_steps = 30 estimator_kwargs = {'fit_intercept': False} sort_col = 'date' steps_estimator = StepsSelectorEstimator(estimator_class, amount_of_steps, estimator_kwargs, sort_col) # Set params estimator_class = LogisticRegression amount_of_steps = 40 estimator_kwargs = {'fit_intercept': True} sort_col = 'month' steps_estimator.set_params( estimator_class=LogisticRegression, amount_of_steps=amount_of_steps, estimator_kwargs=estimator_kwargs, sort_col=sort_col, ) # Assert deep params = steps_estimator.get_params() self.assertEqual(len(params), 4) self.assertEqual(params['estimator_class'], Classer(estimator_class)) self.assertEqual(params['amount_of_steps'], amount_of_steps) self.assertEqual(params['estimator_kwargs'], estimator_kwargs) self.assertEqual(params['sort_col'], sort_col)
def test_estimate(self): # Fake data with a v shape. First 30 drop. Last 30 Increase. x = list(range(30)) + list(range(30)) y = pd.Series(list(range(29, -1, -1)) + list(range(30))) X = pd.DataFrame({'x': x}) # Initialize estimator_class = LinearRegression steps_estimator = StepsSelectorEstimator(estimator_class, 30) # Fit steps_estimator.fit(X, y) # Predict y_pred = steps_estimator.predict(X) # Assert that only the increasing slope was fitted for i in range(60): self.assertAlmostEqual(y_pred[i], i % 30)
def test_get_params_shallow(self): # Initialize estimator_class = LinearRegression amount_of_steps = 30 estimator_kwargs = {'fit_intercept': False} sort_col = 'date' steps_estimator = StepsSelectorEstimator(estimator_class, amount_of_steps, estimator_kwargs, sort_col) # Assert shallow params = steps_estimator.get_params(deep=False) self.assertEqual(params['estimator_class'], Classer(estimator_class)) self.assertEqual(params['amount_of_steps'], amount_of_steps) self.assertEqual(params['estimator_kwargs'], estimator_kwargs) self.assertTrue(params['estimator_kwargs'] is estimator_kwargs) self.assertEqual(params['sort_col'], sort_col)
def test_fit_unsorted(self): # Fake data with a v shape. First 30 drop. Last 30 Increase. x = list(range(30)) + list(range(30)) y = pd.Series(list(range(29, -1, -1)) + list(range(30))) X = pd.DataFrame({'x': x}) # Initialize estimator_class = LinearRegression steps_estimator = StepsSelectorEstimator(estimator_class, 30) steps_estimator._estimator = MagicMock() # Fit steps_estimator.fit(X, y) # Assert args, kwargs = steps_estimator._estimator.fit.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 0) X_call, y_call = args self.assertEqual(X.iloc[-30:].values.tolist(), X_call.values.tolist()) self.assertEqual(y.iloc[-30:].values.tolist(), y_call.values.tolist())
def test_repr(self): # Initialize estimator_class = LinearRegression amount_of_steps = 30 estimator_kwargs = {'fit_intercept': False} sort_col = 'date' steps_estimator = StepsSelectorEstimator(estimator_class, amount_of_steps, estimator_kwargs, sort_col) # Assert expected = ('StepsSelectorEstimator(' 'estimator_class=Classer(LinearRegression), ' 'amount_of_steps=30, ' 'estimator_kwargs={\'fit_intercept\': False})') self.assertEqual(str(steps_estimator), expected)
def test_init(self): # Arguments estimator_class = LinearRegression amount_of_steps = 30 estimator_kwargs = {'fit_intercept': False} sort_col = 'date' # Initialize se = StepsSelectorEstimator(estimator_class, amount_of_steps, estimator_kwargs, sort_col) # Asserts self.assertEqual(se._estimator.__class__, estimator_class) self.assertFalse(se._estimator.fit_intercept) self.assertEqual(se.sort_col, sort_col) self.assertEqual(se.estimator_kwargs, estimator_kwargs) self.assertTrue(se.estimator_kwargs is estimator_kwargs)