o
    $iV                     @   s   d dl mZmZmZmZ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 d dlmZ erDd d	lmZ ed
dG dd deZdS )    )TYPE_CHECKINGAnyDictListOptionalUnionN)TENSOR_COLUMN_NAME)DataBatchType)%_unwrap_ndarray_object_type_if_needed)	Predictor)XGBoostCheckpoint)	PublicAPI)Preprocessorbeta)	stabilityc                
       s   e Zd ZdZ	ddejded f fddZdd	 Ze	d
e
dd fddZ		ddedeeee ee f  deeeef  defddZ		ddddeeee ee f  deeeef  ddfddZ  ZS )XGBoostPredictorzA predictor for XGBoost models.

    Args:
        model: The XGBoost booster to use for predictions.
        preprocessor: A preprocessor used to transform data batches prior
            to prediction.
    Nmodelpreprocessorr   c                    s   || _ t | d S N)r   super__init__)selfr   r   	__class__ `/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/train/xgboost/xgboost_predictor.pyr      s   zXGBoostPredictor.__init__c                 C   s   | j j d| jd| jdS )Nz(model=z, preprocessor=))r   __name__r   _preprocessor)r   r   r   r   __repr__!   s   zXGBoostPredictor.__repr__
checkpointreturnc                 C   s   |  }| }| ||dS )a  Instantiate the predictor from a Checkpoint.

        This is a helper constructor that instantiates the predictor from a
        framework-specific XGBoost checkpoint.

        Args:
            checkpoint: The checkpoint to load the model and preprocessor from.

        )r   r   )	get_modelget_preprocessor)clsr    r   r   r   r   r   from_checkpoint'   s   z XGBoostPredictor.from_checkpointdatafeature_columnsdmatrix_kwargsc                 K   s   t j| |f||d|S )a  Run inference on data batch.

        The data is converted into an XGBoost DMatrix before being inputted to
        the model.

        Args:
            data: A batch of input data.
            feature_columns: The names or indices of the columns in the
                data to use as features to predict on. If None, then use
                all columns in ``data``.
            dmatrix_kwargs: Dict of keyword arguments passed to ``xgboost.DMatrix``.
            **predict_kwargs: Keyword arguments passed to ``xgboost.Booster.predict``.


        Examples:

        .. testcode::

            import numpy as np
            import xgboost as xgb
            from ray.train.xgboost import XGBoostPredictor
            train_X = np.array([[1, 2], [3, 4]])
            train_y = np.array([0, 1])
            model = xgb.XGBClassifier().fit(train_X, train_y)
            predictor = XGBoostPredictor(model=model.get_booster())
            data = np.array([[1, 2], [3, 4]])
            predictions = predictor.predict(data)
            # Only use first and second column as the feature
            data = np.array([[1, 2, 8], [3, 4, 9]])
            predictions = predictor.predict(data, feature_columns=[0, 1])

        .. testcode::

            import pandas as pd
            import xgboost as xgb
            from ray.train.xgboost import XGBoostPredictor
            train_X = pd.DataFrame([[1, 2], [3, 4]], columns=["A", "B"])
            train_y = pd.Series([0, 1])
            model = xgb.XGBClassifier().fit(train_X, train_y)
            predictor = XGBoostPredictor(model=model.get_booster())
            # Pandas dataframe.
            data = pd.DataFrame([[1, 2], [3, 4]], columns=["A", "B"])
            predictions = predictor.predict(data)
            # Only use first and second column as the feature
            data = pd.DataFrame([[1, 2, 8], [3, 4, 9]], columns=["A", "B", "C"])
            predictions = predictor.predict(data, feature_columns=["A", "B"])


        Returns:
            Prediction result.

        )r'   r(   )r   predict)r   r&   r'   r(   predict_kwargsr   r   r   r)   6   s   ;zXGBoostPredictor.predictzpd.DataFramec                 K   s   |pi }d }t |v r|t   }t|}|r|d d |f }n(|r3||  }tdd |D r2|}n|j }| }tdd |D rG|}|rM||d< tj|fi |}t	| j
j|fi |}t|jdkrpdg|_|S dd tt|jD |_|S )	Nc                 s       | ]}t |tV  qd S r   
isinstancestr.0fcr   r   r   	<genexpr>       z3XGBoostPredictor._predict_pandas.<locals>.<genexpr>c                 s   r+   r   r,   r/   r   r   r   r2      r3   feature_names   predictionsc                 S   s   g | ]}d | qS )predictions_r   )r0   ir   r   r   
<listcomp>   s    z4XGBoostPredictor._predict_pandas.<locals>.<listcomp>)r   to_numpyr
   allcolumnstolistxgboostDMatrixpd	DataFramer   r)   lenrange)r   r&   r'   r(   r*   r4   matrixdfr   r   r   _predict_pandasy   s8   
z XGBoostPredictor._predict_pandasr   )NN)r   
__module____qualname____doc__r>   Boosterr   r   r   classmethodr   r%   r	   r   r   r.   intr   r   r)   rF   __classcell__r   r   r   r   r      sB    	
Fr   )typingr   r   r   r   r   r   pandasr@   r>   ray.air.constantsr   ray.air.data_batch_typer	   "ray.air.util.data_batch_conversionr
   ray.train.predictorr   ray.train.xgboostr   ray.util.annotationsr   ray.data.preprocessorr   r   r   r   r   r   <module>   s     