(self)
| 83 | assert (from_view == from_array).all() |
| 84 | |
| 85 | def test_slice(self): |
| 86 | X = rng.randn(100, 100) |
| 87 | y = rng.randint(low=0, high=3, size=100).astype(np.float32) |
| 88 | d = xgb.DMatrix(X, y) |
| 89 | np.testing.assert_equal(d.get_label(), y) |
| 90 | |
| 91 | fw = rng.uniform(size=100).astype(np.float32) |
| 92 | d.set_info(feature_weights=fw) |
| 93 | |
| 94 | # base margin is per-class in multi-class classifier |
| 95 | base_margin = rng.randn(100, 3).astype(np.float32) |
| 96 | d.set_base_margin(base_margin) |
| 97 | np.testing.assert_allclose(d.get_base_margin().reshape(100, 3), base_margin) |
| 98 | |
| 99 | ridxs = [1, 2, 3, 4, 5, 6] |
| 100 | sliced = d.slice(ridxs) |
| 101 | |
| 102 | # Slicing works with label and other meta info fields |
| 103 | np.testing.assert_equal(sliced.get_label(), y[1:7]) |
| 104 | np.testing.assert_equal(sliced.get_float_info("feature_weights"), fw) |
| 105 | np.testing.assert_equal(sliced.get_base_margin(), base_margin[1:7, :]) |
| 106 | |
| 107 | # Slicing a DMatrix results into a DMatrix that's equivalent to a DMatrix that's |
| 108 | # constructed from the corresponding NumPy slice |
| 109 | d2 = xgb.DMatrix(X[1:7, :], y[1:7]) |
| 110 | d2.set_base_margin(base_margin[1:7, :]) |
| 111 | eval_res = {} |
| 112 | _ = xgb.train( |
| 113 | {"num_class": 3, "objective": "multi:softprob", "eval_metric": "mlogloss"}, |
| 114 | d, |
| 115 | num_boost_round=2, |
| 116 | evals=[(d2, "d2"), (sliced, "sliced")], |
| 117 | evals_result=eval_res, |
| 118 | ) |
| 119 | np.testing.assert_equal( |
| 120 | eval_res["d2"]["mlogloss"], eval_res["sliced"]["mlogloss"] |
| 121 | ) |
| 122 | |
| 123 | ridxs_arr = np.array(ridxs)[1:] # handles numpy slice correctly |
| 124 | sliced = d.slice(ridxs_arr) |
| 125 | np.testing.assert_equal(sliced.get_label(), y[2:7]) |
| 126 | |
| 127 | def test_feature_names_slice(self): |
| 128 | data = np.random.randn(5, 5) |
nothing calls this directly
no test coverage detected