put-dfanalytics.asciidoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 index: `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. +
  25. --
  26. NOTE: It is possible that secondary authorization headers are supplied in the
  27. request. If this is the case, the secondary authorization headers are used
  28. instead of the primary headers.
  29. --
  30. [[ml-put-dfanalytics-desc]]
  31. ==== {api-description-title}
  32. This API creates a {dfanalytics-job} that performs an analysis on the source
  33. index and stores the outcome in a destination index.
  34. The destination index will be automatically created if it does not exist. The
  35. `index.number_of_shards` and `index.number_of_replicas` settings of the source
  36. index will be copied over the destination index. When the source index matches
  37. multiple indices, these settings will be set to the maximum values found in the
  38. source indices.
  39. The mappings of the source indices are also attempted to be copied over
  40. to the destination index, however, if the mappings of any of the fields don't
  41. match among the source indices, the attempt will fail with an error message.
  42. If the destination index already exists, then it will be use as is. This makes
  43. it possible to set up the destination index in advance with custom settings
  44. and mappings.
  45. [discrete]
  46. [[ml-hyperparam-optimization]]
  47. ===== Hyperparameter optimization
  48. If you don't supply {regression} or {classification} parameters, _hyperparameter
  49. optimization_ occurs, which sets a value for the undefined parameters. The
  50. starting point is calculated for data dependent parameters by examining the loss
  51. on the training data. Subject to the size constraint, this operation provides an
  52. upper bound on the improvement in validation loss.
  53. A fixed number of rounds is used for optimization which depends on the number of
  54. parameters being optimized. The optimization starts with random search, then
  55. Bayesian optimization is performed that is targeting maximum expected
  56. improvement. If you override any parameters by explicitely setting it, the
  57. optimization calculates the value of the remaining parameters accordingly and
  58. uses the value you provided for the overridden parameter. The number of rounds
  59. are reduced respectively. The validation error is estimated in each round by
  60. using 4-fold cross validation.
  61. [[ml-put-dfanalytics-path-params]]
  62. ==== {api-path-parms-title}
  63. `<data_frame_analytics_id>`::
  64. (Required, string)
  65. include::{docdir}/ml/ml-shared.asciidoc[tag=job-id-data-frame-analytics-define]
  66. [role="child_attributes"]
  67. [[ml-put-dfanalytics-request-body]]
  68. ==== {api-request-body-title}
  69. `allow_lazy_start`::
  70. (Optional, boolean)
  71. include::{docdir}/ml/ml-shared.asciidoc[tag=allow-lazy-start]
  72. //Begin analysis
  73. `analysis`::
  74. (Required, object)
  75. The analysis configuration, which contains the information necessary to perform
  76. one of the following types of analysis: {classification}, {oldetection}, or
  77. {regression}.
  78. +
  79. .Properties of `analysis`
  80. [%collapsible%open]
  81. ====
  82. //Begin classification
  83. `classification`:::
  84. (Required^*^, object)
  85. The configuration information necessary to perform
  86. {ml-docs}/dfa-classification.html[{classification}].
  87. +
  88. TIP: Advanced parameters are for fine-tuning {classanalysis}. They are set
  89. automatically by <<ml-hyperparam-optimization,hyperparameter optimization>>
  90. to give minimum validation error. It is highly recommended to use the default
  91. values unless you fully understand the function of these parameters.
  92. +
  93. .Properties of `classification`
  94. [%collapsible%open]
  95. =====
  96. `class_assignment_objective`::::
  97. (Optional, string)
  98. include::{docdir}/ml/ml-shared.asciidoc[tag=class-assignment-objective]
  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`, `keyword`, `text`), or boolean.
  106. `eta`::::
  107. (Optional, double)
  108. include::{docdir}/ml/ml-shared.asciidoc[tag=eta]
  109. `feature_bag_fraction`::::
  110. (Optional, double)
  111. include::{docdir}/ml/ml-shared.asciidoc[tag=feature-bag-fraction]
  112. `gamma`::::
  113. (Optional, double)
  114. include::{docdir}/ml/ml-shared.asciidoc[tag=gamma]
  115. `lambda`::::
  116. (Optional, double)
  117. include::{docdir}/ml/ml-shared.asciidoc[tag=lambda]
  118. `max_trees`::::
  119. (Optional, integer)
  120. include::{docdir}/ml/ml-shared.asciidoc[tag=max-trees]
  121. `num_top_classes`::::
  122. (Optional, integer)
  123. include::{docdir}/ml/ml-shared.asciidoc[tag=num-top-classes]
  124. `num_top_feature_importance_values`::::
  125. (Optional, integer)
  126. Advanced configuration option. Specifies the maximum number of
  127. {ml-docs}/dfa-classification.html#dfa-classification-feature-importance[feature
  128. importance] values per document to return. By default, it is zero and no feature importance
  129. calculation occurs.
  130. `prediction_field_name`::::
  131. (Optional, string)
  132. include::{docdir}/ml/ml-shared.asciidoc[tag=prediction-field-name]
  133. `randomize_seed`::::
  134. (Optional, long)
  135. include::{docdir}/ml/ml-shared.asciidoc[tag=randomize-seed]
  136. `training_percent`::::
  137. (Optional, integer)
  138. include::{docdir}/ml/ml-shared.asciidoc[tag=training-percent]
  139. //End classification
  140. =====
  141. //Begin outlier_detection
  142. `outlier_detection`:::
  143. (Required^*^, object)
  144. The configuration information necessary to perform
  145. {ml-docs}/dfa-outlier-detection.html[{oldetection}]:
  146. +
  147. .Properties of `outlier_detection`
  148. [%collapsible%open]
  149. =====
  150. `compute_feature_influence`::::
  151. (Optional, boolean)
  152. include::{docdir}/ml/ml-shared.asciidoc[tag=compute-feature-influence]
  153. `feature_influence_threshold`::::
  154. (Optional, double)
  155. include::{docdir}/ml/ml-shared.asciidoc[tag=feature-influence-threshold]
  156. `method`::::
  157. (Optional, string)
  158. include::{docdir}/ml/ml-shared.asciidoc[tag=method]
  159. `n_neighbors`::::
  160. (Optional, integer)
  161. include::{docdir}/ml/ml-shared.asciidoc[tag=n-neighbors]
  162. `outlier_fraction`::::
  163. (Optional, double)
  164. include::{docdir}/ml/ml-shared.asciidoc[tag=outlier-fraction]
  165. `standardization_enabled`::::
  166. (Optional, boolean)
  167. include::{docdir}/ml/ml-shared.asciidoc[tag=standardization-enabled]
  168. //End outlier_detection
  169. =====
  170. //Begin regression
  171. `regression`:::
  172. (Required^*^, object)
  173. The configuration information necessary to perform
  174. {ml-docs}/dfa-regression.html[{regression}].
  175. +
  176. TIP: Advanced parameters are for fine-tuning {reganalysis}. They are set
  177. automatically by <<ml-hyperparam-optimization,hyperparameter optimization>>
  178. to give minimum validation error. It is highly recommended to use the default
  179. values unless you fully understand the function of these parameters.
  180. +
  181. .Properties of `regression`
  182. [%collapsible%open]
  183. =====
  184. `dependent_variable`::::
  185. (Required, string)
  186. +
  187. include::{docdir}/ml/ml-shared.asciidoc[tag=dependent-variable]
  188. +
  189. The data type of the field must be numeric.
  190. `eta`::::
  191. (Optional, double)
  192. include::{docdir}/ml/ml-shared.asciidoc[tag=eta]
  193. `feature_bag_fraction`::::
  194. (Optional, double)
  195. include::{docdir}/ml/ml-shared.asciidoc[tag=feature-bag-fraction]
  196. `gamma`::::
  197. (Optional, double)
  198. include::{docdir}/ml/ml-shared.asciidoc[tag=gamma]
  199. `lambda`::::
  200. (Optional, double)
  201. include::{docdir}/ml/ml-shared.asciidoc[tag=lambda]
  202. `max_trees`::::
  203. (Optional, integer)
  204. include::{docdir}/ml/ml-shared.asciidoc[tag=max-trees]
  205. `num_top_feature_importance_values`::::
  206. (Optional, integer)
  207. Advanced configuration option. Specifies the maximum number of
  208. {ml-docs}/dfa-regression.html#dfa-regression-feature-importance[feature importance]
  209. values per document to return. By default, it is zero and no feature importance
  210. calculation occurs.
  211. `prediction_field_name`::::
  212. (Optional, string)
  213. include::{docdir}/ml/ml-shared.asciidoc[tag=prediction-field-name]
  214. `randomize_seed`::::
  215. (Optional, long)
  216. include::{docdir}/ml/ml-shared.asciidoc[tag=randomize-seed]
  217. `training_percent`::::
  218. (Optional, integer)
  219. include::{docdir}/ml/ml-shared.asciidoc[tag=training-percent]
  220. =====
  221. //End regression
  222. ====
  223. //End analysis
  224. //Begin analyzed_fields
  225. `analyzed_fields`::
  226. (Optional, object)
  227. include::{docdir}/ml/ml-shared.asciidoc[tag=analyzed-fields]
  228. +
  229. .Properties of `analyzed_fields`
  230. [%collapsible%open]
  231. ====
  232. `excludes`:::
  233. (Optional, array)
  234. include::{docdir}/ml/ml-shared.asciidoc[tag=analyzed-fields-excludes]
  235. `includes`:::
  236. (Optional, array)
  237. include::{docdir}/ml/ml-shared.asciidoc[tag=analyzed-fields-includes]
  238. //End analyzed_fields
  239. ====
  240. `description`::
  241. (Optional, string)
  242. include::{docdir}/ml/ml-shared.asciidoc[tag=description-dfa]
  243. `dest`::
  244. (Required, object)
  245. include::{docdir}/ml/ml-shared.asciidoc[tag=dest]
  246. `model_memory_limit`::
  247. (Optional, string)
  248. include::{docdir}/ml/ml-shared.asciidoc[tag=model-memory-limit-dfa]
  249. `source`::
  250. (object)
  251. include::{docdir}/ml/ml-shared.asciidoc[tag=source-put-dfa]
  252. [[ml-put-dfanalytics-example]]
  253. ==== {api-examples-title}
  254. [[ml-put-dfanalytics-example-preprocess]]
  255. ===== Preprocessing actions example
  256. The following example shows how to limit the scope of the analysis to certain
  257. fields, specify excluded fields in the destination index, and use a query to
  258. filter your data before analysis.
  259. [source,console]
  260. --------------------------------------------------
  261. PUT _ml/data_frame/analytics/model-flight-delays-pre
  262. {
  263. "source": {
  264. "index": [
  265. "kibana_sample_data_flights" <1>
  266. ],
  267. "query": { <2>
  268. "range": {
  269. "DistanceKilometers": {
  270. "gt": 0
  271. }
  272. }
  273. },
  274. "_source": { <3>
  275. "includes": [],
  276. "excludes": [
  277. "FlightDelay",
  278. "FlightDelayType"
  279. ]
  280. }
  281. },
  282. "dest": { <4>
  283. "index": "df-flight-delays",
  284. "results_field": "ml-results"
  285. },
  286. "analysis": {
  287. "regression": {
  288. "dependent_variable": "FlightDelayMin",
  289. "training_percent": 90
  290. }
  291. },
  292. "analyzed_fields": { <5>
  293. "includes": [],
  294. "excludes": [
  295. "FlightNum"
  296. ]
  297. },
  298. "model_memory_limit": "100mb"
  299. }
  300. --------------------------------------------------
  301. // TEST[skip:setup kibana sample data]
  302. <1> The source index to analyze.
  303. <2> This query filters out entire documents that will not be present in the
  304. destination index.
  305. <3> The `_source` object defines fields in the dataset that will be included or
  306. excluded in the destination index. In this case, `includes` does not specify any
  307. fields, so the default behavior takes place: all the fields of the source index
  308. will included except the ones that are explicitly specified in `excludes`.
  309. <4> Defines the destination index that contains the results of the analysis and
  310. the fields of the source index specified in the `_source` object. Also defines
  311. the name of the `results_field`.
  312. <5> Specifies fields to be included in or excluded from the analysis. This does
  313. not affect whether the fields will be present in the destination index, only
  314. affects whether they are used in the analysis.
  315. In this example, we can see that all the fields of the source index are included
  316. in the destination index except `FlightDelay` and `FlightDelayType` because
  317. these are defined as excluded fields by the `excludes` parameter of the
  318. `_source` object. The `FlightNum` field is included in the destination index,
  319. however it is not included in the analysis because it is explicitly specified as
  320. excluded field by the `excludes` parameter of the `analyzed_fields` object.
  321. [[ml-put-dfanalytics-example-od]]
  322. ===== {oldetection-cap} example
  323. The following example creates the `loganalytics` {dfanalytics-job}, the analysis
  324. type is `outlier_detection`:
  325. [source,console]
  326. --------------------------------------------------
  327. PUT _ml/data_frame/analytics/loganalytics
  328. {
  329. "description": "Outlier detection on log data",
  330. "source": {
  331. "index": "logdata"
  332. },
  333. "dest": {
  334. "index": "logdata_out"
  335. },
  336. "analysis": {
  337. "outlier_detection": {
  338. "compute_feature_influence": true,
  339. "outlier_fraction": 0.05,
  340. "standardization_enabled": true
  341. }
  342. }
  343. }
  344. --------------------------------------------------
  345. // TEST[setup:setup_logdata]
  346. The API returns the following result:
  347. [source,console-result]
  348. ----
  349. {
  350. "id": "loganalytics",
  351. "description": "Outlier detection on log data",
  352. "source": {
  353. "index": ["logdata"],
  354. "query": {
  355. "match_all": {}
  356. }
  357. },
  358. "dest": {
  359. "index": "logdata_out",
  360. "results_field": "ml"
  361. },
  362. "analysis": {
  363. "outlier_detection": {
  364. "compute_feature_influence": true,
  365. "outlier_fraction": 0.05,
  366. "standardization_enabled": true
  367. }
  368. },
  369. "model_memory_limit": "1gb",
  370. "create_time" : 1562265491319,
  371. "version" : "8.0.0",
  372. "allow_lazy_start" : false
  373. }
  374. ----
  375. // TESTRESPONSE[s/1562265491319/$body.$_path/]
  376. // TESTRESPONSE[s/"version" : "8.0.0"/"version" : $body.version/]
  377. [[ml-put-dfanalytics-example-r]]
  378. ===== {regression-cap} examples
  379. The following example creates the `house_price_regression_analysis`
  380. {dfanalytics-job}, the analysis type is `regression`:
  381. [source,console]
  382. --------------------------------------------------
  383. PUT _ml/data_frame/analytics/house_price_regression_analysis
  384. {
  385. "source": {
  386. "index": "houses_sold_last_10_yrs"
  387. },
  388. "dest": {
  389. "index": "house_price_predictions"
  390. },
  391. "analysis":
  392. {
  393. "regression": {
  394. "dependent_variable": "price"
  395. }
  396. }
  397. }
  398. --------------------------------------------------
  399. // TEST[skip:TBD]
  400. The API returns the following result:
  401. [source,console-result]
  402. ----
  403. {
  404. "id" : "house_price_regression_analysis",
  405. "source" : {
  406. "index" : [
  407. "houses_sold_last_10_yrs"
  408. ],
  409. "query" : {
  410. "match_all" : { }
  411. }
  412. },
  413. "dest" : {
  414. "index" : "house_price_predictions",
  415. "results_field" : "ml"
  416. },
  417. "analysis" : {
  418. "regression" : {
  419. "dependent_variable" : "price",
  420. "training_percent" : 100
  421. }
  422. },
  423. "model_memory_limit" : "1gb",
  424. "create_time" : 1567168659127,
  425. "version" : "8.0.0",
  426. "allow_lazy_start" : false
  427. }
  428. ----
  429. // TESTRESPONSE[s/1567168659127/$body.$_path/]
  430. // TESTRESPONSE[s/"version": "8.0.0"/"version": $body.version/]
  431. The following example creates a job and specifies a training percent:
  432. [source,console]
  433. --------------------------------------------------
  434. PUT _ml/data_frame/analytics/student_performance_mathematics_0.3
  435. {
  436. "source": {
  437. "index": "student_performance_mathematics"
  438. },
  439. "dest": {
  440. "index":"student_performance_mathematics_reg"
  441. },
  442. "analysis":
  443. {
  444. "regression": {
  445. "dependent_variable": "G3",
  446. "training_percent": 70, <1>
  447. "randomize_seed": 19673948271 <2>
  448. }
  449. }
  450. }
  451. --------------------------------------------------
  452. // TEST[skip:TBD]
  453. <1> The `training_percent` defines the percentage of the data set that will be
  454. used for training the model.
  455. <2> The `randomize_seed` is the seed used to randomly pick which data is used
  456. for training.
  457. [[ml-put-dfanalytics-example-c]]
  458. ===== {classification-cap} example
  459. The following example creates the `loan_classification` {dfanalytics-job}, the
  460. analysis type is `classification`:
  461. [source,console]
  462. --------------------------------------------------
  463. PUT _ml/data_frame/analytics/loan_classification
  464. {
  465. "source" : {
  466. "index": "loan-applicants"
  467. },
  468. "dest" : {
  469. "index": "loan-applicants-classified"
  470. },
  471. "analysis" : {
  472. "classification": {
  473. "dependent_variable": "label",
  474. "training_percent": 75,
  475. "num_top_classes": 2
  476. }
  477. }
  478. }
  479. --------------------------------------------------
  480. // TEST[skip:TBD]