snowflake-ml-python 1.4.0__py3-none-any.whl → 1.4.1__py3-none-any.whl
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.
- snowflake/ml/_internal/env_utils.py +11 -1
- snowflake/ml/_internal/utils/identifier.py +3 -1
- snowflake/ml/_internal/utils/sql_identifier.py +2 -6
- snowflake/ml/feature_store/feature_store.py +151 -78
- snowflake/ml/feature_store/feature_view.py +12 -24
- snowflake/ml/fileset/sfcfs.py +56 -50
- snowflake/ml/fileset/stage_fs.py +48 -13
- snowflake/ml/model/_client/model/model_version_impl.py +2 -50
- snowflake/ml/model/_client/ops/model_ops.py +78 -29
- snowflake/ml/model/_client/sql/model.py +23 -2
- snowflake/ml/model/_client/sql/model_version.py +22 -1
- snowflake/ml/model/_model_composer/model_manifest/model_manifest.py +19 -54
- snowflake/ml/model/_model_composer/model_manifest/model_manifest_schema.py +8 -1
- snowflake/ml/model/_model_composer/model_method/model_method.py +6 -10
- snowflake/ml/model/_packager/model_handlers/catboost.py +206 -0
- snowflake/ml/model/_packager/model_handlers/lightgbm.py +218 -0
- snowflake/ml/model/_packager/model_handlers/sklearn.py +3 -0
- snowflake/ml/model/_packager/model_meta/_core_requirements.py +1 -1
- snowflake/ml/model/_packager/model_meta/model_meta.py +36 -6
- snowflake/ml/model/_packager/model_meta/model_meta_schema.py +20 -1
- snowflake/ml/model/_packager/model_meta_migrator/migrator_plans.py +3 -1
- snowflake/ml/model/_packager/model_packager.py +2 -2
- snowflake/ml/model/{_model_composer/model_runtime/_runtime_requirements.py → _packager/model_runtime/_snowml_inference_alternative_requirements.py} +1 -1
- snowflake/ml/model/_packager/model_runtime/model_runtime.py +137 -0
- snowflake/ml/model/type_hints.py +21 -2
- snowflake/ml/modeling/_internal/estimator_utils.py +16 -11
- snowflake/ml/modeling/_internal/local_implementations/pandas_handlers.py +4 -1
- snowflake/ml/modeling/_internal/snowpark_implementations/distributed_hpo_trainer.py +13 -14
- snowflake/ml/modeling/_internal/snowpark_implementations/snowpark_handlers.py +5 -5
- snowflake/ml/modeling/calibration/calibrated_classifier_cv.py +195 -123
- snowflake/ml/modeling/cluster/affinity_propagation.py +195 -123
- snowflake/ml/modeling/cluster/agglomerative_clustering.py +195 -123
- snowflake/ml/modeling/cluster/birch.py +195 -123
- snowflake/ml/modeling/cluster/bisecting_k_means.py +195 -123
- snowflake/ml/modeling/cluster/dbscan.py +195 -123
- snowflake/ml/modeling/cluster/feature_agglomeration.py +195 -123
- snowflake/ml/modeling/cluster/k_means.py +195 -123
- snowflake/ml/modeling/cluster/mean_shift.py +195 -123
- snowflake/ml/modeling/cluster/mini_batch_k_means.py +195 -123
- snowflake/ml/modeling/cluster/optics.py +195 -123
- snowflake/ml/modeling/cluster/spectral_biclustering.py +195 -123
- snowflake/ml/modeling/cluster/spectral_clustering.py +195 -123
- snowflake/ml/modeling/cluster/spectral_coclustering.py +195 -123
- snowflake/ml/modeling/compose/column_transformer.py +195 -123
- snowflake/ml/modeling/compose/transformed_target_regressor.py +195 -123
- snowflake/ml/modeling/covariance/elliptic_envelope.py +195 -123
- snowflake/ml/modeling/covariance/empirical_covariance.py +195 -123
- snowflake/ml/modeling/covariance/graphical_lasso.py +195 -123
- snowflake/ml/modeling/covariance/graphical_lasso_cv.py +195 -123
- snowflake/ml/modeling/covariance/ledoit_wolf.py +195 -123
- snowflake/ml/modeling/covariance/min_cov_det.py +195 -123
- snowflake/ml/modeling/covariance/oas.py +195 -123
- snowflake/ml/modeling/covariance/shrunk_covariance.py +195 -123
- snowflake/ml/modeling/decomposition/dictionary_learning.py +195 -123
- snowflake/ml/modeling/decomposition/factor_analysis.py +195 -123
- snowflake/ml/modeling/decomposition/fast_ica.py +195 -123
- snowflake/ml/modeling/decomposition/incremental_pca.py +195 -123
- snowflake/ml/modeling/decomposition/kernel_pca.py +195 -123
- snowflake/ml/modeling/decomposition/mini_batch_dictionary_learning.py +195 -123
- snowflake/ml/modeling/decomposition/mini_batch_sparse_pca.py +195 -123
- snowflake/ml/modeling/decomposition/pca.py +195 -123
- snowflake/ml/modeling/decomposition/sparse_pca.py +195 -123
- snowflake/ml/modeling/decomposition/truncated_svd.py +195 -123
- snowflake/ml/modeling/discriminant_analysis/linear_discriminant_analysis.py +195 -123
- snowflake/ml/modeling/discriminant_analysis/quadratic_discriminant_analysis.py +195 -123
- snowflake/ml/modeling/ensemble/ada_boost_classifier.py +195 -123
- snowflake/ml/modeling/ensemble/ada_boost_regressor.py +195 -123
- snowflake/ml/modeling/ensemble/bagging_classifier.py +195 -123
- snowflake/ml/modeling/ensemble/bagging_regressor.py +195 -123
- snowflake/ml/modeling/ensemble/extra_trees_classifier.py +195 -123
- snowflake/ml/modeling/ensemble/extra_trees_regressor.py +195 -123
- snowflake/ml/modeling/ensemble/gradient_boosting_classifier.py +195 -123
- snowflake/ml/modeling/ensemble/gradient_boosting_regressor.py +195 -123
- snowflake/ml/modeling/ensemble/hist_gradient_boosting_classifier.py +195 -123
- snowflake/ml/modeling/ensemble/hist_gradient_boosting_regressor.py +195 -123
- snowflake/ml/modeling/ensemble/isolation_forest.py +195 -123
- snowflake/ml/modeling/ensemble/random_forest_classifier.py +195 -123
- snowflake/ml/modeling/ensemble/random_forest_regressor.py +195 -123
- snowflake/ml/modeling/ensemble/stacking_regressor.py +195 -123
- snowflake/ml/modeling/ensemble/voting_classifier.py +195 -123
- snowflake/ml/modeling/ensemble/voting_regressor.py +195 -123
- snowflake/ml/modeling/feature_selection/generic_univariate_select.py +195 -123
- snowflake/ml/modeling/feature_selection/select_fdr.py +195 -123
- snowflake/ml/modeling/feature_selection/select_fpr.py +195 -123
- snowflake/ml/modeling/feature_selection/select_fwe.py +195 -123
- snowflake/ml/modeling/feature_selection/select_k_best.py +195 -123
- snowflake/ml/modeling/feature_selection/select_percentile.py +195 -123
- snowflake/ml/modeling/feature_selection/sequential_feature_selector.py +195 -123
- snowflake/ml/modeling/feature_selection/variance_threshold.py +195 -123
- snowflake/ml/modeling/framework/_utils.py +8 -1
- snowflake/ml/modeling/framework/base.py +9 -1
- snowflake/ml/modeling/gaussian_process/gaussian_process_classifier.py +195 -123
- snowflake/ml/modeling/gaussian_process/gaussian_process_regressor.py +195 -123
- snowflake/ml/modeling/impute/iterative_imputer.py +195 -123
- snowflake/ml/modeling/impute/knn_imputer.py +195 -123
- snowflake/ml/modeling/impute/missing_indicator.py +195 -123
- snowflake/ml/modeling/kernel_approximation/additive_chi2_sampler.py +195 -123
- snowflake/ml/modeling/kernel_approximation/nystroem.py +195 -123
- snowflake/ml/modeling/kernel_approximation/polynomial_count_sketch.py +195 -123
- snowflake/ml/modeling/kernel_approximation/rbf_sampler.py +195 -123
- snowflake/ml/modeling/kernel_approximation/skewed_chi2_sampler.py +195 -123
- snowflake/ml/modeling/kernel_ridge/kernel_ridge.py +195 -123
- snowflake/ml/modeling/lightgbm/lgbm_classifier.py +195 -123
- snowflake/ml/modeling/lightgbm/lgbm_regressor.py +195 -123
- snowflake/ml/modeling/linear_model/ard_regression.py +195 -123
- snowflake/ml/modeling/linear_model/bayesian_ridge.py +195 -123
- snowflake/ml/modeling/linear_model/elastic_net.py +195 -123
- snowflake/ml/modeling/linear_model/elastic_net_cv.py +195 -123
- snowflake/ml/modeling/linear_model/gamma_regressor.py +195 -123
- snowflake/ml/modeling/linear_model/huber_regressor.py +195 -123
- snowflake/ml/modeling/linear_model/lars.py +195 -123
- snowflake/ml/modeling/linear_model/lars_cv.py +195 -123
- snowflake/ml/modeling/linear_model/lasso.py +195 -123
- snowflake/ml/modeling/linear_model/lasso_cv.py +195 -123
- snowflake/ml/modeling/linear_model/lasso_lars.py +195 -123
- snowflake/ml/modeling/linear_model/lasso_lars_cv.py +195 -123
- snowflake/ml/modeling/linear_model/lasso_lars_ic.py +195 -123
- snowflake/ml/modeling/linear_model/linear_regression.py +195 -123
- snowflake/ml/modeling/linear_model/logistic_regression.py +195 -123
- snowflake/ml/modeling/linear_model/logistic_regression_cv.py +195 -123
- snowflake/ml/modeling/linear_model/multi_task_elastic_net.py +195 -123
- snowflake/ml/modeling/linear_model/multi_task_elastic_net_cv.py +195 -123
- snowflake/ml/modeling/linear_model/multi_task_lasso.py +195 -123
- snowflake/ml/modeling/linear_model/multi_task_lasso_cv.py +195 -123
- snowflake/ml/modeling/linear_model/orthogonal_matching_pursuit.py +195 -123
- snowflake/ml/modeling/linear_model/passive_aggressive_classifier.py +195 -123
- snowflake/ml/modeling/linear_model/passive_aggressive_regressor.py +195 -123
- snowflake/ml/modeling/linear_model/perceptron.py +195 -123
- snowflake/ml/modeling/linear_model/poisson_regressor.py +195 -123
- snowflake/ml/modeling/linear_model/ransac_regressor.py +195 -123
- snowflake/ml/modeling/linear_model/ridge.py +195 -123
- snowflake/ml/modeling/linear_model/ridge_classifier.py +195 -123
- snowflake/ml/modeling/linear_model/ridge_classifier_cv.py +195 -123
- snowflake/ml/modeling/linear_model/ridge_cv.py +195 -123
- snowflake/ml/modeling/linear_model/sgd_classifier.py +195 -123
- snowflake/ml/modeling/linear_model/sgd_one_class_svm.py +195 -123
- snowflake/ml/modeling/linear_model/sgd_regressor.py +195 -123
- snowflake/ml/modeling/linear_model/theil_sen_regressor.py +195 -123
- snowflake/ml/modeling/linear_model/tweedie_regressor.py +195 -123
- snowflake/ml/modeling/manifold/isomap.py +195 -123
- snowflake/ml/modeling/manifold/mds.py +195 -123
- snowflake/ml/modeling/manifold/spectral_embedding.py +195 -123
- snowflake/ml/modeling/manifold/tsne.py +195 -123
- snowflake/ml/modeling/mixture/bayesian_gaussian_mixture.py +195 -123
- snowflake/ml/modeling/mixture/gaussian_mixture.py +195 -123
- snowflake/ml/modeling/model_selection/grid_search_cv.py +42 -18
- snowflake/ml/modeling/model_selection/randomized_search_cv.py +42 -18
- snowflake/ml/modeling/multiclass/one_vs_one_classifier.py +195 -123
- snowflake/ml/modeling/multiclass/one_vs_rest_classifier.py +195 -123
- snowflake/ml/modeling/multiclass/output_code_classifier.py +195 -123
- snowflake/ml/modeling/naive_bayes/bernoulli_nb.py +195 -123
- snowflake/ml/modeling/naive_bayes/categorical_nb.py +195 -123
- snowflake/ml/modeling/naive_bayes/complement_nb.py +195 -123
- snowflake/ml/modeling/naive_bayes/gaussian_nb.py +195 -123
- snowflake/ml/modeling/naive_bayes/multinomial_nb.py +195 -123
- snowflake/ml/modeling/neighbors/k_neighbors_classifier.py +195 -123
- snowflake/ml/modeling/neighbors/k_neighbors_regressor.py +195 -123
- snowflake/ml/modeling/neighbors/kernel_density.py +195 -123
- snowflake/ml/modeling/neighbors/local_outlier_factor.py +195 -123
- snowflake/ml/modeling/neighbors/nearest_centroid.py +195 -123
- snowflake/ml/modeling/neighbors/nearest_neighbors.py +195 -123
- snowflake/ml/modeling/neighbors/neighborhood_components_analysis.py +195 -123
- snowflake/ml/modeling/neighbors/radius_neighbors_classifier.py +195 -123
- snowflake/ml/modeling/neighbors/radius_neighbors_regressor.py +195 -123
- snowflake/ml/modeling/neural_network/bernoulli_rbm.py +195 -123
- snowflake/ml/modeling/neural_network/mlp_classifier.py +195 -123
- snowflake/ml/modeling/neural_network/mlp_regressor.py +195 -123
- snowflake/ml/modeling/pipeline/pipeline.py +4 -4
- snowflake/ml/modeling/preprocessing/binarizer.py +1 -5
- snowflake/ml/modeling/preprocessing/k_bins_discretizer.py +1 -5
- snowflake/ml/modeling/preprocessing/label_encoder.py +1 -5
- snowflake/ml/modeling/preprocessing/max_abs_scaler.py +1 -5
- snowflake/ml/modeling/preprocessing/min_max_scaler.py +10 -12
- snowflake/ml/modeling/preprocessing/normalizer.py +1 -5
- snowflake/ml/modeling/preprocessing/one_hot_encoder.py +1 -5
- snowflake/ml/modeling/preprocessing/ordinal_encoder.py +1 -5
- snowflake/ml/modeling/preprocessing/polynomial_features.py +195 -123
- snowflake/ml/modeling/preprocessing/robust_scaler.py +1 -5
- snowflake/ml/modeling/preprocessing/standard_scaler.py +11 -11
- snowflake/ml/modeling/semi_supervised/label_propagation.py +195 -123
- snowflake/ml/modeling/semi_supervised/label_spreading.py +195 -123
- snowflake/ml/modeling/svm/linear_svc.py +195 -123
- snowflake/ml/modeling/svm/linear_svr.py +195 -123
- snowflake/ml/modeling/svm/nu_svc.py +195 -123
- snowflake/ml/modeling/svm/nu_svr.py +195 -123
- snowflake/ml/modeling/svm/svc.py +195 -123
- snowflake/ml/modeling/svm/svr.py +195 -123
- snowflake/ml/modeling/tree/decision_tree_classifier.py +195 -123
- snowflake/ml/modeling/tree/decision_tree_regressor.py +195 -123
- snowflake/ml/modeling/tree/extra_tree_classifier.py +195 -123
- snowflake/ml/modeling/tree/extra_tree_regressor.py +195 -123
- snowflake/ml/modeling/xgboost/xgb_classifier.py +195 -123
- snowflake/ml/modeling/xgboost/xgb_regressor.py +195 -123
- snowflake/ml/modeling/xgboost/xgbrf_classifier.py +195 -123
- snowflake/ml/modeling/xgboost/xgbrf_regressor.py +195 -123
- snowflake/ml/registry/registry.py +1 -1
- snowflake/ml/version.py +1 -1
- {snowflake_ml_python-1.4.0.dist-info → snowflake_ml_python-1.4.1.dist-info}/METADATA +68 -57
- {snowflake_ml_python-1.4.0.dist-info → snowflake_ml_python-1.4.1.dist-info}/RECORD +202 -200
- snowflake/ml/model/_model_composer/model_runtime/model_runtime.py +0 -97
- {snowflake_ml_python-1.4.0.dist-info → snowflake_ml_python-1.4.1.dist-info}/LICENSE.txt +0 -0
- {snowflake_ml_python-1.4.0.dist-info → snowflake_ml_python-1.4.1.dist-info}/WHEEL +0 -0
- {snowflake_ml_python-1.4.0.dist-info → snowflake_ml_python-1.4.1.dist-info}/top_level.txt +0 -0
@@ -33,6 +33,15 @@ from snowflake.ml.modeling._internal.transformer_protocols import (
|
|
33
33
|
BatchInferenceKwargsTypedDict,
|
34
34
|
ScoreKwargsTypedDict
|
35
35
|
)
|
36
|
+
from snowflake.ml.model._signatures import utils as model_signature_utils
|
37
|
+
from snowflake.ml.model.model_signature import (
|
38
|
+
BaseFeatureSpec,
|
39
|
+
DataType,
|
40
|
+
FeatureSpec,
|
41
|
+
ModelSignature,
|
42
|
+
_infer_signature,
|
43
|
+
_rename_signature_with_snowflake_identifiers,
|
44
|
+
)
|
36
45
|
|
37
46
|
from snowflake.ml.modeling._internal.model_transformer_builder import ModelTransformerBuilder
|
38
47
|
|
@@ -43,16 +52,6 @@ from snowflake.ml.modeling._internal.estimator_utils import (
|
|
43
52
|
validate_sklearn_args,
|
44
53
|
)
|
45
54
|
|
46
|
-
from snowflake.ml.model.model_signature import (
|
47
|
-
DataType,
|
48
|
-
FeatureSpec,
|
49
|
-
ModelSignature,
|
50
|
-
_infer_signature,
|
51
|
-
_rename_signature_with_snowflake_identifiers,
|
52
|
-
BaseFeatureSpec,
|
53
|
-
)
|
54
|
-
from snowflake.ml.model._signatures import utils as model_signature_utils
|
55
|
-
|
56
55
|
_PROJECT = "ModelDevelopment"
|
57
56
|
# Derive subproject from module name by removing "sklearn"
|
58
57
|
# and converting module name from underscore to CamelCase
|
@@ -255,12 +254,7 @@ class StackingRegressor(BaseTransformer):
|
|
255
254
|
)
|
256
255
|
return selected_cols
|
257
256
|
|
258
|
-
|
259
|
-
project=_PROJECT,
|
260
|
-
subproject=_SUBPROJECT,
|
261
|
-
custom_tags=dict([("autogen", True)]),
|
262
|
-
)
|
263
|
-
def fit(self, dataset: Union[DataFrame, pd.DataFrame]) -> "StackingRegressor":
|
257
|
+
def _fit(self, dataset: Union[DataFrame, pd.DataFrame]) -> "StackingRegressor":
|
264
258
|
"""Fit the estimators
|
265
259
|
For more details on this function, see [sklearn.ensemble.StackingRegressor.fit]
|
266
260
|
(https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.StackingRegressor.html#sklearn.ensemble.StackingRegressor.fit)
|
@@ -287,12 +281,14 @@ class StackingRegressor(BaseTransformer):
|
|
287
281
|
|
288
282
|
self._snowpark_cols = dataset.select(self.input_cols).columns
|
289
283
|
|
290
|
-
|
284
|
+
# If we are already in a stored procedure, no need to kick off another one.
|
291
285
|
if SNOWML_SPROC_ENV in os.environ:
|
292
286
|
statement_params = telemetry.get_function_usage_statement_params(
|
293
287
|
project=_PROJECT,
|
294
288
|
subproject=_SUBPROJECT,
|
295
|
-
function_name=telemetry.get_statement_params_full_func_name(
|
289
|
+
function_name=telemetry.get_statement_params_full_func_name(
|
290
|
+
inspect.currentframe(), StackingRegressor.__class__.__name__
|
291
|
+
),
|
296
292
|
api_calls=[Session.call],
|
297
293
|
custom_tags=dict([("autogen", True)]) if self._autogenerated else None,
|
298
294
|
)
|
@@ -313,7 +309,7 @@ class StackingRegressor(BaseTransformer):
|
|
313
309
|
)
|
314
310
|
self._sklearn_object = model_trainer.train()
|
315
311
|
self._is_fitted = True
|
316
|
-
self.
|
312
|
+
self._generate_model_signatures(dataset)
|
317
313
|
return self
|
318
314
|
|
319
315
|
def _batch_inference_validate_snowpark(
|
@@ -389,7 +385,9 @@ class StackingRegressor(BaseTransformer):
|
|
389
385
|
# when it is classifier, infer the datatype from label columns
|
390
386
|
if expected_type_inferred == "" and 'predict' in self.model_signatures:
|
391
387
|
# Batch inference takes a single expected output column type. Use the first columns type for now.
|
392
|
-
label_cols_signatures = [
|
388
|
+
label_cols_signatures = [
|
389
|
+
row for row in self.model_signatures['predict'].outputs if row.name in self.output_cols
|
390
|
+
]
|
393
391
|
if len(label_cols_signatures) == 0:
|
394
392
|
error_str = f"Output columns {self.output_cols} do not match model signatures {self.model_signatures['predict'].outputs}."
|
395
393
|
raise exceptions.SnowflakeMLException(
|
@@ -397,25 +395,22 @@ class StackingRegressor(BaseTransformer):
|
|
397
395
|
original_exception=ValueError(error_str),
|
398
396
|
)
|
399
397
|
|
400
|
-
expected_type_inferred = convert_sp_to_sf_type(
|
401
|
-
label_cols_signatures[0].as_snowpark_type()
|
402
|
-
)
|
398
|
+
expected_type_inferred = convert_sp_to_sf_type(label_cols_signatures[0].as_snowpark_type())
|
403
399
|
|
404
400
|
self._deps = self._batch_inference_validate_snowpark(dataset=dataset, inference_method=inference_method)
|
405
|
-
assert isinstance(
|
401
|
+
assert isinstance(
|
402
|
+
dataset._session, Session
|
403
|
+
) # mypy does not recognize the check in _batch_inference_validate_snowpark()
|
406
404
|
|
407
405
|
transform_kwargs = dict(
|
408
|
-
session
|
409
|
-
dependencies
|
410
|
-
drop_input_cols
|
411
|
-
expected_output_cols_type
|
406
|
+
session=dataset._session,
|
407
|
+
dependencies=self._deps,
|
408
|
+
drop_input_cols=self._drop_input_cols,
|
409
|
+
expected_output_cols_type=expected_type_inferred,
|
412
410
|
)
|
413
411
|
|
414
412
|
elif isinstance(dataset, pd.DataFrame):
|
415
|
-
transform_kwargs = dict(
|
416
|
-
snowpark_input_cols = self._snowpark_cols,
|
417
|
-
drop_input_cols = self._drop_input_cols
|
418
|
-
)
|
413
|
+
transform_kwargs = dict(snowpark_input_cols=self._snowpark_cols, drop_input_cols=self._drop_input_cols)
|
419
414
|
|
420
415
|
transform_handlers = ModelTransformerBuilder.build(
|
421
416
|
dataset=dataset,
|
@@ -457,7 +452,7 @@ class StackingRegressor(BaseTransformer):
|
|
457
452
|
Transformed dataset.
|
458
453
|
"""
|
459
454
|
super()._check_dataset_type(dataset)
|
460
|
-
inference_method="transform"
|
455
|
+
inference_method = "transform"
|
461
456
|
|
462
457
|
# This dictionary contains optional kwargs for batch inference. These kwargs
|
463
458
|
# are specific to the type of dataset used.
|
@@ -494,17 +489,14 @@ class StackingRegressor(BaseTransformer):
|
|
494
489
|
assert isinstance(dataset._session, Session) # mypy does not recognize the check in _batch_inference_validate_snowpark()
|
495
490
|
|
496
491
|
transform_kwargs = dict(
|
497
|
-
session
|
498
|
-
dependencies
|
499
|
-
drop_input_cols
|
500
|
-
expected_output_cols_type
|
492
|
+
session=dataset._session,
|
493
|
+
dependencies=self._deps,
|
494
|
+
drop_input_cols=self._drop_input_cols,
|
495
|
+
expected_output_cols_type=expected_dtype,
|
501
496
|
)
|
502
497
|
|
503
498
|
elif isinstance(dataset, pd.DataFrame):
|
504
|
-
transform_kwargs = dict(
|
505
|
-
snowpark_input_cols = self._snowpark_cols,
|
506
|
-
drop_input_cols = self._drop_input_cols
|
507
|
-
)
|
499
|
+
transform_kwargs = dict(snowpark_input_cols=self._snowpark_cols, drop_input_cols=self._drop_input_cols)
|
508
500
|
|
509
501
|
transform_handlers = ModelTransformerBuilder.build(
|
510
502
|
dataset=dataset,
|
@@ -523,7 +515,11 @@ class StackingRegressor(BaseTransformer):
|
|
523
515
|
return output_df
|
524
516
|
|
525
517
|
@available_if(original_estimator_has_callable("fit_predict")) # type: ignore[misc]
|
526
|
-
def fit_predict(
|
518
|
+
def fit_predict(
|
519
|
+
self,
|
520
|
+
dataset: Union[DataFrame, pd.DataFrame],
|
521
|
+
output_cols_prefix: str = "fit_predict_",
|
522
|
+
) -> Union[DataFrame, pd.DataFrame]:
|
527
523
|
""" Method not supported for this class.
|
528
524
|
|
529
525
|
|
@@ -548,7 +544,9 @@ class StackingRegressor(BaseTransformer):
|
|
548
544
|
)
|
549
545
|
output_result, fitted_estimator = model_trainer.train_fit_predict(
|
550
546
|
drop_input_cols=self._drop_input_cols,
|
551
|
-
expected_output_cols_list=
|
547
|
+
expected_output_cols_list=(
|
548
|
+
self.output_cols if self.output_cols else self._get_output_column_names(output_cols_prefix)
|
549
|
+
),
|
552
550
|
)
|
553
551
|
self._sklearn_object = fitted_estimator
|
554
552
|
self._is_fitted = True
|
@@ -565,6 +563,62 @@ class StackingRegressor(BaseTransformer):
|
|
565
563
|
assert self._sklearn_object is not None
|
566
564
|
return self._sklearn_object.embedding_
|
567
565
|
|
566
|
+
|
567
|
+
def _get_output_column_names(self, output_cols_prefix: str, output_cols: Optional[List[str]] = None) -> List[str]:
|
568
|
+
""" Returns the list of output columns for predict_proba(), decision_function(), etc.. functions.
|
569
|
+
Returns a list with output_cols_prefix as the only element if the estimator is not a classifier.
|
570
|
+
"""
|
571
|
+
output_cols_prefix = identifier.resolve_identifier(output_cols_prefix)
|
572
|
+
# The following condition is introduced for kneighbors methods, and not used in other methods
|
573
|
+
if output_cols:
|
574
|
+
output_cols = [
|
575
|
+
identifier.concat_names([output_cols_prefix, identifier.resolve_identifier(c)])
|
576
|
+
for c in output_cols
|
577
|
+
]
|
578
|
+
elif getattr(self._sklearn_object, "classes_", None) is None:
|
579
|
+
output_cols = [output_cols_prefix]
|
580
|
+
elif self._sklearn_object is not None:
|
581
|
+
classes = self._sklearn_object.classes_
|
582
|
+
if isinstance(classes, numpy.ndarray):
|
583
|
+
output_cols = [f'{output_cols_prefix}{str(c)}' for c in classes.tolist()]
|
584
|
+
elif isinstance(classes, list) and len(classes) > 0 and isinstance(classes[0], numpy.ndarray):
|
585
|
+
# If the estimator is a multioutput estimator, classes_ will be a list of ndarrays.
|
586
|
+
output_cols = []
|
587
|
+
for i, cl in enumerate(classes):
|
588
|
+
# For binary classification, there is only one output column for each class
|
589
|
+
# ndarray as the two classes are complementary.
|
590
|
+
if len(cl) == 2:
|
591
|
+
output_cols.append(f'{output_cols_prefix}{i}_{cl[0]}')
|
592
|
+
else:
|
593
|
+
output_cols.extend([
|
594
|
+
f'{output_cols_prefix}{i}_{c}' for c in cl.tolist()
|
595
|
+
])
|
596
|
+
else:
|
597
|
+
output_cols = []
|
598
|
+
|
599
|
+
# Make sure column names are valid snowflake identifiers.
|
600
|
+
assert output_cols is not None # Make MyPy happy
|
601
|
+
rv = [identifier.rename_to_valid_snowflake_identifier(c) for c in output_cols]
|
602
|
+
|
603
|
+
return rv
|
604
|
+
|
605
|
+
def _align_expected_output_names(
|
606
|
+
self, method: str, dataset: DataFrame, expected_output_cols_list: List[str], output_cols_prefix: str
|
607
|
+
) -> List[str]:
|
608
|
+
# in case the inferred output column names dimension is different
|
609
|
+
# we use one line of snowpark dataframe and put it into sklearn estimator using pandas
|
610
|
+
output_df_pd = getattr(self, method)(dataset.limit(1).to_pandas(), output_cols_prefix)
|
611
|
+
output_df_columns = list(output_df_pd.columns)
|
612
|
+
output_df_columns_set: Set[str] = set(output_df_columns) - set(dataset.columns)
|
613
|
+
if self.sample_weight_col:
|
614
|
+
output_df_columns_set -= set(self.sample_weight_col)
|
615
|
+
# if the dimension of inferred output column names is correct; use it
|
616
|
+
if len(expected_output_cols_list) == len(output_df_columns_set):
|
617
|
+
return expected_output_cols_list
|
618
|
+
# otherwise, use the sklearn estimator's output
|
619
|
+
else:
|
620
|
+
return sorted(list(output_df_columns_set), key=lambda x: output_df_columns.index(x))
|
621
|
+
|
568
622
|
@available_if(original_estimator_has_callable("predict_proba")) # type: ignore[misc]
|
569
623
|
@telemetry.send_api_usage_telemetry(
|
570
624
|
project=_PROJECT,
|
@@ -595,24 +649,28 @@ class StackingRegressor(BaseTransformer):
|
|
595
649
|
# are specific to the type of dataset used.
|
596
650
|
transform_kwargs: BatchInferenceKwargsTypedDict = dict()
|
597
651
|
|
652
|
+
expected_output_cols = self._get_output_column_names(output_cols_prefix)
|
653
|
+
|
598
654
|
if isinstance(dataset, DataFrame):
|
599
655
|
self._deps = self._batch_inference_validate_snowpark(
|
600
656
|
dataset=dataset,
|
601
657
|
inference_method=inference_method,
|
602
658
|
)
|
603
|
-
assert isinstance(
|
659
|
+
assert isinstance(
|
660
|
+
dataset._session, Session
|
661
|
+
) # mypy does not recognize the check in _batch_inference_validate_snowpark()
|
604
662
|
transform_kwargs = dict(
|
605
663
|
session=dataset._session,
|
606
664
|
dependencies=self._deps,
|
607
|
-
drop_input_cols
|
665
|
+
drop_input_cols=self._drop_input_cols,
|
608
666
|
expected_output_cols_type="float",
|
609
667
|
)
|
668
|
+
expected_output_cols = self._align_expected_output_names(
|
669
|
+
inference_method, dataset, expected_output_cols, output_cols_prefix
|
670
|
+
)
|
610
671
|
|
611
672
|
elif isinstance(dataset, pd.DataFrame):
|
612
|
-
transform_kwargs = dict(
|
613
|
-
snowpark_input_cols = self._snowpark_cols,
|
614
|
-
drop_input_cols = self._drop_input_cols
|
615
|
-
)
|
673
|
+
transform_kwargs = dict(snowpark_input_cols=self._snowpark_cols, drop_input_cols=self._drop_input_cols)
|
616
674
|
|
617
675
|
transform_handlers = ModelTransformerBuilder.build(
|
618
676
|
dataset=dataset,
|
@@ -624,7 +682,7 @@ class StackingRegressor(BaseTransformer):
|
|
624
682
|
output_df: DATAFRAME_TYPE = transform_handlers.batch_inference(
|
625
683
|
inference_method=inference_method,
|
626
684
|
input_cols=self.input_cols,
|
627
|
-
expected_output_cols=
|
685
|
+
expected_output_cols=expected_output_cols,
|
628
686
|
**transform_kwargs
|
629
687
|
)
|
630
688
|
return output_df
|
@@ -654,7 +712,8 @@ class StackingRegressor(BaseTransformer):
|
|
654
712
|
Output dataset with log probability of the sample for each class in the model.
|
655
713
|
"""
|
656
714
|
super()._check_dataset_type(dataset)
|
657
|
-
inference_method="predict_log_proba"
|
715
|
+
inference_method = "predict_log_proba"
|
716
|
+
expected_output_cols = self._get_output_column_names(output_cols_prefix)
|
658
717
|
|
659
718
|
# This dictionary contains optional kwargs for batch inference. These kwargs
|
660
719
|
# are specific to the type of dataset used.
|
@@ -665,18 +724,20 @@ class StackingRegressor(BaseTransformer):
|
|
665
724
|
dataset=dataset,
|
666
725
|
inference_method=inference_method,
|
667
726
|
)
|
668
|
-
assert isinstance(
|
727
|
+
assert isinstance(
|
728
|
+
dataset._session, Session
|
729
|
+
) # mypy does not recognize the check in _batch_inference_validate_snowpark()
|
669
730
|
transform_kwargs = dict(
|
670
731
|
session=dataset._session,
|
671
732
|
dependencies=self._deps,
|
672
|
-
drop_input_cols
|
733
|
+
drop_input_cols=self._drop_input_cols,
|
673
734
|
expected_output_cols_type="float",
|
674
735
|
)
|
736
|
+
expected_output_cols = self._align_expected_output_names(
|
737
|
+
inference_method, dataset, expected_output_cols, output_cols_prefix
|
738
|
+
)
|
675
739
|
elif isinstance(dataset, pd.DataFrame):
|
676
|
-
transform_kwargs = dict(
|
677
|
-
snowpark_input_cols = self._snowpark_cols,
|
678
|
-
drop_input_cols = self._drop_input_cols
|
679
|
-
)
|
740
|
+
transform_kwargs = dict(snowpark_input_cols=self._snowpark_cols, drop_input_cols=self._drop_input_cols)
|
680
741
|
|
681
742
|
transform_handlers = ModelTransformerBuilder.build(
|
682
743
|
dataset=dataset,
|
@@ -689,7 +750,7 @@ class StackingRegressor(BaseTransformer):
|
|
689
750
|
output_df: DATAFRAME_TYPE = transform_handlers.batch_inference(
|
690
751
|
inference_method=inference_method,
|
691
752
|
input_cols=self.input_cols,
|
692
|
-
expected_output_cols=
|
753
|
+
expected_output_cols=expected_output_cols,
|
693
754
|
**transform_kwargs
|
694
755
|
)
|
695
756
|
return output_df
|
@@ -715,30 +776,34 @@ class StackingRegressor(BaseTransformer):
|
|
715
776
|
Output dataset with results of the decision function for the samples in input dataset.
|
716
777
|
"""
|
717
778
|
super()._check_dataset_type(dataset)
|
718
|
-
inference_method="decision_function"
|
779
|
+
inference_method = "decision_function"
|
719
780
|
|
720
781
|
# This dictionary contains optional kwargs for batch inference. These kwargs
|
721
782
|
# are specific to the type of dataset used.
|
722
783
|
transform_kwargs: BatchInferenceKwargsTypedDict = dict()
|
723
784
|
|
785
|
+
expected_output_cols = self._get_output_column_names(output_cols_prefix)
|
786
|
+
|
724
787
|
if isinstance(dataset, DataFrame):
|
725
788
|
self._deps = self._batch_inference_validate_snowpark(
|
726
789
|
dataset=dataset,
|
727
790
|
inference_method=inference_method,
|
728
791
|
)
|
729
|
-
assert isinstance(
|
792
|
+
assert isinstance(
|
793
|
+
dataset._session, Session
|
794
|
+
) # mypy does not recognize the check in _batch_inference_validate_snowpark()
|
730
795
|
transform_kwargs = dict(
|
731
796
|
session=dataset._session,
|
732
797
|
dependencies=self._deps,
|
733
|
-
drop_input_cols
|
798
|
+
drop_input_cols=self._drop_input_cols,
|
734
799
|
expected_output_cols_type="float",
|
735
800
|
)
|
801
|
+
expected_output_cols = self._align_expected_output_names(
|
802
|
+
inference_method, dataset, expected_output_cols, output_cols_prefix
|
803
|
+
)
|
736
804
|
|
737
805
|
elif isinstance(dataset, pd.DataFrame):
|
738
|
-
transform_kwargs = dict(
|
739
|
-
snowpark_input_cols = self._snowpark_cols,
|
740
|
-
drop_input_cols = self._drop_input_cols
|
741
|
-
)
|
806
|
+
transform_kwargs = dict(snowpark_input_cols=self._snowpark_cols, drop_input_cols=self._drop_input_cols)
|
742
807
|
|
743
808
|
transform_handlers = ModelTransformerBuilder.build(
|
744
809
|
dataset=dataset,
|
@@ -751,7 +816,7 @@ class StackingRegressor(BaseTransformer):
|
|
751
816
|
output_df: DATAFRAME_TYPE = transform_handlers.batch_inference(
|
752
817
|
inference_method=inference_method,
|
753
818
|
input_cols=self.input_cols,
|
754
|
-
expected_output_cols=
|
819
|
+
expected_output_cols=expected_output_cols,
|
755
820
|
**transform_kwargs
|
756
821
|
)
|
757
822
|
return output_df
|
@@ -780,12 +845,14 @@ class StackingRegressor(BaseTransformer):
|
|
780
845
|
Output dataset with probability of the sample for each class in the model.
|
781
846
|
"""
|
782
847
|
super()._check_dataset_type(dataset)
|
783
|
-
inference_method="score_samples"
|
848
|
+
inference_method = "score_samples"
|
784
849
|
|
785
850
|
# This dictionary contains optional kwargs for batch inference. These kwargs
|
786
851
|
# are specific to the type of dataset used.
|
787
852
|
transform_kwargs: BatchInferenceKwargsTypedDict = dict()
|
788
853
|
|
854
|
+
expected_output_cols = self._get_output_column_names(output_cols_prefix)
|
855
|
+
|
789
856
|
if isinstance(dataset, DataFrame):
|
790
857
|
self._deps = self._batch_inference_validate_snowpark(
|
791
858
|
dataset=dataset,
|
@@ -798,6 +865,9 @@ class StackingRegressor(BaseTransformer):
|
|
798
865
|
drop_input_cols = self._drop_input_cols,
|
799
866
|
expected_output_cols_type="float",
|
800
867
|
)
|
868
|
+
expected_output_cols = self._align_expected_output_names(
|
869
|
+
inference_method, dataset, expected_output_cols, output_cols_prefix
|
870
|
+
)
|
801
871
|
|
802
872
|
elif isinstance(dataset, pd.DataFrame):
|
803
873
|
transform_kwargs = dict(
|
@@ -816,7 +886,7 @@ class StackingRegressor(BaseTransformer):
|
|
816
886
|
output_df: DATAFRAME_TYPE = transform_handlers.batch_inference(
|
817
887
|
inference_method=inference_method,
|
818
888
|
input_cols=self.input_cols,
|
819
|
-
expected_output_cols=
|
889
|
+
expected_output_cols=expected_output_cols,
|
820
890
|
**transform_kwargs
|
821
891
|
)
|
822
892
|
return output_df
|
@@ -963,50 +1033,84 @@ class StackingRegressor(BaseTransformer):
|
|
963
1033
|
)
|
964
1034
|
return output_df
|
965
1035
|
|
1036
|
+
|
1037
|
+
|
1038
|
+
def to_sklearn(self) -> Any:
|
1039
|
+
"""Get sklearn.ensemble.StackingRegressor object.
|
1040
|
+
"""
|
1041
|
+
if self._sklearn_object is None:
|
1042
|
+
self._sklearn_object = self._create_sklearn_object()
|
1043
|
+
return self._sklearn_object
|
1044
|
+
|
1045
|
+
def to_xgboost(self) -> Any:
|
1046
|
+
raise exceptions.SnowflakeMLException(
|
1047
|
+
error_code=error_codes.METHOD_NOT_ALLOWED,
|
1048
|
+
original_exception=AttributeError(
|
1049
|
+
modeling_error_messages.UNSUPPORTED_MODEL_CONVERSION.format(
|
1050
|
+
"to_xgboost()",
|
1051
|
+
"to_sklearn()"
|
1052
|
+
)
|
1053
|
+
),
|
1054
|
+
)
|
1055
|
+
|
1056
|
+
def to_lightgbm(self) -> Any:
|
1057
|
+
raise exceptions.SnowflakeMLException(
|
1058
|
+
error_code=error_codes.METHOD_NOT_ALLOWED,
|
1059
|
+
original_exception=AttributeError(
|
1060
|
+
modeling_error_messages.UNSUPPORTED_MODEL_CONVERSION.format(
|
1061
|
+
"to_lightgbm()",
|
1062
|
+
"to_sklearn()"
|
1063
|
+
)
|
1064
|
+
),
|
1065
|
+
)
|
966
1066
|
|
967
|
-
def
|
1067
|
+
def _get_dependencies(self) -> List[str]:
|
1068
|
+
return self._deps
|
1069
|
+
|
1070
|
+
|
1071
|
+
def _generate_model_signatures(self, dataset: Union[DataFrame, pd.DataFrame]) -> None:
|
968
1072
|
self._model_signature_dict = dict()
|
969
1073
|
|
970
1074
|
PROB_FUNCTIONS = ["predict_log_proba", "predict_proba", "decision_function"]
|
971
1075
|
|
972
|
-
inputs = list(_infer_signature(dataset[self.input_cols], "input"))
|
1076
|
+
inputs = list(_infer_signature(dataset[self.input_cols], "input", use_snowflake_identifiers=True))
|
973
1077
|
outputs: List[BaseFeatureSpec] = []
|
974
1078
|
if hasattr(self, "predict"):
|
975
1079
|
# keep mypy happy
|
976
|
-
assert self._sklearn_object is not None and hasattr(self._sklearn_object, "_estimator_type")
|
1080
|
+
assert self._sklearn_object is not None and hasattr(self._sklearn_object, "_estimator_type")
|
977
1081
|
# For classifier, the type of predict is the same as the type of label
|
978
|
-
if self._sklearn_object._estimator_type ==
|
979
|
-
|
1082
|
+
if self._sklearn_object._estimator_type == "classifier":
|
1083
|
+
# label columns is the desired type for output
|
980
1084
|
outputs = list(_infer_signature(dataset[self.label_cols], "output", use_snowflake_identifiers=True))
|
981
1085
|
# rename the output columns
|
982
1086
|
outputs = list(model_signature_utils.rename_features(outputs, self.output_cols))
|
983
|
-
self._model_signature_dict["predict"] = ModelSignature(
|
984
|
-
|
985
|
-
|
1087
|
+
self._model_signature_dict["predict"] = ModelSignature(
|
1088
|
+
inputs, ([] if self._drop_input_cols else inputs) + outputs
|
1089
|
+
)
|
986
1090
|
# For mixture models that use the density mixin, `predict` returns the argmax of the log prob.
|
987
1091
|
# For outlier models, returns -1 for outliers and 1 for inliers.
|
988
|
-
# Clusterer returns int64 cluster labels.
|
1092
|
+
# Clusterer returns int64 cluster labels.
|
989
1093
|
elif self._sklearn_object._estimator_type in ["DensityEstimator", "clusterer", "outlier_detector"]:
|
990
1094
|
outputs = [FeatureSpec(dtype=DataType.INT64, name=c) for c in self.output_cols]
|
991
|
-
self._model_signature_dict["predict"] = ModelSignature(
|
992
|
-
|
993
|
-
|
994
|
-
|
1095
|
+
self._model_signature_dict["predict"] = ModelSignature(
|
1096
|
+
inputs, ([] if self._drop_input_cols else inputs) + outputs
|
1097
|
+
)
|
1098
|
+
|
995
1099
|
# For regressor, the type of predict is float64
|
996
|
-
elif self._sklearn_object._estimator_type ==
|
1100
|
+
elif self._sklearn_object._estimator_type == "regressor":
|
997
1101
|
outputs = [FeatureSpec(dtype=DataType.DOUBLE, name=c) for c in self.output_cols]
|
998
|
-
self._model_signature_dict["predict"] = ModelSignature(
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1102
|
+
self._model_signature_dict["predict"] = ModelSignature(
|
1103
|
+
inputs, ([] if self._drop_input_cols else inputs) + outputs
|
1104
|
+
)
|
1105
|
+
|
1002
1106
|
for prob_func in PROB_FUNCTIONS:
|
1003
1107
|
if hasattr(self, prob_func):
|
1004
1108
|
output_cols_prefix: str = f"{prob_func}_"
|
1005
1109
|
output_column_names = self._get_output_column_names(output_cols_prefix)
|
1006
1110
|
outputs = [FeatureSpec(dtype=DataType.DOUBLE, name=c) for c in output_column_names]
|
1007
|
-
self._model_signature_dict[prob_func] = ModelSignature(
|
1008
|
-
|
1009
|
-
|
1111
|
+
self._model_signature_dict[prob_func] = ModelSignature(
|
1112
|
+
inputs, ([] if self._drop_input_cols else inputs) + outputs
|
1113
|
+
)
|
1010
1114
|
|
1011
1115
|
# Output signature names may still need to be renamed, since they were not created with `_infer_signature`.
|
1012
1116
|
items = list(self._model_signature_dict.items())
|
@@ -1019,10 +1123,10 @@ class StackingRegressor(BaseTransformer):
|
|
1019
1123
|
"""Returns model signature of current class.
|
1020
1124
|
|
1021
1125
|
Raises:
|
1022
|
-
|
1126
|
+
SnowflakeMLException: If estimator is not fitted, then model signature cannot be inferred
|
1023
1127
|
|
1024
1128
|
Returns:
|
1025
|
-
Dict
|
1129
|
+
Dict with each method and its input output signature
|
1026
1130
|
"""
|
1027
1131
|
if self._model_signature_dict is None:
|
1028
1132
|
raise exceptions.SnowflakeMLException(
|
@@ -1030,35 +1134,3 @@ class StackingRegressor(BaseTransformer):
|
|
1030
1134
|
original_exception=RuntimeError("Estimator not fitted before accessing property model_signatures!"),
|
1031
1135
|
)
|
1032
1136
|
return self._model_signature_dict
|
1033
|
-
|
1034
|
-
def to_sklearn(self) -> Any:
|
1035
|
-
"""Get sklearn.ensemble.StackingRegressor object.
|
1036
|
-
"""
|
1037
|
-
if self._sklearn_object is None:
|
1038
|
-
self._sklearn_object = self._create_sklearn_object()
|
1039
|
-
return self._sklearn_object
|
1040
|
-
|
1041
|
-
def to_xgboost(self) -> Any:
|
1042
|
-
raise exceptions.SnowflakeMLException(
|
1043
|
-
error_code=error_codes.METHOD_NOT_ALLOWED,
|
1044
|
-
original_exception=AttributeError(
|
1045
|
-
modeling_error_messages.UNSUPPORTED_MODEL_CONVERSION.format(
|
1046
|
-
"to_xgboost()",
|
1047
|
-
"to_sklearn()"
|
1048
|
-
)
|
1049
|
-
),
|
1050
|
-
)
|
1051
|
-
|
1052
|
-
def to_lightgbm(self) -> Any:
|
1053
|
-
raise exceptions.SnowflakeMLException(
|
1054
|
-
error_code=error_codes.METHOD_NOT_ALLOWED,
|
1055
|
-
original_exception=AttributeError(
|
1056
|
-
modeling_error_messages.UNSUPPORTED_MODEL_CONVERSION.format(
|
1057
|
-
"to_lightgbm()",
|
1058
|
-
"to_sklearn()"
|
1059
|
-
)
|
1060
|
-
),
|
1061
|
-
)
|
1062
|
-
|
1063
|
-
def _get_dependencies(self) -> List[str]:
|
1064
|
-
return self._deps
|