Example Anchors Tabular Explaination for Income Prediction

This example uses a US income dataset to show the example of explanation on tabular data. You can also try out the Jupyter notebook for a visual walkthrough.

Create the InferenceService with alibi explainer

We can create a InferenceService with a trained sklearn predictor for this dataset and an associated model explainer. The black box explainer algorithm we will use is the Tabular version of Anchors from the Alibi open source library. More details on this algorithm and configuration settings that can be set can be found in the Seldon Alibi documentation.

The InferenceService is shown below:

  1. apiVersion: "serving.kserve.io/v1beta1"
  2. kind: "InferenceService"
  3. metadata:
  4. name: "income"
  5. spec:
  6. predictor:
  7. minReplicas: 1
  8. sklearn:
  9. storageUri: "gs://seldon-models/sklearn/income/model"
  10. resources:
  11. requests:
  12. cpu: 0.1
  13. memory: 1Gi
  14. limits:
  15. cpu: 1
  16. memory: 1Gi
  17. explainer:
  18. minReplicas: 1
  19. alibi:
  20. type: AnchorTabular
  21. storageUri: "gs://seldon-models/sklearn/income/explainer-py37-0.6.0"
  22. resources:
  23. requests:
  24. cpu: 0.1
  25. memory: 1Gi
  26. limits:
  27. cpu: 1
  28. memory: 4Gi

Create the InferenceService with above yaml:

kubectl

  1. kubectl create -f income.yaml

The first step is to determine the ingress IP and ports and set INGRESS_HOST and INGRESS_PORT

  1. MODEL_NAME=income
  2. SERVICE_HOSTNAME=$(kubectl get inferenceservice income -o jsonpath='{.status.url}' | cut -d "/" -f 3)

Run the inference

Test the predictor:

  1. curl -H "Host: $SERVICE_HOSTNAME" http://${INGRESS_HOST}:${INGRESS_PORT}/v1/models/$MODEL_NAME:predict -d '{"instances":[[39, 7, 1, 1, 1, 1, 4, 1, 2174, 0, 40, 9]]}'

You should receive the response showing the prediction is for low salary:

  1. {"predictions": [0]}

Run the explanation

Now lets get an explanation for this:

  1. curl -H "Host: $SERVICE_HOSTNAME" http://${INGRESS_HOST}:${INGRESS_PORT}/v1/models/$MODEL_NAME:explain -d '{"instances":[[39, 7, 1, 1, 1, 1, 4, 1, 2174, 0, 40, 9]]}'

