Don't set_params at the end of set_state. (#4947)
* Don't set_params at the end of set_state. * Also fix another issue found in dask prediction. * Add note about prediction. Don't support other prediction modes at the moment.
This commit is contained in:
48
tests/python/test_pickling.py
Normal file
48
tests/python/test_pickling.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import pickle
|
||||
import numpy as np
|
||||
import xgboost as xgb
|
||||
import os
|
||||
|
||||
|
||||
kRows = 100
|
||||
kCols = 10
|
||||
|
||||
|
||||
def generate_data():
|
||||
X = np.random.randn(kRows, kCols)
|
||||
y = np.random.randn(kRows)
|
||||
return X, y
|
||||
|
||||
|
||||
def test_model_pickling():
|
||||
xgb_params = {
|
||||
'verbosity': 0,
|
||||
'nthread': 1,
|
||||
'tree_method': 'hist'
|
||||
}
|
||||
|
||||
X, y = generate_data()
|
||||
dtrain = xgb.DMatrix(X, y)
|
||||
bst = xgb.train(xgb_params, dtrain)
|
||||
|
||||
dump_0 = bst.get_dump(dump_format='json')
|
||||
assert dump_0
|
||||
|
||||
filename = 'model.pkl'
|
||||
|
||||
with open(filename, 'wb') as fd:
|
||||
pickle.dump(bst, fd)
|
||||
|
||||
with open(filename, 'rb') as fd:
|
||||
bst = pickle.load(fd)
|
||||
|
||||
with open(filename, 'wb') as fd:
|
||||
pickle.dump(bst, fd)
|
||||
|
||||
with open(filename, 'rb') as fd:
|
||||
bst = pickle.load(fd)
|
||||
|
||||
assert bst.get_dump(dump_format='json') == dump_0
|
||||
|
||||
if os.path.exists(filename):
|
||||
os.remove(filename)
|
||||
@@ -43,14 +43,17 @@ def test_from_dask_dataframe(client):
|
||||
|
||||
prediction = xgb.dask.predict(client, model=booster, data=dtrain)
|
||||
|
||||
assert prediction.ndim == 1
|
||||
assert isinstance(prediction, da.Array)
|
||||
assert prediction.shape[0] == kRows and prediction.shape[1] == kCols
|
||||
assert prediction.shape[0] == kRows
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
# evals_result is not supported in dask interface.
|
||||
xgb.dask.train(
|
||||
client, {}, dtrain, num_boost_round=2, evals_result={})
|
||||
|
||||
prediction = prediction.compute() # force prediction to be computed
|
||||
|
||||
|
||||
def test_from_dask_array(client):
|
||||
X, y = generate_array()
|
||||
@@ -59,10 +62,12 @@ def test_from_dask_array(client):
|
||||
result = xgb.dask.train(client, {}, dtrain)
|
||||
|
||||
prediction = xgb.dask.predict(client, result, dtrain)
|
||||
assert prediction.shape[0] == kRows and prediction.shape[1] == kCols
|
||||
assert prediction.shape[0] == kRows
|
||||
|
||||
assert isinstance(prediction, da.Array)
|
||||
|
||||
prediction = prediction.compute() # force prediction to be computed
|
||||
|
||||
|
||||
def test_regressor(client):
|
||||
X, y = generate_array()
|
||||
@@ -72,7 +77,8 @@ def test_regressor(client):
|
||||
regressor.fit(X, y, eval_set=[(X, y)])
|
||||
prediction = regressor.predict(X)
|
||||
|
||||
assert prediction.shape[0] == kRows and prediction.shape[1] == kCols
|
||||
assert prediction.ndim == 1
|
||||
assert prediction.shape[0] == kRows
|
||||
|
||||
history = regressor.evals_result()
|
||||
|
||||
@@ -91,7 +97,8 @@ def test_classifier(client):
|
||||
classifier.fit(X, y, eval_set=[(X, y)])
|
||||
prediction = classifier.predict(X)
|
||||
|
||||
assert prediction.shape[0] == kRows and prediction.shape[1] == kCols
|
||||
assert prediction.ndim == 1
|
||||
assert prediction.shape[0] == kRows
|
||||
|
||||
history = classifier.evals_result()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user