<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/cts.thresholds.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>cts.thresholds</api:function-name><api:suggest>cts.thresholds</api:suggest><api:suggest>cts</api:suggest><api:suggest>thresholds</api:suggest><api:function-link mode="xquery" fullname="cts:thresholds">/apidoc/8.0/cts:thresholds.xml</api:function-link><api:function mode="javascript" name="thresholds" type="builtin" lib="cts" category="Classifier" hidden="false" bucket="MarkLogic Built-In Functions" prefix="cts" namespace="http://marklogic.com/cts" fullname="cts.thresholds"><api:summary>
  Compute precision, recall, the F measure, and thresholds for the
  classes computed by the classifier, by comparing with the labels
  for the same set.
</api:summary><api:params><api:param class="javascript" name="computedLabels" type="Array"><api:param-description>
 An array of objects containing the labels from classification
(the output from <code xmlns="http://www.w3.org/1999/xhtml">cts.classify</code>) for
  a set of documents.
 </api:param-description><api:param-name>computedLabels</api:param-name><api:param-type>Array</api:param-type></api:param><api:param class="javascript" name="knownLabels" type="Array"><api:param-description>
 An array of objects containing the known labels for the same set
 of documents.
 </api:param-description><api:param-name>knownLabels</api:param-name><api:param-type>Array</api:param-type></api:param><api:param class="javascript" name="recallWeight" type="double" optional="true"><api:param-description>
 The factor to use in the calculation of the F measure. The number should
 be non-negative. A value of 0 means F is just precision and a value
 of +Infinity means F is just recall. The default is 1, which gives the harmonic
 mean between precision and recall.
 </api:param-description><api:param-name>recallWeight</api:param-name><api:param-type>double</api:param-type></api:param></api:params><api:return class="javascript">Array</api:return><api:usage>
  <p xmlns="http://www.w3.org/1999/xhtml">You use the output of  <span class="javascript"><code>cts.thresholds</code></span> to determine
  the best thresholds values for your data, based on the first pass
  through the first part of your training data.  The output of  <span class="javascript"><code>cts.thresholds</code></span> provides you
  with precision and recall measurements at the calculated thresholds
  for each class.  The following are the definitions of the attributes
  of the <code>thresholds</code> element returned by
  
  <span class="javascript"><code>cts.thresholds</code></span>:</p>
  <dl xmlns="http://www.w3.org/1999/xhtml">
  <dt><p><code>name</code></p></dt>
  <dd>The name of the class.</dd>
  <dt><p><code>threshold</code></p></dt>
  <dd>The threshold that is computed by the classifier to give the best
  results.  The threshold is used by
  
  <span class="javascript"><code>cts.classify</code></span> when
  classifying documents, and is defined to be the positive
  or negative distance from the hyperplane which represents the edge of
  the class.
  </dd>
  <dt><p><code>precision</code></p></dt>
  <dd> A number which represents the fraction of nodes identified in a
  class that are actually in that class.  As this aproaches 1, there is
  a higher probability that you over-classified.</dd>
  <dt><p><code>recall</code></p></dt>
  <dd>A number which represents the fraction of nodes in a class that
  were identified by the classifier as being in that class. As this
  aproaches 1, there is a higher probability that you under-classified.</dd>
  <dt><p><code>F</code> (the F-measure)</p></dt>
  <dd>A measure which represents if the classification at the given
  threshold is closer to recall or closer to precision.  A value of 1
  indicates that precision and recall have equal weight.  A value of 0.5
  indicates that precision is weighted 2x recall.  A value of 2 indicates
  that recall is weighted 2x precision.  A value of 0 indicates that the
  weighting is precision only, and a value of 
  <span class="javascript"><code>+Infinity</code></span> indicates
  that weighting is recall only.</dd>
  </dl>
</api:usage><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
//   This returns the computed thresholds for the second half of
//   the plays in a Shakespeare database, based on a classifier
//   trained with the first half of the plays.  For example:

var firsthalf = fn.subsequence(xdmp.directory("/shakespeare/plays/", "1"), 1, 19);
var plays1 = firsthalf.clone();
var secondhalf = fn.subsequence(xdmp.directory("/shakespeare/plays/", "1"), 20, 37);
var plays2 = secondhalf.clone();

var firstlabels = [];
for (var x of firsthalf) {
  var singleClass = [{"name": xdmp.documentProperties(xdmp.nodeUri(x)).next().
                                value.xpath("//playtype/fn:string()")
                     }];
  firstlabels.push({"classes": singleClass});
}

var secondlabels = [];
for (var x of secondhalf) {
  var singleClass = [{"name": xdmp.documentProperties(xdmp.nodeUri(x)).next().
                                value.xpath("//playtype/fn:string()")
                     }];
  secondlabels.push({"classes": singleClass});
};

var classifier = cts.train(plays1.toArray(), firstlabels, 
          {"classifierType": "supports",
           "useDbConfig": true,
           "epsilon": 0.00001});

var classifysecond =
  cts.classify(plays2.toArray(), classifier, {}, plays1.toArray());
cts.thresholds(classifysecond, secondlabels);
=&gt;
[
  {
    "name": "HISTORY",
    "threshold": 4.16419839859009,
    "precision": 1,
    "recall": 0.5,
    "f": 0.666666666666667,
    "count": 4
  },
  {
    "name": "COMEDY",
    "threshold": 3.69728088378906,
    "precision": 0.611111111111111,
    "recall": 1,
    "f": 0.758620689655173,
    "count": 11
  },
  {
    "name": "TRAGEDY",
    "threshold": 2.37126207351685,
    "precision": 0.4,
    "recall": 0.666666666666667,
    "f": 0.5,
    "count": 3
  }
]
</pre></api:example></api:function></api:function-page>