【问题标题】:Retrieve Design Matrix from DMatrix Instance in xgboost从 xgboost 中的 DMatrix 实例中检索设计矩阵
【发布时间】:2016-07-18 13:15:42
【问题描述】:

在 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 值作为数组?

【问题讨论】:

    标签: python xgboost


    【解决方案1】:

    我不这么认为。用你的DMatrixdtrain,你可以看到:

    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
    

    这将返回您的["x1", "x2"]dtrain.feature_types 有点帮助,你可以像dtrain.slice(range(3)) 那样截取片段,但这仍然不是你想要的。

    【讨论】:

      猜你喜欢
      • 2016-07-05
      • 2016-05-19
      • 2018-03-20
      • 2019-04-29
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      相关资源
      最近更新 更多