put-dfanalytics.asciidoc 16 KB

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