1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
import itertools
import warnings
from typing import Type
import numpy as np
import pytest
import scipy.sparse
import xgboost as xgb
from xgboost import testing as tm
@pytest.mark.filterwarnings("error")
@pytest.mark.parametrize(
"DMatrixT,CSR",
[
(m, n)
for m, n in itertools.product(
(xgb.DMatrix, xgb.QuantileDMatrix),
(scipy.sparse.csr_matrix, scipy.sparse.csr_array),
)
],
)
def test_csr(DMatrixT: Type[xgb.DMatrix], CSR: Type) -> None:
with warnings.catch_warnings():
indptr = np.array([0, 2, 3, 6])
indices = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
X = CSR((data, indices, indptr), shape=(3, 3))
dtrain = DMatrixT(X)
assert dtrain.num_row() == 3
assert dtrain.num_col() == 3
assert dtrain.num_nonmissing() == data.size
@pytest.mark.filterwarnings("error")
@pytest.mark.parametrize(
"DMatrixT,CSC",
[
(m, n)
for m, n in itertools.product(
(xgb.DMatrix, xgb.QuantileDMatrix),
(scipy.sparse.csc_matrix, scipy.sparse.csc_array),
)
],
)
def test_csc(DMatrixT: Type[xgb.DMatrix], CSC: Type) -> None:
with warnings.catch_warnings():
row = np.array([0, 2, 2, 0, 1, 2])
col = np.array([0, 0, 1, 2, 2, 2])
data = np.array([1, 2, 3, 4, 5, 6])
X = CSC((data, (row, col)), shape=(3, 3))
dtrain = DMatrixT(X)
assert dtrain.num_row() == 3
assert dtrain.num_col() == 3
assert dtrain.num_nonmissing() == data.size
indptr = np.array([0, 3, 5])
data = np.array([0, 1, 2, 3, 4])
row_idx = np.array([0, 1, 2, 0, 2])
X = CSC((data, row_idx, indptr), shape=(3, 2))
assert tm.predictor_equal(DMatrixT(X.tocsr()), DMatrixT(X))
@pytest.mark.filterwarnings("error")
@pytest.mark.parametrize(
"DMatrixT,COO",
[
(m, n)
for m, n in itertools.product(
(xgb.DMatrix, xgb.QuantileDMatrix),
(scipy.sparse.coo_matrix, scipy.sparse.coo_array),
)
],
)
def test_coo(DMatrixT: Type[xgb.DMatrix], COO: Type) -> None:
with warnings.catch_warnings():
row = np.array([0, 2, 2, 0, 1, 2])
col = np.array([0, 0, 1, 2, 2, 2])
data = np.array([1, 2, 3, 4, 5, 6])
X = COO((data, (row, col)), shape=(3, 3))
dtrain = DMatrixT(X)
assert dtrain.num_row() == 3
assert dtrain.num_col() == 3
assert dtrain.num_nonmissing() == data.size
assert tm.predictor_equal(DMatrixT(X.tocsr()), DMatrixT(X))
|