Introduction

This is a basic tutorial on getting direct info and help of python modules from within a Jupyter notebook. Some of this tutorial is specific to Linux (particularly the commands that start with "!").

Getting Basic Help from the Python interpreter

Let's get help on Python's built-in abs function:


In [6]:
help(abs)


Help on built-in function abs in module __builtin__:

abs(...)
    abs(number) -> number
    
    Return the absolute value of the argument.

We have to import the numpy module in order to get help on any of its functions which we attempt below. But if we ask for help on all of np (numpy), it blocks it due to simply too much output for the browser to handle. Besides, we won't be able to read it all in one sitting. So see the Help menu in the notebook web page for numpy which today points to: https://docs.scipy.org/doc/numpy/reference/


In [14]:
import numpy as np
help(np)


IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_data_rate_limit`.

but we can execute commands from the Linux shell like this:


In [15]:
!echo this is output from the echo command from the Linux shell


this is output from the echo command from the Linux shell

In [16]:
!python --version


Python 2.7.13 :: Continuum Analytics, Inc.

In [18]:
!python -c 'print("foo"); print("bar")'


foo
bar

We can count the number of lines of output of whatever command we want. Here it is the simple one-liner of python that is generating two lines of output:


In [19]:
!python -c 'print("foo"); print("bar")' | wc -l


2

And here is why we got the IOPub data rate exceeded error above! There is a lot of output from the help on the np module, and we do not want to see it all, anyhow.


In [2]:
!python -c 'import numpy as np; help(np)' | wc -l


90529

But we can explore some of it by using basic Linux shell commands:


In [4]:
!python -c 'import numpy as np; help(np)' | head -20


Help on package numpy:

NAME
    numpy

FILE
    /home/brentg/conda/Ubuntu.16.04.miniconda2/envs/env1/lib/python2.7/site-packages/numpy/__init__.py

DESCRIPTION
    NumPy
    =====
    
    Provides
      1. An array object of arbitrary homogeneous items
      2. Fast mathematical operations over arrays
      3. Linear Algebra, Fourier Transforms, Random Number Generation
    
    How to use the documentation
    ----------------------------
    Documentation is available in two forms: docstrings provided
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/brentg/conda/Ubuntu.16.04.miniconda2/envs/env1/lib/python2.7/site.py", line 446, in __call__
    return pydoc.help(*args, **kwds)
  File "/home/brentg/conda/Ubuntu.16.04.miniconda2/envs/env1/lib/python2.7/pydoc.py", line 1795, in __call__
    self.help(request)
  File "/home/brentg/conda/Ubuntu.16.04.miniconda2/envs/env1/lib/python2.7/pydoc.py", line 1842, in help
    else: doc(request, 'Help on %s:')
  File "/home/brentg/conda/Ubuntu.16.04.miniconda2/envs/env1/lib/python2.7/pydoc.py", line 1579, in doc
    pager(render_doc(thing, title, forceload))
  File "/home/brentg/conda/Ubuntu.16.04.miniconda2/envs/env1/lib/python2.7/pydoc.py", line 1376, in pager
    pager(text)
  File "/home/brentg/conda/Ubuntu.16.04.miniconda2/envs/env1/lib/python2.7/pydoc.py", line 1482, in plainpager
    sys.stdout.write(_encode(plain(text), getattr(sys.stdout, 'encoding', _encoding)))
IOError: [Errno 32] Broken pipe

We can ignore the big traceback from python. All it is telling us that it failed to write to the stdout, because the head command is closing its side of the pipe after 20 lines of output received.

That is nothing to worry about, but if we cared, we could just use 2>/dev/null to send all of the error (on stderr!) to the bit bucket to hide it (which normally, we should not do because it would hide errors we do need to pay attention to, but not in this peculiar case):


In [5]:
!python -c 'import numpy as np; help(np)' 2>/dev/null | head -20


Help on package numpy:

NAME
    numpy

FILE
    /home/brentg/conda/Ubuntu.16.04.miniconda2/envs/env1/lib/python2.7/site-packages/numpy/__init__.py

DESCRIPTION
    NumPy
    =====
    
    Provides
      1. An array object of arbitrary homogeneous items
      2. Fast mathematical operations over arrays
      3. Linear Algebra, Fourier Transforms, Random Number Generation
    
    How to use the documentation
    ----------------------------
    Documentation is available in two forms: docstrings provided

Getting help on numpy functions

If we know what function within the particular module from online resources such as https://docs.scipy.org/doc/numpy/reference/) we are interested in, we can directly get help on it within the Jupyter notebook. But we do need to import the module before asking for it, as otherwise we will see an error:


In [6]:
import numpy as np
help(np.linspace)


Help on function linspace in module numpy.core.function_base:

linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
    Return evenly spaced numbers over a specified interval.
    
    Returns `num` evenly spaced samples, calculated over the
    interval [`start`, `stop`].
    
    The endpoint of the interval can optionally be excluded.
    
    Parameters
    ----------
    start : scalar
        The starting value of the sequence.
    stop : scalar
        The end value of the sequence, unless `endpoint` is set to False.
        In that case, the sequence consists of all but the last of ``num + 1``
        evenly spaced samples, so that `stop` is excluded.  Note that the step
        size changes when `endpoint` is False.
    num : int, optional
        Number of samples to generate. Default is 50. Must be non-negative.
    endpoint : bool, optional
        If True, `stop` is the last sample. Otherwise, it is not included.
        Default is True.
    retstep : bool, optional
        If True, return (`samples`, `step`), where `step` is the spacing
        between samples.
    dtype : dtype, optional
        The type of the output array.  If `dtype` is not given, infer the data
        type from the other input arguments.
    
        .. versionadded:: 1.9.0
    
    Returns
    -------
    samples : ndarray
        There are `num` equally spaced samples in the closed interval
        ``[start, stop]`` or the half-open interval ``[start, stop)``
        (depending on whether `endpoint` is True or False).
    step : float, optional
        Only returned if `retstep` is True
    
        Size of spacing between samples.
    
    
    See Also
    --------
    arange : Similar to `linspace`, but uses a step size (instead of the
             number of samples).
    logspace : Samples uniformly distributed in log space.
    
    Examples
    --------
    >>> np.linspace(2.0, 3.0, num=5)
    array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ])
    >>> np.linspace(2.0, 3.0, num=5, endpoint=False)
    array([ 2. ,  2.2,  2.4,  2.6,  2.8])
    >>> np.linspace(2.0, 3.0, num=5, retstep=True)
    (array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)
    
    Graphical illustration:
    
    >>> import matplotlib.pyplot as plt
    >>> N = 8
    >>> y = np.zeros(N)
    >>> x1 = np.linspace(0, 10, N, endpoint=True)
    >>> x2 = np.linspace(0, 10, N, endpoint=False)
    >>> plt.plot(x1, y, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.plot(x2, y + 0.5, 'o')
    [<matplotlib.lines.Line2D object at 0x...>]
    >>> plt.ylim([-0.5, 1])
    (-0.5, 1)
    >>> plt.show()

Getting help on scipy functions

Likewise, we can get help on scipi module functions:


In [15]:
import numpy as np
import matplotlib as mpl

from scipy import linalg, optimize

# I don't know what difference there is between np.info and just Python's built-in help:
#   np.info(optimize.fmin)

help(optimize.fmin)


Help on function fmin in module scipy.optimize.optimize:

fmin(func, x0, args=(), xtol=0.0001, ftol=0.0001, maxiter=None, maxfun=None, full_output=0, disp=1, retall=0, callback=None, initial_simplex=None)
    Minimize a function using the downhill simplex algorithm.
    
    This algorithm only uses function values, not derivatives or second
    derivatives.
    
    Parameters
    ----------
    func : callable func(x,*args)
        The objective function to be minimized.
    x0 : ndarray
        Initial guess.
    args : tuple, optional
        Extra arguments passed to func, i.e. ``f(x,*args)``.
    xtol : float, optional
        Absolute error in xopt between iterations that is acceptable for
        convergence.
    ftol : number, optional
        Absolute error in func(xopt) between iterations that is acceptable for
        convergence.
    maxiter : int, optional
        Maximum number of iterations to perform.
    maxfun : number, optional
        Maximum number of function evaluations to make.
    full_output : bool, optional
        Set to True if fopt and warnflag outputs are desired.
    disp : bool, optional
        Set to True to print convergence messages.
    retall : bool, optional
        Set to True to return list of solutions at each iteration.
    callback : callable, optional
        Called after each iteration, as callback(xk), where xk is the
        current parameter vector.
    initial_simplex : array_like of shape (N + 1, N), optional
        Initial simplex. If given, overrides `x0`.
        ``initial_simplex[j,:]`` should contain the coordinates of
        the j-th vertex of the ``N+1`` vertices in the simplex, where
        ``N`` is the dimension.
    
    Returns
    -------
    xopt : ndarray
        Parameter that minimizes function.
    fopt : float
        Value of function at minimum: ``fopt = func(xopt)``.
    iter : int
        Number of iterations performed.
    funcalls : int
        Number of function calls made.
    warnflag : int
        1 : Maximum number of function evaluations made.
        2 : Maximum number of iterations reached.
    allvecs : list
        Solution at each iteration.
    
    See also
    --------
    minimize: Interface to minimization algorithms for multivariate
        functions. See the 'Nelder-Mead' `method` in particular.
    
    Notes
    -----
    Uses a Nelder-Mead simplex algorithm to find the minimum of function of
    one or more variables.
    
    This algorithm has a long history of successful use in applications.
    But it will usually be slower than an algorithm that uses first or
    second derivative information. In practice it can have poor
    performance in high-dimensional problems and is not robust to
    minimizing complicated functions. Additionally, there currently is no
    complete theory describing when the algorithm will successfully
    converge to the minimum, or how fast it will if it does. Both the ftol and
    xtol criteria must be met for convergence.
    
    References
    ----------
    .. [1] Nelder, J.A. and Mead, R. (1965), "A simplex method for function
           minimization", The Computer Journal, 7, pp. 308-313
    
    .. [2] Wright, M.H. (1996), "Direct Search Methods: Once Scorned, Now
           Respectable", in Numerical Analysis 1995, Proceedings of the
           1995 Dundee Biennial Conference in Numerical Analysis, D.F.
           Griffiths and G.A. Watson (Eds.), Addison Wesley Longman,
           Harlow, UK, pp. 191-208.

It seems that the np.info function is close to the same as the builtin help function:


In [14]:
help(np.info)


Help on function info in module numpy.lib.utils:

info(object=None, maxwidth=76, output=<ipykernel.iostream.OutStream object>, toplevel='numpy')
    Get help information for a function, class, or module.
    
    Parameters
    ----------
    object : object or str, optional
        Input object or name to get information about. If `object` is a
        numpy object, its docstring is given. If it is a string, available
        modules are searched for matching objects.  If None, information
        about `info` itself is returned.
    maxwidth : int, optional
        Printing width.
    output : file like object, optional
        File like object that the output is written to, default is
        ``stdout``.  The object has to be opened in 'w' or 'a' mode.
    toplevel : str, optional
        Start search at this level.
    
    See Also
    --------
    source, lookfor
    
    Notes
    -----
    When used interactively with an object, ``np.info(obj)`` is equivalent
    to ``help(obj)`` on the Python prompt or ``obj?`` on the IPython
    prompt.
    
    Examples
    --------
    >>> np.info(np.polyval) # doctest: +SKIP
       polyval(p, x)
         Evaluate the polynomial p at x.
         ...
    
    When using a string for `object` it is possible to get multiple results.
    
    >>> np.info('fft') # doctest: +SKIP
         *** Found in numpy ***
    Core FFT routines
    ...
         *** Found in numpy.fft ***
     fft(a, n=None, axis=-1)
    ...
         *** Repeat reference found in numpy.fft.fftpack ***
         *** Total of 3 references found. ***

Finding numpy functions with keywords in doc strings


In [24]:
help(np.lookfor)


Help on function lookfor in module numpy.lib.utils:

lookfor(what, module=None, import_modules=True, regenerate=False, output=None)
    Do a keyword search on docstrings.
    
    A list of of objects that matched the search is displayed,
    sorted by relevance. All given keywords need to be found in the
    docstring for it to be returned as a result, but the order does
    not matter.
    
    Parameters
    ----------
    what : str
        String containing words to look for.
    module : str or list, optional
        Name of module(s) whose docstrings to go through.
    import_modules : bool, optional
        Whether to import sub-modules in packages. Default is True.
    regenerate : bool, optional
        Whether to re-generate the docstring cache. Default is False.
    output : file-like, optional
        File-like object to write the output to. If omitted, use a pager.
    
    See Also
    --------
    source, info
    
    Notes
    -----
    Relevance is determined only roughly, by checking if the keywords occur
    in the function name, at the start of a docstring, etc.
    
    Examples
    --------
    >>> np.lookfor('binary representation')
    Search results for 'binary representation'
    ------------------------------------------
    numpy.binary_repr
        Return the binary representation of the input number as a string.
    numpy.core.setup_common.long_double_representation
        Given a binary dump as given by GNU od -b, look for long double
    numpy.base_repr
        Return a string representation of a number in the given base system.
    ...


In [16]:
np.lookfor('root')


Search results for 'root'
-------------------------
numpy.roots
    Return the roots of a polynomial with coefficients given in p.
numpy.cbrt
    Return the cube-root of an array, element-wise.
numpy.poly
    Find the coefficients of a polynomial with the given sequence of roots.
numpy.sqrt
    Return the positive square-root of an array, element-wise.
numpy.ma.sqrt
    Return the positive square-root of an array, element-wise.
numpy.polynomial.Hermite.roots
    Return the roots of the series polynomial.
numpy.polynomial.Hermite._roots
    Compute the roots of a Hermite series.
numpy.polynomial.HermiteE.roots
    Return the roots of the series polynomial.
numpy.polynomial.Laguerre.roots
    Return the roots of the series polynomial.
numpy.polynomial.Legendre.roots
    Return the roots of the series polynomial.
numpy.polynomial.HermiteE._roots
    Compute the roots of a HermiteE series.
numpy.polynomial.Laguerre._roots
    Compute the roots of a Laguerre series.
numpy.polynomial.Legendre._roots
    Compute the roots of a Legendre series.
numpy.polynomial.Chebyshev._roots
    Compute the roots of a Chebyshev series.
numpy.polynomial.Polynomial.roots
    Return the roots of the series polynomial.
numpy.polynomial.Hermite.fromroots
    Return series instance that has the specified roots.
numpy.polynomial.Polynomial._roots
    Compute the roots of a polynomial.
numpy.polynomial.Hermite._fromroots
    Generate a Hermite series with given roots.
numpy.polynomial.HermiteE.fromroots
    Return series instance that has the specified roots.
numpy.polynomial.Laguerre.fromroots
    Return series instance that has the specified roots.
numpy.polynomial.Legendre.fromroots
    Return series instance that has the specified roots.
numpy.polynomial.HermiteE._fromroots
    Generate a HermiteE series with given roots.
numpy.polynomial.Laguerre._fromroots
    Generate a Laguerre series with given roots.
numpy.polynomial.Legendre._fromroots
    Generate a Legendre series with given roots.
numpy.polynomial.Chebyshev._fromroots
    Generate a Chebyshev series with given roots.
numpy.polynomial.Polynomial.fromroots
    Return series instance that has the specified roots.
numpy.polynomial.Polynomial._fromroots
    Generate a monic polynomial with given roots.
numpy.std
    Compute the standard deviation along the specified axis.
numpy.polynomial.polynomial.polyvalfromroots
    Evaluate a polynomial specified by its roots at points x.
numpy.nanstd
    Compute the standard deviation along the specified axis, while
numpy.poly1d
    A one-dimensional polynomial class.
numpy.histogram
    Compute the histogram of a set of data.
numpy.linalg.cond
    Compute the condition number of a matrix.
numpy.distutils.command.develop.develop.expand_basedirs
    Calls `os.path.expanduser` on install_base, install_platbase and
numpy.polynomial.hermite.hermgauss
    Gauss-Hermite quadrature.
numpy.polynomial.laguerre.laggauss
    Gauss-Laguerre quadrature.
numpy.polynomial.legendre.leggauss
    Gauss-Legendre quadrature.
numpy.polynomial.hermite_e.hermegauss
    Gauss-HermiteE quadrature.