从xgboost中的DMatrix实例检索设计矩阵



在xgboost中,我正在做一些类似的事情

import numpy as np
import xgboost as xgb
y = np.arange(10)
X = np.arange(20).reshape(10, 2)
dtrain = xgb.DMatrix(X, y, feature_names=["x1", "x2"])

如果我想从dtrain中提取y值作为数组,我可以进行

y = dtrain.get_label()

有什么方法可以从dtrain中提取X值作为数组吗?

我不这么认为。使用您的DMatrix dtrain,您可以看到:

dir(dtrain)
['__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_feature_names', '_feature_types', '_init_from_csc', '_init_from_csr', '_init_from_npy2d', 'feature_names', 'feature_types', 'get_base_margin', 'get_float_info', 'get_label', 'get_uint_info', 'get_weight', 'handle', 'num_col', 'num_row', 'save_binary', 'set_base_margin', 'set_float_info', 'set_group', 'set_label', 'set_uint_info', 'set_weight', 'slice']

我能找到的最好的是

dtrain.feature_names

它将返回您的CCD_ 3。dtrain.feature_types有一定的帮助,你可以像dtrain.slice(range(3))一样切片,但这仍然不是你想要的。

最新更新