@elaraai/east-py-datascience 0.0.2-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/LICENSE.md +18 -0
  2. package/README.md +104 -0
  3. package/dist/gp/gp.d.ts +398 -0
  4. package/dist/gp/gp.d.ts.map +1 -0
  5. package/dist/gp/gp.js +170 -0
  6. package/dist/gp/gp.js.map +1 -0
  7. package/dist/index.d.ts +27 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +39 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/lightgbm/lightgbm.d.ts +494 -0
  12. package/dist/lightgbm/lightgbm.d.ts.map +1 -0
  13. package/dist/lightgbm/lightgbm.js +155 -0
  14. package/dist/lightgbm/lightgbm.js.map +1 -0
  15. package/dist/mads/mads.d.ts +413 -0
  16. package/dist/mads/mads.d.ts.map +1 -0
  17. package/dist/mads/mads.js +221 -0
  18. package/dist/mads/mads.js.map +1 -0
  19. package/dist/ngboost/ngboost.d.ts +433 -0
  20. package/dist/ngboost/ngboost.d.ts.map +1 -0
  21. package/dist/ngboost/ngboost.js +178 -0
  22. package/dist/ngboost/ngboost.js.map +1 -0
  23. package/dist/optuna/optuna.d.ts +797 -0
  24. package/dist/optuna/optuna.d.ts.map +1 -0
  25. package/dist/optuna/optuna.js +268 -0
  26. package/dist/optuna/optuna.js.map +1 -0
  27. package/dist/scipy/scipy.d.ts +954 -0
  28. package/dist/scipy/scipy.d.ts.map +1 -0
  29. package/dist/scipy/scipy.js +287 -0
  30. package/dist/scipy/scipy.js.map +1 -0
  31. package/dist/shap/shap.d.ts +657 -0
  32. package/dist/shap/shap.d.ts.map +1 -0
  33. package/dist/shap/shap.js +241 -0
  34. package/dist/shap/shap.js.map +1 -0
  35. package/dist/simanneal/simanneal.d.ts +531 -0
  36. package/dist/simanneal/simanneal.d.ts.map +1 -0
  37. package/dist/simanneal/simanneal.js +231 -0
  38. package/dist/simanneal/simanneal.js.map +1 -0
  39. package/dist/sklearn/sklearn.d.ts +1272 -0
  40. package/dist/sklearn/sklearn.d.ts.map +1 -0
  41. package/dist/sklearn/sklearn.js +307 -0
  42. package/dist/sklearn/sklearn.js.map +1 -0
  43. package/dist/torch/torch.d.ts +658 -0
  44. package/dist/torch/torch.d.ts.map +1 -0
  45. package/dist/torch/torch.js +233 -0
  46. package/dist/torch/torch.js.map +1 -0
  47. package/dist/tsconfig.tsbuildinfo +1 -0
  48. package/dist/types.d.ts +80 -0
  49. package/dist/types.d.ts.map +1 -0
  50. package/dist/types.js +81 -0
  51. package/dist/types.js.map +1 -0
  52. package/dist/xgboost/xgboost.d.ts +504 -0
  53. package/dist/xgboost/xgboost.d.ts.map +1 -0
  54. package/dist/xgboost/xgboost.js +177 -0
  55. package/dist/xgboost/xgboost.js.map +1 -0
  56. package/package.json +82 -0
