Fix plotting test. (#6040)

Previously the test loads a model generated by `test_basic.py`, now we generate
the model explicitly.

* Cleanup saved files for basic tests.
This commit is contained in:
Jiaming Yuan 2020-08-22 13:18:48 +08:00 committed by GitHub
parent 7a46515d3d
commit b9ebbffc57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 31 deletions

View File

@ -110,16 +110,19 @@ class TestBasic(unittest.TestCase):
# error must be smaller than 10% # error must be smaller than 10%
assert err < 0.1 assert err < 0.1
# save dmatrix into binary buffer with tempfile.TemporaryDirectory() as tmpdir:
dtest.save_binary('dtest.buffer') dtest_path = os.path.join(tmpdir, 'dtest.buffer')
# save model model_path = os.path.join(tmpdir, 'xgb.model')
bst.save_model('xgb.model') # save dmatrix into binary buffer
# load model and data in dtest.save_binary(dtest_path)
bst2 = xgb.Booster(model_file='xgb.model') # save model
dtest2 = xgb.DMatrix('dtest.buffer') bst.save_model(model_path)
preds2 = bst2.predict(dtest2) # load model and data in
# assert they are the same bst2 = xgb.Booster(model_file=model_path)
assert np.sum(np.abs(preds2 - preds)) == 0 dtest2 = xgb.DMatrix(dtest_path)
preds2 = bst2.predict(dtest2)
# assert they are the same
assert np.sum(np.abs(preds2 - preds)) == 0
def test_dump(self): def test_dump(self):
data = np.random.randn(100, 2) data = np.random.randn(100, 2)

View File

@ -6,6 +6,7 @@ import json
import testing as tm import testing as tm
import pytest import pytest
import locale import locale
import tempfile
dpath = 'demo/data/' dpath = 'demo/data/'
dtrain = xgb.DMatrix(dpath + 'agaricus.txt.train') dtrain = xgb.DMatrix(dpath + 'agaricus.txt.train')
@ -60,15 +61,20 @@ class TestModels(unittest.TestCase):
# error must be smaller than 10% # error must be smaller than 10%
assert err < 0.1 assert err < 0.1
# save dmatrix into binary buffer with tempfile.TemporaryDirectory() as tmpdir:
dtest.save_binary('dtest.buffer') dtest_path = os.path.join(tmpdir, 'dtest.dmatrix')
model_path = 'xgb.model.dart' model_path = os.path.join(tmpdir, 'xgboost.model.dart')
# save model # save dmatrix into binary buffer
bst.save_model(model_path) dtest.save_binary(dtest_path)
# load model and data in model_path = model_path
bst2 = xgb.Booster(params=param, model_file='xgb.model.dart') # save model
dtest2 = xgb.DMatrix('dtest.buffer') bst.save_model(model_path)
# load model and data in
bst2 = xgb.Booster(params=param, model_file=model_path)
dtest2 = xgb.DMatrix(dtest_path)
preds2 = bst2.predict(dtest2, ntree_limit=num_round) preds2 = bst2.predict(dtest2, ntree_limit=num_round)
# assert they are the same # assert they are the same
assert np.sum(np.abs(preds2 - preds)) == 0 assert np.sum(np.abs(preds2 - preds)) == 0
@ -103,7 +109,6 @@ class TestModels(unittest.TestCase):
for ii in range(len(preds_list)): for ii in range(len(preds_list)):
for jj in range(ii + 1, len(preds_list)): for jj in range(ii + 1, len(preds_list)):
assert np.sum(np.abs(preds_list[ii] - preds_list[jj])) > 0 assert np.sum(np.abs(preds_list[ii] - preds_list[jj])) > 0
os.remove(model_path)
def run_eta_decay(self, tree_method): def run_eta_decay(self, tree_method):
watchlist = [(dtest, 'eval'), (dtrain, 'train')] watchlist = [(dtest, 'eval'), (dtrain, 'train')]

View File

@ -14,27 +14,27 @@ try:
except ImportError: except ImportError:
pass pass
pytestmark = pytest.mark.skipif(**tm.no_multiple(tm.no_matplotlib(),
tm.no_graphviz()))
pytestmark = pytest.mark.skipif(**tm.no_multiple(tm.no_matplotlib(), tm.no_graphviz())) dpath = 'demo/data/agaricus.txt.train'
dpath = 'demo/data/'
rng = np.random.RandomState(1994)
class TestPlotting(unittest.TestCase): class TestPlotting(unittest.TestCase):
def test_plotting(self): def test_plotting(self):
bst2 = xgb.Booster(model_file='xgb.model') m = xgb.DMatrix(dpath)
booster = xgb.train({'max_depth': 2, 'eta': 1,
'objective': 'binary:logistic'}, m,
num_boost_round=2)
ax = xgb.plot_importance(bst2) ax = xgb.plot_importance(booster)
assert isinstance(ax, Axes) assert isinstance(ax, Axes)
assert ax.get_title() == 'Feature importance' assert ax.get_title() == 'Feature importance'
assert ax.get_xlabel() == 'F score' assert ax.get_xlabel() == 'F score'
assert ax.get_ylabel() == 'Features' assert ax.get_ylabel() == 'Features'
assert len(ax.patches) == 4 assert len(ax.patches) == 4
ax = xgb.plot_importance(bst2, color='r', ax = xgb.plot_importance(booster, color='r',
title='t', xlabel='x', ylabel='y') title='t', xlabel='x', ylabel='y')
assert isinstance(ax, Axes) assert isinstance(ax, Axes)
assert ax.get_title() == 't' assert ax.get_title() == 't'
@ -44,7 +44,7 @@ class TestPlotting(unittest.TestCase):
for p in ax.patches: for p in ax.patches:
assert p.get_facecolor() == (1.0, 0, 0, 1.0) # red assert p.get_facecolor() == (1.0, 0, 0, 1.0) # red
ax = xgb.plot_importance(bst2, color=['r', 'r', 'b', 'b'], ax = xgb.plot_importance(booster, color=['r', 'r', 'b', 'b'],
title=None, xlabel=None, ylabel=None) title=None, xlabel=None, ylabel=None)
assert isinstance(ax, Axes) assert isinstance(ax, Axes)
assert ax.get_title() == '' assert ax.get_title() == ''
@ -56,10 +56,10 @@ class TestPlotting(unittest.TestCase):
assert ax.patches[2].get_facecolor() == (0, 0, 1.0, 1.0) # blue assert ax.patches[2].get_facecolor() == (0, 0, 1.0, 1.0) # blue
assert ax.patches[3].get_facecolor() == (0, 0, 1.0, 1.0) # blue assert ax.patches[3].get_facecolor() == (0, 0, 1.0, 1.0) # blue
g = xgb.to_graphviz(bst2, num_trees=0) g = xgb.to_graphviz(booster, num_trees=0)
assert isinstance(g, Source) assert isinstance(g, Source)
ax = xgb.plot_tree(bst2, num_trees=0) ax = xgb.plot_tree(booster, num_trees=0)
assert isinstance(ax, Axes) assert isinstance(ax, Axes)
def test_importance_plot_lim(self): def test_importance_plot_lim(self):