
    3i                     |    d Z ddlmZmZ ddlZddlZddlm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ  G d	 d
ee	      Zy)zwClassifier utilities for MySQL Connector/Python.

Provides a scikit-learn compatible classifier backed by HeatWave ML.
    )OptionalUnionN)ClassifierMixin)MyBaseMLModel)ML_TASK)	copy_dict)MySQLConnectionAbstractc                   P   e Zd ZdZ	 	 	 	 ddedee   dee   dee   dee   f
dZd	e	e
j                  ej                  f   d
ej                  fdZd	e	e
j                  ej                  f   d
ej                  fdZd	e	e
j                  ej                  f   d
e
j                  fdZy)MyClassifiera  
    MySQL HeatWave scikit-learn compatible classifier estimator.

    Provides prediction and probability output from a model deployed in MySQL,
    and manages fit, explain, and prediction options as per HeatWave ML interface.

    Attributes:
        predict_extra_options (dict): Dictionary of optional parameters passed through
            to the MySQL backend for prediction and probability inference.
        _model (MyModel): Underlying interface for database model operations.
        fit_extra_options (dict): See MyBaseMLModel.

    Args:
        db_connection (MySQLConnectionAbstract): Active MySQL connector DB connection.
        model_name (str, optional): Custom name for the model.
        fit_extra_options (dict, optional): Extra options for fitting.
        explain_extra_options (dict, optional): Extra options for explanations.
        predict_extra_options (dict, optional): Extra options for predict/predict_proba.

    Methods:
        predict(X): Predict class labels.
        predict_proba(X): Predict class probabilities.
    Ndb_connection
model_namefit_extra_optionsexplain_extra_optionspredict_extra_optionsc                     t        j                  | |t        j                  ||       t	        |      | _        t	        |      | _        y)a  
        Initialize a MyClassifier.

        Args:
            db_connection: Active MySQL connector database connection.
            model_name: Optional, custom model name.
            fit_extra_options: Optional fit options.
            explain_extra_options: Optional explain options.
            predict_extra_options: Optional predict/predict_proba options.

        Raises:
            DatabaseError:
                If a database connection issue occurs.
                If an operational error occurs during execution.
        )r   r   N)r   __init__r   CLASSIFICATIONr   r   r   )selfr   r   r   r   r   s         ;D:\jyotish\venv\Lib\site-packages\mysql/ai/ml/classifier.pyr   zMyClassifier.__init__G   sE    . 	""!/	
 &//D%E"%./D%E"    Xreturnc                 v    | j                   j                  || j                        }|d   j                         S )a  
        Predict class labels for the input features using the MySQL model.

        References:
            https://dev.mysql.com/doc/heatwave/en/mys-hwaml-ml-predict-table.html
                A full list of supported options can be found under "ML_PREDICT_TABLE Options"

        Args:
            X: Input samples as a numpy array or pandas DataFrame.

        Returns:
            ndarray: Array of predicted class labels, shape (n_samples,).

        Raises:
            DatabaseError:
                If provided options are invalid or unsupported,
                or if the model is not initialized, i.e., fit or import has not
                been called
                If a database connection issue occurs.
                If an operational error occurs during execution.
        options
Prediction)_modelpredictr   to_numpy)r   r   results      r   r   zMyClassifier.predicth   s7    0 $$Q0J0J$Kl#,,..r   c                     | j                   j                  || j                        }t        |d   j                  d   d   j                               t        j                  |d   j                  fd            S )a*  
        Predict class probabilities for the input features using the MySQL model.

        References:
            https://dev.mysql.com/doc/heatwave/en/mys-hwaml-ml-predict-table.html
                A full list of supported options can be found under "ML_PREDICT_TABLE Options"

        Args:
            X: Input samples as a numpy array or pandas DataFrame.

        Returns:
            ndarray: Array of shape (n_samples, n_classes) with class probabilities.

        Raises:
            DatabaseError:
                If provided options are invalid or unsupported,
                or if the model is not initialized, i.e., fit or import has not
                been called
                If a database connection issue occurs.
                If an operational error occurs during execution.
        r   
ml_resultsr   probabilitiesc                 :    D cg c]
  }| d   |    c}S c c}w )Nr#    )	ml_result
class_nameclassess     r   <lambda>z,MyClassifier.predict_proba.<locals>.<lambda>   s)    MT#MTzIo.z:W# #s   )	r   r   r   sortedilockeysnpstackmap)r   r   r    r(   s      @r   predict_probazMyClassifier.predict_proba   su    0 $$Q0J0J$K-2215oFKKMNxx< $$
 	
r   c                 R    | j                   j                  || j                         y)ai  
        Explain model predictions using provided data.

        References:
            https://dev.mysql.com/doc/heatwave/en/mys-hwaml-ml-explain-table.html
                A full list of supported options can be found under "ML_EXPLAIN_TABLE Options"

        Args:
            X: DataFrame for which predictions should be explained.

        Returns:
            DataFrame containing explanation details (feature attributions, etc.)

        Raises:
            DatabaseError:
                If provided options are invalid or unsupported,
                or if the model is not initialized, i.e., fit or import has not
                been called
                If a database connection issue occurs.
                If an operational error occurs during execution.

        Notes:
            Temporary input/output tables are cleaned up after explanation.
        r   N)r   explain_predictionsr   )r   r   s     r   r2   z MyClassifier.explain_predictions   s!    6 	''43M3M'Nr   )NNNN)__name__
__module____qualname____doc__r	   r   strdictr   r   pd	DataFramer-   ndarrayr   r0   r2   r%   r   r   r   r   .   s    6 %),00404F.F SMF $D>	F
  (~F  (~FB/r||RZZ/0/	/6"
r||RZZ/0"
	"
HOr||RZZ/0O	Or   r   )r6   typingr   r   numpyr-   pandasr9   sklearn.baser   mysql.ai.ml.baser   mysql.ai.ml.modelr   mysql.ai.utilsr   mysql.connector.abstractsr	   r   r%   r   r   <module>rD      s6   : #   ( * % $ =TO=/ TOr   