1
0

put-dfanalytics.asciidoc 20 KB

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