Skip to content

Commit

Permalink
[tests] fixed and refactored some tests (#2035)
Browse files Browse the repository at this point in the history
* fixed number of tests in pytest

* fixed data shape and removed unused code

* refactored tests

* hotfix

* hotfix
  • Loading branch information
StrikerRUS authored and guolinke committed Mar 7, 2019
1 parent 219c943 commit 8aa08c4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 67 deletions.
60 changes: 22 additions & 38 deletions tests/python_package_test/test_basic.py
Expand Up @@ -103,9 +103,7 @@ def test_add_features_throws_if_num_data_unequal(self):

def test_add_features_throws_if_datasets_unconstructed(self):
X1 = np.random.random((1000, 1))
X2 = np.random.random((100, 1))
d1 = lgb.Dataset(X1)
d2 = lgb.Dataset(X2)
X2 = np.random.random((1000, 1))
with self.assertRaises(ValueError):
d1 = lgb.Dataset(X1)
d2 = lgb.Dataset(X2)
Expand All @@ -122,7 +120,7 @@ def test_add_features_throws_if_datasets_unconstructed(self):
def test_add_features_equal_data_on_alternating_used_unused(self):
X = np.random.random((1000, 5))
X[:, [1, 3]] = 0
names = ['col_%d' % (i,) for i in range(5)]
names = ['col_%d' % i for i in range(5)]
for j in range(1, 5):
d1 = lgb.Dataset(X[:, :j], feature_name=names[:j]).construct()
d2 = lgb.Dataset(X[:, j:], feature_name=names[j:]).construct()
Expand All @@ -145,7 +143,7 @@ def test_add_features_equal_data_on_alternating_used_unused(self):
def test_add_features_same_booster_behaviour(self):
X = np.random.random((1000, 5))
X[:, [1, 3]] = 0
names = ['col_%d' % (i,) for i in range(5)]
names = ['col_%d' % i for i in range(5)]
for j in range(1, 5):
d1 = lgb.Dataset(X[:, :j], feature_name=names[:j]).construct()
d2 = lgb.Dataset(X[:, j:], feature_name=names[j:]).construct()
Expand All @@ -171,19 +169,15 @@ def test_add_features_same_booster_behaviour(self):
d1txt = d1f.read()
self.assertEqual(dtxt, d1txt)

def test_get_feature_penalty(self):
def test_get_feature_penalty_and_monotone_constraints(self):
X = np.random.random((1000, 1))
d = lgb.Dataset(X, params={'feature_penalty': [0.5]}).construct()
self.assertEqual(np.asarray([0.5]), d.get_feature_penalty())
d = lgb.Dataset(X, params={'feature_penalty': [0.5],
'monotone_constraints': [1]}).construct()
np.testing.assert_almost_equal(d.get_feature_penalty(), [0.5])
np.testing.assert_array_equal(d.get_monotone_constraints(), [1])
d = lgb.Dataset(X).construct()
self.assertEqual(None, d.get_feature_penalty())

def test_get_monotone_constraints(self):
X = np.random.random((1000, 1))
d = lgb.Dataset(X, params={'monotone_constraints': [1]}).construct()
self.assertEqual(np.asarray([1]), d.get_monotone_constraints())
d = lgb.Dataset(X).construct()
self.assertEqual(None, d.get_monotone_constraints())
self.assertIsNone(d.get_feature_penalty())
self.assertIsNone(d.get_monotone_constraints())

def test_add_features_feature_penalty(self):
X = np.random.random((1000, 2))
Expand All @@ -193,21 +187,16 @@ def test_add_features_feature_penalty(self):
(None, [0.5], [1, 0.5]),
([0.5], [0.5], [0.5, 0.5])]
for (p1, p2, expected) in test_cases:
if p1 is not None:
params1 = {'feature_penalty': p1}
else:
params1 = {}
params1 = {'feature_penalty': p1} if p1 is not None else {}
d1 = lgb.Dataset(X[:, 0].reshape((-1, 1)), params=params1).construct()
if p2 is not None:
params2 = {'feature_penalty': p2}
else:
params2 = {}
params2 = {'feature_penalty': p2} if p2 is not None else {}
d2 = lgb.Dataset(X[:, 1].reshape((-1, 1)), params=params2).construct()
d1.add_features_from(d2)
actual = d1.get_feature_penalty()
if isinstance(actual, np.ndarray):
actual = list(actual)
self.assertEqual(expected, actual)
if expected is None:
self.assertIsNone(actual)
else:
np.testing.assert_almost_equal(actual, expected)

