Skip to content

Commit

Permalink
[tests] use numpy.testing.assert_allclose (#2207)
Browse files Browse the repository at this point in the history
* Update test.py

* Update test_consistency.py

* Update test_basic.py

* Update test_sklearn.py

* Update test_sklearn.py

* Update test_engine.py

* more replacements
  • Loading branch information
StrikerRUS authored and chivee committed Jun 20, 2019
1 parent 5b24834 commit 86269ee
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 50 deletions.
2 changes: 1 addition & 1 deletion tests/cpp_test/test.py
Expand Up @@ -3,4 +3,4 @@
import numpy as np

preds = [np.loadtxt(name) for name in glob.glob('*.pred')]
np.testing.assert_array_almost_equal(preds[0], preds[1], decimal=5)
np.testing.assert_allclose(preds[0], preds[1])
4 changes: 2 additions & 2 deletions tests/python_package_test/test_basic.py
Expand Up @@ -173,7 +173,7 @@ def test_get_feature_penalty_and_monotone_constraints(self):
X = np.random.random((1000, 1))
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_allclose(d.get_feature_penalty(), [0.5])
np.testing.assert_array_equal(d.get_monotone_constraints(), [1])
d = lgb.Dataset(X).construct()
self.assertIsNone(d.get_feature_penalty())
Expand All @@ -196,7 +196,7 @@ def test_add_features_feature_penalty(self):
if expected is None:
self.assertIsNone(actual)
else:
np.testing.assert_almost_equal(actual, expected)
np.testing.assert_allclose(actual, expected)

def test_add_features_monotone_types(self):
X = np.random.random((1000, 2))
Expand Down
6 changes: 3 additions & 3 deletions tests/python_package_test/test_consistency.py
Expand Up @@ -42,8 +42,8 @@ def train_predict_check(self, lgb_train, X_test, X_test_fn, sk_pred):
gbm = lgb.train(self.params, lgb_train)
y_pred = gbm.predict(X_test)
cpp_pred = gbm.predict(X_test_fn)
np.testing.assert_array_almost_equal(y_pred, cpp_pred, decimal=5)
np.testing.assert_array_almost_equal(y_pred, sk_pred, decimal=5)
np.testing.assert_allclose(y_pred, cpp_pred)
np.testing.assert_allclose(y_pred, sk_pred)

def file_load_check(self, lgb_train, name):
lgb_train_f = lgb.Dataset(self.path(name), params=self.params).construct()
Expand All @@ -55,7 +55,7 @@ def file_load_check(self, lgb_train, name):
elif a is None:
assert np.all(b == 1), f
elif isinstance(b, (list, np.ndarray)):
np.testing.assert_array_almost_equal(a, b)
np.testing.assert_allclose(a, b)
else:
assert a == b, f

Expand Down
64 changes: 32 additions & 32 deletions tests/python_package_test/test_engine.py
Expand Up @@ -152,7 +152,7 @@ def test_missing_value_handle_na(self):
verbose_eval=True,
evals_result=evals_result)
pred = gbm.predict(X_train)
np.testing.assert_almost_equal(pred, y)
np.testing.assert_allclose(pred, y)
ret = roc_auc_score(y_train, pred)
self.assertGreater(ret, 0.999)
self.assertAlmostEqual(evals_result['valid_0']['auc'][-1], ret, places=5)
Expand Down Expand Up @@ -184,7 +184,7 @@ def test_missing_value_handle_zero(self):
verbose_eval=True,
evals_result=evals_result)
pred = gbm.predict(X_train)
np.testing.assert_almost_equal(pred, y)
np.testing.assert_allclose(pred, y)
ret = roc_auc_score(y_train, pred)
self.assertGreater(ret, 0.999)
self.assertAlmostEqual(evals_result['valid_0']['auc'][-1], ret, places=5)
Expand Down Expand Up @@ -254,7 +254,7 @@ def test_categorical_handle(self):
verbose_eval=True,
evals_result=evals_result)
pred = gbm.predict(X_train)
np.testing.assert_almost_equal(pred, y)
np.testing.assert_allclose(pred, y)
ret = roc_auc_score(y_train, pred)
self.assertGreater(ret, 0.999)
self.assertAlmostEqual(evals_result['valid_0']['auc'][-1], ret, places=5)
Expand Down Expand Up @@ -291,7 +291,7 @@ def test_categorical_handle_na(self):
verbose_eval=True,
evals_result=evals_result)
pred = gbm.predict(X_train)
np.testing.assert_almost_equal(pred, y)
np.testing.assert_allclose(pred, y)
ret = roc_auc_score(y_train, pred)
self.assertGreater(ret, 0.999)
self.assertAlmostEqual(evals_result['valid_0']['auc'][-1], ret, places=5)
Expand Down Expand Up @@ -384,26 +384,26 @@ def test_multi_class_error(self):
est = lgb.train(params, lgb_data, valid_sets=[lgb_data], valid_names=['train'], evals_result=results)
predict_1 = est.predict(X)
# check that default gives same result as k = 1
np.testing.assert_array_almost_equal(predict_1, predict_default, 5)
np.testing.assert_allclose(predict_1, predict_default)
# check against independent calculation for k = 1
err = top_k_error(y, predict_1, 1)
np.testing.assert_almost_equal(results['train']['multi_error'][-1], err, 5)
np.testing.assert_allclose(results['train']['multi_error'][-1], err)
# check against independent calculation for k = 2
params = {'objective': 'multiclass', 'num_classes': 10, 'metric': 'multi_error', 'multi_error_top_k': 2,
'num_leaves': 4, 'seed': 0, 'num_rounds': 30, 'verbose': -1, 'metric_freq': 10}
results = {}
est = lgb.train(params, lgb_data, valid_sets=[lgb_data], valid_names=['train'], evals_result=results)
predict_2 = est.predict(X)
err = top_k_error(y, predict_2, 2)
np.testing.assert_almost_equal(results['train']['multi_error@2'][-1], err, 5)
np.testing.assert_allclose(results['train']['multi_error@2'][-1], err)
# check against independent calculation for k = 10
params = {'objective': 'multiclass', 'num_classes': 10, 'metric': 'multi_error', 'multi_error_top_k': 10,
'num_leaves': 4, 'seed': 0, 'num_rounds': 30, 'verbose': -1, 'metric_freq': 10}
results = {}
est = lgb.train(params, lgb_data, valid_sets=[lgb_data], valid_names=['train'], evals_result=results)
predict_2 = est.predict(X)
err = top_k_error(y, predict_2, 10)
np.testing.assert_almost_equal(results['train']['multi_error@10'][-1], err, 5)
np.testing.assert_allclose(results['train']['multi_error@10'][-1], err)
# check case where predictions are equal
X = np.array([[0, 0], [0, 0]])
y = np.array([0, 1])
Expand All @@ -412,13 +412,13 @@ def test_multi_class_error(self):
'num_leaves': 4, 'seed': 0, 'num_rounds': 1, 'verbose': -1, 'metric_freq': 10}
results = {}
lgb.train(params, lgb_data, valid_sets=[lgb_data], valid_names=['train'], evals_result=results)
np.testing.assert_almost_equal(results['train']['multi_error'][-1], 1, 5)
np.testing.assert_allclose(results['train']['multi_error'][-1], 1)
lgb_data = lgb.Dataset(X, label=y)
params = {'objective': 'multiclass', 'num_classes': 2, 'metric': 'multi_error', 'multi_error_top_k': 2,
'num_leaves': 4, 'seed': 0, 'num_rounds': 1, 'verbose': -1, 'metric_freq': 10}
results = {}
lgb.train(params, lgb_data, valid_sets=[lgb_data], valid_names=['train'], evals_result=results)
np.testing.assert_almost_equal(results['train']['multi_error@2'][-1], 0, 5)
np.testing.assert_allclose(results['train']['multi_error@2'][-1], 0)