@@ -0,0 +1,241 @@
1
+ /**
2
+ * Copyright (c) 2025 Elara AI Pty Ltd
3
+ * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
4
+ */
5
+ /**
6
+ * SHAP platform functions for East.
7
+ *
8
+ * Provides model-agnostic feature importance and explainability using SHAP values.
9
+ * Uses cloudpickle for explainer serialization.
10
+ *
11
+ * @packageDocumentation
12
+ */
13
+ import { East, StructType, VariantType, OptionType, IntegerType, FloatType, StringType, ArrayType, BlobType, } from "@elaraai/east";
14
+ import { VectorType, MatrixType } from "../types.js";
15
+ // Re-export shared types for convenience
16
+ export { VectorType, MatrixType } from "../types.js";
17
+ // ============================================================================
18
+ // Data Types
19
+ // ============================================================================
20
+ /** String vector type for feature names */
21
+ export const StringVectorType = ArrayType(StringType);
22
+ // ============================================================================
23
+ // Result Types
24
+ // ============================================================================
25
+ /**
26
+ * Result type for SHAP value computation.
27
+ */
28
+ export const ShapResultType = StructType({
29
+ /** SHAP values matrix (n_samples x n_features) */
30
+ shap_values: MatrixType,
31
+ /** Base value (expected model output) */
32
+ base_value: FloatType,
33
+ /** Feature names */
34
+ feature_names: StringVectorType,
35
+ });
36
+ /**
37
+ * Result type for feature importance.
38
+ */
39
+ export const FeatureImportanceType = StructType({
40
+ /** Feature names */
41
+ feature_names: StringVectorType,
42
+ /** Mean absolute SHAP value for each feature */
43
+ importances: VectorType,
44
+ /** Standard deviation of absolute SHAP values */
45
+ std: OptionType(VectorType),
46
+ });
47
+ // ============================================================================
48
+ // Model Blob Types
49
+ // ============================================================================
50
+ /**
51
+ * Model blob type for serialized SHAP explainers.
52
+ */
53
+ export const ShapModelBlobType = VariantType({
54
+ /** SHAP TreeExplainer for tree-based models */
55
+ shap_tree_explainer: StructType({
56
+ /** Cloudpickle serialized explainer */
57
+ data: BlobType,
58
+ /** Number of input features */
59
+ n_features: IntegerType,
60
+ }),
61
+ /** SHAP KernelExplainer for any model */
62
+ shap_kernel_explainer: StructType({
63
+ /** Cloudpickle serialized explainer */
64
+ data: BlobType,
65
+ /** Number of input features */
66
+ n_features: IntegerType,
67
+ }),
68
+ });
69
+ /**
70
+ * Tree-based model blob type - accepts XGBoost and LightGBM models.
71
+ */
72
+ export const TreeModelBlobType = VariantType({
73
+ /** XGBoost regressor */
74
+ xgboost_regressor: StructType({
75
+ data: BlobType,
76
+ n_features: IntegerType,
77
+ }),
78
+ /** XGBoost classifier */
79
+ xgboost_classifier: StructType({
80
+ data: BlobType,
81
+ n_features: IntegerType,
82
+ n_classes: IntegerType,
83
+ }),
84
+ /** LightGBM regressor */
85
+ lightgbm_regressor: StructType({
86
+ data: BlobType,
87
+ n_features: IntegerType,
88
+ }),
89
+ /** LightGBM classifier */
90
+ lightgbm_classifier: StructType({
91
+ data: BlobType,
92
+ n_features: IntegerType,
93
+ n_classes: IntegerType,
94
+ }),
95
+ });
96
+ /**
97
+ * Any model blob type - accepts any model for kernel explainer.
98
+ * Includes all tree-based models plus NGBoost, GP, and Torch.
99
+ */
100
+ export const AnyModelBlobType = VariantType({
101
+ // Tree-based
102
+ xgboost_regressor: StructType({
103
+ data: BlobType,
104
+ n_features: IntegerType,
105
+ }),
106
+ xgboost_classifier: StructType({
107
+ data: BlobType,
108
+ n_features: IntegerType,
109
+ n_classes: IntegerType,
110
+ }),
111
+ lightgbm_regressor: StructType({
112
+ data: BlobType,
113
+ n_features: IntegerType,
114
+ }),
115
+ lightgbm_classifier: StructType({
116
+ data: BlobType,
117
+ n_features: IntegerType,
118
+ n_classes: IntegerType,
119
+ }),
120
+ // NGBoost
121
+ ngboost_regressor: StructType({
122
+ data: BlobType,
123
+ distribution: VariantType({
124
+ normal: StructType({}),
125
+ lognormal: StructType({}),
126
+ }),
127
+ n_features: IntegerType,
128
+ }),
129
+ // GP
130
+ gp_regressor: StructType({
131
+ data: BlobType,
132
+ n_features: IntegerType,
133
+ kernel_type: StringType,
134
+ }),
135
+ // Torch
136
+ torch_mlp: StructType({
137
+ data: BlobType,
138
+ n_features: IntegerType,
139
+ hidden_layers: ArrayType(IntegerType),
140
+ output_dim: IntegerType,
141
+ }),
142
+ });
143
+ // ============================================================================
144
+ // Platform Functions
145
+ // ============================================================================
146
+ /**
147
+ * Create a SHAP TreeExplainer for tree-based models.
148
+ *
149
+ * Works with XGBoost and LightGBM models (regressor and classifier).
150
+ *
151
+ * @param model - Tree-based model blob (XGBoost or LightGBM)
152
+ * @returns SHAP TreeExplainer blob
153
+ */
154
+ export const shap_tree_explainer_create = East.platform("shap_tree_explainer_create", [TreeModelBlobType], ShapModelBlobType);
155
+ /**
156
+ * Create a SHAP KernelExplainer for any model.
157
+ *
158
+ * Works with any model that has a predict method (NGBoost, GP, Torch, etc.).
159
+ * Requires background data for computing expected values.
160
+ *
161
+ * @param model - Any model blob
162
+ * @param X_background - Background data for computing expected values
163
+ * @returns SHAP KernelExplainer blob
164
+ */
165
+ export const shap_kernel_explainer_create = East.platform("shap_kernel_explainer_create", [AnyModelBlobType, MatrixType], ShapModelBlobType);
166
+ /**
167
+ * Compute SHAP values for samples.
168
+ *
169
+ * @param explainer - SHAP explainer blob
170
+ * @param X - Feature matrix to explain
171
+ * @param feature_names - Names of features
172
+ * @returns SHAP values, base value, and feature names
173
+ */
174
+ export const shap_compute_values = East.platform("shap_compute_values", [ShapModelBlobType, MatrixType, StringVectorType], ShapResultType);
175
+ /**
176
+ * Compute global feature importance from SHAP values.
177
+ *
178
+ * @param shap_values - SHAP values matrix
179
+ * @param feature_names - Names of features
180
+ * @returns Feature importance with mean |SHAP| values
181
+ */
182
+ export const shap_feature_importance = East.platform("shap_feature_importance", [MatrixType, StringVectorType], FeatureImportanceType);
183
+ // ============================================================================
184
+ // Grouped Export
185
+ // ============================================================================
186
+ /**
187
+ * Type definitions for SHAP functions.
188
+ */
189
+ export const ShapTypes = {
190
+ /** Vector type (array of floats) */
191
+ VectorType,
192
+ /** Matrix type (2D array of floats) */
193
+ MatrixType,
194
+ /** String vector type */
195
+ StringVectorType,
196
+ /** SHAP result type */
197
+ ShapResultType,
198
+ /** Feature importance type */
199
+ FeatureImportanceType,
200
+ /** SHAP explainer model blob type */
201
+ ShapModelBlobType,
202
+ /** Tree model blob type for input */
203
+ TreeModelBlobType,
204
+ /** Any model blob type for kernel explainer */
205
+ AnyModelBlobType,
206
+ };
207
+ /**
208
+ * SHAP explainability functions.
209
+ *
210
+ * Provides model-agnostic feature importance and SHAP value computation.
211
+ *
212
+ * @example
213
+ * ```ts
214
+ * import { East, variant } from "@elaraai/east";
215
+ * import { Shap, LightGBM } from "@elaraai/east-py-datascience";
216
+ *
217
+ * const explain = East.function([LightGBM.Types.ModelBlobType, Shap.Types.MatrixType], Shap.Types.ShapResultType, ($, model, X) => {
218
+ * // Create explainer
219
+ * const explainer = $.let(Shap.treeExplainerCreate(model));
220
+ *
221
+ * // Compute SHAP values
222
+ * const feature_names = $.let(["feature1", "feature2"]);
223
+ * const result = $.let(Shap.computeValues(explainer, X, feature_names));
224
+ *
225
+ * return $.return(result);
226
+ * });
227
+ * ```
228
+ */
229
+ export const Shap = {
230
+ /** Create TreeExplainer for tree-based models */
231
+ treeExplainerCreate: shap_tree_explainer_create,
232
+ /** Create KernelExplainer for any model */
233
+ kernelExplainerCreate: shap_kernel_explainer_create,
234
+ /** Compute SHAP values */
235
+ computeValues: shap_compute_values,
236
+ /** Compute feature importance from SHAP values */
237
+ featureImportance: shap_feature_importance,
238
+ /** Type definitions */
239
+ Types: ShapTypes,
240
+ };
241
+ //# sourceMappingURL=shap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shap.js","sourceRoot":"","sources":["../../src/shap/shap.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;GAOG;AAEH,OAAO,EACH,IAAI,EACJ,UAAU,EACV,WAAW,EACX,UAAU,EACV,WAAW,EACX,SAAS,EACT,UAAU,EACV,SAAS,EACT,QAAQ,GACX,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAErD,yCAAyC;AACzC,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAErD,+EAA+E;AAC/E,aAAa;AACb,+EAA+E;AAE/E,2CAA2C;AAC3C,MAAM,CAAC,MAAM,gBAAgB,GAAG,SAAS,CAAC,UAAU,CAAC,CAAC;AAEtD,+EAA+E;AAC/E,eAAe;AACf,+EAA+E;AAE/E;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,UAAU,CAAC;IACrC,kDAAkD;IAClD,WAAW,EAAE,UAAU;IACvB,yCAAyC;IACzC,UAAU,EAAE,SAAS;IACrB,oBAAoB;IACpB,aAAa,EAAE,gBAAgB;CAClC,CAAC,CAAC;AAEH;;GAEG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,UAAU,CAAC;IAC5C,oBAAoB;IACpB,aAAa,EAAE,gBAAgB;IAC/B,gDAAgD;IAChD,WAAW,EAAE,UAAU;IACvB,iDAAiD;IACjD,GAAG,EAAE,UAAU,CAAC,UAAU,CAAC;CAC9B,CAAC,CAAC;AAEH,+EAA+E;AAC/E,mBAAmB;AACnB,+E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