evaluate-dfanalytics.asciidoc 13 KB

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