MCPcopy Create free account
hub / github.com/dmlc/xgboost / test_predict

Method test_predict

tests/python/test_predict.py:102–156  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

100 cls.booster = xgb.train({"tree_method": "hist"}, dtrain, num_boost_round=10)
101
102 def test_predict(self):
103 booster = self.booster
104 X = self.X
105 test = self.test
106
107 predt_from_array = booster.inplace_predict(X[:10, ...], missing=self.missing)
108 predt_from_dmatrix = booster.predict(test)
109
110 X_obj = X.copy().astype(object)
111
112 assert X_obj.dtype.hasobject is True
113 assert X.dtype.hasobject is False
114 np.testing.assert_allclose(
115 booster.inplace_predict(X_obj), booster.inplace_predict(X)
116 )
117
118 np.testing.assert_allclose(predt_from_dmatrix, predt_from_array)
119
120 predt_from_array = booster.inplace_predict(
121 X[:10, ...], iteration_range=(0, 4), missing=self.missing
122 )
123 predt_from_dmatrix = booster.predict(test, iteration_range=(0, 4))
124
125 np.testing.assert_allclose(predt_from_dmatrix, predt_from_array)
126
127 with pytest.raises(ValueError):
128 booster.predict(test, iteration_range=(0, booster.num_boosted_rounds() + 2))
129
130 default = booster.predict(test)
131
132 range_full = booster.predict(test, iteration_range=(0, self.num_boost_round))
133 np.testing.assert_allclose(range_full, default)
134
135 range_full = booster.predict(
136 test, iteration_range=(0, booster.num_boosted_rounds())
137 )
138 np.testing.assert_allclose(range_full, default)
139
140 def predict_dense(x):
141 inplace_predt = booster.inplace_predict(x)
142 d = xgb.DMatrix(x)
143 copied_predt = booster.predict(d)
144 return np.all(copied_predt == inplace_predt)
145
146 for i in range(10):
147 run_threaded_predict(X, self.rows, predict_dense)
148
149 def predict_csr(x):
150 inplace_predt = booster.inplace_predict(sparse.csr_matrix(x))
151 d = xgb.DMatrix(x)
152 copied_predt = booster.predict(d)
153 return np.all(copied_predt == inplace_predt)
154
155 for i in range(10):
156 run_threaded_predict(X, self.rows, predict_csr)
157
158 @pytest.mark.skipif(**tm.no_pandas())
159 def test_predict_pd(self):

Callers

nothing calls this directly

Calls 5

run_threaded_predictFunction · 0.85
copyMethod · 0.80
inplace_predictMethod · 0.45
predictMethod · 0.45
num_boosted_roundsMethod · 0.45

Tested by

no test coverage detected