.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "python/examples/feature_weights.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_python_examples_feature_weights.py: Demo for using feature weight to change column sampling ======================================================= .. versionadded:: 1.3.0 .. GENERATED FROM PYTHON SOURCE LINES 7-60 .. code-block:: Python import argparse import numpy as np from matplotlib import pyplot as plt import xgboost def main(args: argparse.Namespace) -> None: rng = np.random.RandomState(1994) kRows = 4196 kCols = 10 X = rng.randn(kRows, kCols) y = rng.randn(kRows) fw = np.ones(shape=(kCols,)) for i in range(kCols): fw[i] *= float(i) dtrain = xgboost.DMatrix(X, y) dtrain.set_info(feature_weights=fw) # Perform column sampling for each node split evaluation, the sampling process is # weighted by feature weights. bst = xgboost.train( {"tree_method": "hist", "colsample_bynode": 0.2}, dtrain, num_boost_round=10, evals=[(dtrain, "d")], ) feature_map = bst.get_fscore() # feature zero has 0 weight assert feature_map.get("f0", None) is None assert max(feature_map.values()) == feature_map.get("f9") if args.plot: xgboost.plot_importance(bst) plt.show() if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "--plot", type=int, default=1, help="Set to 0 to disable plotting the evaluation history.", ) args = parser.parse_args() main(args) .. _sphx_glr_download_python_examples_feature_weights.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: feature_weights.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: feature_weights.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: feature_weights.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_