def test_add_features_monotone_types(self):
X = np.random.random((1000, 2))
Expand All @@ -217,18 +206,13 @@ def test_add_features_monotone_types(self):
(None, [1], [0, 1]),
([1], [-1], [1, -1])]
for (p1, p2, expected) in test_cases:
if p1 is not None:
params1 = {'monotone_constraints': p1}
else:
params1 = {}
params1 = {'monotone_constraints': p1} if p1 is not None else {}
d1 = lgb.Dataset(X[:, 0].reshape((-1, 1)), params=params1).construct()
if p2 is not None:
params2 = {'monotone_constraints': p2}
else:
params2 = {}
params2 = {'monotone_constraints': p2} if p2 is not None else {}
d2 = lgb.Dataset(X[:, 1].reshape((-1, 1)), params=params2).construct()
d1.add_features_from(d2)
actual = d1.get_monotone_constraints()
if isinstance(actual, np.ndarray):
actual = list(actual)
self.assertEqual(expected, actual)
if actual is None:
self.assertIsNone(actual)
else:
np.testing.assert_array_equal(actual, expected)
56 changes: 27 additions & 29 deletions tests/python_package_test/test_engine.py
Expand Up @@ -766,57 +766,55 @@ def test_mape_dart(self):
pred_mean = pred.mean()
self.assertGreater(pred_mean, 18)

def test_constant_features(self, y_true=None, expected_pred=None, more_params=None):
if y_true is not None and expected_pred is not None:
X_train = np.ones((len(y_true), 1))
y_train = np.array(y_true)
params = {
'objective': 'regression',
'num_class': 1,
'verbose': -1,
'min_data': 1,
'num_leaves': 2,
'learning_rate': 1,
'min_data_in_bin': 1,
'boost_from_average': True
}
params.update(more_params)
lgb_train = lgb.Dataset(X_train, y_train, params=params)
gbm = lgb.train(params, lgb_train,
num_boost_round=2)
pred = gbm.predict(X_train)
self.assertTrue(np.allclose(pred, expected_pred))
def check_constant_features(self, y_true, expected_pred, more_params):
X_train = np.ones((len(y_true), 1))
y_train = np.array(y_true)
params = {
'objective': 'regression',
'num_class': 1,
'verbose': -1,
'min_data': 1,
'num_leaves': 2,
'learning_rate': 1,
'min_data_in_bin': 1,
'boost_from_average': True
}
params.update(more_params)
lgb_train = lgb.Dataset(X_train, y_train, params=params)
gbm = lgb.train(params, lgb_train, num_boost_round=2)
pred = gbm.predict(X_train)
self.assertTrue(np.allclose(pred, expected_pred))

def test_constant_features_regression(self):
params = {
'objective': 'regression'
}
self.test_constant_features([0.0, 10.0, 0.0, 10.0], 5.0, params)
self.test_constant_features([0.0, 1.0, 2.0, 3.0], 1.5, params)
self.test_constant_features([-1.0, 1.0, -2.0, 2.0], 0.0, params)
self.check_constant_features([0.0, 10.0, 0.0, 10.0], 5.0, params)
self.check_constant_features([0.0, 1.0, 2.0, 3.0], 1.5, params)
self.check_constant_features([-1.0, 1.0, -2.0, 2.0], 0.0, params)

def test_constant_features_binary(self):
params = {
'objective': 'binary'
}
self.test_constant_features([0.0, 10.0, 0.0, 10.0], 0.5, params)
self.test_constant_features([0.0, 1.0, 2.0, 3.0], 0.75, params)
self.check_constant_features([0.0, 10.0, 0.0, 10.0], 0.5, params)
self.check_constant_features([0.0, 1.0, 2.0, 3.0], 0.75, params)

def test_constant_features_multiclass(self):
params = {
'objective': 'multiclass',
'num_class': 3
}
self.test_constant_features([0.0, 1.0, 2.0, 0.0], [0.5, 0.25, 0.25], params)
self.test_constant_features([0.0, 1.0, 2.0, 1.0], [0.25, 0.5, 0.25], params)
self.check_constant_features([0.0, 1.0, 2.0, 0.0], [0.5, 0.25, 0.25], params)
self.check_constant_features([0.0, 1.0, 2.0, 1.0], [0.25, 0.5, 0.25], params)

def test_constant_features_multiclassova(self):
params = {
'objective': 'multiclassova',
'num_class': 3
}
self.test_constant_features([0.0, 1.0, 2.0, 0.0], [0.5, 0.25, 0.25], params)
self.test_constant_features([0.0, 1.0, 2.0, 1.0], [0.25, 0.5, 0.25], params)
self.check_constant_features([0.0, 1.0, 2.0, 0.0], [0.5, 0.25, 0.25], params)
self.check_constant_features([0.0, 1.0, 2.0, 1.0], [0.25, 0.5, 0.25], params)

def test_fpreproc(self):
def preprocess_data(dtrain, dtest, params):
Expand Down

0 comments on commit 8aa08c4

Please sign in to comment.