From 3403597529bbdc0ecf15ccf8757523952471b13a Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Fri, 9 Apr 2021 10:39:45 -0400 Subject: [PATCH 01/14] Add curated schema for AdditiveChi2Sampler --- lale/lib/sklearn/__init__.py | 3 + lale/lib/sklearn/additive_chi2_sampler.py | 93 +++++++++++++++++++++++ test/test_core_transformers.py | 2 + 3 files changed, 98 insertions(+) create mode 100644 lale/lib/sklearn/additive_chi2_sampler.py diff --git a/lale/lib/sklearn/__init__.py b/lale/lib/sklearn/__init__.py index c28c9a802..bb4a6b3bb 100644 --- a/lale/lib/sklearn/__init__.py +++ b/lale/lib/sklearn/__init__.py @@ -58,6 +58,7 @@ Transformers: +* lale.lib.sklearn. `AdditiveChi2Sampler`_ * lale.lib.sklearn. `ColumnTransformer`_ * lale.lib.sklearn. `FeatureAgglomeration`_ * lale.lib.sklearn. `FunctionTransformer`_ @@ -83,6 +84,7 @@ .. _`AdaBoostClassifier`: lale.lib.sklearn.ada_boost_classifier.html .. _`AdaBoostRegressor`: lale.lib.sklearn.ada_boost_regressor.html +.. _`AdditiveChi2Sampler`: lale.lib.sklearn.additive_chi2_sampler.html .. _`BaggingClassifier`: lale.lib.sklearn.bagging_classifier.html .. _`ColumnTransformer`: lale.lib.sklearn.column_transformer.html .. _`DecisionTreeClassifier`: lale.lib.sklearn.decision_tree_classifier.html @@ -137,6 +139,7 @@ from .ada_boost_classifier import AdaBoostClassifier from .ada_boost_regressor import AdaBoostRegressor +from .additive_chi2_sampler import AdditiveChi2Sampler from .bagging_classifier import BaggingClassifier from .column_transformer import ColumnTransformer from .decision_tree_classifier import DecisionTreeClassifier diff --git a/lale/lib/sklearn/additive_chi2_sampler.py b/lale/lib/sklearn/additive_chi2_sampler.py new file mode 100644 index 000000000..e07d28ba4 --- /dev/null +++ b/lale/lib/sklearn/additive_chi2_sampler.py @@ -0,0 +1,93 @@ +from sklearn.kernel_approximation import AdditiveChi2Sampler as Op + +from lale.docstrings import set_docstrings +from lale.operators import make_operator + +_hyperparams_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Approximate feature map for additive chi2 kernel.", + "allOf": [ + { + "type": "object", + "required": ["sample_steps", "sample_interval"], + "relevantToOptimizer": ["sample_steps"], + "additionalProperties": False, + "properties": { + "sample_steps": { + "type": "integer", + "minimumForOptimizer": 1, + "maximumForOptimizer": 3, + "distribution": "uniform", + "default": 2, + "description": "Gives the number of (complex) sampling points.", + }, + "sample_interval": { + "anyOf": [{"type": "number"}, {"enum": [None]}], + "default": None, + "description": "Sampling interval", + }, + }, + }, + { + "description": "sample_interval must be specified when sample_steps not in {1,2,3}", + "anyOf": [ + { + "type": "object", + "properties:": {"sample_steps": {"enum": [1, 2, 3]}}, + }, + { + "type": "object", + "properties:": {"sample_interval": {"type": "number"}}, + }, + ], + }, + ], +} +_input_fit_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Set the parameters", + "type": "object", + "required": ["X"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Training data, where n_samples in the number of samples and n_features is the number of features.", + } + }, +} +_input_transform_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Apply approximate feature map to X.", + "type": "object", + "required": ["X"], + "properties": { + "X": {"type": "array", "items": {"type": "array", "items": {"type": "number"}}} + }, +} +_output_transform_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Whether the return value is an array of sparse matrix depends on the type of the input X.", + "laleType": "Any", + "XXX TODO XXX": "{array, sparse matrix}, shape = (n_samples, n_features * (2*sample_steps + 1))", +} +_combined_schemas = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": """`Approximate feature map for additive chi2 kernel`_ from sklearn + +.. _`Approximate feature map for additive chi2 kernel.`: https://scikit-learn.org/stable/modules/generated/sklearn.kernel_approximation.AdditiveChi2Sampler +""", + "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.additive_chi2_sampler.html", + "import_from": "sklearn.kernel_approximation", + "type": "object", + "tags": {"pre": [], "op": ["transformer"], "post": []}, + "properties": { + "hyperparams": _hyperparams_schema, + "input_fit": _input_fit_schema, + "input_transform": _input_transform_schema, + "output_transform": _output_transform_schema, + }, +} +AdditiveChi2Sampler = make_operator(Op, _combined_schemas) + +set_docstrings(AdditiveChi2Sampler) diff --git a/test/test_core_transformers.py b/test/test_core_transformers.py index e7695f8b8..a706b8164 100644 --- a/test/test_core_transformers.py +++ b/test/test_core_transformers.py @@ -11,6 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +# limitations under the License. import unittest from test import EnableSchemaValidation @@ -99,6 +100,7 @@ def test_feature_preprocessor(self): feature_preprocessors = [ + "lale.lib.sklearn.AdditiveChi2Sampler", "lale.lib.sklearn.PolynomialFeatures", "lale.lib.sklearn.PCA", "lale.lib.sklearn.Nystroem", From f81a113b579e401b5709ec0099cad2099aac6483 Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Fri, 9 Apr 2021 11:01:34 -0400 Subject: [PATCH 02/14] Add curated schema for ArdRegression --- lale/lib/sklearn/__init__.py | 3 + lale/lib/sklearn/ard_regression.py | 165 +++++++++++++++++++++++++++++ test/test_core_regressors.py | 1 + 3 files changed, 169 insertions(+) create mode 100644 lale/lib/sklearn/ard_regression.py diff --git a/lale/lib/sklearn/__init__.py b/lale/lib/sklearn/__init__.py index bb4a6b3bb..edd4981ed 100644 --- a/lale/lib/sklearn/__init__.py +++ b/lale/lib/sklearn/__init__.py @@ -44,6 +44,7 @@ Regressors: * lale.lib.sklearn. `AdaBoostRegressor`_ +* lale.lib.sklearn. `ARDRegression`_ * lale.lib.sklearn. `DecisionTreeRegressor`_ * lale.lib.sklearn. `DummyRegressor`_ * lale.lib.sklearn. `ExtraTreesRegressor`_ @@ -85,6 +86,7 @@ .. _`AdaBoostClassifier`: lale.lib.sklearn.ada_boost_classifier.html .. _`AdaBoostRegressor`: lale.lib.sklearn.ada_boost_regressor.html .. _`AdditiveChi2Sampler`: lale.lib.sklearn.additive_chi2_sampler.html +.. _`ARDRegression`: lale.lib.sklearn.ard_regression.html .. _`BaggingClassifier`: lale.lib.sklearn.bagging_classifier.html .. _`ColumnTransformer`: lale.lib.sklearn.column_transformer.html .. _`DecisionTreeClassifier`: lale.lib.sklearn.decision_tree_classifier.html @@ -140,6 +142,7 @@ from .ada_boost_classifier import AdaBoostClassifier from .ada_boost_regressor import AdaBoostRegressor from .additive_chi2_sampler import AdditiveChi2Sampler +from .ard_regression import ARDRegression from .bagging_classifier import BaggingClassifier from .column_transformer import ColumnTransformer from .decision_tree_classifier import DecisionTreeClassifier diff --git a/lale/lib/sklearn/ard_regression.py b/lale/lib/sklearn/ard_regression.py new file mode 100644 index 000000000..dcc0c0d0c --- /dev/null +++ b/lale/lib/sklearn/ard_regression.py @@ -0,0 +1,165 @@ +from sklearn.linear_model import ARDRegression as Op + +from lale.docstrings import set_docstrings +from lale.operators import make_operator + +_hyperparams_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "ARDRegression Hyperparameter schema.", + "allOf": [ + { + "type": "object", + "required": [ + "n_iter", + "tol", + "alpha_1", + "alpha_2", + "lambda_1", + "lambda_2", + "compute_score", + "threshold_lambda", + "fit_intercept", + "normalize", + "copy_X", + "verbose", + ], + "relevantToOptimizer": [ + "n_iter", + "tol", + "compute_score", + "fit_intercept", + "normalize", + ], + "additionalProperties": False, + "properties": { + "n_iter": { + "type": "integer", + "minimumForOptimizer": 5, + "maximumForOptimizer": 1000, + "distribution": "uniform", + "default": 300, + "description": "Maximum number of iterations", + }, + "tol": { + "type": "number", + "minimumForOptimizer": 1e-08, + "maximumForOptimizer": 0.01, + "distribution": "loguniform", + "default": 0.001, + "description": "Stop the algorithm if w has converged", + }, + "alpha_1": { + "type": "number", + "default": 1e-06, + "description": "Hyper-parameter : shape parameter for the Gamma distribution prior over the alpha parameter", + }, + "alpha_2": { + "type": "number", + "default": 1e-06, + "description": "Hyper-parameter : inverse scale parameter (rate parameter) for the Gamma distribution prior over the alpha parameter", + }, + "lambda_1": { + "type": "number", + "default": 1e-06, + "description": "Hyper-parameter : shape parameter for the Gamma distribution prior over the lambda parameter", + }, + "lambda_2": { + "type": "number", + "default": 1e-06, + "description": "Hyper-parameter : inverse scale parameter (rate parameter) for the Gamma distribution prior over the lambda parameter", + }, + "compute_score": { + "type": "boolean", + "default": False, + "description": "If True, compute the objective function at each step of the model", + }, + "threshold_lambda": { + "type": "number", + "default": 10000.0, + "description": "threshold for removing (pruning) weights with high precision from the computation", + }, + "fit_intercept": { + "type": "boolean", + "default": True, + "description": "whether to calculate the intercept for this model", + }, + "normalize": { + "type": "boolean", + "default": False, + "description": "This parameter is ignored when ``fit_intercept`` is set to False", + }, + "copy_X": { + "type": "boolean", + "default": True, + "description": "If True, X will be copied; else, it may be overwritten.", + }, + "verbose": { + "type": "boolean", + "default": False, + "description": "Verbose mode when fitting the model.", + }, + }, + } + ], +} +_input_fit_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Fit the ARDRegression model according to the given training data", + "type": "object", + "required": ["X", "y"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Training vector, where n_samples in the number of samples and n_features is the number of features.", + }, + "y": { + "type": "array", + "items": {"type": "number"}, + "description": "Target values (integers)", + }, + }, +} +_input_predict_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Predict using the linear model.", + "type": "object", + "required": ["X"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Samples.", + }, + "return_std": { + "anyOf": [{"type": "boolean"}, {"enum": [None]}], + "default": None, + "description": "Whether to return the standard deviation of posterior prediction.", + }, + }, +} +_output_predict_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Predict using the linear model.", + "laleType": "Any", +} +_combined_schemas = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": """`ARDRegression`_ Bayesian ARD regression from sklearn. + +.. _`ARDRegression`: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.ARDRegression" +""", + "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.ard_regression.html", + "import_from": "sklearn.linear_model", + "type": "object", + "tags": {"pre": [], "op": ["estimator"], "post": []}, + "properties": { + "hyperparams": _hyperparams_schema, + "input_fit": _input_fit_schema, + "input_predict": _input_predict_schema, + "output_predict": _output_predict_schema, + }, +} +ARDRegression = make_operator(Op, _combined_schemas) + +set_docstrings(ARDRegression) diff --git a/test/test_core_regressors.py b/test/test_core_regressors.py index 82a52b430..2863ab956 100644 --- a/test/test_core_regressors.py +++ b/test/test_core_regressors.py @@ -88,6 +88,7 @@ def test_regressor(self): regressors = [ + "lale.lib.sklearn.ARDRegression", "lale.lib.sklearn.DummyRegressor", "lale.lib.sklearn.RandomForestRegressor", "lale.lib.sklearn.DecisionTreeRegressor", From 7203054dbda4bd603bada47c90488f737644c2ec Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Fri, 9 Apr 2021 16:23:17 -0400 Subject: [PATCH 03/14] Add curated schema for BayesianRidge --- lale/lib/sklearn/__init__.py | 3 + lale/lib/sklearn/bayesian_ridge.py | 187 +++++++++++++++++++++++++++++ test/test_core_regressors.py | 1 + 3 files changed, 191 insertions(+) create mode 100644 lale/lib/sklearn/bayesian_ridge.py diff --git a/lale/lib/sklearn/__init__.py b/lale/lib/sklearn/__init__.py index edd4981ed..375dbfc01 100644 --- a/lale/lib/sklearn/__init__.py +++ b/lale/lib/sklearn/__init__.py @@ -45,6 +45,7 @@ * lale.lib.sklearn. `AdaBoostRegressor`_ * lale.lib.sklearn. `ARDRegression`_ +* lale.lib.sklearn. `BayesianRidge`_ * lale.lib.sklearn. `DecisionTreeRegressor`_ * lale.lib.sklearn. `DummyRegressor`_ * lale.lib.sklearn. `ExtraTreesRegressor`_ @@ -88,6 +89,7 @@ .. _`AdditiveChi2Sampler`: lale.lib.sklearn.additive_chi2_sampler.html .. _`ARDRegression`: lale.lib.sklearn.ard_regression.html .. _`BaggingClassifier`: lale.lib.sklearn.bagging_classifier.html +.. _`BayesianRidge`: lale.lib.sklearn.bayesian_ridge.html .. _`ColumnTransformer`: lale.lib.sklearn.column_transformer.html .. _`DecisionTreeClassifier`: lale.lib.sklearn.decision_tree_classifier.html .. _`DecisionTreeRegressor`: lale.lib.sklearn.decision_tree_regressor.html @@ -144,6 +146,7 @@ from .additive_chi2_sampler import AdditiveChi2Sampler from .ard_regression import ARDRegression from .bagging_classifier import BaggingClassifier +from .bayesian_ridge import BayesianRidge from .column_transformer import ColumnTransformer from .decision_tree_classifier import DecisionTreeClassifier from .decision_tree_regressor import DecisionTreeRegressor diff --git a/lale/lib/sklearn/bayesian_ridge.py b/lale/lib/sklearn/bayesian_ridge.py new file mode 100644 index 000000000..4fa8c996e --- /dev/null +++ b/lale/lib/sklearn/bayesian_ridge.py @@ -0,0 +1,187 @@ +import typing + +import sklearn +from sklearn.linear_model import BayesianRidge as Op + +import lale.operators +from lale.docstrings import set_docstrings +from lale.operators import make_operator +from lale.schemas import AnyOf, Float, Null + +_hyperparams_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Bayesian ridge regression", + "allOf": [ + { + "type": "object", + "required": [ + "n_iter", + "tol", + "alpha_1", + "alpha_2", + "lambda_1", + "lambda_2", + "compute_score", + "fit_intercept", + "normalize", + "copy_X", + "verbose", + ], + "relevantToOptimizer": [ + "n_iter", + "tol", + "compute_score", + "fit_intercept", + "normalize", + ], + "additionalProperties": False, + "properties": { + "n_iter": { + "type": "integer", + "minimumForOptimizer": 5, + "maximumForOptimizer": 1000, + "distribution": "uniform", + "default": 300, + "description": "Maximum number of iterations", + }, + "tol": { + "type": "number", + "minimumForOptimizer": 1e-08, + "maximumForOptimizer": 0.01, + "distribution": "loguniform", + "default": 0.001, + "description": "Stop the algorithm if w has converged", + }, + "alpha_1": { + "type": "number", + "default": 1e-06, + "description": "Hyper-parameter : shape parameter for the Gamma distribution prior over the alpha parameter", + }, + "alpha_2": { + "type": "number", + "default": 1e-06, + "description": "Hyper-parameter : inverse scale parameter (rate parameter) for the Gamma distribution prior over the alpha parameter", + }, + "lambda_1": { + "type": "number", + "default": 1e-06, + "description": "Hyper-parameter : shape parameter for the Gamma distribution prior over the lambda parameter", + }, + "lambda_2": { + "type": "number", + "default": 1e-06, + "description": "Hyper-parameter : inverse scale parameter (rate parameter) for the Gamma distribution prior over the lambda parameter", + }, + "compute_score": { + "type": "boolean", + "default": False, + "description": "If True, compute the objective function at each step of the model", + }, + "fit_intercept": { + "type": "boolean", + "default": True, + "description": "whether to calculate the intercept for this model", + }, + "normalize": { + "type": "boolean", + "default": False, + "description": "This parameter is ignored when ``fit_intercept`` is set to False", + }, + "copy_X": { + "type": "boolean", + "default": True, + "description": "If True, X will be copied; else, it may be overwritten.", + }, + "verbose": { + "type": "boolean", + "default": False, + "description": "Verbose mode when fitting the model.", + }, + }, + } + ], +} +_input_fit_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Fit the model", + "type": "object", + "required": ["X", "y"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Training data", + }, + "y": { + "type": "array", + "items": {"type": "number"}, + "description": "Target values", + }, + "sample_weight": { + "type": "array", + "items": {"type": "number"}, + "description": "Individual weights for each sample ", + }, + }, +} +_input_predict_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Predict using the linear model.", + "type": "object", + "required": ["X"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Samples.", + }, + "return_std": { + "anyOf": [{"type": "boolean"}, {"enum": [None]}], + "default": None, + "description": "Whether to return the standard deviation of posterior prediction.", + }, + }, +} +_output_predict_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Predict using the linear model.", + "laleType": "Any", +} +_combined_schemas = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": """`Bayesian ridge regression`_ from sklearn + +.. _`Bayesian ridge regression`: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.BayesianRidge +""", + "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.bayesian_ridge.html", + "import_from": "sklearn.linear_model", + "type": "object", + "tags": {"pre": [], "op": ["estimator"], "post": []}, + "properties": { + "hyperparams": _hyperparams_schema, + "input_fit": _input_fit_schema, + "input_predict": _input_predict_schema, + "output_predict": _output_predict_schema, + }, +} +BayesianRidge = make_operator(Op, _combined_schemas) + +if sklearn.__version__ >= "0.22": + BayesianRidge = typing.cast( + lale.operators.PlannedIndividualOp, + BayesianRidge.customize_schema( + alpha_init=AnyOf( + types=[Float(), Null()], + desc="Initial value for alpha (precision of the noise). If not set, alpha_init is 1/Var(y).", + default=None, + ), + lambda_init=AnyOf( + types=[Float(), Null()], + desc="Initial value for lambda (precision of the weights). If not set, lambda_init is 1.", + default=None, + ), + ), + ) + + +set_docstrings(BayesianRidge) diff --git a/test/test_core_regressors.py b/test/test_core_regressors.py index 2863ab956..efe5c5282 100644 --- a/test/test_core_regressors.py +++ b/test/test_core_regressors.py @@ -89,6 +89,7 @@ def test_regressor(self): regressors = [ "lale.lib.sklearn.ARDRegression", + "lale.lib.sklearn.BayesianRidge", "lale.lib.sklearn.DummyRegressor", "lale.lib.sklearn.RandomForestRegressor", "lale.lib.sklearn.DecisionTreeRegressor", From 7a7449e868c4f97535a08f081dce103fb76f42a2 Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Fri, 9 Apr 2021 16:31:37 -0400 Subject: [PATCH 04/14] Add curated schema for BernoulliNB --- lale/lib/sklearn/__init__.py | 4 ++ lale/lib/sklearn/bernoulli_nb.py | 120 +++++++++++++++++++++++++++++++ test/test_core_classifiers.py | 1 + 3 files changed, 125 insertions(+) create mode 100644 lale/lib/sklearn/bernoulli_nb.py diff --git a/lale/lib/sklearn/__init__.py b/lale/lib/sklearn/__init__.py index 375dbfc01..afaad4e93 100644 --- a/lale/lib/sklearn/__init__.py +++ b/lale/lib/sklearn/__init__.py @@ -24,6 +24,7 @@ * lale.lib.sklearn. `AdaBoostClassifier`_ * lale.lib.sklearn. `BaggingClassifier`_ +* lale.lib.sklearn. `BernoulliNB`_ * lale.lib.sklearn. `DecisionTreeClassifier`_ * lale.lib.sklearn. `DummyClassifier`_ * lale.lib.sklearn. `ExtraTreesClassifier`_ @@ -90,6 +91,8 @@ .. _`ARDRegression`: lale.lib.sklearn.ard_regression.html .. _`BaggingClassifier`: lale.lib.sklearn.bagging_classifier.html .. _`BayesianRidge`: lale.lib.sklearn.bayesian_ridge.html +.. _`BernoulliNB`: lale.lib.sklearn.bernoulli_nb.html + .. _`ColumnTransformer`: lale.lib.sklearn.column_transformer.html .. _`DecisionTreeClassifier`: lale.lib.sklearn.decision_tree_classifier.html .. _`DecisionTreeRegressor`: lale.lib.sklearn.decision_tree_regressor.html @@ -147,6 +150,7 @@ from .ard_regression import ARDRegression from .bagging_classifier import BaggingClassifier from .bayesian_ridge import BayesianRidge +from .bernoulli_nb import BernoulliNB from .column_transformer import ColumnTransformer from .decision_tree_classifier import DecisionTreeClassifier from .decision_tree_regressor import DecisionTreeRegressor diff --git a/lale/lib/sklearn/bernoulli_nb.py b/lale/lib/sklearn/bernoulli_nb.py new file mode 100644 index 000000000..f65d8bc1b --- /dev/null +++ b/lale/lib/sklearn/bernoulli_nb.py @@ -0,0 +1,120 @@ +from sklearn.naive_bayes import BernoulliNB as Op + +from lale.docstrings import set_docstrings +from lale.operators import make_operator + +_hyperparams_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "BernoulliNB: Naive Bayes classifier for multivariate Bernoulli models.", + "allOf": [ + { + "type": "object", + "required": ["alpha", "binarize", "fit_prior", "class_prior"], + "relevantToOptimizer": ["alpha", "fit_prior"], + "additionalProperties": False, + "properties": { + "alpha": { + "type": "number", + "minimumForOptimizer": 1e-10, + "maximumForOptimizer": 1.0, + "distribution": "loguniform", + "default": 1.0, + "description": "Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).", + }, + "binarize": { + "anyOf": [{"type": "number"}, {"enum": [None]}], + "default": 0.0, + "description": "Threshold for binarizing (mapping to booleans) of sample features", + }, + "fit_prior": { + "type": "boolean", + "default": True, + "description": "Whether to learn class prior probabilities or not", + }, + "class_prior": { + "anyOf": [ + {"type": "array", "items": {"type": "number"}}, + {"enum": [None]}, + ], + "default": None, + "description": "Prior probabilities of the classes", + }, + }, + } + ], +} +_input_fit_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Fit Naive Bayes classifier according to X, y", + "type": "object", + "required": ["X", "y"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Training vectors, where n_samples is the number of samples and n_features is the number of features.", + }, + "y": { + "type": "array", + "items": {"type": "number"}, + "description": "Target values.", + }, + "sample_weight": { + "anyOf": [{"type": "array", "items": {"type": "number"}}, {"enum": [None]}], + "default": None, + "description": "Weights applied to individual samples (1", + }, + }, +} +_input_predict_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Perform classification on an array of test vectors X.", + "type": "object", + "required": ["X"], + "properties": { + "X": {"type": "array", "items": {"type": "array", "items": {"type": "number"}}} + }, +} +_output_predict_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Predicted target values for X", + "type": "array", + "items": {"type": "number"}, +} +_input_predict_proba_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Return probability estimates for the test vector X.", + "type": "object", + "required": ["X"], + "properties": { + "X": {"type": "array", "items": {"type": "array", "items": {"type": "number"}}} + }, +} +_output_predict_proba_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Returns the probability of the samples for each class in the model", + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, +} +_combined_schemas = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": """`BernoulliNB`_ : Naive Bayes classifier for multivariate Bernoulli models from sklearn. + +.. _`BernoulliNB`: https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.BernoulliNB#sklearn-naive_bayes-bernoullinb + """, + "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.bernoulli_nb.html", + "import_from": "sklearn.naive_bayes", + "type": "object", + "tags": {"pre": [], "op": ["estimator"], "post": []}, + "properties": { + "hyperparams": _hyperparams_schema, + "input_fit": _input_fit_schema, + "input_predict": _input_predict_schema, + "output_predict": _output_predict_schema, + "input_predict_proba": _input_predict_proba_schema, + "output_predict_proba": _output_predict_proba_schema, + }, +} +BernoulliNB = make_operator(Op, _combined_schemas) + +set_docstrings(BernoulliNB) diff --git a/test/test_core_classifiers.py b/test/test_core_classifiers.py index aa6e7109e..dc5d27334 100644 --- a/test/test_core_classifiers.py +++ b/test/test_core_classifiers.py @@ -128,6 +128,7 @@ def test_classifier(self): classifiers = [ + "lale.lib.sklearn.BernoulliNB", "lale.lib.sklearn.DummyClassifier", "lale.lib.sklearn.RandomForestClassifier", "lale.lib.sklearn.DecisionTreeClassifier", From 08e5077a498a8220a5fd95b7b58d8a31fba50225 Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Fri, 9 Apr 2021 19:00:00 -0400 Subject: [PATCH 05/14] Add curated schema for BernoulliRBM --- lale/lib/sklearn/__init__.py | 4 +- lale/lib/sklearn/bernoulli_rbm.py | 133 ++++++++++++++++++++++++++++++ test/test_core_classifiers.py | 1 + 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 lale/lib/sklearn/bernoulli_rbm.py diff --git a/lale/lib/sklearn/__init__.py b/lale/lib/sklearn/__init__.py index afaad4e93..c3d2df54e 100644 --- a/lale/lib/sklearn/__init__.py +++ b/lale/lib/sklearn/__init__.py @@ -25,6 +25,7 @@ * lale.lib.sklearn. `AdaBoostClassifier`_ * lale.lib.sklearn. `BaggingClassifier`_ * lale.lib.sklearn. `BernoulliNB`_ +* lale.lib.sklearn. `BernoulliRBM`_ * lale.lib.sklearn. `DecisionTreeClassifier`_ * lale.lib.sklearn. `DummyClassifier`_ * lale.lib.sklearn. `ExtraTreesClassifier`_ @@ -92,7 +93,7 @@ .. _`BaggingClassifier`: lale.lib.sklearn.bagging_classifier.html .. _`BayesianRidge`: lale.lib.sklearn.bayesian_ridge.html .. _`BernoulliNB`: lale.lib.sklearn.bernoulli_nb.html - +.. _`BernoulliRBM`: lale.lib.sklearn.bernoulli_rbm.html .. _`ColumnTransformer`: lale.lib.sklearn.column_transformer.html .. _`DecisionTreeClassifier`: lale.lib.sklearn.decision_tree_classifier.html .. _`DecisionTreeRegressor`: lale.lib.sklearn.decision_tree_regressor.html @@ -151,6 +152,7 @@ from .bagging_classifier import BaggingClassifier from .bayesian_ridge import BayesianRidge from .bernoulli_nb import BernoulliNB +from .bernoulli_rbm import BernoulliRBM from .column_transformer import ColumnTransformer from .decision_tree_classifier import DecisionTreeClassifier from .decision_tree_regressor import DecisionTreeRegressor diff --git a/lale/lib/sklearn/bernoulli_rbm.py b/lale/lib/sklearn/bernoulli_rbm.py new file mode 100644 index 000000000..aac043112 --- /dev/null +++ b/lale/lib/sklearn/bernoulli_rbm.py @@ -0,0 +1,133 @@ +from sklearn.neural_network import BernoulliRBM as Op + +from lale.docstrings import set_docstrings +from lale.operators import make_operator + +_hyperparams_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "BernoulliRBM: Bernoulli Restricted Boltzmann Machine (RBM).", + "allOf": [ + { + "type": "object", + "required": [ + "n_components", + "learning_rate", + "batch_size", + "n_iter", + "verbose", + "random_state", + ], + "relevantToOptimizer": ["n_components", "batch_size", "n_iter"], + "additionalProperties": False, + "properties": { + "n_components": { + "type": "integer", + "minimumForOptimizer": 2, + "maximumForOptimizer": 256, + "distribution": "uniform", + "default": 256, + "description": "Number of binary hidden units.", + }, + "learning_rate": { + "type": "number", + "minimumForOptimizer": 1e-3, + "maximumForOptimizer": 1.0, + "default": 0.1, + "description": "The learning rate for weight updates", + }, + "batch_size": { + "type": "integer", + "minimumForOptimizer": 3, + "maximumForOptimizer": 128, + "distribution": "uniform", + "default": 10, + "description": "Number of examples per minibatch.", + }, + "n_iter": { + "type": "integer", + "minimumForOptimizer": 5, + "maximumForOptimizer": 1000, + "distribution": "uniform", + "default": 10, + "description": "Number of iterations/sweeps over the training dataset to perform during training.", + }, + "verbose": { + "type": "integer", + "default": 0, + "description": "The verbosity level", + }, + "random_state": { + "anyOf": [ + { + "description": "RandomState used by np.random", + "enum": [None], + }, + { + "description": "Use the provided random state, only affecting other users of that same random state instance.", + "laleType": "numpy.random.RandomState", + }, + {"description": "Explicit seed.", "type": "integer"}, + ], + "default": None, + "description": """Determines random number generation for: +Gibbs sampling from visible and hidden layers. +Initializing components, sampling from layers during fit. +Corrupting the data when scoring samples. +Pass an int for reproducible results across multiple function calls.""", + }, + }, + } + ], +} +_input_fit_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Fit the model to the data X.", + "type": "object", + "required": ["X"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Training data.", + } + }, +} +_input_transform_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Compute the hidden layer activation probabilities, P(h=1|v=X).", + "type": "object", + "required": ["X"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "The data to be transformed.", + } + }, +} +_output_transform_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Latent representations of the data.", + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, +} +_combined_schemas = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": """`BernoulliRBM`_ : Bernoulli Restricted Boltzmann Machine (RBM). + +.. _`BernoulliRBM: https://scikit-learn.org/stable/modules/generated/sklearn.neural_network.BernoulliRBM +""", + "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.bernoulli_rbm.html", + "import_from": "sklearn.neural_network", + "type": "object", + "tags": {"pre": [], "op": ["transformer"], "post": []}, + "properties": { + "hyperparams": _hyperparams_schema, + "input_fit": _input_fit_schema, + "input_transform": _input_transform_schema, + "output_transform": _output_transform_schema, + }, +} +BernoulliRBM = make_operator(Op, _combined_schemas) + +set_docstrings(BernoulliRBM) diff --git a/test/test_core_classifiers.py b/test/test_core_classifiers.py index dc5d27334..61ef0740d 100644 --- a/test/test_core_classifiers.py +++ b/test/test_core_classifiers.py @@ -129,6 +129,7 @@ def test_classifier(self): classifiers = [ "lale.lib.sklearn.BernoulliNB", + "lale.lib.sklearn.BernoulliRBM", "lale.lib.sklearn.DummyClassifier", "lale.lib.sklearn.RandomForestClassifier", "lale.lib.sklearn.DecisionTreeClassifier", From fdfcac3b427caa6a4097caa5fedcab4302fe76e6 Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Sat, 10 Apr 2021 21:56:03 -0400 Subject: [PATCH 06/14] Add curated schema for Binarizer --- lale/lib/sklearn/__init__.py | 3 ++ lale/lib/sklearn/binarizer.py | 82 ++++++++++++++++++++++++++++++++++ test/test_core_transformers.py | 1 + 3 files changed, 86 insertions(+) create mode 100644 lale/lib/sklearn/binarizer.py diff --git a/lale/lib/sklearn/__init__.py b/lale/lib/sklearn/__init__.py index c3d2df54e..bd4975bf1 100644 --- a/lale/lib/sklearn/__init__.py +++ b/lale/lib/sklearn/__init__.py @@ -63,6 +63,7 @@ Transformers: * lale.lib.sklearn. `AdditiveChi2Sampler`_ +* lale.lib.sklearn. `Binarizer`_ * lale.lib.sklearn. `ColumnTransformer`_ * lale.lib.sklearn. `FeatureAgglomeration`_ * lale.lib.sklearn. `FunctionTransformer`_ @@ -94,6 +95,7 @@ .. _`BayesianRidge`: lale.lib.sklearn.bayesian_ridge.html .. _`BernoulliNB`: lale.lib.sklearn.bernoulli_nb.html .. _`BernoulliRBM`: lale.lib.sklearn.bernoulli_rbm.html +.. _`Binarizer`: lale.lib.sklearn.binarizer.html .. _`ColumnTransformer`: lale.lib.sklearn.column_transformer.html .. _`DecisionTreeClassifier`: lale.lib.sklearn.decision_tree_classifier.html .. _`DecisionTreeRegressor`: lale.lib.sklearn.decision_tree_regressor.html @@ -153,6 +155,7 @@ from .bayesian_ridge import BayesianRidge from .bernoulli_nb import BernoulliNB from .bernoulli_rbm import BernoulliRBM +from .binarizer import Binarizer from .column_transformer import ColumnTransformer from .decision_tree_classifier import DecisionTreeClassifier from .decision_tree_regressor import DecisionTreeRegressor diff --git a/lale/lib/sklearn/binarizer.py b/lale/lib/sklearn/binarizer.py new file mode 100644 index 000000000..cb7e1edc6 --- /dev/null +++ b/lale/lib/sklearn/binarizer.py @@ -0,0 +1,82 @@ +from sklearn.preprocessing import Binarizer as Op + +from lale.docstrings import set_docstrings +from lale.operators import make_operator + +_hyperparams_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Binarize data (set feature values to 0 or 1) according to a threshold", + "allOf": [ + { + "type": "object", + "required": ["threshold", "copy"], + "relevantToOptimizer": [], + "additionalProperties": False, + "properties": { + "threshold": { + "description": "Feature values below or equal to this are replaced by 0, above it by 1. Threshold may not be less than 0 for operations on sparse matrices.", + "type": "number", + "default": 0.0, + }, + "copy": { + "type": "boolean", + "default": True, + "description": "set to False to perform inplace binarization and avoid a copy (if the input is already a numpy array or a scipy.sparse CSR matrix).", + }, + }, + } + ], +} +_input_fit_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Do nothing and return the estimator unchanged", + "type": "object", + "required": ["X"], + "properties": { + "X": { + "type": "array", + "items": {"laleType": "Any", "XXX TODO XXX": "item type"}, + "XXX TODO XXX": "array-like", + } + }, +} +_input_transform_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Binarize each element of X", + "type": "object", + "required": ["X"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "The data to binarize, element by element", + }, + "y": {"laleType": "Any", "XXX TODO XXX": "(ignored)", "description": ""}, + "copy": {"type": "boolean", "description": "Copy the input X or not."}, + }, +} +_output_transform_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Binarize each element of X", + "laleType": "Any", +} +_combined_schemas = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": """`Binarize data`_ (set feature values to 0 or 1) according to a threshold + +.. _`Binarize data`: https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.Binarizer +""", + "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.binarizer.html", + "import_from": "sklearn.preprocessing", + "type": "object", + "tags": {"pre": [], "op": ["transformer"], "post": []}, + "properties": { + "hyperparams": _hyperparams_schema, + "input_fit": _input_fit_schema, + "input_transform": _input_transform_schema, + "output_transform": _output_transform_schema, + }, +} +Binarizer = make_operator(Op, _combined_schemas) + +set_docstrings(Binarizer) diff --git a/test/test_core_transformers.py b/test/test_core_transformers.py index a706b8164..0d9384614 100644 --- a/test/test_core_transformers.py +++ b/test/test_core_transformers.py @@ -101,6 +101,7 @@ def test_feature_preprocessor(self): feature_preprocessors = [ "lale.lib.sklearn.AdditiveChi2Sampler", + "lale.lib.sklearn.Binarizer", "lale.lib.sklearn.PolynomialFeatures", "lale.lib.sklearn.PCA", "lale.lib.sklearn.Nystroem", From 6cb20c70190d0857fd4c1bb581d2bc37edf32709 Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Sat, 10 Apr 2021 22:18:13 -0400 Subject: [PATCH 07/14] Add curated schema for Birch --- lale/lib/sklearn/__init__.py | 3 + lale/lib/sklearn/birch.py | 148 ++++++++++++++++++++++++++++++++++ test/test_core_classifiers.py | 1 + 3 files changed, 152 insertions(+) create mode 100644 lale/lib/sklearn/birch.py diff --git a/lale/lib/sklearn/__init__.py b/lale/lib/sklearn/__init__.py index bd4975bf1..f683e90c3 100644 --- a/lale/lib/sklearn/__init__.py +++ b/lale/lib/sklearn/__init__.py @@ -26,6 +26,7 @@ * lale.lib.sklearn. `BaggingClassifier`_ * lale.lib.sklearn. `BernoulliNB`_ * lale.lib.sklearn. `BernoulliRBM`_ +* lale.lib.sklearn. `Birch`_ * lale.lib.sklearn. `DecisionTreeClassifier`_ * lale.lib.sklearn. `DummyClassifier`_ * lale.lib.sklearn. `ExtraTreesClassifier`_ @@ -96,6 +97,7 @@ .. _`BernoulliNB`: lale.lib.sklearn.bernoulli_nb.html .. _`BernoulliRBM`: lale.lib.sklearn.bernoulli_rbm.html .. _`Binarizer`: lale.lib.sklearn.binarizer.html +.. _`Birch`: lale.lib.sklearn.birch.html .. _`ColumnTransformer`: lale.lib.sklearn.column_transformer.html .. _`DecisionTreeClassifier`: lale.lib.sklearn.decision_tree_classifier.html .. _`DecisionTreeRegressor`: lale.lib.sklearn.decision_tree_regressor.html @@ -156,6 +158,7 @@ from .bernoulli_nb import BernoulliNB from .bernoulli_rbm import BernoulliRBM from .binarizer import Binarizer +from .birch import Birch from .column_transformer import ColumnTransformer from .decision_tree_classifier import DecisionTreeClassifier from .decision_tree_regressor import DecisionTreeRegressor diff --git a/lale/lib/sklearn/birch.py b/lale/lib/sklearn/birch.py new file mode 100644 index 000000000..c2a6de7f8 --- /dev/null +++ b/lale/lib/sklearn/birch.py @@ -0,0 +1,148 @@ +from sklearn.cluster import Birch as Op + +from lale.docstrings import set_docstrings +from lale.operators import make_operator + +_hyperparams_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Implements the Birch clustering algorithm.", + "allOf": [ + { + "type": "object", + "required": [ + "threshold", + "branching_factor", + "n_clusters", + "compute_labels", + "copy", + ], + "relevantToOptimizer": [ + "branching_factor", + "n_clusters", + "compute_labels", + ], + "additionalProperties": False, + "properties": { + "threshold": { + "type": "number", + "default": 0.5, + "description": "The radius of the subcluster obtained by merging a new sample and the closest subcluster should be lesser than the threshold", + }, + "branching_factor": { + "type": "integer", + "minimumForOptimizer": 50, + "maximumForOptimizer": 51, + "distribution": "uniform", + "default": 50, + "description": "Maximum number of CF subclusters in each node", + }, + "n_clusters": { + "description": "Number of clusters after the final clustering step, which treats the subclusters from the leaves as new samples", + "default": 3, + "anyOf": [ + { + "description": "The model fit is AgglomerativeClustering with n_clusters set to be equal to the int.", + "type": "integer", + "minimumForOptimizer": 2, + "maximumForOptimizer": 8, + "distribution": "uniform", + }, + { + "forOptimizer": False, + "description": "sklearn.cluster Estimator: If a model is provided, the model is fit treating the subclusters as new samples and the initial data is mapped to the label of the closest subcluster.", + "laleType": "operator", + }, + { + "enum": [None], + "description": "The final clustering step is not performed and the subclusters are returned as they are.", + }, + ], + }, + "compute_labels": { + "type": "boolean", + "default": True, + "description": "Whether or not to compute labels for each fit.", + }, + "copy": { + "type": "boolean", + "default": True, + "description": "Whether or not to make a copy of the given data", + }, + }, + } + ], +} +_input_fit_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Build a CF Tree for the input data.", + "type": "object", + "required": ["X", "y"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Input data.", + }, + "y": {}, + }, +} +_input_transform_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Transform X into subcluster centroids dimension.", + "type": "object", + "required": ["X"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Input data.", + } + }, +} +_output_transform_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Transformed data.", + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, +} +_input_predict_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Predict data using the ``centroids_`` of subclusters.", + "type": "object", + "required": ["X"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Input data.", + } + }, +} +_output_predict_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Labelled data.", + "laleType": "Any", + "XXX TODO XXX": "ndarray, shape(n_samples)", +} +_combined_schemas = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": """`Birch`_ clustering algorithm. + + .. _`Birch`: https://scikit-learn.org/stable/modules/generated/sklearn.cluster.Birch + """, + "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.birch.html", + "import_from": "sklearn.cluster", + "type": "object", + "tags": {"pre": [], "op": ["transformer", "estimator"], "post": []}, + "properties": { + "hyperparams": _hyperparams_schema, + "input_fit": _input_fit_schema, + "input_transform": _input_transform_schema, + "output_transform": _output_transform_schema, + "input_predict": _input_predict_schema, + "output_predict": _output_predict_schema, + }, +} +Birch = make_operator(Op, _combined_schemas) + +set_docstrings(Birch) diff --git a/test/test_core_classifiers.py b/test/test_core_classifiers.py index 61ef0740d..3facd9d63 100644 --- a/test/test_core_classifiers.py +++ b/test/test_core_classifiers.py @@ -130,6 +130,7 @@ def test_classifier(self): classifiers = [ "lale.lib.sklearn.BernoulliNB", "lale.lib.sklearn.BernoulliRBM", + "lale.lib.sklearn.Birch", "lale.lib.sklearn.DummyClassifier", "lale.lib.sklearn.RandomForestClassifier", "lale.lib.sklearn.DecisionTreeClassifier", From 393139590fa8e3f96ed9433c6565eb30bf924b31 Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Sun, 11 Apr 2021 21:20:00 -0400 Subject: [PATCH 08/14] Add curated schema for CalibratedClassifierCV --- lale/lib/sklearn/__init__.py | 3 + lale/lib/sklearn/calibrated_classifier_cv.py | 164 +++++++++++++++++++ test/test_core_classifiers.py | 1 + 3 files changed, 168 insertions(+) create mode 100644 lale/lib/sklearn/calibrated_classifier_cv.py diff --git a/lale/lib/sklearn/__init__.py b/lale/lib/sklearn/__init__.py index f683e90c3..ca1d1d310 100644 --- a/lale/lib/sklearn/__init__.py +++ b/lale/lib/sklearn/__init__.py @@ -27,6 +27,7 @@ * lale.lib.sklearn. `BernoulliNB`_ * lale.lib.sklearn. `BernoulliRBM`_ * lale.lib.sklearn. `Birch`_ +* lale.lib.sklearn. `CalibratedClassifierCV`_ * lale.lib.sklearn. `DecisionTreeClassifier`_ * lale.lib.sklearn. `DummyClassifier`_ * lale.lib.sklearn. `ExtraTreesClassifier`_ @@ -98,6 +99,7 @@ .. _`BernoulliRBM`: lale.lib.sklearn.bernoulli_rbm.html .. _`Binarizer`: lale.lib.sklearn.binarizer.html .. _`Birch`: lale.lib.sklearn.birch.html +.. _`CalibratedClassifierCV`: lale.lib.sklearn.calibrated_classifier_cv.html .. _`ColumnTransformer`: lale.lib.sklearn.column_transformer.html .. _`DecisionTreeClassifier`: lale.lib.sklearn.decision_tree_classifier.html .. _`DecisionTreeRegressor`: lale.lib.sklearn.decision_tree_regressor.html @@ -159,6 +161,7 @@ from .bernoulli_rbm import BernoulliRBM from .binarizer import Binarizer from .birch import Birch +from .calibrated_classifier_cv import CalibratedClassifierCV from .column_transformer import ColumnTransformer from .decision_tree_classifier import DecisionTreeClassifier from .decision_tree_regressor import DecisionTreeRegressor diff --git a/lale/lib/sklearn/calibrated_classifier_cv.py b/lale/lib/sklearn/calibrated_classifier_cv.py new file mode 100644 index 000000000..7a1bd6f34 --- /dev/null +++ b/lale/lib/sklearn/calibrated_classifier_cv.py @@ -0,0 +1,164 @@ +import typing + +import sklearn +from sklearn.calibration import CalibratedClassifierCV as Op + +import lale.operators +from lale.docstrings import set_docstrings +from lale.operators import make_operator +from lale.schemas import AnyOf, Bool, Int, Null + +_hyperparams_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Probability calibration with isotonic regression or sigmoid.", + "allOf": [ + { + "type": "object", + "required": ["base_estimator", "method", "cv"], + "relevantToOptimizer": ["method", "cv"], + "additionalProperties": False, + "properties": { + "base_estimator": { + "description": "The classifier whose output decision function needs to be calibrated to offer more accurate predict_proba outputs", + "default": None, + "anyOf": [ + { + "description": "None uses the default classifier, LinearSVC.", + "enum": [None], + }, + {"laleType": "operator"}, + ], + }, + "method": { + "description": "The method to use for calibration. Can be ‘sigmoid’ which corresponds to Platt’s method (i.e. a logistic regression model) or ‘isotonic’ which is a non-parametric approach. It is not advised to use isotonic calibration with too few calibration samples (<<1000) since it tends to overfit.", + "enum": ["sigmoid", "isotonic"], + "default": "sigmoid", + }, + "cv": { + "description": "Determines the cross-validation splitting strategy", + "default": None, + "anyOf": [ + { + "description": "use the default 5-fold cross-validation", + "enum": [None], + }, + { + "type": "integer", + "minimumForOptimizer": 3, + "maximumForOptimizer": 4, + "distribution": "uniform", + }, + { + "forOptimizer": False, + "laleType": "Any", + "description": "CV splitter or an iterable yielding (train, test) splits as arrays of indices.", + }, + ], + }, + }, + } + ], +} +_input_fit_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Fit the calibrated model", + "type": "object", + "required": ["X", "y"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Training data.", + }, + "y": { + "type": "array", + "items": {"type": "number"}, + "description": "Target values.", + }, + "sample_weight": { + "anyOf": [{"type": "array", "items": {"type": "number"}}, {"enum": [None]}], + "description": "Sample weights", + }, + }, +} +_input_predict_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Predict the target of new samples. Can be different from the", + "type": "object", + "required": ["X"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "The samples.", + } + }, +} +_output_predict_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "The predicted class.", + "type": "array", + "items": {"type": "number"}, +} +_input_predict_proba_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Posterior probabilities of classification", + "type": "object", + "required": ["X"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "The samples.", + } + }, +} +_output_predict_proba_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "The predicted probas.", + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, +} +_combined_schemas = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": """`CalibratedClassifierCV`_ : Probability calibration with isotonic regression or sigmoid. + +.. _`CalibratedClassifierCV`: https://scikit-learn.org/stable/modules/generated/sklearn.calibration.CalibratedClassifierCV + """, + "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.calibrated_classifier_cv.html", + "import_from": "sklearn.calibration", + "type": "object", + "tags": {"pre": [], "op": ["estimator"], "post": []}, + "properties": { + "hyperparams": _hyperparams_schema, + "input_fit": _input_fit_schema, + "input_predict": _input_predict_schema, + "output_predict": _output_predict_schema, + "input_predict_proba": _input_predict_proba_schema, + "output_predict_proba": _output_predict_proba_schema, + }, +} +CalibratedClassifierCV = make_operator(Op, _combined_schemas) + +if sklearn.__version__ >= "0.24": + # old: https://scikit-learn.org/0.23/modules/generated/sklearn.calibration.CalibratedClassifierCV.html + # new: https://scikit-learn.org/0.24/modules/generated/sklearn.calibration.CalibratedClassifierCV.html + CalibratedClassifierCV = typing.cast( + lale.operators.PlannedIndividualOp, + CalibratedClassifierCV.customize_schema( + n_jobs=AnyOf( + types=[ + Int(minimum=1), + Int(minimum=-1, maximum=-1, desc="Use all the processors"), + Null(), + ], + desc="Number of jobs to run in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors.", + default=None, + ), + ensemble=Bool( + default=True, desc="Determines how the calibrator is fitted." + ), + ), + ) + +set_docstrings(CalibratedClassifierCV) diff --git a/test/test_core_classifiers.py b/test/test_core_classifiers.py index 3facd9d63..030564d7f 100644 --- a/test/test_core_classifiers.py +++ b/test/test_core_classifiers.py @@ -131,6 +131,7 @@ def test_classifier(self): "lale.lib.sklearn.BernoulliNB", "lale.lib.sklearn.BernoulliRBM", "lale.lib.sklearn.Birch", + "lale.lib.sklearn.CalibratedClassifierCV", "lale.lib.sklearn.DummyClassifier", "lale.lib.sklearn.RandomForestClassifier", "lale.lib.sklearn.DecisionTreeClassifier", From 02b56bea6a2a922970c451b03d7f4865d089980d Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Tue, 13 Apr 2021 18:21:17 -0400 Subject: [PATCH 09/14] fix bernoulliRBM --- lale/lib/sklearn/__init__.py | 2 +- test/test_core_classifiers.py | 1 - test/test_core_transformers.py | 1 + 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lale/lib/sklearn/__init__.py b/lale/lib/sklearn/__init__.py index ca1d1d310..aea72456b 100644 --- a/lale/lib/sklearn/__init__.py +++ b/lale/lib/sklearn/__init__.py @@ -25,7 +25,6 @@ * lale.lib.sklearn. `AdaBoostClassifier`_ * lale.lib.sklearn. `BaggingClassifier`_ * lale.lib.sklearn. `BernoulliNB`_ -* lale.lib.sklearn. `BernoulliRBM`_ * lale.lib.sklearn. `Birch`_ * lale.lib.sklearn. `CalibratedClassifierCV`_ * lale.lib.sklearn. `DecisionTreeClassifier`_ @@ -65,6 +64,7 @@ Transformers: * lale.lib.sklearn. `AdditiveChi2Sampler`_ +* lale.lib.sklearn. `BernoulliRBM`_ * lale.lib.sklearn. `Binarizer`_ * lale.lib.sklearn. `ColumnTransformer`_ * lale.lib.sklearn. `FeatureAgglomeration`_ diff --git a/test/test_core_classifiers.py b/test/test_core_classifiers.py index 030564d7f..87b8cffde 100644 --- a/test/test_core_classifiers.py +++ b/test/test_core_classifiers.py @@ -129,7 +129,6 @@ def test_classifier(self): classifiers = [ "lale.lib.sklearn.BernoulliNB", - "lale.lib.sklearn.BernoulliRBM", "lale.lib.sklearn.Birch", "lale.lib.sklearn.CalibratedClassifierCV", "lale.lib.sklearn.DummyClassifier", diff --git a/test/test_core_transformers.py b/test/test_core_transformers.py index 0d9384614..477afde1c 100644 --- a/test/test_core_transformers.py +++ b/test/test_core_transformers.py @@ -101,6 +101,7 @@ def test_feature_preprocessor(self): feature_preprocessors = [ "lale.lib.sklearn.AdditiveChi2Sampler", + "lale.lib.sklearn.BernoulliRBM", "lale.lib.sklearn.Binarizer", "lale.lib.sklearn.PolynomialFeatures", "lale.lib.sklearn.PCA", From ab78fab45967e44e6bd2607369fc5ed49398a87d Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Tue, 13 Apr 2021 18:28:40 -0400 Subject: [PATCH 10/14] fix Birch --- lale/lib/sklearn/__init__.py | 2 +- test/test_core_classifiers.py | 1 - test/test_core_transformers.py | 1 + 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lale/lib/sklearn/__init__.py b/lale/lib/sklearn/__init__.py index aea72456b..0b6c1211d 100644 --- a/lale/lib/sklearn/__init__.py +++ b/lale/lib/sklearn/__init__.py @@ -25,7 +25,6 @@ * lale.lib.sklearn. `AdaBoostClassifier`_ * lale.lib.sklearn. `BaggingClassifier`_ * lale.lib.sklearn. `BernoulliNB`_ -* lale.lib.sklearn. `Birch`_ * lale.lib.sklearn. `CalibratedClassifierCV`_ * lale.lib.sklearn. `DecisionTreeClassifier`_ * lale.lib.sklearn. `DummyClassifier`_ @@ -66,6 +65,7 @@ * lale.lib.sklearn. `AdditiveChi2Sampler`_ * lale.lib.sklearn. `BernoulliRBM`_ * lale.lib.sklearn. `Binarizer`_ +* lale.lib.sklearn. `Birch`_ * lale.lib.sklearn. `ColumnTransformer`_ * lale.lib.sklearn. `FeatureAgglomeration`_ * lale.lib.sklearn. `FunctionTransformer`_ diff --git a/test/test_core_classifiers.py b/test/test_core_classifiers.py index 87b8cffde..ce62e43ab 100644 --- a/test/test_core_classifiers.py +++ b/test/test_core_classifiers.py @@ -129,7 +129,6 @@ def test_classifier(self): classifiers = [ "lale.lib.sklearn.BernoulliNB", - "lale.lib.sklearn.Birch", "lale.lib.sklearn.CalibratedClassifierCV", "lale.lib.sklearn.DummyClassifier", "lale.lib.sklearn.RandomForestClassifier", diff --git a/test/test_core_transformers.py b/test/test_core_transformers.py index 477afde1c..ce8d03dc0 100644 --- a/test/test_core_transformers.py +++ b/test/test_core_transformers.py @@ -103,6 +103,7 @@ def test_feature_preprocessor(self): "lale.lib.sklearn.AdditiveChi2Sampler", "lale.lib.sklearn.BernoulliRBM", "lale.lib.sklearn.Binarizer", + "lale.lib.sklearn.Birch", "lale.lib.sklearn.PolynomialFeatures", "lale.lib.sklearn.PCA", "lale.lib.sklearn.Nystroem", From ded9f38c5f195551ce7e7c394a4d7111086e5080 Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Sun, 11 Apr 2021 21:34:33 -0400 Subject: [PATCH 11/14] Add curated schema for CCA --- lale/lib/sklearn/__init__.py | 2 + lale/lib/sklearn/cca.py | 147 ++++++++++++++++++++++++++++++++++ test/test_core_classifiers.py | 1 + 3 files changed, 150 insertions(+) create mode 100644 lale/lib/sklearn/cca.py diff --git a/lale/lib/sklearn/__init__.py b/lale/lib/sklearn/__init__.py index 0b6c1211d..beb8db879 100644 --- a/lale/lib/sklearn/__init__.py +++ b/lale/lib/sklearn/__init__.py @@ -26,6 +26,7 @@ * lale.lib.sklearn. `BaggingClassifier`_ * lale.lib.sklearn. `BernoulliNB`_ * lale.lib.sklearn. `CalibratedClassifierCV`_ +* lale.lib.sklearn. `CCA`_ * lale.lib.sklearn. `DecisionTreeClassifier`_ * lale.lib.sklearn. `DummyClassifier`_ * lale.lib.sklearn. `ExtraTreesClassifier`_ @@ -162,6 +163,7 @@ from .binarizer import Binarizer from .birch import Birch from .calibrated_classifier_cv import CalibratedClassifierCV +from .cca import CCA from .column_transformer import ColumnTransformer from .decision_tree_classifier import DecisionTreeClassifier from .decision_tree_regressor import DecisionTreeRegressor diff --git a/lale/lib/sklearn/cca.py b/lale/lib/sklearn/cca.py new file mode 100644 index 000000000..ed9e58fb1 --- /dev/null +++ b/lale/lib/sklearn/cca.py @@ -0,0 +1,147 @@ +from sklearn.cross_decomposition import CCA as Op + +from lale.docstrings import set_docstrings +from lale.operators import make_operator + +_hyperparams_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Canonical Correlation Analysis.", + "allOf": [ + { + "type": "object", + "required": ["n_components", "scale", "max_iter", "tol", "copy"], + "relevantToOptimizer": ["n_components", "scale", "max_iter", "tol"], + "additionalProperties": False, + "properties": { + "n_components": { + "type": "integer", + "minimum": 1, + "minimumForOptimizer": 2, + "maximumForOptimizer": 256, + "distribution": "uniform", + "default": 2, + "description": "number of components to keep.", + }, + "scale": { + "type": "boolean", + "default": True, + "description": "whether to scale the data?", + }, + "max_iter": { + "description": "the maximum number of the power method.", + "type": "integer", + "minimum": 0, + "minimumForOptimizer": 10, + "maximumForOptimizer": 1000, + "distribution": "uniform", + "default": 500, + }, + "tol": { + "description": "the tolerance used in the iterative algorithm", + "type": "number", + "minimumForOptimizer": 1e-08, + "maximumForOptimizer": 0.01, + "distribution": "loguniform", + "default": 1e-06, + }, + "copy": { + "type": "boolean", + "default": True, + "description": "Whether the deflation be done on a copy", + }, + }, + } + ], +} +_input_fit_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Fit model to data.", + "type": "object", + "required": ["X"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Training vectors, where n_samples is the number of samples and n_features is the number of predictors.", + }, + "Y": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Target vectors, where n_samples is the number of samples and n_targets is the number of response variables.", + }, + }, +} +_input_transform_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Apply the dimension reduction learned on the train data.", + "type": "object", + "required": ["X"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Training vectors, where n_samples is the number of samples and n_features is the number of predictors.", + }, + "Y": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Target vectors, where n_samples is the number of samples and n_targets is the number of response variables.", + }, + "copy": { + "type": "boolean", + "default": True, + "description": "Whether to copy X and Y, or perform in-place normalization.", + }, + }, +} +_output_transform_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Apply the dimension reduction learned on the train data.", + "laleType": "Any", + "XXX TODO XXX": "x_scores if Y is not given, (x_scores, y_scores) otherwise.", +} +_input_predict_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Apply the dimension reduction learned on the train data.", + "type": "object", + "required": ["X"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Training vectors, where n_samples is the number of samples and n_features is the number of predictors.", + }, + "copy": { + "type": "boolean", + "default": True, + "description": "Whether to copy X and Y, or perform in-place normalization.", + }, + }, +} +_output_predict_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Apply the dimension reduction learned on the train data.", + "laleType": "Any", +} +_combined_schemas = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": """`Canonical Correlation Analysis`_ from sklearn + +.. _`Canonical Correlation Analysis`: https://scikit-learn.org/stable/modules/generated/sklearn.cross_decomposition.CCA + """, + "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.cca.html", + "import_from": "sklearn.cross_decomposition", + "type": "object", + "tags": {"pre": [], "op": ["transformer", "estimator"], "post": []}, + "properties": { + "hyperparams": _hyperparams_schema, + "input_fit": _input_fit_schema, + "input_transform": _input_transform_schema, + "output_transform": _output_transform_schema, + "input_predict": _input_predict_schema, + "output_predict": _output_predict_schema, + }, +} +CCA = make_operator(Op, _combined_schemas) + +set_docstrings(CCA) diff --git a/test/test_core_classifiers.py b/test/test_core_classifiers.py index ce62e43ab..7b4215269 100644 --- a/test/test_core_classifiers.py +++ b/test/test_core_classifiers.py @@ -130,6 +130,7 @@ def test_classifier(self): classifiers = [ "lale.lib.sklearn.BernoulliNB", "lale.lib.sklearn.CalibratedClassifierCV", + "lale.lib.sklearn.CCA", "lale.lib.sklearn.DummyClassifier", "lale.lib.sklearn.RandomForestClassifier", "lale.lib.sklearn.DecisionTreeClassifier", From 080b1672ac7a5d4e096801142965f0a91545d170 Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Mon, 12 Apr 2021 08:12:36 -0400 Subject: [PATCH 12/14] Add curated schema for complement_nb --- lale/lib/sklearn/__init__.py | 3 + lale/lib/sklearn/complement_nb.py | 117 ++++++++++++++++++++++++++++++ test/test_core_classifiers.py | 1 + 3 files changed, 121 insertions(+) create mode 100644 lale/lib/sklearn/complement_nb.py diff --git a/lale/lib/sklearn/__init__.py b/lale/lib/sklearn/__init__.py index beb8db879..7bdca9f1b 100644 --- a/lale/lib/sklearn/__init__.py +++ b/lale/lib/sklearn/__init__.py @@ -27,6 +27,7 @@ * lale.lib.sklearn. `BernoulliNB`_ * lale.lib.sklearn. `CalibratedClassifierCV`_ * lale.lib.sklearn. `CCA`_ +* lale.lib.sklearn. `ComplementNB`_ * lale.lib.sklearn. `DecisionTreeClassifier`_ * lale.lib.sklearn. `DummyClassifier`_ * lale.lib.sklearn. `ExtraTreesClassifier`_ @@ -102,6 +103,7 @@ .. _`Birch`: lale.lib.sklearn.birch.html .. _`CalibratedClassifierCV`: lale.lib.sklearn.calibrated_classifier_cv.html .. _`ColumnTransformer`: lale.lib.sklearn.column_transformer.html +.. _`ComplementNB`: lale.lib.sklearn.complement_nb.html .. _`DecisionTreeClassifier`: lale.lib.sklearn.decision_tree_classifier.html .. _`DecisionTreeRegressor`: lale.lib.sklearn.decision_tree_regressor.html .. _`DummyClassifier`: lale.lib.sklearn.dummy_classifier.html @@ -165,6 +167,7 @@ from .calibrated_classifier_cv import CalibratedClassifierCV from .cca import CCA from .column_transformer import ColumnTransformer +from .complement_nb import ComplementNB from .decision_tree_classifier import DecisionTreeClassifier from .decision_tree_regressor import DecisionTreeRegressor from .dummy_classifier import DummyClassifier diff --git a/lale/lib/sklearn/complement_nb.py b/lale/lib/sklearn/complement_nb.py new file mode 100644 index 000000000..32fa56bcd --- /dev/null +++ b/lale/lib/sklearn/complement_nb.py @@ -0,0 +1,117 @@ +from sklearn.naive_bayes import ComplementNB as Op + +from lale.docstrings import set_docstrings +from lale.operators import make_operator + +_hyperparams_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "The Complement Naive Bayes classifier described in Rennie et al. (2003).", + "allOf": [ + { + "type": "object", + "required": ["alpha", "fit_prior", "class_prior", "norm"], + "relevantToOptimizer": [], + "additionalProperties": False, + "properties": { + "alpha": { + "type": "number", + "default": 1.0, + "description": "Additive (Laplace/Lidstone) smoothing parameter (0 for no smoothing).", + }, + "fit_prior": { + "type": "boolean", + "default": True, + "description": "Only used in edge case with a single class in the training set.", + }, + "class_prior": { + "anyOf": [ + {"type": "array", "items": {"type": "number"}}, + {"enum": [None]}, + ], + "default": None, + "description": "Prior probabilities of the classes. Not used.", + }, + "norm": { + "type": "boolean", + "default": False, + "description": "Whether or not a second normalization of the weights is performed", + }, + }, + }, + ], +} +_input_fit_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Fit Naive Bayes classifier according to X, y", + "type": "object", + "required": ["X", "y"], + "properties": { + "X": { + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, + "description": "Training vectors, where n_samples is the number of samples and n_features is the number of features.", + }, + "y": { + "type": "array", + "items": {"type": "number"}, + "description": "Target values.", + }, + "sample_weight": { + "anyOf": [{"type": "array", "items": {"type": "number"}}, {"enum": [None]}], + "default": None, + "description": "Weights applied to individual samples (1", + }, + }, +} +_input_predict_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Perform classification on an array of test vectors X.", + "type": "object", + "required": ["X"], + "properties": { + "X": {"type": "array", "items": {"type": "array", "items": {"type": "number"}}} + }, +} +_output_predict_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Predicted target values for X", + "type": "array", + "items": {"type": "number"}, +} +_input_predict_proba_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Return probability estimates for the test vector X.", + "type": "object", + "required": ["X"], + "properties": { + "X": {"type": "array", "items": {"type": "array", "items": {"type": "number"}}} + }, +} +_output_predict_proba_schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "Returns the probability of the samples for each class in the model", + "type": "array", + "items": {"type": "array", "items": {"type": "number"}}, +} +_combined_schemas = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": """`Complement Naive Bayes`_ classifier described in Rennie et al. (2003). + +.. _`Complement Naive Bayes`: https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.ComplementNB + """, + "documentation_url": "https://lale.readthedocs.io/en/latest/modules/lale.lib.sklearn.complement_nb.html", + "import_from": "sklearn.naive_bayes", + "type": "object", + "tags": {"pre": [], "op": ["estimator"], "post": []}, + "properties": { + "hyperparams": _hyperparams_schema, + "input_fit": _input_fit_schema, + "input_predict": _input_predict_schema, + "output_predict": _output_predict_schema, + "input_predict_proba": _input_predict_proba_schema, + "output_predict_proba": _output_predict_proba_schema, + }, +} +ComplementNB = make_operator(Op, _combined_schemas) + +set_docstrings(ComplementNB) diff --git a/test/test_core_classifiers.py b/test/test_core_classifiers.py index 7b4215269..5b1e82aea 100644 --- a/test/test_core_classifiers.py +++ b/test/test_core_classifiers.py @@ -131,6 +131,7 @@ def test_classifier(self): "lale.lib.sklearn.BernoulliNB", "lale.lib.sklearn.CalibratedClassifierCV", "lale.lib.sklearn.CCA", + "lale.lib.sklearn.ComplementNB", "lale.lib.sklearn.DummyClassifier", "lale.lib.sklearn.RandomForestClassifier", "lale.lib.sklearn.DecisionTreeClassifier", From 85af17e8e774bf153537f44e471d5716ef93364a Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Tue, 4 May 2021 18:59:16 -0400 Subject: [PATCH 13/14] add laleMaximum constraints to cca CCA is actually a transformer --- lale/lib/sklearn/__init__.py | 3 ++- lale/lib/sklearn/cca.py | 19 ++++++++++++++----- test/test_core_classifiers.py | 1 - test/test_core_transformers.py | 1 + 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lale/lib/sklearn/__init__.py b/lale/lib/sklearn/__init__.py index 7bdca9f1b..6178580e4 100644 --- a/lale/lib/sklearn/__init__.py +++ b/lale/lib/sklearn/__init__.py @@ -26,7 +26,6 @@ * lale.lib.sklearn. `BaggingClassifier`_ * lale.lib.sklearn. `BernoulliNB`_ * lale.lib.sklearn. `CalibratedClassifierCV`_ -* lale.lib.sklearn. `CCA`_ * lale.lib.sklearn. `ComplementNB`_ * lale.lib.sklearn. `DecisionTreeClassifier`_ * lale.lib.sklearn. `DummyClassifier`_ @@ -68,6 +67,7 @@ * lale.lib.sklearn. `BernoulliRBM`_ * lale.lib.sklearn. `Binarizer`_ * lale.lib.sklearn. `Birch`_ +* lale.lib.sklearn. `CCA`_ * lale.lib.sklearn. `ColumnTransformer`_ * lale.lib.sklearn. `FeatureAgglomeration`_ * lale.lib.sklearn. `FunctionTransformer`_ @@ -101,6 +101,7 @@ .. _`BernoulliRBM`: lale.lib.sklearn.bernoulli_rbm.html .. _`Binarizer`: lale.lib.sklearn.binarizer.html .. _`Birch`: lale.lib.sklearn.birch.html +.. _`CCA`: lale.lib.sklearn.cca.html .. _`CalibratedClassifierCV`: lale.lib.sklearn.calibrated_classifier_cv.html .. _`ColumnTransformer`: lale.lib.sklearn.column_transformer.html .. _`ComplementNB`: lale.lib.sklearn.complement_nb.html diff --git a/lale/lib/sklearn/cca.py b/lale/lib/sklearn/cca.py index ed9e58fb1..333dd430c 100644 --- a/lale/lib/sklearn/cca.py +++ b/lale/lib/sklearn/cca.py @@ -14,13 +14,22 @@ "additionalProperties": False, "properties": { "n_components": { - "type": "integer", - "minimum": 1, - "minimumForOptimizer": 2, - "maximumForOptimizer": 256, - "distribution": "uniform", "default": 2, "description": "number of components to keep.", + "allOf": [ + { + "type": "integer", + "minimum": 1, + "minimumForOptimizer": 2, + "maximumForOptimizer": 256, + "distribution": "uniform", + "laleMaximum": "X/items/maxItems", # number of columns (n_features) + }, + { + "type": "integer", + "laleMaximum": "X/maxItems", # number of rows (n_samples) + }, + ], }, "scale": { "type": "boolean", diff --git a/test/test_core_classifiers.py b/test/test_core_classifiers.py index 5b1e82aea..3b01a3b82 100644 --- a/test/test_core_classifiers.py +++ b/test/test_core_classifiers.py @@ -130,7 +130,6 @@ def test_classifier(self): classifiers = [ "lale.lib.sklearn.BernoulliNB", "lale.lib.sklearn.CalibratedClassifierCV", - "lale.lib.sklearn.CCA", "lale.lib.sklearn.ComplementNB", "lale.lib.sklearn.DummyClassifier", "lale.lib.sklearn.RandomForestClassifier", diff --git a/test/test_core_transformers.py b/test/test_core_transformers.py index ce8d03dc0..5a458f8d4 100644 --- a/test/test_core_transformers.py +++ b/test/test_core_transformers.py @@ -104,6 +104,7 @@ def test_feature_preprocessor(self): "lale.lib.sklearn.BernoulliRBM", "lale.lib.sklearn.Binarizer", "lale.lib.sklearn.Birch", + "lale.lib.sklearn.CCA", "lale.lib.sklearn.PolynomialFeatures", "lale.lib.sklearn.PCA", "lale.lib.sklearn.Nystroem", From 6f647031b4b77ce0cc160086abbe841e97f205a6 Mon Sep 17 00:00:00 2001 From: Avi Shinnar Date: Thu, 6 May 2021 17:57:34 -0400 Subject: [PATCH 14/14] fix cca schema --- lale/lib/sklearn/cca.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lale/lib/sklearn/cca.py b/lale/lib/sklearn/cca.py index 333dd430c..b1d0aab98 100644 --- a/lale/lib/sklearn/cca.py +++ b/lale/lib/sklearn/cca.py @@ -84,14 +84,14 @@ "$schema": "http://json-schema.org/draft-04/schema#", "description": "Apply the dimension reduction learned on the train data.", "type": "object", - "required": ["X"], + "required": ["X", "y"], "properties": { "X": { "type": "array", "items": {"type": "array", "items": {"type": "number"}}, "description": "Training vectors, where n_samples is the number of samples and n_features is the number of predictors.", }, - "Y": { + "y": { "type": "array", "items": {"type": "array", "items": {"type": "number"}}, "description": "Target vectors, where n_samples is the number of samples and n_targets is the number of response variables.",