put-dfanalytics.asciidoc 15 KB

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