Skip to content

Commit

Permalink
add gallery-sphinx
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Simpson committed Dec 11, 2020
1 parent ac91e8b commit 9944de1
Show file tree
Hide file tree
Showing 24 changed files with 1,069 additions and 5 deletions.
6 changes: 5 additions & 1 deletion classo/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,11 @@ def __init__(self, method = "not specified"):
def __repr__(self):
string = "\n numerical_method = " + str(self.numerical_method)
string += "\n rescaled lam : " + str(self.rescaled_lam)
string += "\n threshold = " + str(round(self.threshold, 3))
if self.threshold is None:
string += "\n threshold : average of the absolute value of beta"
else:
string += "\n threshold = " + str(round(self.threshold, 3))

if type(self.lam) is str:
string += "\n lam : " + self.lam
else:
Expand Down
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions docs/source/auto_examples/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
:orphan:



.. _sphx_glr_auto_examples:


Examples Gallery
================

Bellow is a gallery of examples.



.. raw:: html

<div class="sphx-glr-thumbcontainer" tooltip="Let&#x27;s present what classo does when using its default parameters on synthetic data">

.. only:: html

.. figure:: /auto_examples/images/thumb/sphx_glr_plot_basic_example_thumb.png
:alt: Basic example

:ref:`sphx_glr_auto_examples_plot_basic_example.py`

.. raw:: html

</div>


.. toctree::
:hidden:

/auto_examples/plot_basic_example

.. raw:: html

<div class="sphx-glr-thumbcontainer" tooltip="Let&#x27;s present how one can specify different aspects of the problem formulation and model selec...">

.. only:: html

.. figure:: /auto_examples/images/thumb/sphx_glr_plot_advanced_example_thumb.png
:alt: Advanced example

:ref:`sphx_glr_auto_examples_plot_advanced_example.py`

.. raw:: html

</div>


.. toctree::
:hidden:

/auto_examples/plot_advanced_example
.. raw:: html

<div class="sphx-glr-clear"></div>



.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-gallery
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download all examples in Python source code: auto_examples_python.zip </auto_examples/auto_examples_python.zip>`
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download all examples in Jupyter notebooks: auto_examples_jupyter.zip </auto_examples/auto_examples_jupyter.zip>`
.. only:: html

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

`Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
162 changes: 162 additions & 0 deletions docs/source/auto_examples/plot_advanced_example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Advanced example\n\nLet's present how one can specify different aspects of the problem \nformulation and model selection strategy on classo, using synthetic data\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from classo import classo_problem, random_data\nimport numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generate the data\nThis code snippet generates a problem instance with sparse \u00df in dimension\nd=100 (sparsity d_nonzero=5). The design matrix X comprises n=100 samples generated from an i.i.d standard normal\ndistribution. The dimension of the constraint matrix C is d x k matrix. The noise level is \u03c3=0.5. \nThe input `zerosum=True` implies that C is the all-ones vector and C\u00df=0. The n-dimensional outcome vector y\nand the regression vector \u00df is then generated to satisfy the given constraints. \n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"m, d, d_nonzero, k, sigma = 100, 200, 5, 1, 0.5\n(X, C, y), sol = random_data(m, d, d_nonzero, k, sigma, zerosum=True, seed=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the classo instance\nNext we can define a default c-lasso problem instance with the generated data:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"problem = classo_problem(X, y, C)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Change the parameters\nLet's see some example of change in the parameters\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"problem.formulation.huber = True\nproblem.formulation.concomitant = False\nproblem.model_selection.CV = True\nproblem.model_selection.LAMfixed = True\nproblem.model_selection.PATH = True\nproblem.model_selection.StabSelparameters.method = 'max'\nproblem.model_selection.CVparameters.seed = 1\nproblem.model_selection.LAMfixedparameters.rescaled_lam = True\nproblem.model_selection.LAMfixedparameters.lam = .1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Check parameters\nYou can look at the generated problem instance by typing:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(problem)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solve optimization problems\n We only use stability selection as default model selection strategy. \nThe command also allows you to inspect the computed stability profile for all variables \nat the theoretical \u03bb\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"problem.solve()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualisation\nAfter completion, the results of the optimization and model selection routines \ncan be visualized using\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(problem.solution)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
71 changes: 71 additions & 0 deletions docs/source/auto_examples/plot_advanced_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
r"""
Advanced example
===============
Let's present how one can specify different aspects of the problem
formulation and model selection strategy on classo, using synthetic data
"""

from classo import classo_problem, random_data
import numpy as np

# %%
# Generate the data
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^
# This code snippet generates a problem instance with sparse ß in dimension
# d=100 (sparsity d_nonzero=5). The design matrix X comprises n=100 samples generated from an i.i.d standard normal
# distribution. The dimension of the constraint matrix C is d x k matrix. The noise level is σ=0.5.
# The input `zerosum=True` implies that C is the all-ones vector and Cß=0. The n-dimensional outcome vector y
# and the regression vector ß is then generated to satisfy the given constraints.

m, d, d_nonzero, k, sigma = 100, 200, 5, 1, 0.5
(X, C, y), sol = random_data(m, d, d_nonzero, k, sigma, zerosum=True, seed=1)

# %%
# Define the classo instance
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Next we can define a default c-lasso problem instance with the generated data:

problem = classo_problem(X, y, C)


# %%
# Change the parameters
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Let's see some example of change in the parameters

problem.formulation.huber = True
problem.formulation.concomitant = False
problem.model_selection.CV = True
problem.model_selection.LAMfixed = True
problem.model_selection.PATH = True
problem.model_selection.StabSelparameters.method = 'max'
problem.model_selection.CVparameters.seed = 1
problem.model_selection.LAMfixedparameters.rescaled_lam = True
problem.model_selection.LAMfixedparameters.lam = .1



# %%
# Check parameters
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^
# You can look at the generated problem instance by typing:

print(problem)

# %%
# Solve optimization problems
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# We only use stability selection as default model selection strategy.
# The command also allows you to inspect the computed stability profile for all variables
# at the theoretical λ

problem.solve()

# %%
# Visualisation
# ^^^^^^^^^^^^^^^
# After completion, the results of the optimization and model selection routines
# can be visualized using

print(problem.solution)
Loading

0 comments on commit 9944de1

Please sign in to comment.