evaluate-dfanalytics.asciidoc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. * You must have `monitor_ml` privilege to use this API. For more
  17. information, see <<security-privileges>> and <<built-in-roles>>.
  18. [[ml-evaluate-dfanalytics-desc]]
  19. ==== {api-description-title}
  20. The API packages together commonly used evaluation metrics for various types of
  21. machine learning features. This has been designed for use on indexes created by
  22. {dfanalytics}. Evaluation requires both a ground truth field and an analytics
  23. result field to be present.
  24. [[ml-evaluate-dfanalytics-request-body]]
  25. ==== {api-request-body-title}
  26. `index`::
  27. (Required, object) Defines the `index` in which the evaluation will be
  28. performed.
  29. `query`::
  30. (Optional, object) A query clause that retrieves a subset of data from the
  31. source index. See <<query-dsl>>.
  32. `evaluation`::
  33. (Required, object) Defines the type of evaluation you want to perform. See
  34. <<ml-evaluate-dfanalytics-resources>>.
  35. +
  36. --
  37. Available evaluation types:
  38. * `binary_soft_classification`
  39. * `regression`
  40. * `classification`
  41. --
  42. ////
  43. [[ml-evaluate-dfanalytics-results]]
  44. ==== {api-response-body-title}
  45. `binary_soft_classification`::
  46. (object) If you chose to do binary soft classification, the API returns the
  47. following evaluation metrics:
  48. `auc_roc`::: TBD
  49. `confusion_matrix`::: TBD
  50. `precision`::: TBD
  51. `recall`::: TBD
  52. ////
  53. [[ml-evaluate-dfanalytics-example]]
  54. ==== {api-examples-title}
  55. ===== Binary soft classification
  56. [source,console]
  57. --------------------------------------------------
  58. POST _ml/data_frame/_evaluate
  59. {
  60. "index": "my_analytics_dest_index",
  61. "evaluation": {
  62. "binary_soft_classification": {
  63. "actual_field": "is_outlier",
  64. "predicted_probability_field": "ml.outlier_score"
  65. }
  66. }
  67. }
  68. --------------------------------------------------
  69. // TEST[skip:TBD]
  70. The API returns the following results:
  71. [source,console-result]
  72. ----
  73. {
  74. "binary_soft_classification": {
  75. "auc_roc": {
  76. "score": 0.92584757746414444
  77. },
  78. "confusion_matrix": {
  79. "0.25": {
  80. "tp": 5,
  81. "fp": 9,
  82. "tn": 204,
  83. "fn": 5
  84. },
  85. "0.5": {
  86. "tp": 1,
  87. "fp": 5,
  88. "tn": 208,
  89. "fn": 9
  90. },
  91. "0.75": {
  92. "tp": 0,
  93. "fp": 4,
  94. "tn": 209,
  95. "fn": 10
  96. }
  97. },
  98. "precision": {
  99. "0.25": 0.35714285714285715,
  100. "0.5": 0.16666666666666666,
  101. "0.75": 0
  102. },
  103. "recall": {
  104. "0.25": 0.5,
  105. "0.5": 0.1,
  106. "0.75": 0
  107. }
  108. }
  109. }
  110. ----
  111. ===== {regression-cap}
  112. [source,console]
  113. --------------------------------------------------
  114. POST _ml/data_frame/_evaluate
  115. {
  116. "index": "house_price_predictions", <1>
  117. "query": {
  118. "bool": {
  119. "filter": [
  120. { "term": { "ml.is_training": false } } <2>
  121. ]
  122. }
  123. },
  124. "evaluation": {
  125. "regression": {
  126. "actual_field": "price", <3>
  127. "predicted_field": "ml.price_prediction", <4>
  128. "metrics": {
  129. "r_squared": {},
  130. "mean_squared_error": {}
  131. }
  132. }
  133. }
  134. }
  135. --------------------------------------------------
  136. // TEST[skip:TBD]
  137. <1> The output destination index from a {dfanalytics} {reganalysis}.
  138. <2> In this example, a test/train split (`training_percent`) was defined for the
  139. {reganalysis}. This query limits evaluation to be performed on the test split
  140. only.
  141. <3> The ground truth value for the actual house price. This is required in order
  142. to evaluate results.
  143. <4> The predicted value for house price calculated by the {reganalysis}.
  144. The following example calculates the training error:
  145. [source,console]
  146. --------------------------------------------------
  147. POST _ml/data_frame/_evaluate
  148. {
  149. "index": "student_performance_mathematics_reg",
  150. "query": {
  151. "term": {
  152. "ml.is_training": {
  153. "value": true <1>
  154. }
  155. }
  156. },
  157. "evaluation": {
  158. "regression": {
  159. "actual_field": "G3", <2>
  160. "predicted_field": "ml.G3_prediction", <3>
  161. "metrics": {
  162. "r_squared": {},
  163. "mean_squared_error": {}
  164. }
  165. }
  166. }
  167. }
  168. --------------------------------------------------
  169. // TEST[skip:TBD]
  170. <1> In this example, a test/train split (`training_percent`) was defined for the
  171. {reganalysis}. This query limits evaluation to be performed on the train split
  172. only. It means that a training error will be calculated.
  173. <2> The field that contains the ground truth value for the actual student
  174. performance. This is required in order to evaluate results.
  175. <3> The field that contains the predicted value for student performance
  176. calculated by the {reganalysis}.
  177. The next example calculates the testing error. The only difference compared with
  178. the previous example is that `ml.is_training` is set to `false` this time, so
  179. the query excludes the train split from the evaluation.
  180. [source,console]
  181. --------------------------------------------------
  182. POST _ml/data_frame/_evaluate
  183. {
  184. "index": "student_performance_mathematics_reg",
  185. "query": {
  186. "term": {
  187. "ml.is_training": {
  188. "value": false <1>
  189. }
  190. }
  191. },
  192. "evaluation": {
  193. "regression": {
  194. "actual_field": "G3", <2>
  195. "predicted_field": "ml.G3_prediction", <3>
  196. "metrics": {
  197. "r_squared": {},
  198. "mean_squared_error": {}
  199. }
  200. }
  201. }
  202. }
  203. --------------------------------------------------
  204. // TEST[skip:TBD]
  205. <1> In this example, a test/train split (`training_percent`) was defined for the
  206. {reganalysis}. This query limits evaluation to be performed on the test split
  207. only. It means that a testing error will be calculated.
  208. <2> The field that contains the ground truth value for the actual student
  209. performance. This is required in order to evaluate results.
  210. <3> The field that contains the predicted value for student performance
  211. calculated by the {reganalysis}.
  212. ===== {classification-cap}
  213. [source,console]
  214. --------------------------------------------------
  215. POST _ml/data_frame/_evaluate
  216. {
  217. "index": "animal_classification",
  218. "evaluation": {
  219. "classification": { <1>
  220. "actual_field": "animal_class", <2>
  221. "predicted_field": "ml.animal_class_prediction.keyword", <3>
  222. "metrics": {
  223. "multiclass_confusion_matrix" : {} <4>
  224. }
  225. }
  226. }
  227. }
  228. --------------------------------------------------
  229. // TEST[skip:TBD]
  230. <1> The evaluation type.
  231. <2> The field that contains the ground truth value for the actual animal
  232. classification. This is required in order to evaluate results.
  233. <3> The field that contains the predicted value for animal classification by
  234. the {classanalysis}. Since the field storing predicted class is dynamically
  235. mapped as text and keyword, you need to add the `.keyword` suffix to the name.
  236. <4> Specifies the metric for the evaluation.
  237. The API returns the following result:
  238. [source,console-result]
  239. --------------------------------------------------
  240. {
  241. "classification" : {
  242. "multiclass_confusion_matrix" : {
  243. "confusion_matrix" : [
  244. {
  245. "actual_class" : "cat", <1>
  246. "actual_class_doc_count" : 12, <2>
  247. "predicted_classes" : [ <3>
  248. {
  249. "predicted_class" : "cat",
  250. "count" : 12 <4>
  251. },
  252. {
  253. "predicted_class" : "dog",
  254. "count" : 0 <5>
  255. }
  256. ],
  257. "other_predicted_class_doc_count" : 0 <6>
  258. },
  259. {
  260. "actual_class" : "dog",
  261. "actual_class_doc_count" : 11,
  262. "predicted_classes" : [
  263. {
  264. "predicted_class" : "dog",
  265. "count" : 11
  266. },
  267. {
  268. "predicted_class" : "cat",
  269. "count" : 4
  270. }
  271. ],
  272. "other_predicted_class_doc_count" : 4
  273. }
  274. ],
  275. "other_actual_class_count" : 0
  276. }
  277. }
  278. }
  279. --------------------------------------------------
  280. <1> The name of the actual class that the analysis tried to predict.
  281. <2> The number of documents in the index that belong to the `actual_class`.
  282. <3> This object contains the list of the predicted classes and the number of
  283. predictions associated with the class.
  284. <4> The number of cats in the dataset that are correctly identified as cats.
  285. <5> The number of cats in the dataset that are incorrectly classified as dogs.
  286. <6> The number of documents that are classified as a class that is not listed as
  287. a `predicted_class`.