put-dfanalytics.asciidoc 20 KB

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