snowflake-ml-python 1.4.0__py3-none-any.whl → 1.5.0__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 +77 -32
- snowflake/ml/_internal/exceptions/dataset_error_messages.py +5 -0
- snowflake/ml/_internal/exceptions/dataset_errors.py +24 -0
- snowflake/ml/_internal/exceptions/error_codes.py +3 -0
- snowflake/ml/_internal/lineage/data_source.py +10 -0
- snowflake/ml/_internal/lineage/dataset_dataframe.py +44 -0
- snowflake/ml/_internal/utils/identifier.py +3 -1
- snowflake/ml/_internal/utils/sql_identifier.py +2 -6
- snowflake/ml/dataset/__init__.py +10 -0
- snowflake/ml/dataset/dataset.py +454 -129
- snowflake/ml/dataset/dataset_factory.py +53 -0
- snowflake/ml/dataset/dataset_metadata.py +103 -0
- snowflake/ml/dataset/dataset_reader.py +202 -0
- snowflake/ml/feature_store/feature_store.py +531 -332
- snowflake/ml/feature_store/feature_view.py +40 -23
- snowflake/ml/fileset/embedded_stage_fs.py +146 -0
- snowflake/ml/fileset/sfcfs.py +56 -54
- snowflake/ml/fileset/snowfs.py +159 -0
- snowflake/ml/fileset/stage_fs.py +49 -17
- snowflake/ml/model/__init__.py +2 -2
- snowflake/ml/model/_api.py +16 -1
- snowflake/ml/model/_client/model/model_impl.py +27 -0
- snowflake/ml/model/_client/model/model_version_impl.py +137 -50
- snowflake/ml/model/_client/ops/model_ops.py +159 -40
- snowflake/ml/model/_client/sql/model.py +25 -2
- snowflake/ml/model/_client/sql/model_version.py +131 -2
- snowflake/ml/model/_deploy_client/image_builds/server_image_builder.py +5 -1
- snowflake/ml/model/_deploy_client/image_builds/templates/dockerfile_template +1 -0
- snowflake/ml/model/_deploy_client/snowservice/deploy.py +2 -0
- snowflake/ml/model/_deploy_client/utils/constants.py +0 -5
- snowflake/ml/model/_deploy_client/utils/snowservice_client.py +21 -50
- snowflake/ml/model/_model_composer/model_composer.py +22 -1
- snowflake/ml/model/_model_composer/model_manifest/model_manifest.py +38 -51
- snowflake/ml/model/_model_composer/model_manifest/model_manifest_schema.py +19 -1
- snowflake/ml/model/_model_composer/model_method/model_method.py +6 -10
- snowflake/ml/model/_packager/model_env/model_env.py +41 -0
- 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 +37 -11
- 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 -5
- 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/local_implementations/pandas_trainer.py +55 -3
- snowflake/ml/modeling/_internal/ml_runtime_implementations/ml_runtime_handlers.py +34 -18
- snowflake/ml/modeling/_internal/model_trainer.py +7 -0
- snowflake/ml/modeling/_internal/model_trainer_builder.py +42 -9
- snowflake/ml/modeling/_internal/snowpark_implementations/distributed_hpo_trainer.py +13 -14
- snowflake/ml/modeling/_internal/snowpark_implementations/snowpark_handlers.py +29 -7
- snowflake/ml/modeling/_internal/snowpark_implementations/snowpark_trainer.py +261 -16
- snowflake/ml/modeling/calibration/calibrated_classifier_cv.py +246 -175
- snowflake/ml/modeling/cluster/affinity_propagation.py +246 -175
- snowflake/ml/modeling/cluster/agglomerative_clustering.py +246 -175
- snowflake/ml/modeling/cluster/birch.py +248 -175
- snowflake/ml/modeling/cluster/bisecting_k_means.py +248 -175
- snowflake/ml/modeling/cluster/dbscan.py +246 -175
- snowflake/ml/modeling/cluster/feature_agglomeration.py +248 -175
- snowflake/ml/modeling/cluster/k_means.py +248 -175
- snowflake/ml/modeling/cluster/mean_shift.py +246 -175
- snowflake/ml/modeling/cluster/mini_batch_k_means.py +248 -175
- snowflake/ml/modeling/cluster/optics.py +246 -175
- snowflake/ml/modeling/cluster/spectral_biclustering.py +246 -175
- snowflake/ml/modeling/cluster/spectral_clustering.py +246 -175
- snowflake/ml/modeling/cluster/spectral_coclustering.py +246 -175
- snowflake/ml/modeling/compose/column_transformer.py +248 -175
- snowflake/ml/modeling/compose/transformed_target_regressor.py +246 -175
- snowflake/ml/modeling/covariance/elliptic_envelope.py +246 -175
- snowflake/ml/modeling/covariance/empirical_covariance.py +246 -175
- snowflake/ml/modeling/covariance/graphical_lasso.py +246 -175
- snowflake/ml/modeling/covariance/graphical_lasso_cv.py +246 -175
- snowflake/ml/modeling/covariance/ledoit_wolf.py +246 -175
- snowflake/ml/modeling/covariance/min_cov_det.py +246 -175
- snowflake/ml/modeling/covariance/oas.py +246 -175
- snowflake/ml/modeling/covariance/shrunk_covariance.py +246 -175
- snowflake/ml/modeling/decomposition/dictionary_learning.py +248 -175
- snowflake/ml/modeling/decomposition/factor_analysis.py +248 -175
- snowflake/ml/modeling/decomposition/fast_ica.py +248 -175
- snowflake/ml/modeling/decomposition/incremental_pca.py +248 -175
- snowflake/ml/modeling/decomposition/kernel_pca.py +248 -175
- snowflake/ml/modeling/decomposition/mini_batch_dictionary_learning.py +248 -175
- snowflake/ml/modeling/decomposition/mini_batch_sparse_pca.py +248 -175
- snowflake/ml/modeling/decomposition/pca.py +248 -175
- snowflake/ml/modeling/decomposition/sparse_pca.py +248 -175
- snowflake/ml/modeling/decomposition/truncated_svd.py +248 -175
- snowflake/ml/modeling/discriminant_analysis/linear_discriminant_analysis.py +248 -175
- snowflake/ml/modeling/discriminant_analysis/quadratic_discriminant_analysis.py +246 -175
- snowflake/ml/modeling/ensemble/ada_boost_classifier.py +246 -175
- snowflake/ml/modeling/ensemble/ada_boost_regressor.py +246 -175
- snowflake/ml/modeling/ensemble/bagging_classifier.py +246 -175
- snowflake/ml/modeling/ensemble/bagging_regressor.py +246 -175
- snowflake/ml/modeling/ensemble/extra_trees_classifier.py +246 -175
- snowflake/ml/modeling/ensemble/extra_trees_regressor.py +246 -175
- snowflake/ml/modeling/ensemble/gradient_boosting_classifier.py +246 -175
- snowflake/ml/modeling/ensemble/gradient_boosting_regressor.py +246 -175
- snowflake/ml/modeling/ensemble/hist_gradient_boosting_classifier.py +246 -175
- snowflake/ml/modeling/ensemble/hist_gradient_boosting_regressor.py +246 -175
- snowflake/ml/modeling/ensemble/isolation_forest.py +246 -175
- snowflake/ml/modeling/ensemble/random_forest_classifier.py +246 -175
- snowflake/ml/modeling/ensemble/random_forest_regressor.py +246 -175
- snowflake/ml/modeling/ensemble/stacking_regressor.py +248 -175
- snowflake/ml/modeling/ensemble/voting_classifier.py +248 -175
- snowflake/ml/modeling/ensemble/voting_regressor.py +248 -175
- snowflake/ml/modeling/feature_selection/generic_univariate_select.py +248 -175
- snowflake/ml/modeling/feature_selection/select_fdr.py +248 -175
- snowflake/ml/modeling/feature_selection/select_fpr.py +248 -175
- snowflake/ml/modeling/feature_selection/select_fwe.py +248 -175
- snowflake/ml/modeling/feature_selection/select_k_best.py +248 -175
- snowflake/ml/modeling/feature_selection/select_percentile.py +248 -175
- snowflake/ml/modeling/feature_selection/sequential_feature_selector.py +248 -175
- snowflake/ml/modeling/feature_selection/variance_threshold.py +248 -175
- snowflake/ml/modeling/framework/_utils.py +8 -1
- snowflake/ml/modeling/framework/base.py +72 -37
- snowflake/ml/modeling/gaussian_process/gaussian_process_classifier.py +246 -175
- snowflake/ml/modeling/gaussian_process/gaussian_process_regressor.py +246 -175
- snowflake/ml/modeling/impute/iterative_imputer.py +248 -175
- snowflake/ml/modeling/impute/knn_imputer.py +248 -175
- snowflake/ml/modeling/impute/missing_indicator.py +248 -175
- snowflake/ml/modeling/kernel_approximation/additive_chi2_sampler.py +248 -175
- snowflake/ml/modeling/kernel_approximation/nystroem.py +248 -175
- snowflake/ml/modeling/kernel_approximation/polynomial_count_sketch.py +248 -175
- snowflake/ml/modeling/kernel_approximation/rbf_sampler.py +248 -175
- snowflake/ml/modeling/kernel_approximation/skewed_chi2_sampler.py +248 -175
- snowflake/ml/modeling/kernel_ridge/kernel_ridge.py +246 -175
- snowflake/ml/modeling/lightgbm/lgbm_classifier.py +246 -175
- snowflake/ml/modeling/lightgbm/lgbm_regressor.py +246 -175
- snowflake/ml/modeling/linear_model/ard_regression.py +246 -175
- snowflake/ml/modeling/linear_model/bayesian_ridge.py +246 -175
- snowflake/ml/modeling/linear_model/elastic_net.py +246 -175
- snowflake/ml/modeling/linear_model/elastic_net_cv.py +246 -175
- snowflake/ml/modeling/linear_model/gamma_regressor.py +246 -175
- snowflake/ml/modeling/linear_model/huber_regressor.py +246 -175
- snowflake/ml/modeling/linear_model/lars.py +246 -175
- snowflake/ml/modeling/linear_model/lars_cv.py +246 -175
- snowflake/ml/modeling/linear_model/lasso.py +246 -175
- snowflake/ml/modeling/linear_model/lasso_cv.py +246 -175
- snowflake/ml/modeling/linear_model/lasso_lars.py +246 -175
- snowflake/ml/modeling/linear_model/lasso_lars_cv.py +246 -175
- snowflake/ml/modeling/linear_model/lasso_lars_ic.py +246 -175
- snowflake/ml/modeling/linear_model/linear_regression.py +246 -175
- snowflake/ml/modeling/linear_model/logistic_regression.py +246 -175
- snowflake/ml/modeling/linear_model/logistic_regression_cv.py +246 -175
- snowflake/ml/modeling/linear_model/multi_task_elastic_net.py +246 -175
- snowflake/ml/modeling/linear_model/multi_task_elastic_net_cv.py +246 -175
- snowflake/ml/modeling/linear_model/multi_task_lasso.py +246 -175
- snowflake/ml/modeling/linear_model/multi_task_lasso_cv.py +246 -175
- snowflake/ml/modeling/linear_model/orthogonal_matching_pursuit.py +246 -175
- snowflake/ml/modeling/linear_model/passive_aggressive_classifier.py +246 -175
- snowflake/ml/modeling/linear_model/passive_aggressive_regressor.py +246 -175
- snowflake/ml/modeling/linear_model/perceptron.py +246 -175
- snowflake/ml/modeling/linear_model/poisson_regressor.py +246 -175
- snowflake/ml/modeling/linear_model/ransac_regressor.py +246 -175
- snowflake/ml/modeling/linear_model/ridge.py +246 -175
- snowflake/ml/modeling/linear_model/ridge_classifier.py +246 -175
- snowflake/ml/modeling/linear_model/ridge_classifier_cv.py +246 -175
- snowflake/ml/modeling/linear_model/ridge_cv.py +246 -175
- snowflake/ml/modeling/linear_model/sgd_classifier.py +246 -175
- snowflake/ml/modeling/linear_model/sgd_one_class_svm.py +246 -175
- snowflake/ml/modeling/linear_model/sgd_regressor.py +246 -175
- snowflake/ml/modeling/linear_model/theil_sen_regressor.py +246 -175
- snowflake/ml/modeling/linear_model/tweedie_regressor.py +246 -175
- snowflake/ml/modeling/manifold/isomap.py +248 -175
- snowflake/ml/modeling/manifold/mds.py +248 -175
- snowflake/ml/modeling/manifold/spectral_embedding.py +248 -175
- snowflake/ml/modeling/manifold/tsne.py +248 -175
- snowflake/ml/modeling/mixture/bayesian_gaussian_mixture.py +246 -175
- snowflake/ml/modeling/mixture/gaussian_mixture.py +246 -175
- snowflake/ml/modeling/model_selection/grid_search_cv.py +63 -41
- snowflake/ml/modeling/model_selection/randomized_search_cv.py +80 -38
- snowflake/ml/modeling/multiclass/one_vs_one_classifier.py +246 -175
- snowflake/ml/modeling/multiclass/one_vs_rest_classifier.py +246 -175
- snowflake/ml/modeling/multiclass/output_code_classifier.py +246 -175
- snowflake/ml/modeling/naive_bayes/bernoulli_nb.py +246 -175
- snowflake/ml/modeling/naive_bayes/categorical_nb.py +246 -175
- snowflake/ml/modeling/naive_bayes/complement_nb.py +246 -175
- snowflake/ml/modeling/naive_bayes/gaussian_nb.py +246 -175
- snowflake/ml/modeling/naive_bayes/multinomial_nb.py +246 -175
- snowflake/ml/modeling/neighbors/k_neighbors_classifier.py +246 -175
- snowflake/ml/modeling/neighbors/k_neighbors_regressor.py +246 -175
- snowflake/ml/modeling/neighbors/kernel_density.py +246 -175
- snowflake/ml/modeling/neighbors/local_outlier_factor.py +246 -175
- snowflake/ml/modeling/neighbors/nearest_centroid.py +246 -175
- snowflake/ml/modeling/neighbors/nearest_neighbors.py +246 -175
- snowflake/ml/modeling/neighbors/neighborhood_components_analysis.py +248 -175
- snowflake/ml/modeling/neighbors/radius_neighbors_classifier.py +246 -175
- snowflake/ml/modeling/neighbors/radius_neighbors_regressor.py +246 -175
- snowflake/ml/modeling/neural_network/bernoulli_rbm.py +248 -175
- snowflake/ml/modeling/neural_network/mlp_classifier.py +246 -175
- snowflake/ml/modeling/neural_network/mlp_regressor.py +246 -175
- snowflake/ml/modeling/pipeline/pipeline.py +517 -35
- 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 +13 -5
- snowflake/ml/modeling/preprocessing/ordinal_encoder.py +1 -5
- snowflake/ml/modeling/preprocessing/polynomial_features.py +248 -175
- 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 +246 -175
- snowflake/ml/modeling/semi_supervised/label_spreading.py +246 -175
- snowflake/ml/modeling/svm/linear_svc.py +246 -175
- snowflake/ml/modeling/svm/linear_svr.py +246 -175
- snowflake/ml/modeling/svm/nu_svc.py +246 -175
- snowflake/ml/modeling/svm/nu_svr.py +246 -175
- snowflake/ml/modeling/svm/svc.py +246 -175
- snowflake/ml/modeling/svm/svr.py +246 -175
- snowflake/ml/modeling/tree/decision_tree_classifier.py +246 -175
- snowflake/ml/modeling/tree/decision_tree_regressor.py +246 -175
- snowflake/ml/modeling/tree/extra_tree_classifier.py +246 -175
- snowflake/ml/modeling/tree/extra_tree_regressor.py +246 -175
- snowflake/ml/modeling/xgboost/xgb_classifier.py +246 -175
- snowflake/ml/modeling/xgboost/xgb_regressor.py +246 -175
- snowflake/ml/modeling/xgboost/xgbrf_classifier.py +246 -175
- snowflake/ml/modeling/xgboost/xgbrf_regressor.py +246 -175
- snowflake/ml/registry/model_registry.py +3 -149
- 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.5.0.dist-info}/METADATA +129 -57
- snowflake_ml_python-1.5.0.dist-info/RECORD +380 -0
- snowflake/ml/model/_model_composer/model_runtime/model_runtime.py +0 -97
- snowflake/ml/registry/_artifact_manager.py +0 -156
- snowflake/ml/registry/artifact.py +0 -46
- snowflake_ml_python-1.4.0.dist-info/RECORD +0 -370
- {snowflake_ml_python-1.4.0.dist-info → snowflake_ml_python-1.5.0.dist-info}/LICENSE.txt +0 -0
- {snowflake_ml_python-1.4.0.dist-info → snowflake_ml_python-1.5.0.dist-info}/WHEEL +0 -0
- {snowflake_ml_python-1.4.0.dist-info → snowflake_ml_python-1.5.0.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
|
@@ -61,12 +60,6 @@ _SUBPROJECT = "".join([s.capitalize() for s in "sklearn.neighbors".replace("skle
|
|
61
60
|
|
62
61
|
DATAFRAME_TYPE = Union[DataFrame, pd.DataFrame]
|
63
62
|
|
64
|
-
def _is_fit_transform_method_enabled() -> Callable[[Any], bool]:
|
65
|
-
def check(self: BaseTransformer) -> TypeGuard[Callable[..., object]]:
|
66
|
-
return False and callable(getattr(self._sklearn_object, "fit_transform", None))
|
67
|
-
return check
|
68
|
-
|
69
|
-
|
70
63
|
class LocalOutlierFactor(BaseTransformer):
|
71
64
|
r"""Unsupervised Outlier Detection using the Local Outlier Factor (LOF)
|
72
65
|
For more details on this class, see [sklearn.neighbors.LocalOutlierFactor]
|
@@ -280,12 +273,7 @@ class LocalOutlierFactor(BaseTransformer):
|
|
280
273
|
)
|
281
274
|
return selected_cols
|
282
275
|
|
283
|
-
|
284
|
-
project=_PROJECT,
|
285
|
-
subproject=_SUBPROJECT,
|
286
|
-
custom_tags=dict([("autogen", True)]),
|
287
|
-
)
|
288
|
-
def fit(self, dataset: Union[DataFrame, pd.DataFrame]) -> "LocalOutlierFactor":
|
276
|
+
def _fit(self, dataset: Union[DataFrame, pd.DataFrame]) -> "LocalOutlierFactor":
|
289
277
|
"""Fit the local outlier factor detector from the training dataset
|
290
278
|
For more details on this function, see [sklearn.neighbors.LocalOutlierFactor.fit]
|
291
279
|
(https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor.fit)
|
@@ -312,12 +300,14 @@ class LocalOutlierFactor(BaseTransformer):
|
|
312
300
|
|
313
301
|
self._snowpark_cols = dataset.select(self.input_cols).columns
|
314
302
|
|
315
|
-
|
303
|
+
# If we are already in a stored procedure, no need to kick off another one.
|
316
304
|
if SNOWML_SPROC_ENV in os.environ:
|
317
305
|
statement_params = telemetry.get_function_usage_statement_params(
|
318
306
|
project=_PROJECT,
|
319
307
|
subproject=_SUBPROJECT,
|
320
|
-
function_name=telemetry.get_statement_params_full_func_name(
|
308
|
+
function_name=telemetry.get_statement_params_full_func_name(
|
309
|
+
inspect.currentframe(), LocalOutlierFactor.__class__.__name__
|
310
|
+
),
|
321
311
|
api_calls=[Session.call],
|
322
312
|
custom_tags=dict([("autogen", True)]) if self._autogenerated else None,
|
323
313
|
)
|
@@ -338,27 +328,24 @@ class LocalOutlierFactor(BaseTransformer):
|
|
338
328
|
)
|
339
329
|
self._sklearn_object = model_trainer.train()
|
340
330
|
self._is_fitted = True
|
341
|
-
self.
|
331
|
+
self._generate_model_signatures(dataset)
|
342
332
|
return self
|
343
333
|
|
344
334
|
def _batch_inference_validate_snowpark(
|
345
335
|
self,
|
346
336
|
dataset: DataFrame,
|
347
337
|
inference_method: str,
|
348
|
-
) ->
|
349
|
-
"""Util method to run validate that batch inference can be run on a snowpark dataframe
|
350
|
-
return the available package that exists in the snowflake anaconda channel
|
338
|
+
) -> None:
|
339
|
+
"""Util method to run validate that batch inference can be run on a snowpark dataframe.
|
351
340
|
|
352
341
|
Args:
|
353
342
|
dataset: snowpark dataframe
|
354
343
|
inference_method: the inference method such as predict, score...
|
355
|
-
|
344
|
+
|
356
345
|
Raises:
|
357
346
|
SnowflakeMLException: If the estimator is not fitted, raise error
|
358
347
|
SnowflakeMLException: If the session is None, raise error
|
359
348
|
|
360
|
-
Returns:
|
361
|
-
A list of available package that exists in the snowflake anaconda channel
|
362
349
|
"""
|
363
350
|
if not self._is_fitted:
|
364
351
|
raise exceptions.SnowflakeMLException(
|
@@ -376,9 +363,7 @@ class LocalOutlierFactor(BaseTransformer):
|
|
376
363
|
"Session must not specified for snowpark dataset."
|
377
364
|
),
|
378
365
|
)
|
379
|
-
|
380
|
-
return pkg_version_utils.get_valid_pkg_versions_supported_in_snowflake_conda_channel(
|
381
|
-
pkg_versions=self._get_dependencies(), session=session, subproject=_SUBPROJECT)
|
366
|
+
|
382
367
|
|
383
368
|
@available_if(original_estimator_has_callable("predict")) # type: ignore[misc]
|
384
369
|
@telemetry.send_api_usage_telemetry(
|
@@ -414,7 +399,9 @@ class LocalOutlierFactor(BaseTransformer):
|
|
414
399
|
# when it is classifier, infer the datatype from label columns
|
415
400
|
if expected_type_inferred == "" and 'predict' in self.model_signatures:
|
416
401
|
# Batch inference takes a single expected output column type. Use the first columns type for now.
|
417
|
-
label_cols_signatures = [
|
402
|
+
label_cols_signatures = [
|
403
|
+
row for row in self.model_signatures['predict'].outputs if row.name in self.output_cols
|
404
|
+
]
|
418
405
|
if len(label_cols_signatures) == 0:
|
419
406
|
error_str = f"Output columns {self.output_cols} do not match model signatures {self.model_signatures['predict'].outputs}."
|
420
407
|
raise exceptions.SnowflakeMLException(
|
@@ -422,25 +409,23 @@ class LocalOutlierFactor(BaseTransformer):
|
|
422
409
|
original_exception=ValueError(error_str),
|
423
410
|
)
|
424
411
|
|
425
|
-
expected_type_inferred = convert_sp_to_sf_type(
|
426
|
-
label_cols_signatures[0].as_snowpark_type()
|
427
|
-
)
|
412
|
+
expected_type_inferred = convert_sp_to_sf_type(label_cols_signatures[0].as_snowpark_type())
|
428
413
|
|
429
|
-
self.
|
430
|
-
|
414
|
+
self._batch_inference_validate_snowpark(dataset=dataset, inference_method=inference_method)
|
415
|
+
self._deps = self._get_dependencies()
|
416
|
+
assert isinstance(
|
417
|
+
dataset._session, Session
|
418
|
+
) # mypy does not recognize the check in _batch_inference_validate_snowpark()
|
431
419
|
|
432
420
|
transform_kwargs = dict(
|
433
|
-
session
|
434
|
-
dependencies
|
435
|
-
drop_input_cols
|
436
|
-
expected_output_cols_type
|
421
|
+
session=dataset._session,
|
422
|
+
dependencies=self._deps,
|
423
|
+
drop_input_cols=self._drop_input_cols,
|
424
|
+
expected_output_cols_type=expected_type_inferred,
|
437
425
|
)
|
438
426
|
|
439
427
|
elif isinstance(dataset, pd.DataFrame):
|
440
|
-
transform_kwargs = dict(
|
441
|
-
snowpark_input_cols = self._snowpark_cols,
|
442
|
-
drop_input_cols = self._drop_input_cols
|
443
|
-
)
|
428
|
+
transform_kwargs = dict(snowpark_input_cols=self._snowpark_cols, drop_input_cols=self._drop_input_cols)
|
444
429
|
|
445
430
|
transform_handlers = ModelTransformerBuilder.build(
|
446
431
|
dataset=dataset,
|
@@ -480,7 +465,7 @@ class LocalOutlierFactor(BaseTransformer):
|
|
480
465
|
Transformed dataset.
|
481
466
|
"""
|
482
467
|
super()._check_dataset_type(dataset)
|
483
|
-
inference_method="transform"
|
468
|
+
inference_method = "transform"
|
484
469
|
|
485
470
|
# This dictionary contains optional kwargs for batch inference. These kwargs
|
486
471
|
# are specific to the type of dataset used.
|
@@ -510,24 +495,19 @@ class LocalOutlierFactor(BaseTransformer):
|
|
510
495
|
if all(x == output_types[0] for x in output_types) and len(output_types) == len(self.output_cols):
|
511
496
|
expected_dtype = convert_sp_to_sf_type(output_types[0])
|
512
497
|
|
513
|
-
self.
|
514
|
-
|
515
|
-
inference_method=inference_method,
|
516
|
-
)
|
498
|
+
self._batch_inference_validate_snowpark(dataset=dataset, inference_method=inference_method)
|
499
|
+
self._deps = self._get_dependencies()
|
517
500
|
assert isinstance(dataset._session, Session) # mypy does not recognize the check in _batch_inference_validate_snowpark()
|
518
501
|
|
519
502
|
transform_kwargs = dict(
|
520
|
-
session
|
521
|
-
dependencies
|
522
|
-
drop_input_cols
|
523
|
-
expected_output_cols_type
|
503
|
+
session=dataset._session,
|
504
|
+
dependencies=self._deps,
|
505
|
+
drop_input_cols=self._drop_input_cols,
|
506
|
+
expected_output_cols_type=expected_dtype,
|
524
507
|
)
|
525
508
|
|
526
509
|
elif isinstance(dataset, pd.DataFrame):
|
527
|
-
transform_kwargs = dict(
|
528
|
-
snowpark_input_cols = self._snowpark_cols,
|
529
|
-
drop_input_cols = self._drop_input_cols
|
530
|
-
)
|
510
|
+
transform_kwargs = dict(snowpark_input_cols=self._snowpark_cols, drop_input_cols=self._drop_input_cols)
|
531
511
|
|
532
512
|
transform_handlers = ModelTransformerBuilder.build(
|
533
513
|
dataset=dataset,
|
@@ -546,7 +526,11 @@ class LocalOutlierFactor(BaseTransformer):
|
|
546
526
|
return output_df
|
547
527
|
|
548
528
|
@available_if(original_estimator_has_callable("fit_predict")) # type: ignore[misc]
|
549
|
-
def fit_predict(
|
529
|
+
def fit_predict(
|
530
|
+
self,
|
531
|
+
dataset: Union[DataFrame, pd.DataFrame],
|
532
|
+
output_cols_prefix: str = "fit_predict_",
|
533
|
+
) -> Union[DataFrame, pd.DataFrame]:
|
550
534
|
""" Fit the model to the training set X and return the labels
|
551
535
|
For more details on this function, see [sklearn.neighbors.LocalOutlierFactor.fit_predict]
|
552
536
|
(https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.LocalOutlierFactor.html#sklearn.neighbors.LocalOutlierFactor.fit_predict)
|
@@ -573,22 +557,104 @@ class LocalOutlierFactor(BaseTransformer):
|
|
573
557
|
)
|
574
558
|
output_result, fitted_estimator = model_trainer.train_fit_predict(
|
575
559
|
drop_input_cols=self._drop_input_cols,
|
576
|
-
expected_output_cols_list=
|
560
|
+
expected_output_cols_list=(
|
561
|
+
self.output_cols if self.output_cols else self._get_output_column_names(output_cols_prefix)
|
562
|
+
),
|
577
563
|
)
|
578
564
|
self._sklearn_object = fitted_estimator
|
579
565
|
self._is_fitted = True
|
580
566
|
return output_result
|
581
567
|
|
568
|
+
|
569
|
+
@available_if(original_estimator_has_callable("fit_transform")) # type: ignore[misc]
|
570
|
+
def fit_transform(self, dataset: Union[DataFrame, pd.DataFrame], output_cols_prefix: str = "fit_transform_",) -> Union[DataFrame, pd.DataFrame]:
|
571
|
+
""" Method not supported for this class.
|
572
|
+
|
573
|
+
|
574
|
+
Raises:
|
575
|
+
TypeError: Supported dataset types: snowpark.DataFrame, pandas.DataFrame.
|
582
576
|
|
583
|
-
|
584
|
-
|
585
|
-
|
577
|
+
Args:
|
578
|
+
dataset: Union[snowflake.snowpark.DataFrame, pandas.DataFrame]
|
579
|
+
Snowpark or Pandas DataFrame.
|
580
|
+
output_cols_prefix: Prefix for the response columns
|
586
581
|
Returns:
|
587
582
|
Transformed dataset.
|
588
583
|
"""
|
589
|
-
self.
|
590
|
-
|
591
|
-
|
584
|
+
self._infer_input_output_cols(dataset)
|
585
|
+
super()._check_dataset_type(dataset)
|
586
|
+
model_trainer = ModelTrainerBuilder.build_fit_transform(
|
587
|
+
estimator=self._sklearn_object,
|
588
|
+
dataset=dataset,
|
589
|
+
input_cols=self.input_cols,
|
590
|
+
label_cols=self.label_cols,
|
591
|
+
sample_weight_col=self.sample_weight_col,
|
592
|
+
autogenerated=self._autogenerated,
|
593
|
+
subproject=_SUBPROJECT,
|
594
|
+
)
|
595
|
+
output_result, fitted_estimator = model_trainer.train_fit_transform(
|
596
|
+
drop_input_cols=self._drop_input_cols,
|
597
|
+
expected_output_cols_list=self.output_cols,
|
598
|
+
)
|
599
|
+
self._sklearn_object = fitted_estimator
|
600
|
+
self._is_fitted = True
|
601
|
+
return output_result
|
602
|
+
|
603
|
+
|
604
|
+
def _get_output_column_names(self, output_cols_prefix: str, output_cols: Optional[List[str]] = None) -> List[str]:
|
605
|
+
""" Returns the list of output columns for predict_proba(), decision_function(), etc.. functions.
|
606
|
+
Returns a list with output_cols_prefix as the only element if the estimator is not a classifier.
|
607
|
+
"""
|
608
|
+
output_cols_prefix = identifier.resolve_identifier(output_cols_prefix)
|
609
|
+
# The following condition is introduced for kneighbors methods, and not used in other methods
|
610
|
+
if output_cols:
|
611
|
+
output_cols = [
|
612
|
+
identifier.concat_names([output_cols_prefix, identifier.resolve_identifier(c)])
|
613
|
+
for c in output_cols
|
614
|
+
]
|
615
|
+
elif getattr(self._sklearn_object, "classes_", None) is None:
|
616
|
+
output_cols = [output_cols_prefix]
|
617
|
+
elif self._sklearn_object is not None:
|
618
|
+
classes = self._sklearn_object.classes_
|
619
|
+
if isinstance(classes, numpy.ndarray):
|
620
|
+
output_cols = [f'{output_cols_prefix}{str(c)}' for c in classes.tolist()]
|
621
|
+
elif isinstance(classes, list) and len(classes) > 0 and isinstance(classes[0], numpy.ndarray):
|
622
|
+
# If the estimator is a multioutput estimator, classes_ will be a list of ndarrays.
|
623
|
+
output_cols = []
|
624
|
+
for i, cl in enumerate(classes):
|
625
|
+
# For binary classification, there is only one output column for each class
|
626
|
+
# ndarray as the two classes are complementary.
|
627
|
+
if len(cl) == 2:
|
628
|
+
output_cols.append(f'{output_cols_prefix}{i}_{cl[0]}')
|
629
|
+
else:
|
630
|
+
output_cols.extend([
|
631
|
+
f'{output_cols_prefix}{i}_{c}' for c in cl.tolist()
|
632
|
+
])
|
633
|
+
else:
|
634
|
+
output_cols = []
|
635
|
+
|
636
|
+
# Make sure column names are valid snowflake identifiers.
|
637
|
+
assert output_cols is not None # Make MyPy happy
|
638
|
+
rv = [identifier.rename_to_valid_snowflake_identifier(c) for c in output_cols]
|
639
|
+
|
640
|
+
return rv
|
641
|
+
|
642
|
+
def _align_expected_output_names(
|
643
|
+
self, method: str, dataset: DataFrame, expected_output_cols_list: List[str], output_cols_prefix: str
|
644
|
+
) -> List[str]:
|
645
|
+
# in case the inferred output column names dimension is different
|
646
|
+
# we use one line of snowpark dataframe and put it into sklearn estimator using pandas
|
647
|
+
output_df_pd = getattr(self, method)(dataset.limit(1).to_pandas(), output_cols_prefix)
|
648
|
+
output_df_columns = list(output_df_pd.columns)
|
649
|
+
output_df_columns_set: Set[str] = set(output_df_columns) - set(dataset.columns)
|
650
|
+
if self.sample_weight_col:
|
651
|
+
output_df_columns_set -= set(self.sample_weight_col)
|
652
|
+
# if the dimension of inferred output column names is correct; use it
|
653
|
+
if len(expected_output_cols_list) == len(output_df_columns_set):
|
654
|
+
return expected_output_cols_list
|
655
|
+
# otherwise, use the sklearn estimator's output
|
656
|
+
else:
|
657
|
+
return sorted(list(output_df_columns_set), key=lambda x: output_df_columns.index(x))
|
592
658
|
|
593
659
|
@available_if(original_estimator_has_callable("predict_proba")) # type: ignore[misc]
|
594
660
|
@telemetry.send_api_usage_telemetry(
|
@@ -620,24 +686,26 @@ class LocalOutlierFactor(BaseTransformer):
|
|
620
686
|
# are specific to the type of dataset used.
|
621
687
|
transform_kwargs: BatchInferenceKwargsTypedDict = dict()
|
622
688
|
|
689
|
+
expected_output_cols = self._get_output_column_names(output_cols_prefix)
|
690
|
+
|
623
691
|
if isinstance(dataset, DataFrame):
|
624
|
-
self.
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
692
|
+
self._batch_inference_validate_snowpark(dataset=dataset, inference_method=inference_method)
|
693
|
+
self._deps = self._get_dependencies()
|
694
|
+
assert isinstance(
|
695
|
+
dataset._session, Session
|
696
|
+
) # mypy does not recognize the check in _batch_inference_validate_snowpark()
|
629
697
|
transform_kwargs = dict(
|
630
698
|
session=dataset._session,
|
631
699
|
dependencies=self._deps,
|
632
|
-
drop_input_cols
|
700
|
+
drop_input_cols=self._drop_input_cols,
|
633
701
|
expected_output_cols_type="float",
|
634
702
|
)
|
703
|
+
expected_output_cols = self._align_expected_output_names(
|
704
|
+
inference_method, dataset, expected_output_cols, output_cols_prefix
|
705
|
+
)
|
635
706
|
|
636
707
|
elif isinstance(dataset, pd.DataFrame):
|
637
|
-
transform_kwargs = dict(
|
638
|
-
snowpark_input_cols = self._snowpark_cols,
|
639
|
-
drop_input_cols = self._drop_input_cols
|
640
|
-
)
|
708
|
+
transform_kwargs = dict(snowpark_input_cols=self._snowpark_cols, drop_input_cols=self._drop_input_cols)
|
641
709
|
|
642
710
|
transform_handlers = ModelTransformerBuilder.build(
|
643
711
|
dataset=dataset,
|
@@ -649,7 +717,7 @@ class LocalOutlierFactor(BaseTransformer):
|
|
649
717
|
output_df: DATAFRAME_TYPE = transform_handlers.batch_inference(
|
650
718
|
inference_method=inference_method,
|
651
719
|
input_cols=self.input_cols,
|
652
|
-
expected_output_cols=
|
720
|
+
expected_output_cols=expected_output_cols,
|
653
721
|
**transform_kwargs
|
654
722
|
)
|
655
723
|
return output_df
|
@@ -679,29 +747,30 @@ class LocalOutlierFactor(BaseTransformer):
|
|
679
747
|
Output dataset with log probability of the sample for each class in the model.
|
680
748
|
"""
|
681
749
|
super()._check_dataset_type(dataset)
|
682
|
-
inference_method="predict_log_proba"
|
750
|
+
inference_method = "predict_log_proba"
|
751
|
+
expected_output_cols = self._get_output_column_names(output_cols_prefix)
|
683
752
|
|
684
753
|
# This dictionary contains optional kwargs for batch inference. These kwargs
|
685
754
|
# are specific to the type of dataset used.
|
686
755
|
transform_kwargs: BatchInferenceKwargsTypedDict = dict()
|
687
756
|
|
688
757
|
if isinstance(dataset, DataFrame):
|
689
|
-
self.
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
758
|
+
self._batch_inference_validate_snowpark(dataset=dataset, inference_method=inference_method)
|
759
|
+
self._deps = self._get_dependencies()
|
760
|
+
assert isinstance(
|
761
|
+
dataset._session, Session
|
762
|
+
) # mypy does not recognize the check in _batch_inference_validate_snowpark()
|
694
763
|
transform_kwargs = dict(
|
695
764
|
session=dataset._session,
|
696
765
|
dependencies=self._deps,
|
697
|
-
drop_input_cols
|
766
|
+
drop_input_cols=self._drop_input_cols,
|
698
767
|
expected_output_cols_type="float",
|
699
768
|
)
|
769
|
+
expected_output_cols = self._align_expected_output_names(
|
770
|
+
inference_method, dataset, expected_output_cols, output_cols_prefix
|
771
|
+
)
|
700
772
|
elif isinstance(dataset, pd.DataFrame):
|
701
|
-
transform_kwargs = dict(
|
702
|
-
snowpark_input_cols = self._snowpark_cols,
|
703
|
-
drop_input_cols = self._drop_input_cols
|
704
|
-
)
|
773
|
+
transform_kwargs = dict(snowpark_input_cols=self._snowpark_cols, drop_input_cols=self._drop_input_cols)
|
705
774
|
|
706
775
|
transform_handlers = ModelTransformerBuilder.build(
|
707
776
|
dataset=dataset,
|
@@ -714,7 +783,7 @@ class LocalOutlierFactor(BaseTransformer):
|
|
714
783
|
output_df: DATAFRAME_TYPE = transform_handlers.batch_inference(
|
715
784
|
inference_method=inference_method,
|
716
785
|
input_cols=self.input_cols,
|
717
|
-
expected_output_cols=
|
786
|
+
expected_output_cols=expected_output_cols,
|
718
787
|
**transform_kwargs
|
719
788
|
)
|
720
789
|
return output_df
|
@@ -742,30 +811,32 @@ class LocalOutlierFactor(BaseTransformer):
|
|
742
811
|
Output dataset with results of the decision function for the samples in input dataset.
|
743
812
|
"""
|
744
813
|
super()._check_dataset_type(dataset)
|
745
|
-
inference_method="decision_function"
|
814
|
+
inference_method = "decision_function"
|
746
815
|
|
747
816
|
# This dictionary contains optional kwargs for batch inference. These kwargs
|
748
817
|
# are specific to the type of dataset used.
|
749
818
|
transform_kwargs: BatchInferenceKwargsTypedDict = dict()
|
750
819
|
|
820
|
+
expected_output_cols = self._get_output_column_names(output_cols_prefix)
|
821
|
+
|
751
822
|
if isinstance(dataset, DataFrame):
|
752
|
-
self.
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
823
|
+
self._batch_inference_validate_snowpark(dataset=dataset, inference_method=inference_method)
|
824
|
+
self._deps = self._get_dependencies()
|
825
|
+
assert isinstance(
|
826
|
+
dataset._session, Session
|
827
|
+
) # mypy does not recognize the check in _batch_inference_validate_snowpark()
|
757
828
|
transform_kwargs = dict(
|
758
829
|
session=dataset._session,
|
759
830
|
dependencies=self._deps,
|
760
|
-
drop_input_cols
|
831
|
+
drop_input_cols=self._drop_input_cols,
|
761
832
|
expected_output_cols_type="float",
|
762
833
|
)
|
834
|
+
expected_output_cols = self._align_expected_output_names(
|
835
|
+
inference_method, dataset, expected_output_cols, output_cols_prefix
|
836
|
+
)
|
763
837
|
|
764
838
|
elif isinstance(dataset, pd.DataFrame):
|
765
|
-
transform_kwargs = dict(
|
766
|
-
snowpark_input_cols = self._snowpark_cols,
|
767
|
-
drop_input_cols = self._drop_input_cols
|
768
|
-
)
|
839
|
+
transform_kwargs = dict(snowpark_input_cols=self._snowpark_cols, drop_input_cols=self._drop_input_cols)
|
769
840
|
|
770
841
|
transform_handlers = ModelTransformerBuilder.build(
|
771
842
|
dataset=dataset,
|
@@ -778,7 +849,7 @@ class LocalOutlierFactor(BaseTransformer):
|
|
778
849
|
output_df: DATAFRAME_TYPE = transform_handlers.batch_inference(
|
779
850
|
inference_method=inference_method,
|
780
851
|
input_cols=self.input_cols,
|
781
|
-
expected_output_cols=
|
852
|
+
expected_output_cols=expected_output_cols,
|
782
853
|
**transform_kwargs
|
783
854
|
)
|
784
855
|
return output_df
|
@@ -809,17 +880,17 @@ class LocalOutlierFactor(BaseTransformer):
|
|
809
880
|
Output dataset with probability of the sample for each class in the model.
|
810
881
|
"""
|
811
882
|
super()._check_dataset_type(dataset)
|
812
|
-
inference_method="score_samples"
|
883
|
+
inference_method = "score_samples"
|
813
884
|
|
814
885
|
# This dictionary contains optional kwargs for batch inference. These kwargs
|
815
886
|
# are specific to the type of dataset used.
|
816
887
|
transform_kwargs: BatchInferenceKwargsTypedDict = dict()
|
817
888
|
|
889
|
+
expected_output_cols = self._get_output_column_names(output_cols_prefix)
|
890
|
+
|
818
891
|
if isinstance(dataset, DataFrame):
|
819
|
-
self.
|
820
|
-
|
821
|
-
inference_method=inference_method,
|
822
|
-
)
|
892
|
+
self._batch_inference_validate_snowpark(dataset=dataset, inference_method=inference_method)
|
893
|
+
self._deps = self._get_dependencies()
|
823
894
|
assert isinstance(dataset._session, Session) # mypy does not recognize the check in _batch_inference_validate_snowpark()
|
824
895
|
transform_kwargs = dict(
|
825
896
|
session=dataset._session,
|
@@ -827,6 +898,9 @@ class LocalOutlierFactor(BaseTransformer):
|
|
827
898
|
drop_input_cols = self._drop_input_cols,
|
828
899
|
expected_output_cols_type="float",
|
829
900
|
)
|
901
|
+
expected_output_cols = self._align_expected_output_names(
|
902
|
+
inference_method, dataset, expected_output_cols, output_cols_prefix
|
903
|
+
)
|
830
904
|
|
831
905
|
elif isinstance(dataset, pd.DataFrame):
|
832
906
|
transform_kwargs = dict(
|
@@ -845,7 +919,7 @@ class LocalOutlierFactor(BaseTransformer):
|
|
845
919
|
output_df: DATAFRAME_TYPE = transform_handlers.batch_inference(
|
846
920
|
inference_method=inference_method,
|
847
921
|
input_cols=self.input_cols,
|
848
|
-
expected_output_cols=
|
922
|
+
expected_output_cols=expected_output_cols,
|
849
923
|
**transform_kwargs
|
850
924
|
)
|
851
925
|
return output_df
|
@@ -878,17 +952,15 @@ class LocalOutlierFactor(BaseTransformer):
|
|
878
952
|
transform_kwargs: ScoreKwargsTypedDict = dict()
|
879
953
|
|
880
954
|
if isinstance(dataset, DataFrame):
|
881
|
-
self.
|
882
|
-
|
883
|
-
inference_method="score",
|
884
|
-
)
|
955
|
+
self._batch_inference_validate_snowpark(dataset=dataset, inference_method="score")
|
956
|
+
self._deps = self._get_dependencies()
|
885
957
|
selected_cols = self._get_active_columns()
|
886
958
|
if len(selected_cols) > 0:
|
887
959
|
dataset = dataset.select(selected_cols)
|
888
960
|
assert isinstance(dataset._session, Session) # keep mypy happy
|
889
961
|
transform_kwargs = dict(
|
890
962
|
session=dataset._session,
|
891
|
-
dependencies=
|
963
|
+
dependencies=self._deps,
|
892
964
|
score_sproc_imports=['sklearn'],
|
893
965
|
)
|
894
966
|
elif isinstance(dataset, pd.DataFrame):
|
@@ -955,11 +1027,8 @@ class LocalOutlierFactor(BaseTransformer):
|
|
955
1027
|
|
956
1028
|
if isinstance(dataset, DataFrame):
|
957
1029
|
|
958
|
-
self.
|
959
|
-
|
960
|
-
inference_method=inference_method,
|
961
|
-
|
962
|
-
)
|
1030
|
+
self._batch_inference_validate_snowpark(dataset=dataset, inference_method=inference_method)
|
1031
|
+
self._deps = self._get_dependencies()
|
963
1032
|
assert isinstance(dataset._session, Session) # mypy does not recognize the check in _batch_inference_validate_snowpark()
|
964
1033
|
transform_kwargs = dict(
|
965
1034
|
session = dataset._session,
|
@@ -992,50 +1061,84 @@ class LocalOutlierFactor(BaseTransformer):
|
|
992
1061
|
)
|
993
1062
|
return output_df
|
994
1063
|
|
1064
|
+
|
1065
|
+
|
1066
|
+
def to_sklearn(self) -> Any:
|
1067
|
+
"""Get sklearn.neighbors.LocalOutlierFactor object.
|
1068
|
+
"""
|
1069
|
+
if self._sklearn_object is None:
|
1070
|
+
self._sklearn_object = self._create_sklearn_object()
|
1071
|
+
return self._sklearn_object
|
1072
|
+
|
1073
|
+
def to_xgboost(self) -> Any:
|
1074
|
+
raise exceptions.SnowflakeMLException(
|
1075
|
+
error_code=error_codes.METHOD_NOT_ALLOWED,
|
1076
|
+
original_exception=AttributeError(
|
1077
|
+
modeling_error_messages.UNSUPPORTED_MODEL_CONVERSION.format(
|
1078
|
+
"to_xgboost()",
|
1079
|
+
"to_sklearn()"
|
1080
|
+
)
|
1081
|
+
),
|
1082
|
+
)
|
995
1083
|
|
996
|
-
def
|
1084
|
+
def to_lightgbm(self) -> Any:
|
1085
|
+
raise exceptions.SnowflakeMLException(
|
1086
|
+
error_code=error_codes.METHOD_NOT_ALLOWED,
|
1087
|
+
original_exception=AttributeError(
|
1088
|
+
modeling_error_messages.UNSUPPORTED_MODEL_CONVERSION.format(
|
1089
|
+
"to_lightgbm()",
|
1090
|
+
"to_sklearn()"
|
1091
|
+
)
|
1092
|
+
),
|
1093
|
+
)
|
1094
|
+
|
1095
|
+
def _get_dependencies(self) -> List[str]:
|
1096
|
+
return self._deps
|
1097
|
+
|
1098
|
+
|
1099
|
+
def _generate_model_signatures(self, dataset: Union[DataFrame, pd.DataFrame]) -> None:
|
997
1100
|
self._model_signature_dict = dict()
|
998
1101
|
|
999
1102
|
PROB_FUNCTIONS = ["predict_log_proba", "predict_proba", "decision_function"]
|
1000
1103
|
|
1001
|
-
inputs = list(_infer_signature(dataset[self.input_cols], "input"))
|
1104
|
+
inputs = list(_infer_signature(dataset[self.input_cols], "input", use_snowflake_identifiers=True))
|
1002
1105
|
outputs: List[BaseFeatureSpec] = []
|
1003
1106
|
if hasattr(self, "predict"):
|
1004
1107
|
# keep mypy happy
|
1005
|
-
assert self._sklearn_object is not None and hasattr(self._sklearn_object, "_estimator_type")
|
1108
|
+
assert self._sklearn_object is not None and hasattr(self._sklearn_object, "_estimator_type")
|
1006
1109
|
# For classifier, the type of predict is the same as the type of label
|
1007
|
-
if self._sklearn_object._estimator_type ==
|
1008
|
-
|
1110
|
+
if self._sklearn_object._estimator_type == "classifier":
|
1111
|
+
# label columns is the desired type for output
|
1009
1112
|
outputs = list(_infer_signature(dataset[self.label_cols], "output", use_snowflake_identifiers=True))
|
1010
1113
|
# rename the output columns
|
1011
1114
|
outputs = list(model_signature_utils.rename_features(outputs, self.output_cols))
|
1012
|
-
self._model_signature_dict["predict"] = ModelSignature(
|
1013
|
-
|
1014
|
-
|
1115
|
+
self._model_signature_dict["predict"] = ModelSignature(
|
1116
|
+
inputs, ([] if self._drop_input_cols else inputs) + outputs
|
1117
|
+
)
|
1015
1118
|
# For mixture models that use the density mixin, `predict` returns the argmax of the log prob.
|
1016
1119
|
# For outlier models, returns -1 for outliers and 1 for inliers.
|
1017
|
-
# Clusterer returns int64 cluster labels.
|
1120
|
+
# Clusterer returns int64 cluster labels.
|
1018
1121
|
elif self._sklearn_object._estimator_type in ["DensityEstimator", "clusterer", "outlier_detector"]:
|
1019
1122
|
outputs = [FeatureSpec(dtype=DataType.INT64, name=c) for c in self.output_cols]
|
1020
|
-
self._model_signature_dict["predict"] = ModelSignature(
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1123
|
+
self._model_signature_dict["predict"] = ModelSignature(
|
1124
|
+
inputs, ([] if self._drop_input_cols else inputs) + outputs
|
1125
|
+
)
|
1126
|
+
|
1024
1127
|
# For regressor, the type of predict is float64
|
1025
|
-
elif self._sklearn_object._estimator_type ==
|
1128
|
+
elif self._sklearn_object._estimator_type == "regressor":
|
1026
1129
|
outputs = [FeatureSpec(dtype=DataType.DOUBLE, name=c) for c in self.output_cols]
|
1027
|
-
self._model_signature_dict["predict"] = ModelSignature(
|
1028
|
-
|
1029
|
-
|
1030
|
-
|
1130
|
+
self._model_signature_dict["predict"] = ModelSignature(
|
1131
|
+
inputs, ([] if self._drop_input_cols else inputs) + outputs
|
1132
|
+
)
|
1133
|
+
|
1031
1134
|
for prob_func in PROB_FUNCTIONS:
|
1032
1135
|
if hasattr(self, prob_func):
|
1033
1136
|
output_cols_prefix: str = f"{prob_func}_"
|
1034
1137
|
output_column_names = self._get_output_column_names(output_cols_prefix)
|
1035
1138
|
outputs = [FeatureSpec(dtype=DataType.DOUBLE, name=c) for c in output_column_names]
|
1036
|
-
self._model_signature_dict[prob_func] = ModelSignature(
|
1037
|
-
|
1038
|
-
|
1139
|
+
self._model_signature_dict[prob_func] = ModelSignature(
|
1140
|
+
inputs, ([] if self._drop_input_cols else inputs) + outputs
|
1141
|
+
)
|
1039
1142
|
|
1040
1143
|
# Output signature names may still need to be renamed, since they were not created with `_infer_signature`.
|
1041
1144
|
items = list(self._model_signature_dict.items())
|
@@ -1048,10 +1151,10 @@ class LocalOutlierFactor(BaseTransformer):
|
|
1048
1151
|
"""Returns model signature of current class.
|
1049
1152
|
|
1050
1153
|
Raises:
|
1051
|
-
|
1154
|
+
SnowflakeMLException: If estimator is not fitted, then model signature cannot be inferred
|
1052
1155
|
|
1053
1156
|
Returns:
|
1054
|
-
Dict
|
1157
|
+
Dict with each method and its input output signature
|
1055
1158
|
"""
|
1056
1159
|
if self._model_signature_dict is None:
|
1057
1160
|
raise exceptions.SnowflakeMLException(
|
@@ -1059,35 +1162,3 @@ class LocalOutlierFactor(BaseTransformer):
|
|
1059
1162
|
original_exception=RuntimeError("Estimator not fitted before accessing property model_signatures!"),
|
1060
1163
|
)
|
1061
1164
|
return self._model_signature_dict
|
1062
|
-
|
1063
|
-
def to_sklearn(self) -> Any:
|
1064
|
-
"""Get sklearn.neighbors.LocalOutlierFactor object.
|
1065
|
-
"""
|
1066
|
-
if self._sklearn_object is None:
|
1067
|
-
self._sklearn_object = self._create_sklearn_object()
|
1068
|
-
return self._sklearn_object
|
1069
|
-
|
1070
|
-
def to_xgboost(self) -> Any:
|
1071
|
-
raise exceptions.SnowflakeMLException(
|
1072
|
-
error_code=error_codes.METHOD_NOT_ALLOWED,
|
1073
|
-
original_exception=AttributeError(
|
1074
|
-
modeling_error_messages.UNSUPPORTED_MODEL_CONVERSION.format(
|
1075
|
-
"to_xgboost()",
|
1076
|
-
"to_sklearn()"
|
1077
|
-
)
|
1078
|
-
),
|
1079
|
-
)
|
1080
|
-
|
1081
|
-
def to_lightgbm(self) -> Any:
|
1082
|
-
raise exceptions.SnowflakeMLException(
|
1083
|
-
error_code=error_codes.METHOD_NOT_ALLOWED,
|
1084
|
-
original_exception=AttributeError(
|
1085
|
-
modeling_error_messages.UNSUPPORTED_MODEL_CONVERSION.format(
|
1086
|
-
"to_lightgbm()",
|
1087
|
-
"to_sklearn()"
|
1088
|
-
)
|
1089
|
-
),
|
1090
|
-
)
|
1091
|
-
|
1092
|
-
def _get_dependencies(self) -> List[str]:
|
1093
|
-
return self._deps
|