|
14 | 14 | from sklearn.ensemble import HistGradientBoostingRegressor
|
15 | 15 | from sklearn.ensemble import HistGradientBoostingClassifier
|
16 | 16 | from sklearn.utils._openmp_helpers import _openmp_effective_n_threads
|
| 17 | +from sklearn.utils._testing import _convert_container |
17 | 18 |
|
18 | 19 | n_threads = _openmp_effective_n_threads()
|
19 | 20 |
|
@@ -212,9 +213,9 @@ def test_predictions(global_random_seed, use_feature_names):
|
212 | 213 | f_0 = rng.rand(n_samples) # positive correlation with y
|
213 | 214 | f_1 = rng.rand(n_samples) # negative correslation with y
|
214 | 215 | X = np.c_[f_0, f_1]
|
215 |
| - if use_feature_names: |
216 |
| - pd = pytest.importorskip("pandas") |
217 |
| - X = pd.DataFrame(X, columns=["f_0", "f_1"]) |
| 216 | + columns_name = ["f_0", "f_1"] |
| 217 | + constructor_name = "dataframe" if use_feature_names else "array" |
| 218 | + X = _convert_container(X, constructor_name, columns_name=columns_name) |
218 | 219 |
|
219 | 220 | noise = rng.normal(loc=0.0, scale=0.01, size=n_samples)
|
220 | 221 | y = 5 * f_0 + np.sin(10 * np.pi * f_0) - 5 * f_1 - np.cos(10 * np.pi * f_1) + noise
|
@@ -244,20 +245,24 @@ def test_predictions(global_random_seed, use_feature_names):
|
244 | 245 | # First feature (POS)
|
245 | 246 | # assert pred is all increasing when f_0 is all increasing
|
246 | 247 | X = np.c_[linspace, constant]
|
| 248 | + X = _convert_container(X, constructor_name, columns_name=columns_name) |
247 | 249 | pred = gbdt.predict(X)
|
248 | 250 | assert is_increasing(pred)
|
249 | 251 | # assert pred actually follows the variations of f_0
|
250 | 252 | X = np.c_[sin, constant]
|
| 253 | + X = _convert_container(X, constructor_name, columns_name=columns_name) |
251
8000
| 254 | pred = gbdt.predict(X)
|
252 | 255 | assert np.all((np.diff(pred) >= 0) == (np.diff(sin) >= 0))
|
253 | 256 |
|
254 | 257 | # Second feature (NEG)
|
255 | 258 | # assert pred is all decreasing when f_1 is all increasing
|
256 | 259 | X = np.c_[constant, linspace]
|
| 260 | + X = _convert_container(X, constructor_name, columns_name=columns_name) |
257 | 261 | pred = gbdt.predict(X)
|
258 | 262 | assert is_decreasing(pred)
|
259 | 263 | # assert pred actually follows the inverse variations of f_1
|
260 | 264 | X = np.c_[constant, sin]
|
| 265 | + X = _convert_container(X, constructor_name, columns_name=columns_name) |
261 | 266 | pred = gbdt.predict(X)
|
262 | 267 | assert ((np.diff(pred) <= 0) == (np.diff(sin) >= 0)).all()
|
263 | 268 |
|
|
0 commit comments