put-inference.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[put-inference]]
  4. === Create {infer} trained model API
  5. [subs="attributes"]
  6. ++++
  7. <titleabbrev>Create {infer} trained model</titleabbrev>
  8. ++++
  9. Creates an {infer} trained model.
  10. experimental[]
  11. [[ml-put-inference-request]]
  12. ==== {api-request-title}
  13. `PUT _ml/inference/<model_id>`
  14. [[ml-put-inference-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. For more information, see <<security-privileges>> and <<built-in-roles>>.
  20. [[ml-put-inference-desc]]
  21. ==== {api-description-title}
  22. The create {infer} trained model API enables you to supply a trained model that
  23. is not created by {dfanalytics}.
  24. [[ml-put-inference-path-params]]
  25. ==== {api-path-parms-title}
  26. `<model_id>`::
  27. (Required, string)
  28. include::{docdir}/ml/ml-shared.asciidoc[tag=model-id]
  29. [[ml-put-inference-request-body]]
  30. ==== {api-request-body-title}
  31. `compressed_definition`::
  32. (Required, string)
  33. The compressed (GZipped and Base64 encoded) {infer} definition of the model.
  34. If `compressed_definition` is specified, then `definition` cannot be specified.
  35. `definition`::
  36. (Required, object)
  37. The {infer} definition for the model. If `definition` is specified, then
  38. `compressed_definition` cannot be specified.
  39. `definition`.`preprocessors`:::
  40. (Optional, object)
  41. Collection of preprocessors. See <<ml-put-inference-preprocessors>> for the full
  42. list of available preprocessors.
  43. `definition`.`trained_model`:::
  44. (Required, object)
  45. The definition of the trained model. See <<ml-put-inference-trained-model>> for
  46. details.
  47. `description`::
  48. (Optional, string)
  49. A human-readable description of the {infer} trained model.
  50. `input`::
  51. (Required, object)
  52. The input field names for the model definition.
  53. `input`.`field_names`:::
  54. (Required, string)
  55. An array of input field names for the model.
  56. `metadata`::
  57. (Optional, object)
  58. An object map that contains metadata about the model.
  59. `tags`::
  60. (Optional, string)
  61. An array of tags to organize the model.
  62. [[ml-put-inference-preprocessors]]
  63. ===== {infer-cap} preprocessor definitions
  64. `frequency_encoding`::
  65. (Required, object)
  66. Defines a frequency encoding for a field.
  67. `frequency_encoding`.`field`:::
  68. (Required, string)
  69. The field name to encode.
  70. `frequency_encoding`.`feature_name`:::
  71. (Required, string)
  72. The name of the resulting feature.
  73. `frequency_encoding`.`frequency_map`:::
  74. (Required, object map of string:double)
  75. Object that maps the field value to the frequency encoded value.
  76. `one_hot_encoding`::
  77. (Required, object)
  78. Defines a one hot encoding map for a field.
  79. `one_hot_encoding`.`field`:::
  80. (Required, string)
  81. The field name to encode.
  82. `one_hot_encoding`.`hot_map`:::
  83. (Required, object map of strings)
  84. String map of "field_value: one_hot_column_name".
  85. `target_mean_encoding`::
  86. (Required, object)
  87. Defines a target mean encoding for a field.
  88. `target_mean_encoding`.`field`:::
  89. (Required, string)
  90. The field name to encode.
  91. `target_mean_encoding`.`feature_name`:::
  92. (Required, string)
  93. The name of the resulting feature.
  94. `target_mean_encoding`.`target_map`:::
  95. (Required, object map of string:double)
  96. Object that maps the field value to the target mean value.
  97. `target_mean_encoding`.`default_value`:::
  98. (Required, double)
  99. The feature value if the field value is not in the `target_map`.
  100. See <<ml-put-inference-preprocessor-example>> for more details.
  101. [[ml-put-inference-trained-model]]
  102. ===== {infer-cap} trained model definitions
  103. `tree`::
  104. (Required, object)
  105. The definition for a binary decision tree.
  106. `tree`.`feature_names`:::
  107. (Required, string)
  108. Features expected by the tree, in their expected order.
  109. `tree`.`tree_structure`:::
  110. (Required, object)
  111. An array of `tree_node` objects. The nodes must be in ordinal order by their
  112. `tree_node.node_index` value.
  113. `tree`.`classification_labels`:::
  114. (Optional, string) An array of classification labels (used for
  115. `classification`).
  116. `tree`.`target_type`:::
  117. (Required, string)
  118. String indicating the model target type; `regression` or `classification`.
  119. There are two major types of nodes: leaf nodes and not-leaf nodes.
  120. * Leaf nodes only need `node_index` and `leaf_value` defined.
  121. * All other nodes need `split_feature`, `left_child`, `right_child`,
  122. `threshold`, `decision_type`, and `default_left` defined.
  123. `tree_node`::
  124. (Required, object)
  125. The definition of a node in a tree.
  126. `tree_node`.`decision_type`:::
  127. (Optional, string)
  128. Indicates the positive value (in other words, when to choose the left node)
  129. decision type. Supported `lt`, `lte`, `gt`, `gte`. Defaults to `lte`.
  130. `tree_node`.`threshold`:::
  131. (Optional, double)
  132. The decision threshold with which to compare the feature value.
  133. `tree_node`.`left_child`:::
  134. (Optional, integer)
  135. The index of the left child.
  136. `tree_node`.`right_child`:::
  137. (Optional, integer)
  138. The index of the right child.
  139. `tree_node`.`default_left`:::
  140. (Optional, boolean)
  141. Indicates whether to default to the left when the feature is missing. Defaults
  142. to `true`.
  143. `tree_node`.`split_feature`:::
  144. (Optional, integer)
  145. The index of the feature value in the feature array.
  146. `tree_node`.`node_index`:::
  147. (Integer)
  148. The index of the current node.
  149. `tree_node`.`split_gain`:::
  150. (Optional, double) The information gain from the split.
  151. `tree_node`.`leaf_value`:::
  152. (Optional, double)
  153. The leaf value of the of the node, if the value is a leaf (in other words, no
  154. children).
  155. `ensemble`::
  156. (Optional, object)
  157. The definition for an ensemble model.
  158. `ensemble`.`feature_names`:::
  159. (Optional, string)
  160. Features expected by the ensemble, in their expected order.
  161. `ensemble`.`trained_models`:::
  162. (Required, object)
  163. An array of `trained_model` objects. Supported trained models are `tree` and
  164. `ensemble`.
  165. `ensemble`.`classification_labels`:::
  166. (Optional, string)
  167. An array of classification labels.
  168. `ensemble`.`target_type`:::
  169. (Required, string)
  170. String indicating the model target type; `regression` or `classification.`
  171. `ensemble`.`aggregate_output`:::
  172. (Required, object)
  173. An aggregated output object that defines how to aggregate the outputs of the
  174. `trained_models`. Supported objects are `weighted_mode`, `weighted_sum`, and
  175. `logistic_regression`.
  176. See <<ml-put-inference-model-example>> for more details.
  177. [[ml-put-inference-aggregated-output]]
  178. ===== Aggregated output types
  179. `logistic_regression`::
  180. (Optional, object)
  181. This `aggregated_output` type works with binary classification (classification
  182. for values [0, 1]). It multiplies the outputs (in the case of the `ensemble`
  183. model, the inference model values) by the supplied `weights`. The resulting
  184. vector is summed and passed to a
  185. https://en.wikipedia.org/wiki/Sigmoid_function[`sigmoid` function]. The result
  186. of the `sigmoid` function is considered the probability of class 1 (`P_1`),
  187. consequently, the probability of class 0 is `1 - P_1`. The class with the
  188. highest probability (either 0 or 1) is then returned. For more information about
  189. logistic regression, see
  190. https://en.wikipedia.org/wiki/Logistic_regression[this wiki article].
  191. `logistic_regression`.`weights`:::
  192. (Required, double)
  193. The weights to multiply by the input values (the inference values of the trained
  194. models).
  195. `weighted_sum`::
  196. (Optional, object)
  197. This `aggregated_output` type works with regression. The weighted sum of the
  198. input values.
  199. `weighted_sum`.`weights`:::
  200. (Required, double)
  201. The weights to multiply by the input values (the inference values of the trained
  202. models).
  203. `weighted_mode`::
  204. (Optional, object)
  205. This `aggregated_output` type works with regression or classification. It takes
  206. a weighted vote of the input values. The most common input value (taking the
  207. weights into account) is returned.
  208. `weighted_mode`.`weights`:::
  209. (Required, double)
  210. The weights to multiply by the input values (the inference values of the trained
  211. models).
  212. See <<ml-put-inference-aggregated-output-example>> for more details.
  213. [[ml-put-inference-example]]
  214. ==== {api-examples-title}
  215. [[ml-put-inference-preprocessor-example]]
  216. ===== Preprocessor examples
  217. The example below shows a `frequency_encoding` preprocessor object:
  218. [source,js]
  219. ----------------------------------
  220. {
  221. "frequency_encoding":{
  222. "field":"FlightDelayType",
  223. "feature_name":"FlightDelayType_frequency",
  224. "frequency_map":{
  225. "Carrier Delay":0.6007414737092798,
  226. "NAS Delay":0.6007414737092798,
  227. "Weather Delay":0.024573576178086153,
  228. "Security Delay":0.02476631010889467,
  229. "No Delay":0.6007414737092798,
  230. "Late Aircraft Delay":0.6007414737092798
  231. }
  232. }
  233. }
  234. ----------------------------------
  235. //NOTCONSOLE
  236. The next example shows a `one_hot_encoding` preprocessor object:
  237. [source,js]
  238. ----------------------------------
  239. {
  240. "one_hot_encoding":{
  241. "field":"FlightDelayType",
  242. "hot_map":{
  243. "Carrier Delay":"FlightDelayType_Carrier Delay",
  244. "NAS Delay":"FlightDelayType_NAS Delay",
  245. "No Delay":"FlightDelayType_No Delay",
  246. "Late Aircraft Delay":"FlightDelayType_Late Aircraft Delay"
  247. }
  248. }
  249. }
  250. ----------------------------------
  251. //NOTCONSOLE
  252. This example shows a `target_mean_encoding` preprocessor object:
  253. [source,js]
  254. ----------------------------------
  255. {
  256. "target_mean_encoding":{
  257. "field":"FlightDelayType",
  258. "feature_name":"FlightDelayType_targetmean",
  259. "target_map":{
  260. "Carrier Delay":39.97465788139886,
  261. "NAS Delay":39.97465788139886,
  262. "Security Delay":203.171206225681,
  263. "Weather Delay":187.64705882352948,
  264. "No Delay":39.97465788139886,
  265. "Late Aircraft Delay":39.97465788139886
  266. },
  267. "default_value":158.17995752420433
  268. }
  269. }
  270. ----------------------------------
  271. //NOTCONSOLE
  272. [[ml-put-inference-model-example]]
  273. ===== Model examples
  274. The first example shows a `trained_model` object:
  275. [source,js]
  276. ----------------------------------
  277. {
  278. "tree":{
  279. "feature_names":[
  280. "DistanceKilometers",
  281. "FlightTimeMin",
  282. "FlightDelayType_NAS Delay",
  283. "Origin_targetmean",
  284. "DestRegion_targetmean",
  285. "DestCityName_targetmean",
  286. "OriginAirportID_targetmean",
  287. "OriginCityName_frequency",
  288. "DistanceMiles",
  289. "FlightDelayType_Late Aircraft Delay"
  290. ],
  291. "tree_structure":[
  292. {
  293. "decision_type":"lt",
  294. "threshold":9069.33437193022,
  295. "split_feature":0,
  296. "split_gain":4112.094574306927,
  297. "node_index":0,
  298. "default_left":true,
  299. "left_child":1,
  300. "right_child":2
  301. },
  302. ...
  303. {
  304. "node_index":9,
  305. "leaf_value":-27.68987349695448
  306. },
  307. ...
  308. ],
  309. "target_type":"regression"
  310. }
  311. }
  312. ----------------------------------
  313. //NOTCONSOLE
  314. The following example shows an `ensemble` model object:
  315. [source,js]
  316. ----------------------------------
  317. "ensemble":{
  318. "feature_names":[
  319. ...
  320. ],
  321. "trained_models":[
  322. {
  323. "tree":{
  324. "feature_names":[],
  325. "tree_structure":[
  326. {
  327. "decision_type":"lte",
  328. "node_index":0,
  329. "leaf_value":47.64069875778043,
  330. "default_left":false
  331. }
  332. ],
  333. "target_type":"regression"
  334. }
  335. },
  336. ...
  337. ],
  338. "aggregate_output":{
  339. "weighted_sum":{
  340. "weights":[
  341. ...
  342. ]
  343. }
  344. },
  345. "target_type":"regression"
  346. }
  347. ----------------------------------
  348. //NOTCONSOLE
  349. [[ml-put-inference-aggregated-output-example]]
  350. ===== Aggregated output example
  351. Example of a `logistic_regression` object:
  352. [source,js]
  353. ----------------------------------
  354. "aggregate_output" : {
  355. "logistic_regression" : {
  356. "weights" : [2.0, 1.0, .5, -1.0, 5.0, 1.0, 1.0]
  357. }
  358. }
  359. ----------------------------------
  360. //NOTCONSOLE
  361. Example of a `weighted_sum` object:
  362. [source,js]
  363. ----------------------------------
  364. "aggregate_output" : {
  365. "weighted_sum" : {
  366. "weights" : [1.0, -1.0, .5, 1.0, 5.0]
  367. }
  368. }
  369. ----------------------------------
  370. //NOTCONSOLE
  371. Example of a `weighted_mode` object:
  372. [source,js]
  373. ----------------------------------
  374. "aggregate_output" : {
  375. "weighted_mode" : {
  376. "weights" : [1.0, 1.0, 1.0, 1.0, 1.0]
  377. }
  378. }
  379. ----------------------------------
  380. //NOTCONSOLE
  381. [[ml-put-inference-json-schema]]
  382. ===== {infer-cap} JSON schema
  383. For the full JSON schema of model {infer},
  384. https://github.com/elastic/ml-json-schemas[click here].