More robust DMatrix creation from a sparse matrix (#1606)
* [CORE] DMatrix from sparse w/ explicit #col #row; safer arg types * [python-package] c-api change for _init_from_csr _init_from_csc * fix spaces * [R-package] adopt the new XGDMatrixCreateFromCSCEx interface * [CORE] redirect old sparse creators to new ones
This commit is contained in:
committed by
Tianqi Chen
parent
e06f6a0df7
commit
693ddb860e
33
tests/python/test_sparse_dmatrix.py
Normal file
33
tests/python/test_sparse_dmatrix.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import numpy as np
|
||||
import xgboost as xgb
|
||||
from scipy.sparse import rand
|
||||
|
||||
rng = np.random.RandomState(1)
|
||||
|
||||
param = {'max_depth': 3, 'objective': 'binary:logistic', 'silent': 1}
|
||||
|
||||
|
||||
def test_sparse_dmatrix_csr():
|
||||
nrow = 100
|
||||
ncol = 1000
|
||||
x = rand(nrow, ncol, density=0.0005, format='csr', random_state=rng)
|
||||
assert x.indices.max() < ncol - 1
|
||||
x.data[:] = 1
|
||||
dtrain = xgb.DMatrix(x, label=np.random.binomial(1, 0.3, nrow))
|
||||
assert (dtrain.num_row(), dtrain.num_col()) == (nrow, ncol)
|
||||
watchlist = [(dtrain, 'train')]
|
||||
bst = xgb.train(param, dtrain, 5, watchlist)
|
||||
bst.predict(dtrain)
|
||||
|
||||
|
||||
def test_sparse_dmatrix_csc():
|
||||
nrow = 1000
|
||||
ncol = 100
|
||||
x = rand(nrow, ncol, density=0.0005, format='csc', random_state=rng)
|
||||
assert x.indices.max() < nrow - 1
|
||||
x.data[:] = 1
|
||||
dtrain = xgb.DMatrix(x, label=np.random.binomial(1, 0.3, nrow))
|
||||
assert (dtrain.num_row(), dtrain.num_col()) == (nrow, ncol)
|
||||
watchlist = [(dtrain, 'train')]
|
||||
bst = xgb.train(param, dtrain, 5, watchlist)
|
||||
bst.predict(dtrain)
|
||||
Reference in New Issue
Block a user