def test_fit(self): thresholder = SimpleThresholder(self.rules, "or") assert thresholder.feature_importances_ is None thresholder.fit(x=self.data["X_train"], y=self.data["y_train"]) np.testing.assert_array_equal( thresholder.feature_importances_, np.array([1, 1, 0, 0]) )
def test__convert_string_rule_to_dict(self): thresholder = SimpleThresholder(self.rules, "or") results = thresholder._convert_string_rule_to_dict(self.rules[0]) expected_results = { "feature_name": "x1", "operator": "gt", "threshold": 0 } assert results == expected_results
def test__convert_string_rule_to_dict(self): thresholder = SimpleThresholder(self.rules, 'or') results = thresholder._convert_string_rule_to_dict(self.rules[0]) expected_results = { 'feature_name': 'x1', 'operator': 'gt', 'threshold': 0 } assert results == expected_results
def test_predict_proba(self): for logical_operator in ["and", "or"]: thresholder = SimpleThresholder(self.rules, logical_operator) thresholder.fit(x=self.data["X_train"], y=self.data["y_train"]) results = thresholder.predict_proba(self.data["X_test"]) if logical_operator == "and": expected_results = np.array([[0, 1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0]]).transpose() elif logical_operator == "or": expected_results = np.array([[1, 1, 1, 1, 1, 1, 0, 1], [1, 1, 1, 1, 1, 1, 0, 1]]).transpose() np.testing.assert_array_equal(results, expected_results)
def test_predict_proba(self): for logical_operator in ['and', 'or']: thresholder = SimpleThresholder(self.rules, logical_operator) thresholder.fit(x=self.data['X_train'], y=self.data['y_train']) results = thresholder.predict_proba(self.data['X_test']) if logical_operator == 'and': expected_results = np.array([[0, 1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0]]).transpose() elif logical_operator == 'or': expected_results = np.array([[1, 1, 1, 1, 1, 1, 0, 1], [1, 1, 1, 1, 1, 1, 0, 1]]).transpose() np.testing.assert_array_equal(results, expected_results)
def test_rule_with_unavailable_feature_raises_error(self): rules = self.rules rules.append("x5 == 3") thresholder = SimpleThresholder(rules, "or") with self.assertRaises(BaselineFeatureNotInMatrix): thresholder.fit(x=self.data["X_train"], y=self.data["y_train"])
def test_all_feature_names_property(self): thresholder = SimpleThresholder(self.rules, "or") assert thresholder.all_feature_names == ["x1", "x2"]
def test_rule_with_unavailable_feature_raises_error(self): rules = self.rules rules.append('x5 == 3') thresholder = SimpleThresholder(rules, 'or') with self.assertRaises(BaselineFeatureNotInMatrix): thresholder.fit(x=self.data['X_train'], y=self.data['y_train'])
def test_fit(self): thresholder = SimpleThresholder(self.rules, 'or') assert thresholder.feature_importances_ == None thresholder.fit(x=self.data['X_train'], y=self.data['y_train']) np.testing.assert_array_equal(thresholder.feature_importances_, np.array([1, 1, 0, 0]))
def test_all_feature_names_property(self): thresholder = SimpleThresholder(self.rules, 'or') assert thresholder.all_feature_names == ['x1', 'x2']