def test_early_stopping(self):
X, y = load_breast_cancer(True)
Expand Down Expand Up @@ -539,7 +539,7 @@ def test_cv(self):
verbose_eval=False)
cv_res_obj = lgb.cv(params_with_metric, lgb_train, num_boost_round=10, folds=tss,
verbose_eval=False)
np.testing.assert_almost_equal(cv_res_gen['l2-mean'], cv_res_obj['l2-mean'])
np.testing.assert_allclose(cv_res_gen['l2-mean'], cv_res_obj['l2-mean'])
# lambdarank
X_train, y_train = load_svmlight_file(os.path.join(os.path.dirname(os.path.realpath(__file__)),
'../../examples/lambdarank/rank.train'))
Expand All @@ -561,7 +561,7 @@ def test_cv(self):
cv_res_lambda_obj = lgb.cv(params_lambdarank, lgb_train, num_boost_round=10,
folds=GroupKFold(n_splits=3),
verbose_eval=False)
np.testing.assert_almost_equal(cv_res_lambda['ndcg@3-mean'], cv_res_lambda_obj['ndcg@3-mean'])
np.testing.assert_allclose(cv_res_lambda['ndcg@3-mean'], cv_res_lambda_obj['ndcg@3-mean'])

def test_feature_name(self):
X, y = load_boston(True)
Expand Down Expand Up @@ -669,21 +669,21 @@ def test_pandas_categorical(self):
pred8 = gbm7.predict(X_test)
self.assertListEqual(lgb_train.categorical_feature, [])
self.assertRaises(AssertionError,
np.testing.assert_almost_equal,
np.testing.assert_allclose,
pred0, pred1)
self.assertRaises(AssertionError,
np.testing.assert_almost_equal,
np.testing.assert_allclose,
pred0, pred2)
np.testing.assert_almost_equal(pred1, pred2)
np.testing.assert_almost_equal(pred0, pred3)
np.testing.assert_almost_equal(pred0, pred4)
np.testing.assert_almost_equal(pred0, pred5)
np.testing.assert_almost_equal(pred0, pred6)
np.testing.assert_allclose(pred1, pred2)
np.testing.assert_allclose(pred0, pred3)
np.testing.assert_allclose(pred0, pred4)
np.testing.assert_allclose(pred0, pred5)
np.testing.assert_allclose(pred0, pred6)
self.assertRaises(AssertionError,
np.testing.assert_almost_equal,
np.testing.assert_allclose,
pred0, pred7) # ordered cat features aren't treated as cat features by default
self.assertRaises(AssertionError,
np.testing.assert_almost_equal,
np.testing.assert_allclose,
pred0, pred8)
self.assertListEqual(gbm0.pandas_categorical, cat_values)
self.assertListEqual(gbm1.pandas_categorical, cat_values)
Expand Down Expand Up @@ -750,7 +750,7 @@ def train_and_get_predictions(features, labels):
stacked_labels = np.column_stack((labels, np.ones(num_samples, dtype=np.float32)))
sliced_labels = stacked_labels[:, 0]
sliced_pred = train_and_get_predictions(features, sliced_labels)
np.testing.assert_almost_equal(origin_pred, sliced_pred)
np.testing.assert_allclose(origin_pred, sliced_pred)
# append some columns
stacked_features = np.column_stack((np.ones(num_samples, dtype=np.float32), features))
stacked_features = np.column_stack((np.ones(num_samples, dtype=np.float32), stacked_features))
Expand All @@ -765,13 +765,13 @@ def train_and_get_predictions(features, labels):
sliced_features = stacked_features[2:102, 2:7]
self.assertTrue(np.all(sliced_features == features))
sliced_pred = train_and_get_predictions(sliced_features, sliced_labels)
np.testing.assert_almost_equal(origin_pred, sliced_pred)
np.testing.assert_allclose(origin_pred, sliced_pred)
# test sliced CSR
stacked_csr = csr_matrix(stacked_features)
sliced_csr = stacked_csr[2:102, 2:7]
self.assertTrue(np.all(sliced_csr == features))
sliced_pred = train_and_get_predictions(sliced_csr, sliced_labels)
np.testing.assert_almost_equal(origin_pred, sliced_pred)
np.testing.assert_allclose(origin_pred, sliced_pred)

