Skip to content

Pycaret

zenml.integrations.pycaret special

Initialization of the PyCaret integration.

PyCaretIntegration (Integration)

Definition of PyCaret integration for ZenML.

Source code in zenml/integrations/pycaret/__init__.py
class PyCaretIntegration(Integration):
    """Definition of PyCaret integration for ZenML."""

    NAME = PYCARET
    REQUIREMENTS = [
        "pycaret>=3.0.0",
        "scikit-learn",
        "xgboost",
        "catboost",
        "lightgbm",
    ]
    REQUIREMENTS_IGNORED_ON_UNINSTALL = [
        "scikit-learn",
        "xgboost",
        "catboost",
        "lightgbm",
    ]

    @classmethod
    def activate(cls) -> None:
        """Activates the integration."""
        from zenml.integrations.pycaret import materializers  # noqa

activate() classmethod

Activates the integration.

Source code in zenml/integrations/pycaret/__init__.py
@classmethod
def activate(cls) -> None:
    """Activates the integration."""
    from zenml.integrations.pycaret import materializers  # noqa

materializers special

Initialization for the PyCaret materializers.

model_materializer

PyCaret materializer.

PyCaretMaterializer (BaseMaterializer)

Materializer to read/write PyCaret models.

Source code in zenml/integrations/pycaret/materializers/model_materializer.py
class PyCaretMaterializer(BaseMaterializer):
    """Materializer to read/write PyCaret models."""

    ASSOCIATED_TYPES = (
        # Classification
        LogisticRegression,
        KNeighborsClassifier,
        GaussianNB,
        DecisionTreeClassifier,
        SGDClassifier,
        SVC,
        GaussianProcessClassifier,
        MLPClassifier,
        RidgeClassifier,
        RandomForestClassifier,
        QuadraticDiscriminantAnalysis,
        AdaBoostClassifier,
        GradientBoostingClassifier,
        LinearDiscriminantAnalysis,
        ExtraTreesClassifier,
        XGBClassifier,
        CatBoostClassifier,
        LGBMClassifier,
        # Regression
        LinearRegression,
        Lasso,
        Ridge,
        ElasticNet,
        Lars,
        LassoLars,
        OrthogonalMatchingPursuit,
        BayesianRidge,
        ARDRegression,
        PassiveAggressiveRegressor,
        RANSACRegressor,
        TheilSenRegressor,
        HuberRegressor,
        KernelRidge,
        SVR,
        KNeighborsRegressor,
        DecisionTreeRegressor,
        RandomForestRegressor,
        ExtraTreesRegressor,
        AdaBoostRegressor,
        GradientBoostingRegressor,
        MLPRegressor,
        XGBRegressor,
        CatBoostRegressor,
        BaggingRegressor,
        AdaBoostRegressor,
        LGBMRegressor,
    )
    ASSOCIATED_ARTIFACT_TYPE = ArtifactType.MODEL

    def load(self, data_type: Type[Any]) -> Any:
        """Reads and returns a PyCaret model after copying it to temporary path.

        Args:
            data_type: The type of the data to read.

        Returns:
            A PyCaret model.
        """
        # Create a temporary directory to store the model
        temp_dir = tempfile.TemporaryDirectory()

        # Copy from artifact store to temporary directory
        io_utils.copy_dir(self.uri, temp_dir.name)

        # Load the model from the temporary directory
        model = load_model(temp_dir.name)

        # Cleanup and return
        fileio.rmtree(temp_dir.name)

        return model

    def save(self, model: Any) -> None:
        """Writes a PyCaret model to the artifact store.

        Args:
            model: Any of the supported models.
        """
        # Create a temporary directory to store the model
        temp_dir = tempfile.TemporaryDirectory()
        save_model(model, temp_dir.name)
        io_utils.copy_dir(temp_dir.name, self.uri)

        # Remove the temporary directory
        fileio.rmtree(temp_dir.name)
load(self, data_type)

Reads and returns a PyCaret model after copying it to temporary path.

Parameters:

Name Type Description Default
data_type Type[Any]

The type of the data to read.

required

Returns:

Type Description
Any

A PyCaret model.

Source code in zenml/integrations/pycaret/materializers/model_materializer.py
def load(self, data_type: Type[Any]) -> Any:
    """Reads and returns a PyCaret model after copying it to temporary path.

    Args:
        data_type: The type of the data to read.

    Returns:
        A PyCaret model.
    """
    # Create a temporary directory to store the model
    temp_dir = tempfile.TemporaryDirectory()

    # Copy from artifact store to temporary directory
    io_utils.copy_dir(self.uri, temp_dir.name)

    # Load the model from the temporary directory
    model = load_model(temp_dir.name)

    # Cleanup and return
    fileio.rmtree(temp_dir.name)

    return model
save(self, model)

Writes a PyCaret model to the artifact store.

Parameters:

Name Type Description Default
model Any

Any of the supported models.

required
Source code in zenml/integrations/pycaret/materializers/model_materializer.py
def save(self, model: Any) -> None:
    """Writes a PyCaret model to the artifact store.

    Args:
        model: Any of the supported models.
    """
    # Create a temporary directory to store the model
    temp_dir = tempfile.TemporaryDirectory()
    save_model(model, temp_dir.name)
    io_utils.copy_dir(temp_dir.name, self.uri)

    # Remove the temporary directory
    fileio.rmtree(temp_dir.name)