put-dfanalytics.asciidoc 19 KB

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