put-dfanalytics.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. * You must have `machine_learning_admin` built-in role to use this API. You must
  17. also have `read` and `view_index_metadata` privileges on the source index and
  18. `read`, `create_index`, and `index` privileges on the destination index. For
  19. more information, see <<security-privileges>> and <<built-in-roles>>.
  20. [[ml-put-dfanalytics-desc]]
  21. ==== {api-description-title}
  22. This API creates a {dfanalytics-job} that performs an analysis on the source
  23. index and stores the outcome in a destination index.
  24. The destination index will be automatically created if it does not exist. The
  25. `index.number_of_shards` and `index.number_of_replicas` settings of the source
  26. index will be copied over the destination index. When the source index matches
  27. multiple indices, these settings will be set to the maximum values found in the
  28. source indices.
  29. The mappings of the source indices are also attempted to be copied over
  30. to the destination index, however, if the mappings of any of the fields don't
  31. match among the source indices, the attempt will fail with an error message.
  32. If the destination index already exists, then it will be use as is. This makes
  33. it possible to set up the destination index in advance with custom settings
  34. and mappings.
  35. [[ml-put-dfanalytics-supported-fields]]
  36. ===== Supported fields
  37. ====== {oldetection-cap}
  38. {oldetection-cap} requires numeric or boolean data to analyze. The algorithms
  39. don't support missing values therefore fields that have data types other than
  40. numeric or boolean are ignored. Documents where included fields contain missing
  41. values, null values, or an array are also ignored. Therefore the `dest` index
  42. may contain documents that don't have an {olscore}.
  43. ====== {regression-cap}
  44. {regression-cap} supports fields that are numeric, `boolean`, `text`, `keyword`,
  45. and `ip`. It is also tolerant of missing values. Fields that are supported are
  46. included in the analysis, other fields are ignored. Documents where included
  47. fields contain an array with two or more values are also ignored. Documents in
  48. the `dest` index that don’t contain a results field are not included in the
  49. {reganalysis}.
  50. ====== {classification-cap}
  51. {classification-cap} supports fields that are numeric, `boolean`, `text`,
  52. `keyword`, and `ip`. It is also tolerant of missing values. Fields that are
  53. supported are included in the analysis, other fields are ignored. Documents
  54. where included fields contain an array with two or more values are also ignored.
  55. Documents in the `dest` index that don’t contain a results field are not
  56. included in the {classanalysis}.
  57. {classanalysis-cap} can be improved by mapping ordinal variable values to a
  58. single number. For example, in case of age ranges, you can model the values as
  59. "0-14" = 0, "15-24" = 1, "25-34" = 2, and so on.
  60. [[ml-put-dfanalytics-path-params]]
  61. ==== {api-path-parms-title}
  62. `<data_frame_analytics_id>`::
  63. (Required, string)
  64. include::{docdir}/ml/ml-shared.asciidoc[tag=job-id-data-frame-analytics-define]
  65. [[ml-put-dfanalytics-request-body]]
  66. ==== {api-request-body-title}
  67. `analysis`::
  68. (Required, object)
  69. include::{docdir}/ml/ml-shared.asciidoc[tag=analysis]
  70. `analyzed_fields`::
  71. (Optional, object)
  72. include::{docdir}/ml/ml-shared.asciidoc[tag=analyzed-fields]
  73. [source,console]
  74. --------------------------------------------------
  75. PUT _ml/data_frame/analytics/loganalytics
  76. {
  77. "source": {
  78. "index": "logdata"
  79. },
  80. "dest": {
  81. "index": "logdata_out"
  82. },
  83. "analysis": {
  84. "outlier_detection": {
  85. }
  86. },
  87. "analyzed_fields": {
  88. "includes": [ "request.bytes", "response.counts.error" ],
  89. "excludes": [ "source.geo" ]
  90. }
  91. }
  92. --------------------------------------------------
  93. // TEST[setup:setup_logdata]
  94. `description`::
  95. (Optional, string)
  96. include::{docdir}/ml/ml-shared.asciidoc[tag=description-dfa]
  97. `dest`::
  98. (Required, object)
  99. include::{docdir}/ml/ml-shared.asciidoc[tag=dest]
  100. `model_memory_limit`::
  101. (Optional, string)
  102. include::{docdir}/ml/ml-shared.asciidoc[tag=model-memory-limit-dfa]
  103. `source`::
  104. (object)
  105. include::{docdir}/ml/ml-shared.asciidoc[tag=source-put-dfa]
  106. `allow_lazy_start`::
  107. (Optional, boolean)
  108. include::{docdir}/ml/ml-shared.asciidoc[tag=allow-lazy-start]
  109. [[ml-put-dfanalytics-example]]
  110. ==== {api-examples-title}
  111. [[ml-put-dfanalytics-example-preprocess]]
  112. ===== Preprocessing actions example
  113. The following example shows how to limit the scope of the analysis to certain
  114. fields, specify excluded fields in the destination index, and use a query to
  115. filter your data before analysis.
  116. [source,console]
  117. --------------------------------------------------
  118. PUT _ml/data_frame/analytics/model-flight-delays-pre
  119. {
  120. "source": {
  121. "index": [
  122. "kibana_sample_data_flights" <1>
  123. ],
  124. "query": { <2>
  125. "range": {
  126. "DistanceKilometers": {
  127. "gt": 0
  128. }
  129. }
  130. },
  131. "_source": { <3>
  132. "includes": [],
  133. "excludes": [
  134. "FlightDelay",
  135. "FlightDelayType"
  136. ]
  137. }
  138. },
  139. "dest": { <4>
  140. "index": "df-flight-delays",
  141. "results_field": "ml-results"
  142. },
  143. "analysis": {
  144. "regression": {
  145. "dependent_variable": "FlightDelayMin",
  146. "training_percent": 90
  147. }
  148. },
  149. "analyzed_fields": { <5>
  150. "includes": [],
  151. "excludes": [
  152. "FlightNum"
  153. ]
  154. },
  155. "model_memory_limit": "100mb"
  156. }
  157. --------------------------------------------------
  158. // TEST[skip:setup kibana sample data]
  159. <1> The source index to analyze.
  160. <2> This query filters out entire documents that will not be present in the
  161. destination index.
  162. <3> The `_source` object defines fields in the dataset that will be included or
  163. excluded in the destination index. In this case, `includes` does not specify any
  164. fields, so the default behavior takes place: all the fields of the source index
  165. will included except the ones that are explicitly specified in `excludes`.
  166. <4> Defines the destination index that contains the results of the analysis and
  167. the fields of the source index specified in the `_source` object. Also defines
  168. the name of the `results_field`.
  169. <5> Specifies fields to be included in or excluded from the analysis. This does
  170. not affect whether the fields will be present in the destination index, only
  171. affects whether they are used in the analysis.
  172. In this example, we can see that all the fields of the source index are included
  173. in the destination index except `FlightDelay` and `FlightDelayType` because
  174. these are defined as excluded fields by the `excludes` parameter of the
  175. `_source` object. The `FlightNum` field is included in the destination index,
  176. however it is not included in the analysis because it is explicitly specified as
  177. excluded field by the `excludes` parameter of the `analyzed_fields` object.
  178. [[ml-put-dfanalytics-example-od]]
  179. ===== {oldetection-cap} example
  180. The following example creates the `loganalytics` {dfanalytics-job}, the analysis
  181. type is `outlier_detection`:
  182. [source,console]
  183. --------------------------------------------------
  184. PUT _ml/data_frame/analytics/loganalytics
  185. {
  186. "description": "Outlier detection on log data",
  187. "source": {
  188. "index": "logdata"
  189. },
  190. "dest": {
  191. "index": "logdata_out"
  192. },
  193. "analysis": {
  194. "outlier_detection": {
  195. "compute_feature_influence": true,
  196. "outlier_fraction": 0.05,
  197. "standardization_enabled": true
  198. }
  199. }
  200. }
  201. --------------------------------------------------
  202. // TEST[setup:setup_logdata]
  203. The API returns the following result:
  204. [source,console-result]
  205. ----
  206. {
  207. "id": "loganalytics",
  208. "description": "Outlier detection on log data",
  209. "source": {
  210. "index": ["logdata"],
  211. "query": {
  212. "match_all": {}
  213. }
  214. },
  215. "dest": {
  216. "index": "logdata_out",
  217. "results_field": "ml"
  218. },
  219. "analysis": {
  220. "outlier_detection": {
  221. "compute_feature_influence": true,
  222. "outlier_fraction": 0.05,
  223. "standardization_enabled": true
  224. }
  225. },
  226. "model_memory_limit": "1gb",
  227. "create_time" : 1562265491319,
  228. "version" : "8.0.0",
  229. "allow_lazy_start" : false
  230. }
  231. ----
  232. // TESTRESPONSE[s/1562265491319/$body.$_path/]
  233. // TESTRESPONSE[s/"version": "8.0.0"/"version": $body.version/]
  234. [[ml-put-dfanalytics-example-r]]
  235. ===== {regression-cap} examples
  236. The following example creates the `house_price_regression_analysis`
  237. {dfanalytics-job}, the analysis type is `regression`:
  238. [source,console]
  239. --------------------------------------------------
  240. PUT _ml/data_frame/analytics/house_price_regression_analysis
  241. {
  242. "source": {
  243. "index": "houses_sold_last_10_yrs"
  244. },
  245. "dest": {
  246. "index": "house_price_predictions"
  247. },
  248. "analysis":
  249. {
  250. "regression": {
  251. "dependent_variable": "price"
  252. }
  253. }
  254. }
  255. --------------------------------------------------
  256. // TEST[skip:TBD]
  257. The API returns the following result:
  258. [source,console-result]
  259. ----
  260. {
  261. "id" : "house_price_regression_analysis",
  262. "source" : {
  263. "index" : [
  264. "houses_sold_last_10_yrs"
  265. ],
  266. "query" : {
  267. "match_all" : { }
  268. }
  269. },
  270. "dest" : {
  271. "index" : "house_price_predictions",
  272. "results_field" : "ml"
  273. },
  274. "analysis" : {
  275. "regression" : {
  276. "dependent_variable" : "price",
  277. "training_percent" : 100
  278. }
  279. },
  280. "model_memory_limit" : "1gb",
  281. "create_time" : 1567168659127,
  282. "version" : "8.0.0",
  283. "allow_lazy_start" : false
  284. }
  285. ----
  286. // TESTRESPONSE[s/1567168659127/$body.$_path/]
  287. // TESTRESPONSE[s/"version": "8.0.0"/"version": $body.version/]
  288. The following example creates a job and specifies a training percent:
  289. [source,console]
  290. --------------------------------------------------
  291. PUT _ml/data_frame/analytics/student_performance_mathematics_0.3
  292. {
  293. "source": {
  294. "index": "student_performance_mathematics"
  295. },
  296. "dest": {
  297. "index":"student_performance_mathematics_reg"
  298. },
  299. "analysis":
  300. {
  301. "regression": {
  302. "dependent_variable": "G3",
  303. "training_percent": 70, <1>
  304. "randomize_seed": 19673948271 <2>
  305. }
  306. }
  307. }
  308. --------------------------------------------------
  309. // TEST[skip:TBD]
  310. <1> The `training_percent` defines the percentage of the data set that will be
  311. used for training the model.
  312. <2> The `randomize_seed` is the seed used to randomly pick which data is used
  313. for training.
  314. [[ml-put-dfanalytics-example-c]]
  315. ===== {classification-cap} example
  316. The following example creates the `loan_classification` {dfanalytics-job}, the
  317. analysis type is `classification`:
  318. [source,console]
  319. --------------------------------------------------
  320. PUT _ml/data_frame/analytics/loan_classification
  321. {
  322. "source" : {
  323. "index": "loan-applicants"
  324. },
  325. "dest" : {
  326. "index": "loan-applicants-classified"
  327. },
  328. "analysis" : {
  329. "classification": {
  330. "dependent_variable": "label",
  331. "training_percent": 75,
  332. "num_top_classes": 2
  333. }
  334. }
  335. }
  336. --------------------------------------------------
  337. // TEST[skip:TBD]