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,
)
| 51 | |
| 52 | # pylint: disable=too-many-arguments,too-many-locals |
| 53 | def 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 |