MCPcopy Create free account
hub / github.com/dmlc/xgboost / get_feature_weights

Function get_feature_weights

python-package/xgboost/testing/shared.py:53–100  ·  view source on GitHub ↗

Get feature weights using the demo parser.

(
    *,
    X: ArrayLike,
    y: ArrayLike,
    fw: np.ndarray,
    parser_path: str,
    tree_method: str,
    model: Type[xgb.XGBModel] = xgb.XGBRegressor,
)

Source from the content-addressed store, hash-verified

51
52# pylint: disable=too-many-arguments,too-many-locals
53def get_feature_weights(
54 *,
55 X: ArrayLike,
56 y: ArrayLike,
57 fw: np.ndarray,
58 parser_path: str,
59 tree_method: str,
60 model: Type[xgb.XGBModel] = xgb.XGBRegressor,
61) -> np.ndarray:
62 """Get feature weights using the demo parser."""
63 with tempfile.TemporaryDirectory() as tmpdir:
64 colsample_bynode = 0.5
65 reg = model(
66 tree_method=tree_method,
67 colsample_bynode=colsample_bynode,
68 feature_weights=fw,
69 )
70
71 reg.fit(X, y)
72 model_path = os.path.join(tmpdir, "model.json")
73 reg.save_model(model_path)
74 with open(model_path, "r", encoding="utf-8") as fd:
75 model = json.load(fd)
76
77 spec = importlib.util.spec_from_file_location("JsonParser", parser_path)
78 assert spec is not None
79 jsonm = importlib.util.module_from_spec(spec)
80 assert spec.loader is not None
81 spec.loader.exec_module(jsonm)
82 model = jsonm.Model(model)
83 splits: Dict[int, int] = {}
84 total_nodes = 0
85 for tree in model.trees:
86 n_nodes = len(tree.nodes)
87 total_nodes += n_nodes
88 for n in range(n_nodes):
89 if tree.is_leaf(n):
90 continue
91 if splits.get(tree.split_index(n), None) is None:
92 splits[tree.split_index(n)] = 1
93 else:
94 splits[tree.split_index(n)] += 1
95
96 od = collections.OrderedDict(sorted(splits.items()))
97 tuples = list(od.items())
98 k, v = list(zip(*tuples))
99 w = np.polyfit(k, v, deg=1)
100 return w

Callers 2

test_feature_weightsFunction · 0.90
test_feature_weightsMethod · 0.90

Calls 7

TemporaryDirectoryMethod · 0.80
is_leafMethod · 0.80
split_indexMethod · 0.80
fitMethod · 0.45
save_modelMethod · 0.45
loadMethod · 0.45
getMethod · 0.45

Tested by 2

test_feature_weightsFunction · 0.72
test_feature_weightsMethod · 0.72