
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/example_linear_regression.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_auto_examples_example_linear_regression.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_example_linear_regression.py:


.. _tut_linear_regression:

Train a linear regression potential
===================================

In this tutorial, we train a linear regression model on the descriptors obtained using the
symmetry functions.

.. GENERATED FROM PYTHON SOURCE LINES 10-36

.. code-block:: default


    from kliff.calculators import CalculatorTorch
    from kliff.dataset import Dataset
    from kliff.descriptors import SymmetryFunction
    from kliff.models import LinearRegression
    from kliff.utils import download_dataset

    descriptor = SymmetryFunction(
        cut_name="cos", cut_dists={"Si-Si": 5.0}, hyperparams="set30", normalize=True
    )


    model = LinearRegression(descriptor)

    # training set
    dataset_path = download_dataset(dataset_name="Si_training_set")
    dataset_path = dataset_path.joinpath("varying_alat")
    tset = Dataset(dataset_path)
    configs = tset.get_configs()
    print("Number of configurations:", len(configs))

    # calculator
    calc = CalculatorTorch(model)
    calc.create(configs, reuse=False)






.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    "SymmetryFunction" descriptor initialized.
    400 configurations read from /Users/mjwen/Applications/kliff/examples/Si_training_set/varying_alat
    Number of configurations: 400
    Found existing fingerprints "fingerprints.pkl".
    Delete existing fingerprints "fingerprints.pkl"
    Start generating fingerprints.
    Calculating mean and stdev.
    Pickling fingerprints to "fingerprints.pkl"
    Processing configuration: 0.
    Processing configuration: 100.
    Processing configuration: 200.
    Processing configuration: 300.
    Pickle 400 configurations finished.
    Finish generating fingerprints.




.. GENERATED FROM PYTHON SOURCE LINES 37-42

We can train a linear regression model by minimizing a loss function as discussed in
:ref:`tut_nn`. But linear regression model has analytic solutions, and thus we can train
the model directly by using this feature. This can be achieved by calling the ``fit()``
function of its calculator.


.. GENERATED FROM PYTHON SOURCE LINES 42-49

.. code-block:: default


    # fit the model
    calc.fit()


    # save model
    model.save("linear_model.pkl")




.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    fit model "LinearRegression" finished.





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  8.653 seconds)


.. _sphx_glr_download_auto_examples_example_linear_regression.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: example_linear_regression.py <example_linear_regression.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: example_linear_regression.ipynb <example_linear_regression.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
