Permalink
Browse files

DOC: merge doc wiki edits. Thanks to everyone who contributed.

  • Loading branch information...
1 parent 0199055 commit b853c347d46a5095870c62db83962288ec1ef4a7 @rgommers rgommers committed Aug 22, 2011
@@ -378,24 +378,26 @@ The following script shows examples for how constraints can be specified.
"""
This script tests fmin_slsqp using Example 14.4 from Numerical Methods for
Engineers by Steven Chapra and Raymond Canale. This example maximizes the
- function f(x) = 2*x*y + 2*x - x**2 - 2*y**2, which has a maximum at x=2,y=1.
+ function f(x) = 2*x0*x1 + 2*x0 - x0**2 - 2*x1**2, which has a maximum
+ at x0=2, x1=1.
"""
from scipy.optimize import fmin_slsqp
- from numpy import array, asfarray, finfo,ones, sqrt, zeros
+ from numpy import array
-
- def testfunc(d, *args):
+ def testfunc(x, *args):
"""
Parameters
----------
- d : list
+ d : list
A list of two elements, where d[0] represents x and
d[1] represents y in the following equation.
- sign : float
- A multiplier for f. Since we want to optimize it, and the scipy
- optimizers can only minimize functions, we need to multiply it by
- -1 to achieve the desired solution.
+ args : tuple
+ First element of args is a multiplier for f.
+ Since the objective function should be maximized, and the scipy
+ optimizers can only minimize functions, it is nessessary to
+ multiply the objective function by -1 to achieve the desired
+ solution.
Returns
-------
res : float
@@ -406,134 +408,104 @@ The following script shows examples for how constraints can be specified.
sign = args[0]
except:
sign = 1.0
- x = d[0]
- y = d[1]
- return sign*(2*x*y + 2*x - x**2 - 2*y**2)
+ return sign*(2*x[0]*x[1] + 2*x[0] - x[0]**2 - 2*x[1]**2)
- def testfunc_deriv(d,*args):
+ def testfunc_deriv(x,*args):
""" This is the derivative of testfunc, returning a numpy array
- representing df/dx and df/dy
-
- """
+ representing df/dx and df/dy """
try:
sign = args[0]
except:
sign = 1.0
- x = d[0]
- y = d[1]
- dfdx = sign*(-2*x + 2*y + 2)
- dfdy = sign*(2*x - 4*y)
- return array([ dfdx, dfdy ],float)
+ dfdx0 = sign*(-2*x[0] + 2*x[1] + 2)
+ dfdx1 = sign*(2*x[0] - 4*x[1])
+ return array([ dfdx0, dfdx1 ])
+ def test_eqcons(x,*args):
+ """ Lefthandside of the equality constraint """
+ return array([ x[0]**3-x[1] ])
- from time import time
+ def test_ieqcons(x,*args):
+ """ Lefthandside of inequality constraint """
+ return array([ x[1]-1 ])
+
+ def test_fprime_eqcons(x,*args):
+ """ First derivative of equality constraint """
+ return array([ 3.0*(x[0]**2.0), -1.0 ])
- print '\n\n'
+ def test_fprime_ieqcons(x,*args):
+ """ First derivative of inequality constraint """
+ return array([ 0.0, 1.0 ])
+
+ from time import time
- print "Unbounded optimization. Derivatives approximated."
+ print "Unbounded optimization."
+ print "Derivatives of objective function approximated."
t0 = time()
- x = fmin_slsqp(testfunc, [-1.0,1.0], args=(-1.0,), iprint=2, full_output=1)
+ result = fmin_slsqp(testfunc, [-1.0,1.0], args=(-1.0,), iprint=2, full_output=1)
print "Elapsed time:", 1000*(time()-t0), "ms"
- print "Results",x
- print "\n\n"
+ print "Results", result, "\n\n"
- print "Unbounded optimization. Derivatives provided."
+ print "Unbounded optimization."
+ print "Derivatives of objective function provided."
t0 = time()
- x = fmin_slsqp(testfunc, [-1.0,1.0], args=(-1.0,), iprint=2, full_output=1)
+ result = fmin_slsqp(testfunc, [-1.0,1.0], fprime=testfunc_deriv, args=(-1.0,),
+ iprint=2, full_output=1)
print "Elapsed time:", 1000*(time()-t0), "ms"
- print "Results",x
- print "\n\n"
+ print "Results", result, "\n\n"
- print "Bound optimization. Derivatives approximated."
+ print "Bound optimization (equality constraints)."
+ print "Constraints implemented via lambda function."
+ print "Derivatives of objective function approximated."
+ print "Derivatives of constraints approximated."
t0 = time()
- x = fmin_slsqp(testfunc, [-1.0,1.0], args=(-1.0,),
- eqcons=[lambda x, y: x[0]-x[1] ], iprint=2, full_output=1)
+ result = fmin_slsqp(testfunc, [-1.0,1.0], args=(-1.0,),
+ eqcons=[lambda x, args: x[0]-x[1] ], iprint=2, full_output=1)
print "Elapsed time:", 1000*(time()-t0), "ms"
- print "Results",x
- print "\n\n"
+ print "Results", result, "\n\n"
- print "Bound optimization (equality constraints). Derivatives provided."
+ print "Bound optimization (equality constraints)."
+ print "Constraints implemented via lambda."
+ print "Derivatives of objective function provided."
+ print "Derivatives of constraints approximated."
t0 = time()
- x = fmin_slsqp(testfunc, [-1.0,1.0], fprime=testfunc_deriv, args=(-1.0,),
- eqcons=[lambda x, y: x[0]-x[1] ], iprint=2, full_output=1)
+ result = fmin_slsqp(testfunc, [-1.0,1.0], fprime=testfunc_deriv, args=(-1.0,),
+ eqcons=[lambda x, args: x[0]-x[1] ], iprint=2, full_output=1)
print "Elapsed time:", 1000*(time()-t0), "ms"
- print "Results",x
- print "\n\n"
+ print "Results", result, "\n\n"
print "Bound optimization (equality and inequality constraints)."
- print "Derivatives provided."
-
+ print "Constraints implemented via lambda."
+ print "Derivatives of objective function provided."
+ print "Derivatives of constraints approximated."
t0 = time()
- x = fmin_slsqp(testfunc,[-1.0,1.0], fprime=testfunc_deriv, args=(-1.0,),
- eqcons=[lambda x, y: x[0]-x[1] ],
- ieqcons=[lambda x, y: x[0]-.5], iprint=2, full_output=1)
+ result = fmin_slsqp(testfunc,[-1.0,1.0], fprime=testfunc_deriv, args=(-1.0,),
+ eqcons=[lambda x, args: x[0]-x[1] ],
+ ieqcons=[lambda x, args: x[0]-.5], iprint=2, full_output=1)
print "Elapsed time:", 1000*(time()-t0), "ms"
- print "Results",x
- print "\n\n"
-
-
- def test_eqcons(d,*args):
- try:
- sign = args[0]
- except:
- sign = 1.0
- x = d[0]
- y = d[1]
- return array([ x**3-y ])
-
-
- def test_ieqcons(d,*args):
- try:
- sign = args[0]
- except:
- sign = 1.0
- x = d[0]
- y = d[1]
- return array([ y-1 ])
+ print "Results", result, "\n\n"
print "Bound optimization (equality and inequality constraints)."
- print "Derivatives provided via functions."
+ print "Constraints implemented via function."
+ print "Derivatives of objective function provided."
+ print "Derivatives of constraints approximated."
t0 = time()
- x = fmin_slsqp(testfunc, [-1.0,1.0], fprime=testfunc_deriv, args=(-1.0,),
+ result = fmin_slsqp(testfunc, [-1.0,1.0], fprime=testfunc_deriv, args=(-1.0,),
f_eqcons=test_eqcons, f_ieqcons=test_ieqcons,
iprint=2, full_output=1)
print "Elapsed time:", 1000*(time()-t0), "ms"
- print "Results",x
- print "\n\n"
-
-
- def test_fprime_eqcons(d,*args):
- try:
- sign = args[0]
- except:
- sign = 1.0
- x = d[0]
- y = d[1]
- return array([ 3.0*(x**2.0), -1.0 ])
-
-
- def test_fprime_ieqcons(d,*args):
- try:
- sign = args[0]
- except:
- sign = 1.0
- x = d[0]
- y = d[1]
- return array([ 0.0, 1.0 ])
+ print "Results", result, "\n\n"
print "Bound optimization (equality and inequality constraints)."
- print "Derivatives provided via functions."
- print "Constraint jacobians provided via functions"
+ print "Constraints implemented via function."
+ print "All derivatives provided."
t0 = time()
- x = fmin_slsqp(testfunc,[-1.0,1.0], fprime=testfunc_deriv, args=(-1.0,),
- f_eqcons=test_eqcons, f_ieqcons=test_ieqcons,
- fprime_eqcons=test_fprime_eqcons,
- fprime_ieqcons=test_fprime_ieqcons, iprint=2, full_output=1)
+ result = fmin_slsqp(testfunc,[-1.0,1.0], fprime=testfunc_deriv, args=(-1.0,),
+ f_eqcons=test_eqcons, fprime_eqcons=test_fprime_eqcons,
+ f_ieqcons=test_ieqcons, fprime_ieqcons=test_fprime_ieqcons,
+ iprint=2, full_output=1)
print "Elapsed time:", 1000*(time()-t0), "ms"
- print "Results",x
- print "\n\n"
-
-
+ print "Results", result, "\n\n"
Scalar function minimizers
@@ -762,7 +734,7 @@ to the Laplace operator part: we know that in one dimension
.. math::
- \partial_x^2 \approx \frac{1}{h_x^2} \begin{pmatrix}
+ \partial_x^2 \approx \frac{1}{h_x^2} \begin{pmatrix}
-2 & 1 & 0 & 0 \cdots \\
1 & -2 & 1 & 0 \cdots \\
0 & 1 & -2 & 1 \cdots \\
View
@@ -1907,8 +1907,9 @@ def dendrogram(Z, p=30, truncate_mode=None, color_threshold=None,
no_leaves=False, show_contracted=False,
link_color_func=None):
"""
- Plots the hiearchical clustering defined by the linkage Z as a
- dendrogram. The dendrogram illustrates how each cluster is
+ Plots the hierarchical clustering as a dendrogram.
+
+ The dendrogram illustrates how each cluster is
composed by drawing a U-shaped link between a non-singleton
cluster and its children. The height of the top of the U-link is
the distance between its children clusters. It is also the
@@ -1930,19 +1931,17 @@ def dendrogram(Z, p=30, truncate_mode=None, color_threshold=None,
large. Truncation is used to condense the dendrogram. There
are several modes:
- * None/'none': no truncation is performed (Default)
-
- * 'lastp': the last ``p`` non-singleton formed in the linkage
- are the only non-leaf nodes in the linkage; they correspond
- to to rows ``Z[n-p-2:end]`` in ``Z``. All other
- non-singleton clusters are contracted into leaf nodes.
-
- * 'mlab': This corresponds to MATLAB(TM) behavior. (not
- implemented yet)
+ * None/'none': no truncation is performed (Default)
+ * 'lastp': the last ``p`` non-singleton formed in the linkage
+ are the only non-leaf nodes in the linkage; they correspond
+ to to rows ``Z[n-p-2:end]`` in ``Z``. All other
+ non-singleton clusters are contracted into leaf nodes.
+ * 'mlab': This corresponds to MATLAB(TM) behavior. (not
+ implemented yet)
+ * 'level'/'mtica': no more than ``p`` levels of the
+ dendrogram tree are displayed. This corresponds to
+ Mathematica(TM) behavior.
- * 'level'/'mtica': no more than ``p`` levels of the
- dendrogram tree are displayed. This corresponds to
- Mathematica(TM) behavior.
color_threshold : double, optional
For brevity, let :math:`t` be the ``color_threshold``.
Colors all the descendent links below a cluster node
@@ -1961,19 +1960,16 @@ def dendrogram(Z, p=30, truncate_mode=None, color_threshold=None,
and :math:`i < n`.
orientation : str, optional
The direction to plot the dendrogram, which can be any
- of the following strings
+ of the following strings:
- * 'top': plots the root at the top, and plot descendent
+ * 'top' plots the root at the top, and plot descendent
links going downwards. (default).
-
- * 'bottom': plots the root at the bottom, and plot descendent
+ * 'bottom'- plots the root at the bottom, and plot descendent
links going upwards.
-
- * 'left': plots the root at the left, and plot descendent
+ * 'left'- plots the root at the left, and plot descendent
links going right.
-
- * 'right': plots the root at the right, and plot descendent
- links going left.
+ * 'right'- plots the root at the right, and plot descendent
+ links going left.
labels : ndarray, optional
By default ``labels`` is ``None`` so the index of the
@@ -1987,13 +1983,11 @@ def dendrogram(Z, p=30, truncate_mode=None, color_threshold=None,
two descendent links are plotted is determined by this
parameter, which can be any of the following values:
- * False: nothing is done.
-
- * 'ascending'/True: the child with the minimum number of
- original objects in its cluster is plotted first.
-
- * 'descendent': the child with the maximum number of
- original objects in its cluster is plotted first.
+ * False: nothing is done.
+ * 'ascending'/True: the child with the minimum number of
+ original objects in its cluster is plotted first.
+ * 'descendent': the child with the maximum number of
+ original objects in its cluster is plotted first.
Note ``distance_sort`` and ``count_sort`` cannot both be
``True``.
@@ -2003,13 +1997,11 @@ def dendrogram(Z, p=30, truncate_mode=None, color_threshold=None,
two descendent links are plotted is determined by this
parameter, which can be any of the following values:
- * False: nothing is done.
-
- * 'ascending'/True: the child with the minimum distance
- between its direct descendents is plotted first.
-
- * 'descending': the child with the maximum distance
- between its direct descendents is plotted first.
+ * False: nothing is done.
+ * 'ascending'/True: the child with the minimum distance
+ between its direct descendents is plotted first.
+ * 'descending': the child with the maximum distance
+ between its direct descendents is plotted first.
Note ``distance_sort`` and ``count_sort`` cannot both be
``True``.
@@ -2040,7 +2032,7 @@ def dendrogram(Z, p=30, truncate_mode=None, color_threshold=None,
leaf.
Indices :math:`k < n` correspond to original observations
- while indices :math:`k \geq n` correspond to non-singleton
+ while indices :math:`k \\geq n` correspond to non-singleton
clusters.
For example, to label singletons with their node id and
@@ -2070,9 +2062,9 @@ def llf(id):
function is expected to return the color to paint the link,
encoded as a matplotlib color string code.
- For example::
+ For example:
- dendrogram(Z, link_color_func=lambda k: colors[k])
+ >>> dendrogram(Z, link_color_func=lambda k: colors[k])
colors the direct links below each untruncated non-singleton node
``k`` using ``colors[k]``.
Oops, something went wrong.

0 comments on commit b853c34

Please sign in to comment.