evaluate-dfanalytics.asciidoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. [[ml-evaluate-dfanalytics-request]]
  11. == {api-request-title}
  12. `POST _ml/data_frame/_evaluate`
  13. [[ml-evaluate-dfanalytics-prereq]]
  14. == {api-prereq-title}
  15. Requires the following privileges:
  16. * cluster: `monitor_ml` (the `machine_learning_user` built-in role grants this
  17. privilege)
  18. * destination index: `read`
  19. [[ml-evaluate-dfanalytics-desc]]
  20. == {api-description-title}
  21. The API packages together commonly used evaluation metrics for various types of
  22. machine learning features. This has been designed for use on indexes created by
  23. {dfanalytics}. Evaluation requires both a ground truth field and an analytics
  24. result field to be present.
  25. [[ml-evaluate-dfanalytics-request-body]]
  26. == {api-request-body-title}
  27. `evaluation`::
  28. (Required, object) Defines the type of evaluation you want to perform.
  29. See <<ml-evaluate-dfanalytics-resources>>.
  30. +
  31. --
  32. Available evaluation types:
  33. * `outlier_detection`
  34. * `regression`
  35. * `classification`
  36. --
  37. `index`::
  38. (Required, object) Defines the `index` in which the evaluation will be
  39. performed.
  40. `query`::
  41. (Optional, object) A query clause that retrieves a subset of data from the
  42. source index. See <<query-dsl>>.
  43. [[ml-evaluate-dfanalytics-resources]]
  44. == {dfanalytics-cap} evaluation resources
  45. [[oldetection-resources]]
  46. === {oldetection-cap} evaluation objects
  47. {oldetection-cap} evaluates the results of an {oldetection} analysis which
  48. outputs the probability that each document is an outlier.
  49. `actual_field`::
  50. (Required, string) The field of the `index` which contains the `ground truth`.
  51. The data type of this field can be boolean or integer. If the data type is
  52. integer, the value has to be either `0` (false) or `1` (true).
  53. `predicted_probability_field`::
  54. (Required, string) The field of the `index` that defines the probability of
  55. whether the item belongs to the class in question or not. It's the field that
  56. contains the results of the analysis.
  57. `metrics`::
  58. (Optional, object) Specifies the metrics that are used for the evaluation. If
  59. no metrics are specified, the following are returned by default:
  60. * `auc_roc` (`include_curve`: false),
  61. * `precision` (`at`: [0.25, 0.5, 0.75]),
  62. * `recall` (`at`: [0.25, 0.5, 0.75]),
  63. * `confusion_matrix` (`at`: [0.25, 0.5, 0.75]).
  64. `auc_roc`:::
  65. (Optional, object) The AUC ROC (area under the curve of the receiver
  66. operating characteristic) score and optionally the curve. Default value is
  67. {"include_curve": false}.
  68. `confusion_matrix`:::
  69. (Optional, object) Set the different thresholds of the {olscore} at where
  70. the metrics (`tp` - true positive, `fp` - false positive, `tn` - true
  71. negative, `fn` - false negative) are calculated. Default value is
  72. {"at": [0.25, 0.50, 0.75]}.
  73. `precision`:::
  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. `recall`:::
  77. (Optional, object) Set the different thresholds of the {olscore} at where
  78. the metric is calculated. Default value is {"at": [0.25, 0.50, 0.75]}.
  79. [[regression-evaluation-resources]]
  80. === {regression-cap} evaluation objects
  81. {regression-cap} evaluation evaluates the results of a {regression} analysis
  82. which outputs a prediction of values.
  83. `actual_field`::
  84. (Required, string) The field of the `index` which contains the `ground truth`.
  85. The data type of this field must be numerical.
  86. `predicted_field`::
  87. (Required, string) The field in the `index` that contains the predicted value,
  88. in other words the results of the {regression} analysis.
  89. `metrics`::
  90. (Optional, object) Specifies the metrics that are used for the evaluation. For
  91. more information on `mse`, `msle`, and `huber`, consult
  92. https://github.com/elastic/examples/tree/master/Machine%20Learning/Regression%20Loss%20Functions[the Jupyter notebook on regression loss functions].
  93. If no metrics are specified, the following are returned by default:
  94. * `mse`,
  95. * `r_squared`,
  96. * `huber` (`delta`: 1.0).
  97. `mse`:::
  98. (Optional, object) Average squared difference between the predicted values
  99. and the actual (`ground truth`) value. For more information, read
  100. {wikipedia}/Mean_squared_error[this wiki article].
  101. `msle`:::
  102. (Optional, object) Average squared difference between the logarithm of the
  103. predicted values and the logarithm of the actual (`ground truth`) value.
  104. `offset`::::
  105. (Optional, double) Defines the transition point at which you switch from
  106. minimizing quadratic error to minimizing quadratic log error. Defaults to
  107. `1`.
  108. `huber`:::
  109. (Optional, object) Pseudo Huber loss function. For more information, read
  110. {wikipedia}/Huber_loss#Pseudo-Huber_loss_function[this wiki article].
  111. `delta`::::
  112. (Optional, double) Approximates 1/2 (prediction - actual)^2^ for values
  113. much less than delta and approximates a straight line with slope delta for
  114. values much larger than delta. Defaults to `1`. Delta needs to be greater
  115. than `0`.
  116. `r_squared`:::
  117. (Optional, object) Proportion of the variance in the dependent variable that
  118. is predictable from the independent variables. For more information, read
  119. {wikipedia}/Coefficient_of_determination[this wiki article].
  120. [[classification-evaluation-resources]]
  121. == {classification-cap} evaluation objects
  122. {classification-cap} evaluation evaluates the results of a {classanalysis} which
  123. outputs a prediction that identifies to which of the classes each document
  124. belongs.
  125. `actual_field`::
  126. (Required, string) The field of the `index` which contains the `ground truth`.
  127. The data type of this field must be categorical.
  128. `predicted_field`::
  129. (Optional, string) The field in the `index` which contains the predicted value,
  130. in other words the results of the {classanalysis}.
  131. `top_classes_field`::
  132. (Optional, string) The field of the `index` which is an array of documents
  133. of the form `{ "class_name": XXX, "class_probability": YYY }`.
  134. This field must be defined as `nested` in the mappings.
  135. `metrics`::
  136. (Optional, object) Specifies the metrics that are used for the evaluation. If
  137. no metrics are specificed, the following are returned by default:
  138. * `accuracy`,
  139. * `multiclass_confusion_matrix`,
  140. * `precision`,
  141. * `recall`.
  142. `accuracy`:::
  143. (Optional, object) Accuracy of predictions (per-class and overall).
  144. `auc_roc`:::
  145. (Optional, object) The AUC ROC (area under the curve of the receiver
  146. operating characteristic) score and optionally the curve.
  147. It is calculated for a specific class (provided as "class_name") treated as
  148. positive.
  149. `class_name`::::
  150. (Required, string) Name of the only class that is treated as positive
  151. during AUC ROC calculation. Other classes are treated as negative
  152. ("one-vs-all" strategy). All the evaluated documents must have
  153. `class_name` in the list of their top classes.
  154. `include_curve`::::
  155. (Optional, Boolean) Whether or not the curve should be returned in
  156. addition to the score. Default value is false.
  157. `multiclass_confusion_matrix`:::
  158. (Optional, object) Multiclass confusion matrix.
  159. `precision`:::
  160. (Optional, object) Precision of predictions (per-class and average).
  161. `recall`:::
  162. (Optional, object) Recall of predictions (per-class and average).
  163. ////
  164. [[ml-evaluate-dfanalytics-results]]
  165. == {api-response-body-title}
  166. `outlier_detection`::
  167. (object) If you chose to do outlier detection, the API returns the
  168. following evaluation metrics:
  169. `auc_roc`::: TBD
  170. `confusion_matrix`::: TBD
  171. `precision`::: TBD
  172. `recall`::: TBD
  173. ////
  174. [[ml-evaluate-dfanalytics-example]]
  175. == {api-examples-title}
  176. [[ml-evaluate-oldetection-example]]
  177. === {oldetection-cap}
  178. [source,console]
  179. --------------------------------------------------
  180. POST _ml/data_frame/_evaluate
  181. {
  182. "index": "my_analytics_dest_index",
  183. "evaluation": {
  184. "outlier_detection": {
  185. "actual_field": "is_outlier",
  186. "predicted_probability_field": "ml.outlier_score"
  187. }
  188. }
  189. }
  190. --------------------------------------------------
  191. // TEST[skip:TBD]
  192. The API returns the following results:
  193. [source,console-result]
  194. ----
  195. {
  196. "outlier_detection": {
  197. "auc_roc": {
  198. "value": 0.92584757746414444
  199. },
  200. "confusion_matrix": {
  201. "0.25": {
  202. "tp": 5,
  203. "fp": 9,
  204. "tn": 204,
  205. "fn": 5
  206. },
  207. "0.5": {
  208. "tp": 1,
  209. "fp": 5,
  210. "tn": 208,
  211. "fn": 9
  212. },
  213. "0.75": {
  214. "tp": 0,
  215. "fp": 4,
  216. "tn": 209,
  217. "fn": 10
  218. }
  219. },
  220. "precision": {
  221. "0.25": 0.35714285714285715,
  222. "0.5": 0.16666666666666666,
  223. "0.75": 0
  224. },
  225. "recall": {
  226. "0.25": 0.5,
  227. "0.5": 0.1,
  228. "0.75": 0
  229. }
  230. }
  231. }
  232. ----
  233. [[ml-evaluate-regression-example]]
  234. === {regression-cap}
  235. [source,console]
  236. --------------------------------------------------
  237. POST _ml/data_frame/_evaluate
  238. {
  239. "index": "house_price_predictions", <1>
  240. "query": {
  241. "bool": {
  242. "filter": [
  243. { "term": { "ml.is_training": false } } <2>
  244. ]
  245. }
  246. },
  247. "evaluation": {
  248. "regression": {
  249. "actual_field": "price", <3>
  250. "predicted_field": "ml.price_prediction", <4>
  251. "metrics": {
  252. "r_squared": {},
  253. "mse": {},
  254. "msle": {"offset": 10},
  255. "huber": {"delta": 1.5}
  256. }
  257. }
  258. }
  259. }
  260. --------------------------------------------------
  261. // TEST[skip:TBD]
  262. <1> The output destination index from a {dfanalytics} {reganalysis}.
  263. <2> In this example, a test/train split (`training_percent`) was defined for the
  264. {reganalysis}. This query limits evaluation to be performed on the test split
  265. only.
  266. <3> The ground truth value for the actual house price. This is required in order
  267. to evaluate results.
  268. <4> The predicted value for house price calculated by the {reganalysis}.
  269. The following example calculates the training error:
  270. [source,console]
  271. --------------------------------------------------
  272. POST _ml/data_frame/_evaluate
  273. {
  274. "index": "student_performance_mathematics_reg",
  275. "query": {
  276. "term": {
  277. "ml.is_training": {
  278. "value": true <1>
  279. }
  280. }
  281. },
  282. "evaluation": {
  283. "regression": {
  284. "actual_field": "G3", <2>
  285. "predicted_field": "ml.G3_prediction", <3>
  286. "metrics": {
  287. "r_squared": {},
  288. "mse": {},
  289. "msle": {},
  290. "huber": {}
  291. }
  292. }
  293. }
  294. }
  295. --------------------------------------------------
  296. // TEST[skip:TBD]
  297. <1> In this example, a test/train split (`training_percent`) was defined for the
  298. {reganalysis}. This query limits evaluation to be performed on the train split
  299. only. It means that a training error will be calculated.
  300. <2> The field that contains the ground truth value for the actual student
  301. performance. This is required in order to evaluate results.
  302. <3> The field that contains the predicted value for student performance
  303. calculated by the {reganalysis}.
  304. The next example calculates the testing error. The only difference compared with
  305. the previous example is that `ml.is_training` is set to `false` this time, so
  306. the query excludes the train split from the evaluation.
  307. [source,console]
  308. --------------------------------------------------
  309. POST _ml/data_frame/_evaluate
  310. {
  311. "index": "student_performance_mathematics_reg",
  312. "query": {
  313. "term": {
  314. "ml.is_training": {
  315. "value": false <1>
  316. }
  317. }
  318. },
  319. "evaluation": {
  320. "regression": {
  321. "actual_field": "G3", <2>
  322. "predicted_field": "ml.G3_prediction", <3>
  323. "metrics": {
  324. "r_squared": {},
  325. "mse": {},
  326. "msle": {},
  327. "huber": {}
  328. }
  329. }
  330. }
  331. }
  332. --------------------------------------------------
  333. // TEST[skip:TBD]
  334. <1> In this example, a test/train split (`training_percent`) was defined for the
  335. {reganalysis}. This query limits evaluation to be performed on the test split
  336. only. It means that a testing error will be calculated.
  337. <2> The field that contains the ground truth value for the actual student
  338. performance. This is required in order to evaluate results.
  339. <3> The field that contains the predicted value for student performance
  340. calculated by the {reganalysis}.
  341. [[ml-evaluate-classification-example]]
  342. === {classification-cap}
  343. [source,console]
  344. --------------------------------------------------
  345. POST _ml/data_frame/_evaluate
  346. {
  347. "index": "animal_classification",
  348. "evaluation": {
  349. "classification": { <1>
  350. "actual_field": "animal_class", <2>
  351. "predicted_field": "ml.animal_class_prediction", <3>
  352. "metrics": {
  353. "multiclass_confusion_matrix" : {} <4>
  354. }
  355. }
  356. }
  357. }
  358. --------------------------------------------------
  359. // TEST[skip:TBD]
  360. <1> The evaluation type.
  361. <2> The field that contains the ground truth value for the actual animal
  362. classification. This is required in order to evaluate results.
  363. <3> The field that contains the predicted value for animal classification by
  364. the {classanalysis}.
  365. <4> Specifies the metric for the evaluation.
  366. The API returns the following result:
  367. [source,console-result]
  368. --------------------------------------------------
  369. {
  370. "classification" : {
  371. "multiclass_confusion_matrix" : {
  372. "confusion_matrix" : [
  373. {
  374. "actual_class" : "cat", <1>
  375. "actual_class_doc_count" : 12, <2>
  376. "predicted_classes" : [ <3>
  377. {
  378. "predicted_class" : "cat",
  379. "count" : 12 <4>
  380. },
  381. {
  382. "predicted_class" : "dog",
  383. "count" : 0 <5>
  384. }
  385. ],
  386. "other_predicted_class_doc_count" : 0 <6>
  387. },
  388. {
  389. "actual_class" : "dog",
  390. "actual_class_doc_count" : 11,
  391. "predicted_classes" : [
  392. {
  393. "predicted_class" : "dog",
  394. "count" : 7
  395. },
  396. {
  397. "predicted_class" : "cat",
  398. "count" : 4
  399. }
  400. ],
  401. "other_predicted_class_doc_count" : 0
  402. }
  403. ],
  404. "other_actual_class_count" : 0
  405. }
  406. }
  407. }
  408. --------------------------------------------------
  409. <1> The name of the actual class that the analysis tried to predict.
  410. <2> The number of documents in the index that belong to the `actual_class`.
  411. <3> This object contains the list of the predicted classes and the number of
  412. predictions associated with the class.
  413. <4> The number of cats in the dataset that are correctly identified as cats.
  414. <5> The number of cats in the dataset that are incorrectly classified as dogs.
  415. <6> The number of documents that are classified as a class that is not listed as
  416. a `predicted_class`.
  417. [source,console]
  418. --------------------------------------------------
  419. POST _ml/data_frame/_evaluate
  420. {
  421. "index": "animal_classification",
  422. "evaluation": {
  423. "classification": { <1>
  424. "actual_field": "animal_class", <2>
  425. "metrics": {
  426. "auc_roc" : { <3>
  427. "class_name": "dog" <4>
  428. }
  429. }
  430. }
  431. }
  432. }
  433. --------------------------------------------------
  434. // TEST[skip:TBD]
  435. <1> The evaluation type.
  436. <2> The field that contains the ground truth value for the actual animal
  437. classification. This is required in order to evaluate results.
  438. <3> Specifies the metric for the evaluation.
  439. <4> Specifies the class name that is treated as positive during the evaluation,
  440. all the other classes are treated as negative.
  441. The API returns the following result:
  442. [source,console-result]
  443. --------------------------------------------------
  444. {
  445. "classification" : {
  446. "auc_roc" : {
  447. "value" : 0.8941788639536681
  448. }
  449. }
  450. }
  451. --------------------------------------------------
  452. // TEST[skip:TBD]