put-inference.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. [role="child_attributes"]
  30. [[ml-put-inference-request-body]]
  31. ==== {api-request-body-title}
  32. `compressed_definition`::
  33. (Required, string)
  34. The compressed (GZipped and Base64 encoded) {infer} definition of the model.
  35. If `compressed_definition` is specified, then `definition` cannot be specified.
  36. //Begin definition
  37. `definition`::
  38. (Required, object)
  39. The {infer} definition for the model. If `definition` is specified, then
  40. `compressed_definition` cannot be specified.
  41. +
  42. .Properties of `definition`
  43. [%collapsible%open]
  44. ====
  45. //Begin preprocessors
  46. `preprocessors`::
  47. (Optional, object)
  48. Collection of preprocessors. See <<ml-put-inference-preprocessor-example>>.
  49. +
  50. .Properties of `preprocessors`
  51. [%collapsible%open]
  52. =====
  53. //Begin frequency encoding
  54. `frequency_encoding`::
  55. (Required, object)
  56. Defines a frequency encoding for a field.
  57. +
  58. .Properties of `frequency_encoding`
  59. [%collapsible%open]
  60. ======
  61. `feature_name`::
  62. (Required, string)
  63. The name of the resulting feature.
  64. `field`::
  65. (Required, string)
  66. The field name to encode.
  67. `frequency_map`::
  68. (Required, object map of string:double)
  69. Object that maps the field value to the frequency encoded value.
  70. ======
  71. //End frequency encoding
  72. //Begin one hot encoding
  73. `one_hot_encoding`::
  74. (Required, object)
  75. Defines a one hot encoding map for a field.
  76. +
  77. .Properties of `one_hot_encoding`
  78. [%collapsible%open]
  79. ======
  80. `field`::
  81. (Required, string)
  82. The field name to encode.
  83. `hot_map`::
  84. (Required, object map of strings)
  85. String map of "field_value: one_hot_column_name".
  86. ======
  87. //End one hot encoding
  88. //Begin target mean encoding
  89. `target_mean_encoding`::
  90. (Required, object)
  91. Defines a target mean encoding for a field.
  92. +
  93. .Properties of `target_mean_encoding`
  94. [%collapsible%open]
  95. ======
  96. `default_value`:::
  97. (Required, double)
  98. The feature value if the field value is not in the `target_map`.
  99. `feature_name`:::
  100. (Required, string)
  101. The name of the resulting feature.
  102. `field`:::
  103. (Required, string)
  104. The field name to encode.
  105. `target_map`:::
  106. (Required, object map of string:double)
  107. Object that maps the field value to the target mean value.
  108. ======
  109. //End target mean encoding
  110. =====
  111. //End preprocessors
  112. //Begin trained model
  113. `trained_model`::
  114. (Required, object)
  115. The definition of the trained model.
  116. +
  117. .Properties of `trained_model`
  118. [%collapsible%open]
  119. =====
  120. //Begin tree
  121. `tree`::
  122. (Required, object)
  123. The definition for a binary decision tree.
  124. +
  125. .Properties of `tree`
  126. [%collapsible%open]
  127. ======
  128. `classification_labels`:::
  129. (Optional, string) An array of classification labels (used for
  130. `classification`).
  131. `feature_names`:::
  132. (Required, string)
  133. Features expected by the tree, in their expected order.
  134. `target_type`:::
  135. (Required, string)
  136. String indicating the model target type; `regression` or `classification`.
  137. `tree_structure`:::
  138. (Required, object)
  139. An array of `tree_node` objects. The nodes must be in ordinal order by their
  140. `tree_node.node_index` value.
  141. ======
  142. //End tree
  143. //Begin tree node
  144. `tree_node`::
  145. (Required, object)
  146. The definition of a node in a tree.
  147. +
  148. --
  149. There are two major types of nodes: leaf nodes and not-leaf nodes.
  150. * Leaf nodes only need `node_index` and `leaf_value` defined.
  151. * All other nodes need `split_feature`, `left_child`, `right_child`,
  152. `threshold`, `decision_type`, and `default_left` defined.
  153. --
  154. +
  155. .Properties of `tree_node`
  156. [%collapsible%open]
  157. ======
  158. `decision_type`::
  159. (Optional, string)
  160. Indicates the positive value (in other words, when to choose the left node)
  161. decision type. Supported `lt`, `lte`, `gt`, `gte`. Defaults to `lte`.
  162. `default_left`::
  163. (Optional, boolean)
  164. Indicates whether to default to the left when the feature is missing. Defaults
  165. to `true`.
  166. `leaf_value`::
  167. (Optional, double)
  168. The leaf value of the of the node, if the value is a leaf (in other words, no
  169. children).
  170. `left_child`::
  171. (Optional, integer)
  172. The index of the left child.
  173. `node_index`::
  174. (Integer)
  175. The index of the current node.
  176. `right_child`::
  177. (Optional, integer)
  178. The index of the right child.
  179. `split_feature`::
  180. (Optional, integer)
  181. The index of the feature value in the feature array.
  182. `split_gain`::
  183. (Optional, double) The information gain from the split.
  184. `threshold`::
  185. (Optional, double)
  186. The decision threshold with which to compare the feature value.
  187. ======
  188. //End tree node
  189. //Begin ensemble
  190. `ensemble`::
  191. (Optional, object)
  192. The definition for an ensemble model. See <<ml-put-inference-model-example>>.
  193. +
  194. .Properties of `ensemble`
  195. [%collapsible%open]
  196. ======
  197. //Begin aggregate output
  198. `aggregate_output`::
  199. (Required, object)
  200. An aggregated output object that defines how to aggregate the outputs of the
  201. `trained_models`. Supported objects are `weighted_mode`, `weighted_sum`, and
  202. `logistic_regression`. See <<ml-put-inference-aggregated-output-example>>.
  203. +
  204. .Properties of `aggregate_output`
  205. [%collapsible%open]
  206. =======
  207. //Begin logistic regression
  208. `logistic_regression`::
  209. (Optional, object)
  210. This `aggregated_output` type works with binary classification (classification
  211. for values [0, 1]). It multiplies the outputs (in the case of the `ensemble`
  212. model, the inference model values) by the supplied `weights`. The resulting
  213. vector is summed and passed to a
  214. https://en.wikipedia.org/wiki/Sigmoid_function[`sigmoid` function]. The result
  215. of the `sigmoid` function is considered the probability of class 1 (`P_1`),
  216. consequently, the probability of class 0 is `1 - P_1`. The class with the
  217. highest probability (either 0 or 1) is then returned. For more information about
  218. logistic regression, see
  219. https://en.wikipedia.org/wiki/Logistic_regression[this wiki article].
  220. +
  221. .Properties of `logistic_regression`
  222. [%collapsible%open]
  223. ========
  224. `weights`:::
  225. (Required, double)
  226. The weights to multiply by the input values (the inference values of the trained
  227. models).
  228. ========
  229. //End logistic regression
  230. //Begin weighted sum
  231. `weighted_sum`::
  232. (Optional, object)
  233. This `aggregated_output` type works with regression. The weighted sum of the
  234. input values.
  235. +
  236. .Properties of `weighted_sum`
  237. [%collapsible%open]
  238. ========
  239. `weights`:::
  240. (Required, double)
  241. The weights to multiply by the input values (the inference values of the trained
  242. models).
  243. ========
  244. //End weighted sum
  245. //Begin weighted mode
  246. `weighted_mode`::
  247. (Optional, object)
  248. This `aggregated_output` type works with regression or classification. It takes
  249. a weighted vote of the input values. The most common input value (taking the
  250. weights into account) is returned.
  251. +
  252. .Properties of `weighted_mode`
  253. [%collapsible%open]
  254. ========
  255. `weights`:::
  256. (Required, double)
  257. The weights to multiply by the input values (the inference values of the trained
  258. models).
  259. ========
  260. //End weighted mode
  261. =======
  262. //End aggregate output
  263. `classification_labels`::
  264. (Optional, string)
  265. An array of classification labels.
  266. `feature_names`::
  267. (Optional, string)
  268. Features expected by the ensemble, in their expected order.
  269. `target_type`::
  270. (Required, string)
  271. String indicating the model target type; `regression` or `classification.`
  272. `trained_models`::
  273. (Required, object)
  274. An array of `trained_model` objects. Supported trained models are `tree` and
  275. `ensemble`.
  276. ======
  277. //End ensemble
  278. =====
  279. //End trained model
  280. ====
  281. //End definition
  282. `description`::
  283. (Optional, string)
  284. A human-readable description of the {infer} trained model.
  285. //Begin inference_config
  286. `inference_config`::
  287. (Required, object)
  288. The default configuration for inference. This can be either a `regression`
  289. or `classification` configuration. It must match the underlying
  290. `definition.trained_model`'s `target_type`.
  291. +
  292. .Properties of `inference_config`
  293. [%collapsible%open]
  294. ====
  295. `regression`:::
  296. (Optional, object)
  297. Regression configuration for inference.
  298. +
  299. .Properties of regression inference
  300. [%collapsible%open]
  301. =====
  302. `num_top_feature_importance_values`::::
  303. (Optional, integer)
  304. include::{docdir}/ml/ml-shared.asciidoc[tag=inference-config-regression-num-top-feature-importance-values]
  305. `results_field`::::
  306. (Optional, string)
  307. include::{docdir}/ml/ml-shared.asciidoc[tag=inference-config-results-field]
  308. =====
  309. `classification`:::
  310. (Optional, object)
  311. Classification configuration for inference.
  312. +
  313. .Properties of classification inference
  314. [%collapsible%open]
  315. =====
  316. `num_top_classes`::::
  317. (Optional, integer)
  318. include::{docdir}/ml/ml-shared.asciidoc[tag=inference-config-classification-num-top-classes]
  319. `num_top_feature_importance_values`::::
  320. (Optional, integer)
  321. include::{docdir}/ml/ml-shared.asciidoc[tag=inference-config-classification-num-top-feature-importance-values]
  322. `results_field`::::
  323. (Optional, string)
  324. include::{docdir}/ml/ml-shared.asciidoc[tag=inference-config-results-field]
  325. `top_classes_results_field`::::
  326. (Optional, string)
  327. include::{docdir}/ml/ml-shared.asciidoc[tag=inference-config-classification-top-classes-results-field]
  328. =====
  329. ====
  330. //End of inference_config
  331. //Begin input
  332. `input`::
  333. (Required, object)
  334. The input field names for the model definition.
  335. +
  336. .Properties of `input`
  337. [%collapsible%open]
  338. ====
  339. `field_names`:::
  340. (Required, string)
  341. An array of input field names for the model.
  342. ====
  343. //End input
  344. `metadata`::
  345. (Optional, object)
  346. An object map that contains metadata about the model.
  347. `tags`::
  348. (Optional, string)
  349. An array of tags to organize the model.
  350. [[ml-put-inference-example]]
  351. ==== {api-examples-title}
  352. [[ml-put-inference-preprocessor-example]]
  353. ===== Preprocessor examples
  354. The example below shows a `frequency_encoding` preprocessor object:
  355. [source,js]
  356. ----------------------------------
  357. {
  358. "frequency_encoding":{
  359. "field":"FlightDelayType",
  360. "feature_name":"FlightDelayType_frequency",
  361. "frequency_map":{
  362. "Carrier Delay":0.6007414737092798,
  363. "NAS Delay":0.6007414737092798,
  364. "Weather Delay":0.024573576178086153,
  365. "Security Delay":0.02476631010889467,
  366. "No Delay":0.6007414737092798,
  367. "Late Aircraft Delay":0.6007414737092798
  368. }
  369. }
  370. }
  371. ----------------------------------
  372. //NOTCONSOLE
  373. The next example shows a `one_hot_encoding` preprocessor object:
  374. [source,js]
  375. ----------------------------------
  376. {
  377. "one_hot_encoding":{
  378. "field":"FlightDelayType",
  379. "hot_map":{
  380. "Carrier Delay":"FlightDelayType_Carrier Delay",
  381. "NAS Delay":"FlightDelayType_NAS Delay",
  382. "No Delay":"FlightDelayType_No Delay",
  383. "Late Aircraft Delay":"FlightDelayType_Late Aircraft Delay"
  384. }
  385. }
  386. }
  387. ----------------------------------
  388. //NOTCONSOLE
  389. This example shows a `target_mean_encoding` preprocessor object:
  390. [source,js]
  391. ----------------------------------
  392. {
  393. "target_mean_encoding":{
  394. "field":"FlightDelayType",
  395. "feature_name":"FlightDelayType_targetmean",
  396. "target_map":{
  397. "Carrier Delay":39.97465788139886,
  398. "NAS Delay":39.97465788139886,
  399. "Security Delay":203.171206225681,
  400. "Weather Delay":187.64705882352948,
  401. "No Delay":39.97465788139886,
  402. "Late Aircraft Delay":39.97465788139886
  403. },
  404. "default_value":158.17995752420433
  405. }
  406. }
  407. ----------------------------------
  408. //NOTCONSOLE
  409. [[ml-put-inference-model-example]]
  410. ===== Model examples
  411. The first example shows a `trained_model` object:
  412. [source,js]
  413. ----------------------------------
  414. {
  415. "tree":{
  416. "feature_names":[
  417. "DistanceKilometers",
  418. "FlightTimeMin",
  419. "FlightDelayType_NAS Delay",
  420. "Origin_targetmean",
  421. "DestRegion_targetmean",
  422. "DestCityName_targetmean",
  423. "OriginAirportID_targetmean",
  424. "OriginCityName_frequency",
  425. "DistanceMiles",
  426. "FlightDelayType_Late Aircraft Delay"
  427. ],
  428. "tree_structure":[
  429. {
  430. "decision_type":"lt",
  431. "threshold":9069.33437193022,
  432. "split_feature":0,
  433. "split_gain":4112.094574306927,
  434. "node_index":0,
  435. "default_left":true,
  436. "left_child":1,
  437. "right_child":2
  438. },
  439. ...
  440. {
  441. "node_index":9,
  442. "leaf_value":-27.68987349695448
  443. },
  444. ...
  445. ],
  446. "target_type":"regression"
  447. }
  448. }
  449. ----------------------------------
  450. //NOTCONSOLE
  451. The following example shows an `ensemble` model object:
  452. [source,js]
  453. ----------------------------------
  454. "ensemble":{
  455. "feature_names":[
  456. ...
  457. ],
  458. "trained_models":[
  459. {
  460. "tree":{
  461. "feature_names":[],
  462. "tree_structure":[
  463. {
  464. "decision_type":"lte",
  465. "node_index":0,
  466. "leaf_value":47.64069875778043,
  467. "default_left":false
  468. }
  469. ],
  470. "target_type":"regression"
  471. }
  472. },
  473. ...
  474. ],
  475. "aggregate_output":{
  476. "weighted_sum":{
  477. "weights":[
  478. ...
  479. ]
  480. }
  481. },
  482. "target_type":"regression"
  483. }
  484. ----------------------------------
  485. //NOTCONSOLE
  486. [[ml-put-inference-aggregated-output-example]]
  487. ===== Aggregated output example
  488. Example of a `logistic_regression` object:
  489. [source,js]
  490. ----------------------------------
  491. "aggregate_output" : {
  492. "logistic_regression" : {
  493. "weights" : [2.0, 1.0, .5, -1.0, 5.0, 1.0, 1.0]
  494. }
  495. }
  496. ----------------------------------
  497. //NOTCONSOLE
  498. Example of a `weighted_sum` object:
  499. [source,js]
  500. ----------------------------------
  501. "aggregate_output" : {
  502. "weighted_sum" : {
  503. "weights" : [1.0, -1.0, .5, 1.0, 5.0]
  504. }
  505. }
  506. ----------------------------------
  507. //NOTCONSOLE
  508. Example of a `weighted_mode` object:
  509. [source,js]
  510. ----------------------------------
  511. "aggregate_output" : {
  512. "weighted_mode" : {
  513. "weights" : [1.0, 1.0, 1.0, 1.0, 1.0]
  514. }
  515. }
  516. ----------------------------------
  517. //NOTCONSOLE
  518. [[ml-put-inference-json-schema]]
  519. ===== {infer-cap} JSON schema
  520. For the full JSON schema of model {infer},
  521. https://github.com/elastic/ml-json-schemas[click here].