put-dfanalytics.asciidoc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. `max_trees`::::
  188. (Optional, integer)
  189. include::{docdir}/ml/ml-shared.asciidoc[tag=max-trees]
  190. `num_top_feature_importance_values`::::
  191. (Optional, integer)
  192. Advanced configuration option. Specifies the maximum number of
  193. {ml-docs}/ml-feature-importance.html[{feat-imp}] values per document to return.
  194. By default, it is zero and no {feat-imp} calculation occurs.
  195. `prediction_field_name`::::
  196. (Optional, string)
  197. include::{docdir}/ml/ml-shared.asciidoc[tag=prediction-field-name]
  198. `randomize_seed`::::
  199. (Optional, long)
  200. include::{docdir}/ml/ml-shared.asciidoc[tag=randomize-seed]
  201. `training_percent`::::
  202. (Optional, integer)
  203. include::{docdir}/ml/ml-shared.asciidoc[tag=training-percent]
  204. =====
  205. //End regression
  206. ====
  207. //End analysis
  208. //Begin analyzed_fields
  209. `analyzed_fields`::
  210. (Optional, object)
  211. Specify `includes` and/or `excludes` patterns to select which fields will be
  212. included in the analysis. The patterns specified in `excludes` are applied last,
  213. therefore `excludes` takes precedence. In other words, if the same field is
  214. specified in both `includes` and `excludes`, then the field will not be included
  215. in the analysis.
  216. +
  217. --
  218. [[dfa-supported-fields]]
  219. The supported fields for each type of analysis are as follows:
  220. * {oldetection-cap} requires numeric or boolean data to analyze. The algorithms
  221. don't support missing values therefore fields that have data types other than
  222. numeric or boolean are ignored. Documents where included fields contain missing
  223. values, null values, or an array are also ignored. Therefore the `dest` index
  224. may contain documents that don't have an {olscore}.
  225. * {regression-cap} supports fields that are numeric, `boolean`, `text`,
  226. `keyword`, and `ip`. It is also tolerant of missing values. Fields that are
  227. supported are included in the analysis, other fields are ignored. Documents
  228. where included fields contain an array with two or more values are also
  229. ignored. Documents in the `dest` index that don’t contain a results field are
  230. not included in the {reganalysis}.
  231. * {classification-cap} supports fields that are numeric, `boolean`, `text`,
  232. `keyword`, and `ip`. It is also tolerant of missing values. Fields that are
  233. supported are included in the analysis, other fields are ignored. Documents
  234. where included fields contain an array with two or more values are also ignored.
  235. Documents in the `dest` index that don’t contain a results field are not
  236. included in the {classanalysis}. {classanalysis-cap} can be improved by mapping
  237. ordinal variable values to a single number. For example, in case of age ranges,
  238. you can model the values as "0-14" = 0, "15-24" = 1, "25-34" = 2, and so on.
  239. If `analyzed_fields` is not set, only the relevant fields will be included. For
  240. example, all the numeric fields for {oldetection}. For more information about
  241. field selection, see <<explain-dfanalytics>>.
  242. --
  243. +
  244. .Properties of `analyzed_fields`
  245. [%collapsible%open]
  246. ====
  247. `excludes`:::
  248. (Optional, array)
  249. An array of strings that defines the fields that will be excluded from the
  250. analysis. You do not need to add fields with unsupported data types to
  251. `excludes`, these fields are excluded from the analysis automatically.
  252. `includes`:::
  253. (Optional, array)
  254. An array of strings that defines the fields that will be included in the
  255. analysis.
  256. //End analyzed_fields
  257. ====
  258. `description`::
  259. (Optional, string)
  260. include::{docdir}/ml/ml-shared.asciidoc[tag=description-dfa]
  261. `dest`::
  262. (Required, object)
  263. include::{docdir}/ml/ml-shared.asciidoc[tag=dest]
  264. `model_memory_limit`::
  265. (Optional, string)
  266. The approximate maximum amount of memory resources that are permitted for
  267. analytical processing. The default value for {dfanalytics-jobs} is `1gb`. If
  268. your `elasticsearch.yml` file contains an `xpack.ml.max_model_memory_limit`
  269. setting, an error occurs when you try to create {dfanalytics-jobs} that have
  270. `model_memory_limit` values greater than that setting. For more information, see
  271. <<ml-settings>>.
  272. `source`::
  273. (object)
  274. The configuration of how to source the analysis data. It requires an `index`.
  275. Optionally, `query` and `_source` may be specified.
  276. +
  277. .Properties of `source`
  278. [%collapsible%open]
  279. ====
  280. `index`:::
  281. (Required, string or array) Index or indices on which to perform the analysis.
  282. It can be a single index or index pattern as well as an array of indices or
  283. patterns.
  284. +
  285. WARNING: If your source indices contain documents with the same IDs, only the
  286. document that is indexed last appears in the destination index.
  287. `query`:::
  288. (Optional, object) The {es} query domain-specific language (<<query-dsl,DSL>>).
  289. This value corresponds to the query object in an {es} search POST body. All the
  290. options that are supported by {es} can be used, as this object is passed
  291. verbatim to {es}. By default, this property has the following value:
  292. `{"match_all": {}}`.
  293. `_source`:::
  294. (Optional, object) Specify `includes` and/or `excludes` patterns to select which
  295. fields will be present in the destination. Fields that are excluded cannot be
  296. included in the analysis.
  297. +
  298. .Properties of `_source`
  299. [%collapsible%open]
  300. =====
  301. `includes`::::
  302. (array) An array of strings that defines the fields that will be included in the
  303. destination.
  304. `excludes`::::
  305. (array) An array of strings that defines the fields that will be excluded from
  306. the destination.
  307. =====
  308. ====
  309. [[ml-put-dfanalytics-example]]
  310. ==== {api-examples-title}
  311. [[ml-put-dfanalytics-example-preprocess]]
  312. ===== Preprocessing actions example
  313. The following example shows how to limit the scope of the analysis to certain
  314. fields, specify excluded fields in the destination index, and use a query to
  315. filter your data before analysis.
  316. [source,console]
  317. --------------------------------------------------
  318. PUT _ml/data_frame/analytics/model-flight-delays-pre
  319. {
  320. "source": {
  321. "index": [
  322. "kibana_sample_data_flights" <1>
  323. ],
  324. "query": { <2>
  325. "range": {
  326. "DistanceKilometers": {
  327. "gt": 0
  328. }
  329. }
  330. },
  331. "_source": { <3>
  332. "includes": [],
  333. "excludes": [
  334. "FlightDelay",
  335. "FlightDelayType"
  336. ]
  337. }
  338. },
  339. "dest": { <4>
  340. "index": "df-flight-delays",
  341. "results_field": "ml-results"
  342. },
  343. "analysis": {
  344. "regression": {
  345. "dependent_variable": "FlightDelayMin",
  346. "training_percent": 90
  347. }
  348. },
  349. "analyzed_fields": { <5>
  350. "includes": [],
  351. "excludes": [
  352. "FlightNum"
  353. ]
  354. },
  355. "model_memory_limit": "100mb"
  356. }
  357. --------------------------------------------------
  358. // TEST[skip:setup kibana sample data]
  359. <1> Source index to analyze.
  360. <2> This query filters out entire documents that will not be present in the
  361. destination index.
  362. <3> The `_source` object defines fields in the dataset that will be included or
  363. excluded in the destination index.
  364. <4> Defines the destination index that contains the results of the analysis and
  365. the fields of the source index specified in the `_source` object. Also defines
  366. the name of the `results_field`.
  367. <5> Specifies fields to be included in or excluded from the analysis. This does
  368. not affect whether the fields will be present in the destination index, only
  369. affects whether they are used in the analysis.
  370. In this example, we can see that all the fields of the source index are included
  371. in the destination index except `FlightDelay` and `FlightDelayType` because
  372. these are defined as excluded fields by the `excludes` parameter of the
  373. `_source` object. The `FlightNum` field is included in the destination index,
  374. however it is not included in the analysis because it is explicitly specified as
  375. excluded field by the `excludes` parameter of the `analyzed_fields` object.
  376. [[ml-put-dfanalytics-example-od]]
  377. ===== {oldetection-cap} example
  378. The following example creates the `loganalytics` {dfanalytics-job}, the analysis
  379. type is `outlier_detection`:
  380. [source,console]
  381. --------------------------------------------------
  382. PUT _ml/data_frame/analytics/loganalytics
  383. {
  384. "description": "Outlier detection on log data",
  385. "source": {
  386. "index": "logdata"
  387. },
  388. "dest": {
  389. "index": "logdata_out"
  390. },
  391. "analysis": {
  392. "outlier_detection": {
  393. "compute_feature_influence": true,
  394. "outlier_fraction": 0.05,
  395. "standardization_enabled": true
  396. }
  397. }
  398. }
  399. --------------------------------------------------
  400. // TEST[setup:setup_logdata]
  401. The API returns the following result:
  402. [source,console-result]
  403. ----
  404. {
  405. "id": "loganalytics",
  406. "description": "Outlier detection on log data",
  407. "source": {
  408. "index": ["logdata"],
  409. "query": {
  410. "match_all": {}
  411. }
  412. },
  413. "dest": {
  414. "index": "logdata_out",
  415. "results_field": "ml"
  416. },
  417. "analysis": {
  418. "outlier_detection": {
  419. "compute_feature_influence": true,
  420. "outlier_fraction": 0.05,
  421. "standardization_enabled": true
  422. }
  423. },
  424. "model_memory_limit": "1gb",
  425. "create_time" : 1562265491319,
  426. "version" : "8.0.0",
  427. "allow_lazy_start" : false
  428. }
  429. ----
  430. // TESTRESPONSE[s/1562265491319/$body.$_path/]
  431. // TESTRESPONSE[s/"version" : "8.0.0"/"version" : $body.version/]
  432. [[ml-put-dfanalytics-example-r]]
  433. ===== {regression-cap} examples
  434. The following example creates the `house_price_regression_analysis`
  435. {dfanalytics-job}, the analysis type is `regression`:
  436. [source,console]
  437. --------------------------------------------------
  438. PUT _ml/data_frame/analytics/house_price_regression_analysis
  439. {
  440. "source": {
  441. "index": "houses_sold_last_10_yrs"
  442. },
  443. "dest": {
  444. "index": "house_price_predictions"
  445. },
  446. "analysis":
  447. {
  448. "regression": {
  449. "dependent_variable": "price"
  450. }
  451. }
  452. }
  453. --------------------------------------------------
  454. // TEST[skip:TBD]
  455. The API returns the following result:
  456. [source,console-result]
  457. ----
  458. {
  459. "id" : "house_price_regression_analysis",
  460. "source" : {
  461. "index" : [
  462. "houses_sold_last_10_yrs"
  463. ],
  464. "query" : {
  465. "match_all" : { }
  466. }
  467. },
  468. "dest" : {
  469. "index" : "house_price_predictions",
  470. "results_field" : "ml"
  471. },
  472. "analysis" : {
  473. "regression" : {
  474. "dependent_variable" : "price",
  475. "training_percent" : 100
  476. }
  477. },
  478. "model_memory_limit" : "1gb",
  479. "create_time" : 1567168659127,
  480. "version" : "8.0.0",
  481. "allow_lazy_start" : false
  482. }
  483. ----
  484. // TESTRESPONSE[s/1567168659127/$body.$_path/]
  485. // TESTRESPONSE[s/"version": "8.0.0"/"version": $body.version/]
  486. The following example creates a job and specifies a training percent:
  487. [source,console]
  488. --------------------------------------------------
  489. PUT _ml/data_frame/analytics/student_performance_mathematics_0.3
  490. {
  491. "source": {
  492. "index": "student_performance_mathematics"
  493. },
  494. "dest": {
  495. "index":"student_performance_mathematics_reg"
  496. },
  497. "analysis":
  498. {
  499. "regression": {
  500. "dependent_variable": "G3",
  501. "training_percent": 70, <1>
  502. "randomize_seed": 19673948271 <2>
  503. }
  504. }
  505. }
  506. --------------------------------------------------
  507. // TEST[skip:TBD]
  508. <1> The percentage of the data set that is used for training the model.
  509. <2> The seed that is used to randomly pick which data is used for training.
  510. [[ml-put-dfanalytics-example-c]]
  511. ===== {classification-cap} example
  512. The following example creates the `loan_classification` {dfanalytics-job}, the
  513. analysis type is `classification`:
  514. [source,console]
  515. --------------------------------------------------
  516. PUT _ml/data_frame/analytics/loan_classification
  517. {
  518. "source" : {
  519. "index": "loan-applicants"
  520. },
  521. "dest" : {
  522. "index": "loan-applicants-classified"
  523. },
  524. "analysis" : {
  525. "classification": {
  526. "dependent_variable": "label",
  527. "training_percent": 75,
  528. "num_top_classes": 2
  529. }
  530. }
  531. }
  532. --------------------------------------------------
  533. // TEST[skip:TBD]