The returned explanation will be like:

  1. {
  2. "names": [
  3. "Marital Status = Never-Married",
  4. "Workclass = State-gov"
  5. ],
  6. "precision": 0.9724770642201835,
  7. "coverage": 0.0147,
  8. "raw": {
  9. "feature": [
  10. 3,
  11. 1
  12. ],
  13. "mean": [
  14. 0.9129746835443038,
  15. 0.9724770642201835
  16. ],
  17. "precision": [
  18. 0.9129746835443038,
  19. 0.9724770642201835
  20. ],
  21. "coverage": [
  22. 0.3327,
  23. 0.0147
  24. ],
  25. "examples": [
  26. {
  27. "covered": [
  28. [
  29. 30,
  30. "Self-emp-not-inc",
  31. "Bachelors",
  32. "Never-Married",
  33. "Sales",
  34. "Unmarried",
  35. "White",
  36. "Male",
  37. "Capital Gain <= 0.00",
  38. "Capital Loss <= 0.00",
  39. 40,
  40. "United-States"
  41. ],
  42. [
  43. 69,
  44. "Private",
  45. "Dropout",
  46. "Never-Married",
  47. "Blue-Collar",
  48. "Husband",
  49. "White",
  50. "Male",
  51. 9386,
  52. "Capital Loss <= 0.00",
  53. 60,
  54. "United-States"
  55. ],
  56. [
  57. 44,
  58. "Local-gov",
  59. "Bachelors",
  60. "Never-Married",
  61. "White-Collar",
  62. "Husband",
  63. "White",
  64. "Male",
  65. "Capital Gain <= 0.00",
  66. "Capital Loss <= 0.00",
  67. 52,
  68. "United-States"
  69. ],
  70. [
  71. 59,
  72. "Private",
  73. "High School grad",
  74. "Never-Married",
  75. "White-Collar",
  76. "Husband",
  77. "White",
  78. "Male",
  79. "Capital Gain <= 0.00",
  80. "Capital Loss <= 0.00",
  81. 50,
  82. "United-States"
  83. ],
  84. [
  85. 55,
  86. "Private",
  87. "Bachelors",
  88. "Never-Married",
  89. "White-Collar",
  90. "Husband",
  91. "White",
  92. "Male",
  93. 15024,
  94. "Capital Loss <= 0.00",
  95. 55,
  96. "United-States"
  97. ],
  98. [
  99. 32,
  100. "?",
  101. "Bachelors",
  102. "Never-Married",
  103. "?",
  104. "Unmarried",
  105. "White",
  106. "Female",
  107. "Capital Gain <= 0.00",
  108. "Capital Loss <= 0.00",
  109. 32,
  110. "United-States"
  111. ],
  112. [
  113. 47,
  114. "Private",
  115. "Dropout",
  116. "Never-Married",
  117. "Blue-Collar",
  118. "Unmarried",
  119. "Black",
  120. "Female",
  121. 6849,
  122. "Capital Loss <= 0.00",
  123. 40,
  124. "United-States"
  125. ],
  126. [
  127. 35,
  128. "Private",
  129. "Associates",
  130. "Never-Married",
  131. "Service",
  132. "Not-in-family",
  133. "White",
  134. "Female",
  135. "Capital Gain <= 0.00",
  136. "Capital Loss <= 0.00",
  137. 65,
  138. "United-States"
  139. ],
  140. [
  141. 32,
  142. "Private",
  143. "High School grad",
  144. "Never-Married",
  145. "Blue-Collar",
  146. "Husband",
  147. "White",
  148. "Male",
  149. "Capital Gain <= 0.00",
  150. "Capital Loss <= 0.00",
  151. 40,
  152. "United-States"
  153. ],
  154. [
  155. 48,
  156. "Private",
  157. "Masters",
  158. "Never-Married",
  159. "White-Collar",
  160. "Husband",
  161. "Black",
  162. "Male",
  163. "Capital Gain <= 0.00",
  164. "Capital Loss <= 0.00",
  165. 45,
  166. "United-States"
  167. ]
  168. ],
  169. "covered_true": [
  170. [
  171. 32,
  172. "Private",
  173. "High School grad",
  174. "Never-Married",
  175. "Blue-Collar",
  176. "Husband",
  177. "White",
  178. "Male",
  179. "Capital Gain <= 0.00",
  180. "Capital Loss <= 0.00",
  181. 40,
  182. "United-States"
  183. ],
  184. [
  185. 44,
  186. "Local-gov",
  187. "Bachelors",
  188. "Never-Married",
  189. "White-Collar",
  190. "Husband",
  191. "White",
  192. "Male",
  193. "Capital Gain <= 0.00",
  194. "Capital Loss <= 0.00",
  195. 52,
  196. "United-States"
  197. ],
  198. [
  199. 36,
  200. "Private",
  201. "High School grad",
  202. "Never-Married",
  203. "Blue-Collar",
  204. "Unmarried",
  205. "White",
  206. "Female",
  207. "Capital Gain <= 0.00",
  208. "Capital Loss <= 0.00",
  209. 30,
  210. "United-States"
  211. ],
  212. [
  213. 56,
  214. "Private",
  215. "High School grad",
  216. "Never-Married",
  217. "Blue-Collar",
  218. "Unmarried",
  219. "Black",
  220. "Male",
  221. "Capital Gain <= 0.00",
  222. "Capital Loss <= 0.00",
  223. 40,
  224. "United-States"
  225. ],
  226. [
  227. 49,
  228. "Local-gov",
  229. "High School grad",
  230. "Never-Married",
  231. "Service",
  232. "Unmarried",
  233. "White",
  234. "Female",
  235. "Capital Gain <= 0.00",
  236. "Capital Loss <= 0.00",
  237. 30,
  238. "United-States"
  239. ],
  240. [
  241. 20,
  242. "?",
  243. "High School grad",
  244. "Never-Married",
  245. "?",
  246. "Own-child",
  247. "White",
  248. "Female",
  249. "Capital Gain <= 0.00",
  250. "Capital Loss <= 0.00",
  251. 10,
  252. "United-States"
  253. ],
  254. [
  255. 22,
  256. "?",
  257. "High School grad",
  258. "Never-Married",
  259. "?",
  260. "Own-child",
  261. "White",
  262. "Male",
  263. "Capital Gain <= 0.00",
  264. "Capital Loss <= 0.00",
  265. "Hours per week > 45.00",
  266. "United-States"
  267. ],
  268. [
  269. 29,
  270. "Private",
  271. "High School grad",
  272. "Never-Married",
  273. "Service",
  274. "Own-child",
  275. "Asian-Pac-Islander",
  276. "Male",
  277. "Capital Gain <= 0.00",
  278. "Capital Loss <= 0.00",
  279. 25,
  280. "SE-Asia"
  281. ],
  282. [
  283. 45,
  284. "Local-gov",
  285. "Masters",
  286. "Never-Married",
  287. "Professional",
  288. "Unmarried",
  289. "White",
  290. "Female",
  291. 1506,
  292. "Capital Loss <= 0.00",
  293. 45,
  294. "United-States"
  295. ],
  296. [
  297. 27,
  298. "Private",
  299. "High School grad",
  300. "Never-Married",
  301. "Blue-Collar",
  302. "Not-in-family",
  303. "White",
  304. "Male",
  305. "Capital Gain <= 0.00",
  306. "Capital Loss <= 0.00",
  307. 50,
  308. "United-States"
  309. ]
  310. ],
  311. "covered_false": [
  312. [
  313. 29,
  314. "Private",
  315. "Bachelors",
  316. "Never-Married",
  317. "Service",
  318. "Husband",
  319. "White",
  320. "Male",
  321. 7298,
  322. "Capital Loss <= 0.00",
  323. 42,
  324. "United-States"
  325. ],
  326. [
  327. 56,
  328. "Private",
  329. "Associates",
  330. "Never-Married",
  331. "Sales",
  332. "Husband",
  333. "White",
  334. "Male",
  335. 15024,
  336. "Capital Loss <= 0.00",
  337. 40,
  338. "United-States"
  339. ],
  340. [
  341. 47,
  342. "Private",
  343. "Masters",
  344. "Never-Married",
  345. "Sales",
  346. "Not-in-family",
  347. "White",
  348. "Male",
  349. 27828,
  350. "Capital Loss <= 0.00",
  351. 60,
  352. "United-States"
  353. ],
  354. [
  355. 40,
  356. "Private",
  357. "Associates",
  358. "Never-Married",
  359. "White-Collar",
  360. "Husband",
  361. "White",
  362. "Male",
  363. 7688,
  364. "Capital Loss <= 0.00",
  365. 44,
  366. "United-States"
  367. ],
  368. [
  369. 55,
  370. "Self-emp-not-inc",
  371. "High School grad",
  372. "Never-Married",
  373. "White-Collar",
  374. "Not-in-family",
  375. "White",
  376. "Male",
  377. 34095,
  378. "Capital Loss <= 0.00",
  379. 60,
  380. "United-States"
  381. ],
  382. [
  383. 53,
  384. "Private",
  385. "Masters",
  386. "Never-Married",
  387. "White-Collar",
  388. "Husband",
  389. "White",
  390. "Male",
  391. "Capital Gain <= 0.00",
  392. "Capital Loss <= 0.00",
  393. 48,
  394. "United-States"
  395. ],
  396. [
  397. 47,
  398. "Federal-gov",
  399. "Doctorate",
  400. "Never-Married",
  401. "Professional",
  402. "Husband",
  403. "White",
  404. "Male",
  405. "Capital Gain <= 0.00",
  406. "Capital Loss <= 0.00",
  407. 40,
  408. "United-States"
  409. ],
  410. [
  411. 53,
  412. "Private",
  413. "High School grad",
  414. "Never-Married",
  415. "White-Collar",
  416. "Husband",
  417. "White",
  418. "Male",
  419. "Capital Gain <= 0.00",
  420. 1977,
  421. 40,
  422. "United-States"
  423. ],
  424. [
  425. 46,
  426. "Private",
  427. "Bachelors",
  428. "Never-Married",
  429. "Sales",
  430. "Not-in-family",
  431. "White",
  432. "Male",
  433. 8614,
  434. "Capital Loss <= 0.00",
  435. 40,
  436. "United-States"
  437. ],
  438. [
  439. 44,
  440. "Local-gov",
  441. "Prof-School",
  442. "Never-Married",
  443. "Professional",
  444. "Not-in-family",
  445. "White",
  446. "Male",
  447. 10520,
  448. "Capital Loss <= 0.00",
  449. 40,
  450. "United-States"
  451. ]
  452. ],
  453. "uncovered_true": [],
  454. "uncovered_false": []
  455. },
  456. {
  457. "covered": [
  458. [
  459. 41,
  460. "State-gov",
  461. "High School grad",
  462. "Never-Married",
  463. "White-Collar",
  464. "Not-in-family",
  465. "White",
  466. "Female",
  467. "Capital Gain <= 0.00",
  468. "Capital Loss <= 0.00",
  469. 40,
  470. "United-States"
  471. ],
  472. [
  473. 64,
  474. "State-gov",
  475. "High School grad",
  476. "Never-Married",
  477. "Blue-Collar",
  478. "Not-in-family",
  479. "White",
  480. "Male",
  481. "Capital Gain <= 0.00",
  482. "Capital Loss <= 0.00",
  483. 40,
  484. "United-States"
  485. ],
  486. [
  487. 33,
  488. "State-gov",
  489. "High School grad",
  490. "Never-Married",
  491. "Service",
  492. "Unmarried",
  493. "Black",
  494. "Female",
  495. 1831,
  496. "Capital Loss <= 0.00",
  497. 40,
  498. "United-States"
  499. ],
  500. [
  501. 35,
  502. "State-gov",
  503. "High School grad",
  504. "Never-Married",
  505. "Blue-Collar",
  506. "Husband",
  507. "White",
  508. "Male",
  509. "Capital Gain <= 0.00",
  510. "Capital Loss <= 0.00",
  511. 60,
  512. "United-States"
  513. ],
  514. [
  515. 25,
  516. "State-gov",
  517. "Dropout",
  518. "Never-Married",
  519. "Blue-Collar",
  520. "Own-child",
  521. "Black",
  522. "Female",
  523. "Capital Gain <= 0.00",
  524. "Capital Loss <= 0.00",
  525. 40,
  526. "United-States"
  527. ],
  528. [
  529. 40,
  530. "State-gov",
  531. "Associates",
  532. "Never-Married",
  533. "Blue-Collar",
  534. "Husband",
  535. "White",
  536. "Male",
  537. "Capital Gain <= 0.00",
  538. "Capital Loss <= 0.00",
  539. 40,
  540. "United-States"
  541. ],
  542. [
  543. 19,
  544. "State-gov",
  545. "High School grad",
  546. "Never-Married",
  547. "Blue-Collar",
  548. "Other-relative",
  549. "White",
  550. "Male",
  551. "Capital Gain <= 0.00",
  552. "Capital Loss <= 0.00",
  553. 20,
  554. "United-States"
  555. ],
  556. [
  557. 44,
  558. "State-gov",
  559. "Dropout",
  560. "Never-Married",
  561. "Blue-Collar",
  562. "Husband",
  563. "White",
  564. "Male",
  565. "Capital Gain <= 0.00",
  566. "Capital Loss <= 0.00",
  567. 88,
  568. "United-States"
  569. ],
  570. [
  571. 80,
  572. "State-gov",
  573. "Associates",
  574. "Never-Married",
  575. "Blue-Collar",
  576. "Husband",
  577. "White",
  578. "Male",
  579. "Capital Gain <= 0.00",
  580. "Capital Loss <= 0.00",
  581. 24,
  582. "United-States"
  583. ],
  584. [
  585. 21,
  586. "State-gov",
  587. "High School grad",
  588. "Never-Married",
  589. "Professional",
  590. "Own-child",
  591. "White",
  592. "Female",
  593. "Capital Gain <= 0.00",
  594. "Capital Loss <= 0.00",
  595. 20,
  596. "United-States"
  597. ]
  598. ],
  599. "covered_true": [
  600. [
  601. 22,
  602. "State-gov",
  603. "High School grad",
  604. "Never-Married",
  605. "Service",
  606. "Husband",
  607. "White",
  608. "Male",
  609. "Capital Gain <= 0.00",
  610. "Capital Loss <= 0.00",
  611. 25,
  612. "United-States"
  613. ],
  614. [
  615. 49,
  616. "State-gov",
  617. "High School grad",
  618. "Never-Married",
  619. "Service",
  620. "Not-in-family",
  621. "White",
  622. "Female",
  623. "Capital Gain <= 0.00",
  624. "Capital Loss <= 0.00",
  625. 40,
  626. "United-States"
  627. ],
  628. [
  629. 22,
  630. "State-gov",
  631. "Bachelors",
  632. "Never-Married",
  633. "?",
  634. "Not-in-family",
  635. "White",
  636. "Male",
  637. "Capital Gain <= 0.00",
  638. "Capital Loss <= 0.00",
  639. 25,
  640. "United-States"
  641. ],
  642. [
  643. 31,
  644. "State-gov",
  645. "Bachelors",
  646. "Never-Married",
  647. "Professional",
  648. "Husband",
  649. "White",
  650. "Male",
  651. "Capital Gain <= 0.00",
  652. "Capital Loss <= 0.00",
  653. 50,
  654. "United-States"
  655. ],
  656. [
  657. 18,
  658. "State-gov",
  659. "Dropout",
  660. "Never-Married",
  661. "Blue-Collar",
  662. "Not-in-family",
  663. "Black",
  664. "Male",
  665. "Capital Gain <= 0.00",
  666. "Capital Loss <= 0.00",
  667. 40,
  668. "United-States"
  669. ],
  670. [
  671. 56,
  672. "State-gov",
  673. "High School grad",
  674. "Never-Married",
  675. "Blue-Collar",
  676. "Unmarried",
  677. "Black",
  678. "Male",
  679. "Capital Gain <= 0.00",
  680. "Capital Loss <= 0.00",
  681. 40,
  682. "United-States"
  683. ],
  684. [
  685. 26,
  686. "State-gov",
  687. "Dropout",
  688. "Never-Married",
  689. "Service",
  690. "Unmarried",
  691. "White",
  692. "Female",
  693. "Capital Gain <= 0.00",
  694. "Capital Loss <= 0.00",
  695. 40,
  696. "United-States"
  697. ],
  698. [
  699. 38,
  700. "State-gov",
  701. "Bachelors",
  702. "Never-Married",
  703. "White-Collar",
  704. "Husband",
  705. "White",
  706. "Male",
  707. "Capital Gain <= 0.00",
  708. "Capital Loss <= 0.00",
  709. 40,
  710. "United-States"
  711. ],
  712. [
  713. 52,
  714. "State-gov",
  715. "High School grad",
  716. "Never-Married",
  717. "Blue-Collar",
  718. "Husband",
  719. "White",
  720. "Male",
  721. "Capital Gain <= 0.00",
  722. "Capital Loss <= 0.00",
  723. 70,
  724. "United-States"
  725. ],
  726. [
  727. 25,
  728. "State-gov",
  729. "Associates",
  730. "Never-Married",
  731. "Professional",
  732. "Wife",
  733. "White",
  734. "Female",
  735. "Capital Gain <= 0.00",
  736. 1887,
  737. 40,
  738. "United-States"
  739. ]
  740. ],
  741. "covered_false": [
  742. [
  743. 46,
  744. "State-gov",
  745. "Prof-School",
  746. "Never-Married",
  747. "Professional",
  748. "Husband",
  749. "White",
  750. "Male",
  751. "Capital Gain <= 0.00",
  752. "Capital Loss <= 0.00",
  753. 45,
  754. "United-States"
  755. ],
  756. [
  757. 42,
  758. "State-gov",
  759. "Bachelors",
  760. "Never-Married",
  761. "White-Collar",
  762. "Husband",
  763. "White",
  764. "Male",
  765. 15024,
  766. "Capital Loss <= 0.00",
  767. 50,
  768. "United-States"
  769. ],
  770. [
  771. 46,
  772. "State-gov",
  773. "Prof-School",
  774. "Never-Married",
  775. "Professional",
  776. "Husband",
  777. "White",
  778. "Male",
  779. 15024,
  780. "Capital Loss <= 0.00",
  781. 40,
  782. "United-States"
  783. ],
  784. [
  785. 54,
  786. "State-gov",
  787. "Doctorate",
  788. "Never-Married",
  789. "White-Collar",
  790. "Not-in-family",
  791. "White",
  792. "Female",
  793. "Capital Gain <= 0.00",
  794. "Capital Loss <= 0.00",
  795. 40,
  796. "United-States"
  797. ],
  798. [
  799. 42,
  800. "State-gov",
  801. "Masters",
  802. "Never-Married",
  803. "White-Collar",
  804. "Not-in-family",
  805. "White",
  806. "Female",
  807. 14084,
  808. "Capital Loss <= 0.00",
  809. 60,
  810. "United-States"
  811. ],
  812. [
  813. 37,
  814. "State-gov",
  815. "Masters",
  816. "Never-Married",
  817. "Professional",
  818. "Husband",
  819. "White",
  820. "Male",
  821. "Capital Gain <= 0.00",
  822. "Capital Loss <= 0.00",
  823. 45,
  824. "United-States"
  825. ]
  826. ],
  827. "uncovered_true": [],
  828. "uncovered_false": []
  829. }
  830. ],
  831. "all_precision": 0,
  832. "num_preds": 1000101,
  833. "names": [
  834. "Marital Status = Never-Married",
  835. "Workclass = State-gov"
  836. ],
  837. "instance": [
  838. [
  839. 39
  840. ],
  841. [
  842. 7
  843. ],
  844. [
  845. "28.00 < Age <= 37.00"
  846. ],
  847. [
  848. "28.00 < Age <= 37.00"
  849. ],
  850. [
  851. "28.00 < Age <= 37.00"
  852. ],
  853. [
  854. "28.00 < Age <= 37.00"
  855. ],
  856. [
  857. 4
  858. ],
  859. [
  860. "28.00 < Age <= 37.00"
  861. ],
  862. [
  863. 2174
  864. ],
  865. [
  866. "Age <= 28.00"
  867. ],
  868. [
  869. 40
  870. ],
  871. [
  872. 9
  873. ]
  874. ],
  875. "prediction": 0
  876. }
  877. }