1
0

put-dfanalytics.asciidoc 19 KB

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