Fix with None input. (#10052)

This commit is contained in:
Jiaming Yuan 2024-02-20 22:34:22 +08:00 committed by GitHub
parent d37b83e8d9
commit 69a17d5114
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 12 deletions

View File

@ -861,9 +861,9 @@ class DMatrix: # pylint: disable=too-many-instance-attributes,too-many-public-m
self.nthread = nthread if nthread is not None else -1 self.nthread = nthread if nthread is not None else -1
self.silent = silent self.silent = silent
# force into void_p, mac need to pass things in as void_p if isinstance(data, ctypes.c_void_p):
if data is None: # Used for constructing DMatrix slice.
self.handle: Optional[ctypes.c_void_p] = None self.handle = data
return return
from .data import _is_iter, dispatch_data_backend from .data import _is_iter, dispatch_data_backend
@ -925,9 +925,10 @@ class DMatrix: # pylint: disable=too-many-instance-attributes,too-many-public-m
self.handle = handle self.handle = handle
def __del__(self) -> None: def __del__(self) -> None:
if hasattr(self, "handle") and self.handle: if hasattr(self, "handle"):
assert self.handle is not None
_check_call(_LIB.XGDMatrixFree(self.handle)) _check_call(_LIB.XGDMatrixFree(self.handle))
self.handle = None del self.handle
@_deprecate_positional_args @_deprecate_positional_args
def set_info( def set_info(
@ -1281,19 +1282,19 @@ class DMatrix: # pylint: disable=too-many-instance-attributes,too-many-public-m
""" """
from .data import _maybe_np_slice from .data import _maybe_np_slice
res = DMatrix(None) handle = ctypes.c_void_p()
res.handle = ctypes.c_void_p()
rindex = _maybe_np_slice(rindex, dtype=np.int32) rindex = _maybe_np_slice(rindex, dtype=np.int32)
_check_call( _check_call(
_LIB.XGDMatrixSliceDMatrixEx( _LIB.XGDMatrixSliceDMatrixEx(
self.handle, self.handle,
c_array(ctypes.c_int, rindex), c_array(ctypes.c_int, rindex),
c_bst_ulong(len(rindex)), c_bst_ulong(len(rindex)),
ctypes.byref(res.handle), ctypes.byref(handle),
ctypes.c_int(1 if allow_groups else 0), ctypes.c_int(1 if allow_groups else 0),
) )
) )
return res return DMatrix(handle)
@property @property
def feature_names(self) -> Optional[FeatureNames]: def feature_names(self) -> Optional[FeatureNames]:

View File

@ -1053,10 +1053,10 @@ def _is_dlpack(data: DataType) -> bool:
def _transform_dlpack(data: DataType) -> bool: def _transform_dlpack(data: DataType) -> bool:
from cupy import fromDlpack # pylint: disable=E0401 from cupy import from_dlpack # pylint: disable=E0401
assert "used_dltensor" not in str(data) assert "used_dltensor" not in str(data)
data = fromDlpack(data) data = from_dlpack(data)
return data return data

View File

@ -202,7 +202,10 @@ class TestFromCupy:
n = 100 n = 100
X = cp.random.random((n, 2)) X = cp.random.random((n, 2))
m = xgb.QuantileDMatrix(X.toDlpack()) m = xgb.QuantileDMatrix(X.toDlpack())
with pytest.raises(xgb.core.XGBoostError):
with pytest.raises(
xgb.core.XGBoostError, match="Slicing DMatrix is not supported"
):
m.slice(rindex=[0, 1, 2]) m.slice(rindex=[0, 1, 2])
@pytest.mark.skipif(**tm.no_cupy()) @pytest.mark.skipif(**tm.no_cupy())

View File

@ -1456,3 +1456,16 @@ def test_intercept() -> None:
result = reg.intercept_ result = reg.intercept_
assert result.dtype == np.float32 assert result.dtype == np.float32
assert result[0] < 0.5 assert result[0] < 0.5
def test_fit_none() -> None:
with pytest.raises(TypeError, match="NoneType"):
xgb.XGBClassifier().fit(None, [0, 1])
X = rng.normal(size=4).reshape(2, 2)
with pytest.raises(ValueError, match="Invalid classes"):
xgb.XGBClassifier().fit(X, None)
with pytest.raises(ValueError, match="labels"):
xgb.XGBRegressor().fit(X, None)