evaluate-dfanalytics.asciidoc 13 KB

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