.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_gallery/3-advanced-features/plot_cross_validation.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_auto_gallery_3-advanced-features_plot_cross_validation.py: Cross-Validation Division ========================= .. GENERATED FROM PYTHON SOURCE LINES 7-18 User-specified cross validation division ---------------------------------------- Sometimes, especially when running a test, we would like to fix the train and valid data used in cross validation, instead of choosing them randomly. One simple method is to fix a random seed, such as ``numpy.random.seed()``. But in some cases, we would also like to specify which samples would be in the same "fold", which has great flexibility. In our program, an additional argument ``cv_fold_id`` is for this user-specified cross validation division. An integer ``numpy`` array with the same size of input samples can be given, and those with same integer would be assigned to the same "fold" in K-fold CV. .. GENERATED FROM PYTHON SOURCE LINES 18-38 .. code-block:: Python import numpy as np from abess.datasets import make_glm_data from abess.linear import LinearRegression n = 100 p = 1000 k = 3 np.random.seed(2) data = make_glm_data(n=n, p=p, k=k, family='gaussian') # cv_fold_id has a size of `n` # cv_fold_id has `cv` different integers cv_fold_id = [1 for i in range(30)] + \ [2 for i in range(30)] + [3 for i in range(40)] model = LinearRegression(support_size=range(0, 5), cv=3) model.fit(data.x, data.y, cv_fold_id=cv_fold_id) print('fitted coefficients\' indexes:', np.nonzero(model.coef_)[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none fitted coefficients' indexes: [243 295 659] .. GENERATED FROM PYTHON SOURCE LINES 39-42 The ``abess`` R package also supports user-defined cross-validation division. For R tutorial, please view https://abess-team.github.io/abess/articles/v07-advancedFeatures.html. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.603 seconds) .. _sphx_glr_download_auto_gallery_3-advanced-features_plot_cross_validation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_cross_validation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_cross_validation.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_