diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index b72a859a8..a2389e7fa 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -584,9 +584,7 @@ class DMatrix(object): data: numpy array The array of data to be set """ - if getattr(data, 'base', None) is not None and \ - data.base is not None and isinstance(data, np.ndarray) \ - and isinstance(data.base, np.ndarray) and (not data.flags.c_contiguous): + if isinstance(data, np.ndarray): self.set_float_info_npy2d(field, data) return c_data = c_array(ctypes.c_float, data) @@ -607,13 +605,15 @@ class DMatrix(object): data: numpy array The array of data to be set """ - if getattr(data, 'base', None) is not None and \ - data.base is not None and isinstance(data, np.ndarray) \ - and isinstance(data.base, np.ndarray) and (not data.flags.c_contiguous): - warnings.warn("Use subset (sliced data) of np.ndarray is not recommended " + - "because it will generate extra copies and increase memory consumption") - data = np.array(data, copy=True, dtype=np.float32) - else: + try: + if not data.flags.c_contiguous: + warnings.warn("Use subset (sliced data) of np.ndarray is not recommended " + + "because it will generate extra copies and increase " + + "memory consumption") + data = np.array(data, copy=True, dtype=np.float32) + else: + data = np.array(data, copy=False, dtype=np.float32) + except AttributeError: data = np.array(data, copy=False, dtype=np.float32) c_data = data.ctypes.data_as(ctypes.POINTER(ctypes.c_float)) _check_call(_LIB.XGDMatrixSetFloatInfo(self.handle, @@ -632,13 +632,15 @@ class DMatrix(object): data: numpy array The array of data to be set """ - if getattr(data, 'base', None) is not None and \ - data.base is not None and isinstance(data, np.ndarray) \ - and isinstance(data.base, np.ndarray) and (not data.flags.c_contiguous): - warnings.warn("Use subset (sliced data) of np.ndarray is not recommended " + - "because it will generate extra copies and increase memory consumption") - data = np.array(data, copy=True, dtype=ctypes.c_uint) - else: + try: + if not data.flags.c_contiguous: + warnings.warn("Use subset (sliced data) of np.ndarray is not recommended " + + "because it will generate extra copies and increase " + + "memory consumption") + data = np.array(data, copy=True, dtype=ctypes.c_uint) + else: + data = np.array(data, copy=False, dtype=ctypes.c_uint) + except AttributeError: data = np.array(data, copy=False, dtype=ctypes.c_uint) _check_call(_LIB.XGDMatrixSetUIntInfo(self.handle, c_str(field),