put-dfanalytics.asciidoc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. [role="xpack"]
  2. [testenv="platinum"]
  3. [[put-dfanalytics]]
  4. = Create {dfanalytics-jobs} API
  5. [subs="attributes"]
  6. ++++
  7. <titleabbrev>Create {dfanalytics-jobs}</titleabbrev>
  8. ++++
  9. Instantiates a {dfanalytics-job}.
  10. beta::[]
  11. [[ml-put-dfanalytics-request]]
  12. == {api-request-title}
  13. `PUT _ml/data_frame/analytics/<data_frame_analytics_id>`
  14. [[ml-put-dfanalytics-prereq]]
  15. == {api-prereq-title}
  16. If the {es} {security-features} are enabled, you must have the following
  17. built-in roles and privileges:
  18. * `machine_learning_admin`
  19. * source indices: `read`, `view_index_metadata`
  20. * destination index: `read`, `create_index`, `manage` and `index`
  21. For more information, see <<built-in-roles>>, <<security-privileges>>, and
  22. {ml-docs-setup-privileges}.
  23. NOTE: The {dfanalytics-job} remembers which roles the user who created it had at
  24. the time of creation. When you start the job, it performs the analysis using
  25. those same roles. If you provide
  26. <<http-clients-secondary-authorization,secondary authorization headers>>,
  27. those credentials are used instead.
  28. [[ml-put-dfanalytics-desc]]
  29. == {api-description-title}
  30. This API creates a {dfanalytics-job} that performs an analysis on the source
  31. indices and stores the outcome in a destination index.
  32. If the destination index does not exist, it is created automatically when you
  33. start the job. See <<start-dfanalytics>>.
  34. If you supply only a subset of the {regression} or {classification} parameters,
  35. {ml-docs}/hyperparameters.html[hyperparameter optimization] occurs. It
  36. determines a value for each of the undefined parameters.
  37. [[ml-put-dfanalytics-path-params]]
  38. == {api-path-parms-title}
  39. `<data_frame_analytics_id>`::
  40. (Required, string)
  41. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=job-id-data-frame-analytics-define]
  42. [role="child_attributes"]
  43. [[ml-put-dfanalytics-request-body]]
  44. == {api-request-body-title}
  45. `allow_lazy_start`::
  46. (Optional, boolean)
  47. Specifies whether this job can start when there is insufficient {ml} node
  48. capacity for it to be immediately assigned to a node. The default is `false`; if
  49. a {ml} node with capacity to run the job cannot immediately be found, the API
  50. returns an error. However, this is also subject to the cluster-wide
  51. `xpack.ml.max_lazy_ml_nodes` setting. See <<advanced-ml-settings>>. If this
  52. option is set to `true`, the API does not return an error and the job waits in
  53. the `starting` state until sufficient {ml} node capacity is available.
  54. //Begin analysis
  55. `analysis`::
  56. (Required, object)
  57. The analysis configuration, which contains the information necessary to perform
  58. one of the following types of analysis: {classification}, {oldetection}, or
  59. {regression}.
  60. +
  61. .Properties of `analysis`
  62. [%collapsible%open]
  63. ====
  64. //Begin classification
  65. `classification`:::
  66. (Required^*^, object)
  67. The configuration information necessary to perform
  68. {ml-docs}/dfa-classification.html[{classification}].
  69. +
  70. TIP: Advanced parameters are for fine-tuning {classanalysis}. They are set
  71. automatically by hyperparameter optimization to give the minimum validation
  72. error. It is highly recommended to use the default values unless you fully
  73. understand the function of these parameters.
  74. +
  75. .Properties of `classification`
  76. [%collapsible%open]
  77. =====
  78. `class_assignment_objective`::::
  79. (Optional, string)
  80. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=class-assignment-objective]
  81. `dependent_variable`::::
  82. (Required, string)
  83. +
  84. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=dependent-variable]
  85. +
  86. The data type of the field must be numeric (`integer`, `short`, `long`, `byte`),
  87. categorical (`ip` or `keyword`), or boolean. There must be no more than 30
  88. different values in this field.
  89. `eta`::::
  90. (Optional, double)
  91. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=eta]
  92. `feature_bag_fraction`::::
  93. (Optional, double)
  94. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=feature-bag-fraction]
  95. `feature_processors`::::
  96. (Optional, list)
  97. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=dfas-feature-processors]
  98. `gamma`::::
  99. (Optional, double)
  100. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=gamma]
  101. `lambda`::::
  102. (Optional, double)
  103. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=lambda]
  104. `max_trees`::::
  105. (Optional, integer)
  106. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=max-trees]
  107. `num_top_classes`::::
  108. (Optional, integer)
  109. Defines the number of categories for which the predicted probabilities are
  110. reported. It must be non-negative or -1. If it is -1 or greater than the total
  111. number of categories, probabilities are reported for all categories; if you have
  112. a large number of categories, there could be a significant effect on the size of your destination index. Defaults to 2.
  113. +
  114. --
  115. NOTE: To use the
  116. {ml-docs}/ml-dfanalytics-evaluate.html#ml-dfanalytics-class-aucroc[AUC ROC evaluation method],
  117. `num_top_classes` must be set to `-1` or a value greater than or equal to the
  118. total number of categories.
  119. --
  120. `num_top_feature_importance_values`::::
  121. (Optional, integer)
  122. Advanced configuration option. Specifies the maximum number of
  123. {ml-docs}/ml-feature-importance.html[{feat-imp}] values per document to return.
  124. By default, it is zero and no {feat-imp} calculation occurs.
  125. `prediction_field_name`::::
  126. (Optional, string)
  127. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=prediction-field-name]
  128. `randomize_seed`::::
  129. (Optional, long)
  130. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=randomize-seed]
  131. `training_percent`::::
  132. (Optional, integer)
  133. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=training-percent]
  134. //End classification
  135. =====
  136. //Begin outlier_detection
  137. `outlier_detection`:::
  138. (Required^*^, object)
  139. The configuration information necessary to perform
  140. {ml-docs}/dfa-outlier-detection.html[{oldetection}]:
  141. +
  142. .Properties of `outlier_detection`
  143. [%collapsible%open]
  144. =====
  145. `compute_feature_influence`::::
  146. (Optional, boolean)
  147. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=compute-feature-influence]
  148. `feature_influence_threshold`::::
  149. (Optional, double)
  150. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=feature-influence-threshold]
  151. `method`::::
  152. (Optional, string)
  153. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=method]
  154. `n_neighbors`::::
  155. (Optional, integer)
  156. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=n-neighbors]
  157. `outlier_fraction`::::
  158. (Optional, double)
  159. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=outlier-fraction]
  160. `standardization_enabled`::::
  161. (Optional, boolean)
  162. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=standardization-enabled]
  163. //End outlier_detection
  164. =====
  165. //Begin regression
  166. `regression`:::
  167. (Required^*^, object)
  168. The configuration information necessary to perform
  169. {ml-docs}/dfa-regression.html[{regression}].
  170. +
  171. TIP: Advanced parameters are for fine-tuning {reganalysis}. They are set
  172. automatically by hyperparameter optimization to give the minimum validation
  173. error. It is highly recommended to use the default values unless you fully
  174. understand the function of these parameters.
  175. +
  176. .Properties of `regression`
  177. [%collapsible%open]
  178. =====
  179. `dependent_variable`::::
  180. (Required, string)
  181. +
  182. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=dependent-variable]
  183. +
  184. The data type of the field must be numeric.
  185. `eta`::::
  186. (Optional, double)
  187. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=eta]
  188. `feature_bag_fraction`::::
  189. (Optional, double)
  190. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=feature-bag-fraction]
  191. `feature_processors`::::
  192. (Optional, list)
  193. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=dfas-feature-processors]
  194. `gamma`::::
  195. (Optional, double)
  196. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=gamma]
  197. `lambda`::::
  198. (Optional, double)
  199. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=lambda]
  200. `loss_function`::::
  201. (Optional, string)
  202. The loss function used during {regression}. Available options are `mse` (mean
  203. squared error), `msle` (mean squared logarithmic error), `huber` (Pseudo-Huber
  204. loss). Defaults to `mse`. Refer to
  205. {ml-docs}/dfa-regression.html#dfa-regression-lossfunction[Loss functions for {regression} analyses]
  206. to learn more.
  207. `loss_function_parameter`::::
  208. (Optional, double)
  209. A positive number that is used as a parameter to the `loss_function`.
  210. `max_trees`::::
  211. (Optional, integer)
  212. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=max-trees]
  213. `num_top_feature_importance_values`::::
  214. (Optional, integer)
  215. Advanced configuration option. Specifies the maximum number of
  216. {ml-docs}/ml-feature-importance.html[{feat-imp}] values per document to return.
  217. By default, it is zero and no {feat-imp} calculation occurs.
  218. `prediction_field_name`::::
  219. (Optional, string)
  220. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=prediction-field-name]
  221. `randomize_seed`::::
  222. (Optional, long)
  223. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=randomize-seed]
  224. `training_percent`::::
  225. (Optional, integer)
  226. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=training-percent]
  227. =====
  228. //End regression
  229. ====
  230. //End analysis
  231. //Begin analyzed_fields
  232. `analyzed_fields`::
  233. (Optional, object)
  234. Specify `includes` and/or `excludes` patterns to select which fields will be
  235. included in the analysis. The patterns specified in `excludes` are applied last,
  236. therefore `excludes` takes precedence. In other words, if the same field is
  237. specified in both `includes` and `excludes`, then the field will not be included
  238. in the analysis.
  239. +
  240. --
  241. [[dfa-supported-fields]]
  242. The supported fields for each type of analysis are as follows:
  243. * {oldetection-cap} requires numeric or boolean data to analyze. The algorithms
  244. don't support missing values therefore fields that have data types other than
  245. numeric or boolean are ignored. Documents where included fields contain missing
  246. values, null values, or an array are also ignored. Therefore the `dest` index
  247. may contain documents that don't have an {olscore}.
  248. * {regression-cap} supports fields that are numeric, `boolean`, `text`,
  249. `keyword`, and `ip`. It is also tolerant of missing values. Fields that are
  250. supported are included in the analysis, other fields are ignored. Documents
  251. where included fields contain an array with two or more values are also
  252. ignored. Documents in the `dest` index that don’t contain a results field are
  253. not included in the {reganalysis}.
  254. * {classification-cap} supports fields that are numeric, `boolean`, `text`,
  255. `keyword`, and `ip`. It is also tolerant of missing values. Fields that are
  256. supported are included in the analysis, other fields are ignored. Documents
  257. where included fields contain an array with two or more values are also ignored.
  258. Documents in the `dest` index that don’t contain a results field are not
  259. included in the {classanalysis}. {classanalysis-cap} can be improved by mapping
  260. ordinal variable values to a single number. For example, in case of age ranges,
  261. you can model the values as "0-14" = 0, "15-24" = 1, "25-34" = 2, and so on.
  262. If `analyzed_fields` is not set, only the relevant fields will be included. For
  263. example, all the numeric fields for {oldetection}. For more information about
  264. field selection, see <<explain-dfanalytics>>.
  265. --
  266. +
  267. .Properties of `analyzed_fields`
  268. [%collapsible%open]
  269. ====
  270. `excludes`:::
  271. (Optional, array)
  272. An array of strings that defines the fields that will be excluded from the
  273. analysis. You do not need to add fields with unsupported data types to
  274. `excludes`, these fields are excluded from the analysis automatically.
  275. `includes`:::
  276. (Optional, array)
  277. An array of strings that defines the fields that will be included in the
  278. analysis.
  279. //End analyzed_fields
  280. ====
  281. `description`::
  282. (Optional, string)
  283. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=description-dfa]
  284. `dest`::
  285. (Required, object)
  286. include::{es-repo-dir}/ml/ml-shared.asciidoc[tag=dest]
  287. `max_num_threads`::
  288. (Optional, integer)
  289. The maximum number of threads to be used by the analysis.
  290. The default value is `1`. Using more threads may decrease the time
  291. necessary to complete the analysis at the cost of using more CPU.
  292. Note that the process may use additional threads for operational
  293. functionality other than the analysis itself.
  294. `model_memory_limit`::
  295. (Optional, string)
  296. The approximate maximum amount of memory resources that are permitted for
  297. analytical processing. The default value for {dfanalytics-jobs} is `1gb`. If
  298. your `elasticsearch.yml` file contains an `xpack.ml.max_model_memory_limit`
  299. setting, an error occurs when you try to create {dfanalytics-jobs} that have
  300. `model_memory_limit` values greater than that setting. For more information, see
  301. <<ml-settings>>.
  302. `source`::
  303. (object)
  304. The configuration of how to source the analysis data. It requires an `index`.
  305. Optionally, `query` and `_source` may be specified.
  306. +
  307. .Properties of `source`
  308. [%collapsible%open]
  309. ====
  310. `index`:::
  311. (Required, string or array) Index or indices on which to perform the analysis.
  312. It can be a single index or index pattern as well as an array of indices or
  313. patterns.
  314. +
  315. WARNING: If your source indices contain documents with the same IDs, only the
  316. document that is indexed last appears in the destination index.
  317. `query`:::
  318. (Optional, object) The {es} query domain-specific language (<<query-dsl,DSL>>).
  319. This value corresponds to the query object in an {es} search POST body. All the
  320. options that are supported by {es} can be used, as this object is passed
  321. verbatim to {es}. By default, this property has the following value:
  322. `{"match_all": {}}`.
  323. `_source`:::
  324. (Optional, object) Specify `includes` and/or `excludes` patterns to select which
  325. fields will be present in the destination. Fields that are excluded cannot be
  326. included in the analysis.
  327. +
  328. .Properties of `_source`
  329. [%collapsible%open]
  330. =====
  331. `includes`::::
  332. (array) An array of strings that defines the fields that will be included in the
  333. destination.
  334. `excludes`::::
  335. (array) An array of strings that defines the fields that will be excluded from
  336. the destination.
  337. =====
  338. ====
  339. [[ml-put-dfanalytics-example]]
  340. == {api-examples-title}
  341. [[ml-put-dfanalytics-example-preprocess]]
  342. === Preprocessing actions example
  343. The following example shows how to limit the scope of the analysis to certain
  344. fields, specify excluded fields in the destination index, and use a query to
  345. filter your data before analysis.
  346. [source,console]
  347. --------------------------------------------------
  348. PUT _ml/data_frame/analytics/model-flight-delays-pre
  349. {
  350. "source": {
  351. "index": [
  352. "kibana_sample_data_flights" <1>
  353. ],
  354. "query": { <2>
  355. "range": {
  356. "DistanceKilometers": {
  357. "gt": 0
  358. }
  359. }
  360. },
  361. "_source": { <3>
  362. "includes": [],
  363. "excludes": [
  364. "FlightDelay",
  365. "FlightDelayType"
  366. ]
  367. }
  368. },
  369. "dest": { <4>
  370. "index": "df-flight-delays",
  371. "results_field": "ml-results"
  372. },
  373. "analysis": {
  374. "regression": {
  375. "dependent_variable": "FlightDelayMin",
  376. "training_percent": 90
  377. }
  378. },
  379. "analyzed_fields": { <5>
  380. "includes": [],
  381. "excludes": [
  382. "FlightNum"
  383. ]
  384. },
  385. "model_memory_limit": "100mb"
  386. }
  387. --------------------------------------------------
  388. // TEST[skip:setup kibana sample data]
  389. <1> Source index to analyze.
  390. <2> This query filters out entire documents that will not be present in the
  391. destination index.
  392. <3> The `_source` object defines fields in the data set that will be included or
  393. excluded in the destination index.
  394. <4> Defines the destination index that contains the results of the analysis and
  395. the fields of the source index specified in the `_source` object. Also defines
  396. the name of the `results_field`.
  397. <5> Specifies fields to be included in or excluded from the analysis. This does
  398. not affect whether the fields will be present in the destination index, only
  399. affects whether they are used in the analysis.
  400. In this example, we can see that all the fields of the source index are included
  401. in the destination index except `FlightDelay` and `FlightDelayType` because
  402. these are defined as excluded fields by the `excludes` parameter of the
  403. `_source` object. The `FlightNum` field is included in the destination index,
  404. however it is not included in the analysis because it is explicitly specified as
  405. excluded field by the `excludes` parameter of the `analyzed_fields` object.
  406. [[ml-put-dfanalytics-example-od]]
  407. === {oldetection-cap} example
  408. The following example creates the `loganalytics` {dfanalytics-job}, the analysis
  409. type is `outlier_detection`:
  410. [source,console]
  411. --------------------------------------------------
  412. PUT _ml/data_frame/analytics/loganalytics
  413. {
  414. "description": "Outlier detection on log data",
  415. "source": {
  416. "index": "logdata"
  417. },
  418. "dest": {
  419. "index": "logdata_out"
  420. },
  421. "analysis": {
  422. "outlier_detection": {
  423. "compute_feature_influence": true,
  424. "outlier_fraction": 0.05,
  425. "standardization_enabled": true
  426. }
  427. }
  428. }
  429. --------------------------------------------------
  430. // TEST[setup:setup_logdata]
  431. The API returns the following result:
  432. [source,console-result]
  433. ----
  434. {
  435. "id": "loganalytics",
  436. "description": "Outlier detection on log data",
  437. "source": {
  438. "index": [ "logdata" ],
  439. "query": {
  440. "match_all": {}
  441. }
  442. },
  443. "dest": {
  444. "index": "logdata_out",
  445. "results_field": "ml"
  446. },
  447. "analysis": {
  448. "outlier_detection": {
  449. "compute_feature_influence": true,
  450. "outlier_fraction": 0.05,
  451. "standardization_enabled": true
  452. }
  453. },
  454. "model_memory_limit": "1gb",
  455. "create_time": 1562265491319,
  456. "version": "8.0.0",
  457. "allow_lazy_start": false,
  458. "max_num_threads": 1
  459. }
  460. ----
  461. // TESTRESPONSE[s/1562265491319/$body.$_path/]
  462. // TESTRESPONSE[s/"version" : "8.0.0"/"version" : $body.version/]
  463. [[ml-put-dfanalytics-example-r]]
  464. === {regression-cap} examples
  465. The following example creates the `house_price_regression_analysis`
  466. {dfanalytics-job}, the analysis type is `regression`:
  467. [source,console]
  468. --------------------------------------------------
  469. PUT _ml/data_frame/analytics/house_price_regression_analysis
  470. {
  471. "source": {
  472. "index": "houses_sold_last_10_yrs"
  473. },
  474. "dest": {
  475. "index": "house_price_predictions"
  476. },
  477. "analysis":
  478. {
  479. "regression": {
  480. "dependent_variable": "price"
  481. }
  482. }
  483. }
  484. --------------------------------------------------
  485. // TEST[skip:TBD]
  486. The API returns the following result:
  487. [source,console-result]
  488. ----
  489. {
  490. "id" : "house_price_regression_analysis",
  491. "source" : {
  492. "index" : [
  493. "houses_sold_last_10_yrs"
  494. ],
  495. "query" : {
  496. "match_all" : { }
  497. }
  498. },
  499. "dest" : {
  500. "index" : "house_price_predictions",
  501. "results_field" : "ml"
  502. },
  503. "analysis" : {
  504. "regression" : {
  505. "dependent_variable" : "price",
  506. "training_percent" : 100
  507. }
  508. },
  509. "model_memory_limit" : "1gb",
  510. "create_time" : 1567168659127,
  511. "version" : "8.0.0",
  512. "allow_lazy_start" : false
  513. }
  514. ----
  515. // TESTRESPONSE[s/1567168659127/$body.$_path/]
  516. // TESTRESPONSE[s/"version": "8.0.0"/"version": $body.version/]
  517. The following example creates a job and specifies a training percent:
  518. [source,console]
  519. --------------------------------------------------
  520. PUT _ml/data_frame/analytics/student_performance_mathematics_0.3
  521. {
  522. "source": {
  523. "index": "student_performance_mathematics"
  524. },
  525. "dest": {
  526. "index":"student_performance_mathematics_reg"
  527. },
  528. "analysis":
  529. {
  530. "regression": {
  531. "dependent_variable": "G3",
  532. "training_percent": 70, <1>
  533. "randomize_seed": 19673948271 <2>
  534. }
  535. }
  536. }
  537. --------------------------------------------------
  538. // TEST[skip:TBD]
  539. <1> The percentage of the data set that is used for training the model.
  540. <2> The seed that is used to randomly pick which data is used for training.
  541. [[ml-put-dfanalytics-example-c]]
  542. === {classification-cap} example
  543. The following example creates the `loan_classification` {dfanalytics-job}, the
  544. analysis type is `classification`:
  545. [source,console]
  546. --------------------------------------------------
  547. PUT _ml/data_frame/analytics/loan_classification
  548. {
  549. "source" : {
  550. "index": "loan-applicants"
  551. },
  552. "dest" : {
  553. "index": "loan-applicants-classified"
  554. },
  555. "analysis" : {
  556. "classification": {
  557. "dependent_variable": "label",
  558. "training_percent": 75,
  559. "num_top_classes": 2
  560. }
  561. }
  562. }
  563. --------------------------------------------------
  564. // TEST[skip:TBD]