put-dfanalytics.asciidoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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`.`maximum_number_trees`::::
  97. (Optional, integer)
  98. include::{docdir}/ml/ml-shared.asciidoc[tag=maximum-number-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`.`num_top_classes`::::
  106. (Optional, integer)
  107. include::{docdir}/ml/ml-shared.asciidoc[tag=num-top-classes]
  108. `analysis`.`classification`.`prediction_field_name`::::
  109. (Optional, string)
  110. include::{docdir}/ml/ml-shared.asciidoc[tag=prediction-field-name]
  111. `analysis`.`classification`.`randomize_seed`::::
  112. (Optional, long)
  113. include::{docdir}/ml/ml-shared.asciidoc[tag=randomize-seed]
  114. `analysis`.`classification`.`num_top_feature_importance_values`::::
  115. (Optional, integer)
  116. Advanced configuration option. Specifies the maximum number of
  117. {ml-docs}/dfa-classification.html#dfa-classification-feature-importance[feature
  118. importance] values per document to return. By default, it is zero and no feature importance
  119. calculation occurs.
  120. `analysis`.`classification`.`training_percent`::::
  121. (Optional, integer)
  122. include::{docdir}/ml/ml-shared.asciidoc[tag=training-percent]
  123. `analysis`.`outlier_detection`:::
  124. (Required^*^, object)
  125. The configuration information necessary to perform
  126. {ml-docs}/dfa-outlier-detection.html[{oldetection}]:
  127. `analysis`.`outlier_detection`.`compute_feature_influence`::::
  128. (Optional, boolean)
  129. include::{docdir}/ml/ml-shared.asciidoc[tag=compute-feature-influence]
  130. `analysis`.`outlier_detection`.`feature_influence_threshold`::::
  131. (Optional, double)
  132. include::{docdir}/ml/ml-shared.asciidoc[tag=feature-influence-threshold]
  133. `analysis`.`outlier_detection`.`method`::::
  134. (Optional, string)
  135. include::{docdir}/ml/ml-shared.asciidoc[tag=method]
  136. `analysis`.`outlier_detection`.`n_neighbors`::::
  137. (Optional, integer)
  138. include::{docdir}/ml/ml-shared.asciidoc[tag=n-neighbors]
  139. `analysis`.`outlier_detection`.`outlier_fraction`::::
  140. (Optional, double)
  141. include::{docdir}/ml/ml-shared.asciidoc[tag=outlier-fraction]
  142. `analysis`.`outlier_detection`.`standardization_enabled`::::
  143. (Optional, boolean)
  144. include::{docdir}/ml/ml-shared.asciidoc[tag=standardization-enabled]
  145. `analysis`.`regression`:::
  146. (Required^*^, object)
  147. The configuration information necessary to perform
  148. {ml-docs}/dfa-regression.html[{regression}].
  149. +
  150. --
  151. TIP: Advanced parameters are for fine-tuning {reganalysis}. They are set
  152. automatically by <<ml-hyperparam-optimization,hyperparameter optimization>>
  153. to give minimum validation error. It is highly recommended to use the default
  154. values unless you fully understand the function of these parameters.
  155. --
  156. `analysis`.`regression`.`dependent_variable`::::
  157. (Required, string)
  158. +
  159. --
  160. include::{docdir}/ml/ml-shared.asciidoc[tag=dependent-variable]
  161. The data type of the field must be numeric.
  162. --
  163. `analysis`.`regression`.`eta`::::
  164. (Optional, double)
  165. include::{docdir}/ml/ml-shared.asciidoc[tag=eta]
  166. `analysis`.`regression`.`feature_bag_fraction`::::
  167. (Optional, double)
  168. include::{docdir}/ml/ml-shared.asciidoc[tag=feature-bag-fraction]
  169. `analysis`.`regression`.`maximum_number_trees`::::
  170. (Optional, integer)
  171. include::{docdir}/ml/ml-shared.asciidoc[tag=maximum-number-trees]
  172. `analysis`.`regression`.`gamma`::::
  173. (Optional, double)
  174. include::{docdir}/ml/ml-shared.asciidoc[tag=gamma]
  175. `analysis`.`regression`.`lambda`::::
  176. (Optional, double)
  177. include::{docdir}/ml/ml-shared.asciidoc[tag=lambda]
  178. `analysis`.`regression`.`prediction_field_name`::::
  179. (Optional, string)
  180. include::{docdir}/ml/ml-shared.asciidoc[tag=prediction-field-name]
  181. `analysis`.`regression`.`num_top_feature_importance_values`::::
  182. (Optional, integer)
  183. Advanced configuration option. Specifies the maximum number of
  184. {ml-docs}/dfa-regression.html#dfa-regression-feature-importance[feature importance]
  185. values per document to return. By default, it is zero and no feature importance calculation
  186. occurs.
  187. `analysis`.`regression`.`training_percent`::::
  188. (Optional, integer)
  189. include::{docdir}/ml/ml-shared.asciidoc[tag=training-percent]
  190. `analysis`.`regression`.`randomize_seed`::::
  191. (Optional, long)
  192. include::{docdir}/ml/ml-shared.asciidoc[tag=randomize-seed]
  193. `analyzed_fields`::
  194. (Optional, object)
  195. include::{docdir}/ml/ml-shared.asciidoc[tag=analyzed-fields]
  196. `analyzed_fields`.`excludes`:::
  197. (Optional, array)
  198. include::{docdir}/ml/ml-shared.asciidoc[tag=analyzed-fields-excludes]
  199. `analyzed_fields`.`includes`:::
  200. (Optional, array)
  201. include::{docdir}/ml/ml-shared.asciidoc[tag=analyzed-fields-includes]
  202. `description`::
  203. (Optional, string)
  204. include::{docdir}/ml/ml-shared.asciidoc[tag=description-dfa]
  205. `dest`::
  206. (Required, object)
  207. include::{docdir}/ml/ml-shared.asciidoc[tag=dest]
  208. `model_memory_limit`::
  209. (Optional, string)
  210. include::{docdir}/ml/ml-shared.asciidoc[tag=model-memory-limit-dfa]
  211. `source`::
  212. (object)
  213. include::{docdir}/ml/ml-shared.asciidoc[tag=source-put-dfa]
  214. [[ml-put-dfanalytics-example]]
  215. ==== {api-examples-title}
  216. [[ml-put-dfanalytics-example-preprocess]]
  217. ===== Preprocessing actions example
  218. The following example shows how to limit the scope of the analysis to certain
  219. fields, specify excluded fields in the destination index, and use a query to
  220. filter your data before analysis.
  221. [source,console]
  222. --------------------------------------------------
  223. PUT _ml/data_frame/analytics/model-flight-delays-pre
  224. {
  225. "source": {
  226. "index": [
  227. "kibana_sample_data_flights" <1>
  228. ],
  229. "query": { <2>
  230. "range": {
  231. "DistanceKilometers": {
  232. "gt": 0
  233. }
  234. }
  235. },
  236. "_source": { <3>
  237. "includes": [],
  238. "excludes": [
  239. "FlightDelay",
  240. "FlightDelayType"
  241. ]
  242. }
  243. },
  244. "dest": { <4>
  245. "index": "df-flight-delays",
  246. "results_field": "ml-results"
  247. },
  248. "analysis": {
  249. "regression": {
  250. "dependent_variable": "FlightDelayMin",
  251. "training_percent": 90
  252. }
  253. },
  254. "analyzed_fields": { <5>
  255. "includes": [],
  256. "excludes": [
  257. "FlightNum"
  258. ]
  259. },
  260. "model_memory_limit": "100mb"
  261. }
  262. --------------------------------------------------
  263. // TEST[skip:setup kibana sample data]
  264. <1> The source index to analyze.
  265. <2> This query filters out entire documents that will not be present in the
  266. destination index.
  267. <3> The `_source` object defines fields in the dataset that will be included or
  268. excluded in the destination index. In this case, `includes` does not specify any
  269. fields, so the default behavior takes place: all the fields of the source index
  270. will included except the ones that are explicitly specified in `excludes`.
  271. <4> Defines the destination index that contains the results of the analysis and
  272. the fields of the source index specified in the `_source` object. Also defines
  273. the name of the `results_field`.
  274. <5> Specifies fields to be included in or excluded from the analysis. This does
  275. not affect whether the fields will be present in the destination index, only
  276. affects whether they are used in the analysis.
  277. In this example, we can see that all the fields of the source index are included
  278. in the destination index except `FlightDelay` and `FlightDelayType` because
  279. these are defined as excluded fields by the `excludes` parameter of the
  280. `_source` object. The `FlightNum` field is included in the destination index,
  281. however it is not included in the analysis because it is explicitly specified as
  282. excluded field by the `excludes` parameter of the `analyzed_fields` object.
  283. [[ml-put-dfanalytics-example-od]]
  284. ===== {oldetection-cap} example
  285. The following example creates the `loganalytics` {dfanalytics-job}, the analysis
  286. type is `outlier_detection`:
  287. [source,console]
  288. --------------------------------------------------
  289. PUT _ml/data_frame/analytics/loganalytics
  290. {
  291. "description": "Outlier detection on log data",
  292. "source": {
  293. "index": "logdata"
  294. },
  295. "dest": {
  296. "index": "logdata_out"
  297. },
  298. "analysis": {
  299. "outlier_detection": {
  300. "compute_feature_influence": true,
  301. "outlier_fraction": 0.05,
  302. "standardization_enabled": true
  303. }
  304. }
  305. }
  306. --------------------------------------------------
  307. // TEST[setup:setup_logdata]
  308. The API returns the following result:
  309. [source,console-result]
  310. ----
  311. {
  312. "id": "loganalytics",
  313. "description": "Outlier detection on log data",
  314. "source": {
  315. "index": ["logdata"],
  316. "query": {
  317. "match_all": {}
  318. }
  319. },
  320. "dest": {
  321. "index": "logdata_out",
  322. "results_field": "ml"
  323. },
  324. "analysis": {
  325. "outlier_detection": {
  326. "compute_feature_influence": true,
  327. "outlier_fraction": 0.05,
  328. "standardization_enabled": true
  329. }
  330. },
  331. "model_memory_limit": "1gb",
  332. "create_time" : 1562265491319,
  333. "version" : "8.0.0",
  334. "allow_lazy_start" : false
  335. }
  336. ----
  337. // TESTRESPONSE[s/1562265491319/$body.$_path/]
  338. // TESTRESPONSE[s/"version" : "8.0.0"/"version" : $body.version/]
  339. [[ml-put-dfanalytics-example-r]]
  340. ===== {regression-cap} examples
  341. The following example creates the `house_price_regression_analysis`
  342. {dfanalytics-job}, the analysis type is `regression`:
  343. [source,console]
  344. --------------------------------------------------
  345. PUT _ml/data_frame/analytics/house_price_regression_analysis
  346. {
  347. "source": {
  348. "index": "houses_sold_last_10_yrs"
  349. },
  350. "dest": {
  351. "index": "house_price_predictions"
  352. },
  353. "analysis":
  354. {
  355. "regression": {
  356. "dependent_variable": "price"
  357. }
  358. }
  359. }
  360. --------------------------------------------------
  361. // TEST[skip:TBD]
  362. The API returns the following result:
  363. [source,console-result]
  364. ----
  365. {
  366. "id" : "house_price_regression_analysis",
  367. "source" : {
  368. "index" : [
  369. "houses_sold_last_10_yrs"
  370. ],
  371. "query" : {
  372. "match_all" : { }
  373. }
  374. },
  375. "dest" : {
  376. "index" : "house_price_predictions",
  377. "results_field" : "ml"
  378. },
  379. "analysis" : {
  380. "regression" : {
  381. "dependent_variable" : "price",
  382. "training_percent" : 100
  383. }
  384. },
  385. "model_memory_limit" : "1gb",
  386. "create_time" : 1567168659127,
  387. "version" : "8.0.0",
  388. "allow_lazy_start" : false
  389. }
  390. ----
  391. // TESTRESPONSE[s/1567168659127/$body.$_path/]
  392. // TESTRESPONSE[s/"version": "8.0.0"/"version": $body.version/]
  393. The following example creates a job and specifies a training percent:
  394. [source,console]
  395. --------------------------------------------------
  396. PUT _ml/data_frame/analytics/student_performance_mathematics_0.3
  397. {
  398. "source": {
  399. "index": "student_performance_mathematics"
  400. },
  401. "dest": {
  402. "index":"student_performance_mathematics_reg"
  403. },
  404. "analysis":
  405. {
  406. "regression": {
  407. "dependent_variable": "G3",
  408. "training_percent": 70, <1>
  409. "randomize_seed": 19673948271 <2>
  410. }
  411. }
  412. }
  413. --------------------------------------------------
  414. // TEST[skip:TBD]
  415. <1> The `training_percent` defines the percentage of the data set that will be
  416. used for training the model.
  417. <2> The `randomize_seed` is the seed used to randomly pick which data is used
  418. for training.
  419. [[ml-put-dfanalytics-example-c]]
  420. ===== {classification-cap} example
  421. The following example creates the `loan_classification` {dfanalytics-job}, the
  422. analysis type is `classification`:
  423. [source,console]
  424. --------------------------------------------------
  425. PUT _ml/data_frame/analytics/loan_classification
  426. {
  427. "source" : {
  428. "index": "loan-applicants"
  429. },
  430. "dest" : {
  431. "index": "loan-applicants-classified"
  432. },
  433. "analysis" : {
  434. "classification": {
  435. "dependent_variable": "label",
  436. "training_percent": 75,
  437. "num_top_classes": 2
  438. }
  439. }
  440. }
  441. --------------------------------------------------
  442. // TEST[skip:TBD]