evaluate-dfanalytics.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. [role="xpack"]
  2. [testenv="platinum"]
  3. [[evaluate-dfanalytics]]
  4. = Evaluate {dfanalytics} API
  5. [subs="attributes"]
  6. ++++
  7. <titleabbrev>Evaluate {dfanalytics}</titleabbrev>
  8. ++++
  9. Evaluates the {dfanalytics} for an annotated index.
  10. experimental[]
  11. [[ml-evaluate-dfanalytics-request]]
  12. == {api-request-title}
  13. `POST _ml/data_frame/_evaluate`
  14. [[ml-evaluate-dfanalytics-prereq]]
  15. == {api-prereq-title}
  16. If the {es} {security-features} are enabled, you must have the following
  17. privileges:
  18. * cluster: `monitor_ml`
  19. For more information, see <<security-privileges>> and {ml-docs-setup-privileges}.
  20. [[ml-evaluate-dfanalytics-desc]]
  21. == {api-description-title}
  22. The API packages together commonly used evaluation metrics for various types of
  23. machine learning features. This has been designed for use on indexes created by
  24. {dfanalytics}. Evaluation requires both a ground truth field and an analytics
  25. result field to be present.
  26. [[ml-evaluate-dfanalytics-request-body]]
  27. == {api-request-body-title}
  28. `evaluation`::
  29. (Required, object) Defines the type of evaluation you want to perform.
  30. See <<ml-evaluate-dfanalytics-resources>>.
  31. +
  32. --
  33. Available evaluation types:
  34. * `outlier_detection`
  35. * `regression`
  36. * `classification`
  37. --
  38. `index`::
  39. (Required, object) Defines the `index` in which the evaluation will be
  40. performed.
  41. `query`::
  42. (Optional, object) A query clause that retrieves a subset of data from the
  43. source index. See <<query-dsl>>.
  44. [[ml-evaluate-dfanalytics-resources]]
  45. == {dfanalytics-cap} evaluation resources
  46. [[oldetection-resources]]
  47. === {oldetection-cap} evaluation objects
  48. {oldetection-cap} evaluates the results of an {oldetection} analysis which outputs
  49. the probability that each document is an outlier.
  50. `actual_field`::
  51. (Required, string) The field of the `index` which contains the `ground truth`.
  52. The data type of this field can be boolean or integer. If the data type is
  53. integer, the value has to be either `0` (false) or `1` (true).
  54. `predicted_probability_field`::
  55. (Required, string) The field of the `index` that defines the probability of
  56. whether the item belongs to the class in question or not. It's the field that
  57. contains the results of the analysis.
  58. `metrics`::
  59. (Optional, object) Specifies the metrics that are used for the evaluation.
  60. Available metrics:
  61. `auc_roc`:::
  62. (Optional, object) The AUC ROC (area under the curve of the receiver
  63. operating characteristic) score and optionally the curve. Default value is
  64. {"includes_curve": false}.
  65. `confusion_matrix`:::
  66. (Optional, object) Set the different thresholds of the {olscore} at where
  67. the metrics (`tp` - true positive, `fp` - false positive, `tn` - true
  68. negative, `fn` - false negative) are calculated. Default value is
  69. {"at": [0.25, 0.50, 0.75]}.
  70. `precision`:::
  71. (Optional, object) Set the different thresholds of the {olscore} at where
  72. the metric is calculated. Default value is {"at": [0.25, 0.50, 0.75]}.
  73. `recall`:::
  74. (Optional, object) Set the different thresholds of the {olscore} at where
  75. the metric is calculated. Default value is {"at": [0.25, 0.50, 0.75]}.
  76. [[regression-evaluation-resources]]
  77. === {regression-cap} evaluation objects
  78. {regression-cap} evaluation evaluates the results of a {regression} analysis
  79. which outputs a prediction of values.
  80. `actual_field`::
  81. (Required, string) The field of the `index` which contains the `ground truth`.
  82. The data type of this field must be numerical.
  83. `predicted_field`::
  84. (Required, string) The field in the `index` that contains the predicted value,
  85. in other words the results of the {regression} analysis.
  86. `metrics`::
  87. (Optional, object) Specifies the metrics that are used for the evaluation.
  88. Available metrics:
  89. `mse`:::
  90. (Optional, object) Average squared difference between the predicted values and the actual (`ground truth`) value.
  91. For more information, read https://en.wikipedia.org/wiki/Mean_squared_error[this wiki article].
  92. `msle`:::
  93. (Optional, object) Average squared difference between the logarithm of the predicted values and the logarithm of the actual
  94. (`ground truth`) value.
  95. `huber`:::
  96. (Optional, object) Pseudo Huber loss function.
  97. For more information, read https://en.wikipedia.org/wiki/Huber_loss#Pseudo-Huber_loss_function[this wiki article].
  98. `r_squared`:::
  99. (Optional, object) Proportion of the variance in the dependent variable that is predictable from the independent variables.
  100. For more information, read https://en.wikipedia.org/wiki/Coefficient_of_determination[this wiki article].
  101. [[classification-evaluation-resources]]
  102. == {classification-cap} evaluation objects
  103. {classification-cap} evaluation evaluates the results of a {classanalysis} which
  104. outputs a prediction that identifies to which of the classes each document
  105. belongs.
  106. `actual_field`::
  107. (Required, string) The field of the `index` which contains the `ground truth`.
  108. The data type of this field must be categorical.
  109. `predicted_field`::
  110. (Required, string) The field in the `index` that contains the predicted value,
  111. in other words the results of the {classanalysis}.
  112. `metrics`::
  113. (Optional, object) Specifies the metrics that are used for the evaluation.
  114. Available metrics:
  115. `accuracy`:::
  116. (Optional, object) Accuracy of predictions (per-class and overall).
  117. `multiclass_confusion_matrix`:::
  118. (Optional, object) Multiclass confusion matrix.
  119. `precision`:::
  120. (Optional, object) Precision of predictions (per-class and average).
  121. `recall`:::
  122. (Optional, object) Recall of predictions (per-class and average).
  123. ////
  124. [[ml-evaluate-dfanalytics-results]]
  125. == {api-response-body-title}
  126. `outlier_detection`::
  127. (object) If you chose to do outlier detection, the API returns the
  128. following evaluation metrics:
  129. `auc_roc`::: TBD
  130. `confusion_matrix`::: TBD
  131. `precision`::: TBD
  132. `recall`::: TBD
  133. ////
  134. [[ml-evaluate-dfanalytics-example]]
  135. == {api-examples-title}
  136. [[ml-evaluate-oldetection-example]]
  137. === {oldetection-cap}
  138. [source,console]
  139. --------------------------------------------------
  140. POST _ml/data_frame/_evaluate
  141. {
  142. "index": "my_analytics_dest_index",
  143. "evaluation": {
  144. "outlier_detection": {
  145. "actual_field": "is_outlier",
  146. "predicted_probability_field": "ml.outlier_score"
  147. }
  148. }
  149. }
  150. --------------------------------------------------
  151. // TEST[skip:TBD]
  152. The API returns the following results:
  153. [source,console-result]
  154. ----
  155. {
  156. "outlier_detection": {
  157. "auc_roc": {
  158. "score": 0.92584757746414444
  159. },
  160. "confusion_matrix": {
  161. "0.25": {
  162. "tp": 5,
  163. "fp": 9,
  164. "tn": 204,
  165. "fn": 5
  166. },
  167. "0.5": {
  168. "tp": 1,
  169. "fp": 5,
  170. "tn": 208,
  171. "fn": 9
  172. },
  173. "0.75": {
  174. "tp": 0,
  175. "fp": 4,
  176. "tn": 209,
  177. "fn": 10
  178. }
  179. },
  180. "precision": {
  181. "0.25": 0.35714285714285715,
  182. "0.5": 0.16666666666666666,
  183. "0.75": 0
  184. },
  185. "recall": {
  186. "0.25": 0.5,
  187. "0.5": 0.1,
  188. "0.75": 0
  189. }
  190. }
  191. }
  192. ----
  193. [[ml-evaluate-regression-example]]
  194. === {regression-cap}
  195. [source,console]
  196. --------------------------------------------------
  197. POST _ml/data_frame/_evaluate
  198. {
  199. "index": "house_price_predictions", <1>
  200. "query": {
  201. "bool": {
  202. "filter": [
  203. { "term": { "ml.is_training": false } } <2>
  204. ]
  205. }
  206. },
  207. "evaluation": {
  208. "regression": {
  209. "actual_field": "price", <3>
  210. "predicted_field": "ml.price_prediction", <4>
  211. "metrics": {
  212. "r_squared": {},
  213. "mse": {}
  214. }
  215. }
  216. }
  217. }
  218. --------------------------------------------------
  219. // TEST[skip:TBD]
  220. <1> The output destination index from a {dfanalytics} {reganalysis}.
  221. <2> In this example, a test/train split (`training_percent`) was defined for the
  222. {reganalysis}. This query limits evaluation to be performed on the test split
  223. only.
  224. <3> The ground truth value for the actual house price. This is required in order
  225. to evaluate results.
  226. <4> The predicted value for house price calculated by the {reganalysis}.
  227. The following example calculates the training error:
  228. [source,console]
  229. --------------------------------------------------
  230. POST _ml/data_frame/_evaluate
  231. {
  232. "index": "student_performance_mathematics_reg",
  233. "query": {
  234. "term": {
  235. "ml.is_training": {
  236. "value": true <1>
  237. }
  238. }
  239. },
  240. "evaluation": {
  241. "regression": {
  242. "actual_field": "G3", <2>
  243. "predicted_field": "ml.G3_prediction", <3>
  244. "metrics": {
  245. "r_squared": {},
  246. "mse": {}
  247. }
  248. }
  249. }
  250. }
  251. --------------------------------------------------
  252. // TEST[skip:TBD]
  253. <1> In this example, a test/train split (`training_percent`) was defined for the
  254. {reganalysis}. This query limits evaluation to be performed on the train split
  255. only. It means that a training error will be calculated.
  256. <2> The field that contains the ground truth value for the actual student
  257. performance. This is required in order to evaluate results.
  258. <3> The field that contains the predicted value for student performance
  259. calculated by the {reganalysis}.
  260. The next example calculates the testing error. The only difference compared with
  261. the previous example is that `ml.is_training` is set to `false` this time, so
  262. the query excludes the train split from the evaluation.
  263. [source,console]
  264. --------------------------------------------------
  265. POST _ml/data_frame/_evaluate
  266. {
  267. "index": "student_performance_mathematics_reg",
  268. "query": {
  269. "term": {
  270. "ml.is_training": {
  271. "value": false <1>
  272. }
  273. }
  274. },
  275. "evaluation": {
  276. "regression": {
  277. "actual_field": "G3", <2>
  278. "predicted_field": "ml.G3_prediction", <3>
  279. "metrics": {
  280. "r_squared": {},
  281. "mse": {}
  282. }
  283. }
  284. }
  285. }
  286. --------------------------------------------------
  287. // TEST[skip:TBD]
  288. <1> In this example, a test/train split (`training_percent`) was defined for the
  289. {reganalysis}. This query limits evaluation to be performed on the test split
  290. only. It means that a testing error will be calculated.
  291. <2> The field that contains the ground truth value for the actual student
  292. performance. This is required in order to evaluate results.
  293. <3> The field that contains the predicted value for student performance
  294. calculated by the {reganalysis}.
  295. [[ml-evaluate-classification-example]]
  296. === {classification-cap}
  297. [source,console]
  298. --------------------------------------------------
  299. POST _ml/data_frame/_evaluate
  300. {
  301. "index": "animal_classification",
  302. "evaluation": {
  303. "classification": { <1>
  304. "actual_field": "animal_class", <2>
  305. "predicted_field": "ml.animal_class_prediction", <3>
  306. "metrics": {
  307. "multiclass_confusion_matrix" : {} <4>
  308. }
  309. }
  310. }
  311. }
  312. --------------------------------------------------
  313. // TEST[skip:TBD]
  314. <1> The evaluation type.
  315. <2> The field that contains the ground truth value for the actual animal
  316. classification. This is required in order to evaluate results.
  317. <3> The field that contains the predicted value for animal classification by
  318. the {classanalysis}.
  319. <4> Specifies the metric for the evaluation.
  320. The API returns the following result:
  321. [source,console-result]
  322. --------------------------------------------------
  323. {
  324. "classification" : {
  325. "multiclass_confusion_matrix" : {
  326. "confusion_matrix" : [
  327. {
  328. "actual_class" : "cat", <1>
  329. "actual_class_doc_count" : 12, <2>
  330. "predicted_classes" : [ <3>
  331. {
  332. "predicted_class" : "cat",
  333. "count" : 12 <4>
  334. },
  335. {
  336. "predicted_class" : "dog",
  337. "count" : 0 <5>
  338. }
  339. ],
  340. "other_predicted_class_doc_count" : 0 <6>
  341. },
  342. {
  343. "actual_class" : "dog",
  344. "actual_class_doc_count" : 11,
  345. "predicted_classes" : [
  346. {
  347. "predicted_class" : "dog",
  348. "count" : 7
  349. },
  350. {
  351. "predicted_class" : "cat",
  352. "count" : 4
  353. }
  354. ],
  355. "other_predicted_class_doc_count" : 0
  356. }
  357. ],
  358. "other_actual_class_count" : 0
  359. }
  360. }
  361. }
  362. --------------------------------------------------
  363. <1> The name of the actual class that the analysis tried to predict.
  364. <2> The number of documents in the index that belong to the `actual_class`.
  365. <3> This object contains the list of the predicted classes and the number of
  366. predictions associated with the class.
  367. <4> The number of cats in the dataset that are correctly identified as cats.
  368. <5> The number of cats in the dataset that are incorrectly classified as dogs.
  369. <6> The number of documents that are classified as a class that is not listed as
  370. a `predicted_class`.