def test_monotone_constraint(self):
def is_increasing(y):
Expand Down Expand Up @@ -1355,20 +1355,20 @@ def test_get_split_value_histogram(self):
self.assertTupleEqual(gbm.get_split_value_histogram(bins=6, **params).shape, (5, 2))
self.assertTupleEqual(gbm.get_split_value_histogram(bins=7, **params).shape, (6, 2))
if lgb.compat.PANDAS_INSTALLED:
np.testing.assert_almost_equal(
np.testing.assert_allclose(
gbm.get_split_value_histogram(0, xgboost_style=True).values,
gbm.get_split_value_histogram(gbm.feature_name()[0], xgboost_style=True).values
)
np.testing.assert_almost_equal(
np.testing.assert_allclose(
gbm.get_split_value_histogram(X.shape[-1] - 1, xgboost_style=True).values,
gbm.get_split_value_histogram(gbm.feature_name()[X.shape[-1] - 1], xgboost_style=True).values
)
else:
np.testing.assert_almost_equal(
np.testing.assert_allclose(
gbm.get_split_value_histogram(0, xgboost_style=True),
gbm.get_split_value_histogram(gbm.feature_name()[0], xgboost_style=True)
)
np.testing.assert_almost_equal(
np.testing.assert_allclose(
gbm.get_split_value_histogram(X.shape[-1] - 1, xgboost_style=True),
gbm.get_split_value_histogram(gbm.feature_name()[X.shape[-1] - 1], xgboost_style=True)
)
Expand Down Expand Up @@ -1396,23 +1396,23 @@ def test_get_split_value_histogram(self):
hist_idx, bins_idx = gbm.get_split_value_histogram(0)
hist_name, bins_name = gbm.get_split_value_histogram(gbm.feature_name()[0])
np.testing.assert_array_equal(hist_idx, hist_name)
np.testing.assert_almost_equal(bins_idx, bins_name)
np.testing.assert_allclose(bins_idx, bins_name)
hist_idx, bins_idx = gbm.get_split_value_histogram(X.shape[-1] - 1)
hist_name, bins_name = gbm.get_split_value_histogram(gbm.feature_name()[X.shape[-1] - 1])
np.testing.assert_array_equal(hist_idx, hist_name)
np.testing.assert_almost_equal(bins_idx, bins_name)
np.testing.assert_allclose(bins_idx, bins_name)
# test bins string type
if np.__version__ > '1.11.0':
hist_vals, bin_edges = gbm.get_split_value_histogram(0, bins='auto')
hist = gbm.get_split_value_histogram(0, bins='auto', xgboost_style=True)
if lgb.compat.PANDAS_INSTALLED:
mask = hist_vals > 0
np.testing.assert_array_equal(hist_vals[mask], hist['Count'].values)
np.testing.assert_almost_equal(bin_edges[1:][mask], hist['SplitValue'].values)
np.testing.assert_allclose(bin_edges[1:][mask], hist['SplitValue'].values)
else:
mask = hist_vals > 0
np.testing.assert_array_equal(hist_vals[mask], hist[:, 1])
np.testing.assert_almost_equal(bin_edges[1:][mask], hist[:, 0])
np.testing.assert_allclose(bin_edges[1:][mask], hist[:, 0])
# test histogram is disabled for categorical features
self.assertRaises(lgb.basic.LightGBMError, gbm.get_split_value_histogram, 2)

Expand Down
24 changes: 12 additions & 12 deletions tests/python_package_test/test_sklearn.py
Expand Up @@ -171,11 +171,11 @@ def test_joblib(self):

for eval_set in gbm.evals_result_:
for metric in gbm.evals_result_[eval_set]:
np.testing.assert_array_almost_equal(gbm.evals_result_[eval_set][metric],
gbm_pickle.evals_result_[eval_set][metric])
np.testing.assert_allclose(gbm.evals_result_[eval_set][metric],
gbm_pickle.evals_result_[eval_set][metric])
pred_origin = gbm.predict(X_test)
pred_pickle = gbm_pickle.predict(X_test)
np.testing.assert_array_almost_equal(pred_origin, pred_pickle)
np.testing.assert_allclose(pred_origin, pred_pickle)

def test_feature_importances_single_leaf(self):
clf = lgb.LGBMClassifier(n_estimators=100)
Expand Down Expand Up @@ -255,19 +255,19 @@ def test_pandas_categorical(self):
gbm6 = lgb.sklearn.LGBMClassifier().fit(X, y, categorical_feature=[])
pred6 = gbm6.predict(X_test, raw_score=True)
self.assertRaises(AssertionError,
np.testing.assert_almost_equal,
np.testing.assert_allclose,
pred0, pred1)
self.assertRaises(AssertionError,
np.testing.assert_almost_equal,
np.testing.assert_allclose,
pred0, pred2)
np.testing.assert_almost_equal(pred1, pred2)
np.testing.assert_almost_equal(pred0, pred3)
np.testing.assert_almost_equal(pred_prob, pred4)
np.testing.assert_allclose(pred1, pred2)
np.testing.assert_allclose(pred0, pred3)
np.testing.assert_allclose(pred_prob, pred4)
self.assertRaises(AssertionError,
np.testing.assert_almost_equal,
np.testing.assert_allclose,
pred0, pred5) # ordered cat features aren't treated as cat features by default
self.assertRaises(AssertionError,
np.testing.assert_almost_equal,
np.testing.assert_allclose,
pred0, pred6)
self.assertListEqual(gbm0.booster_.pandas_categorical, cat_values)
self.assertListEqual(gbm1.booster_.pandas_categorical, cat_values)
Expand Down Expand Up @@ -603,7 +603,7 @@ def test_inf_handle(self):
params_fit = {'X': X, 'y': y, 'sample_weight': weight, 'eval_set': (X, y),
'verbose': False, 'early_stopping_rounds': 5}
gbm = lgb.LGBMRegressor(**params).fit(**params_fit)
np.testing.assert_array_equal(gbm.evals_result_['training']['l2'], np.inf)
np.testing.assert_allclose(gbm.evals_result_['training']['l2'], np.inf)

def test_nan_handle(self):
nrows = 1000
Expand All @@ -615,7 +615,7 @@ def test_nan_handle(self):
params_fit = {'X': X, 'y': y, 'sample_weight': weight, 'eval_set': (X, y),
'verbose': False, 'early_stopping_rounds': 5}
gbm = lgb.LGBMRegressor(**params).fit(**params_fit)
np.testing.assert_array_equal(gbm.evals_result_['training']['l2'], np.nan)
np.testing.assert_allclose(gbm.evals_result_['training']['l2'], np.nan)

def test_class_weight(self):
X, y = load_digits(10, True)
Expand Down

0 comments on commit 86269ee

Please sign in to comment.