# Training predictions
URL: https://docs.datarobot.com/en/docs/api/reference/sdk/training_predictions.html

# Training predictions

### class datarobot.models.training_predictions.TrainingPredictionsIterator

Lazily fetches training predictions from DataRobot API in chunks of specified size and then
iterates rows from responses as named tuples. Each row represents a training prediction
computed for a dataset’s row. Each named tuple has the following structure:

- Variables:

> [!NOTE] Notes
> Each `PredictionValue` dict contains these keys:
> 
> label
>   : describes what this model output corresponds to. For regression
>     projects, it is the name of the target feature. For classification and multiclass
>     projects, it is a label from the target feature.
> value
>   : the output of the prediction. For regression projects, it is the
>     predicted value of the target. For classification and multiclass projects, it is
>     the predicted probability that the row belongs to the class identified by the label.
> 
> Each `PredictionExplanations` dictionary contains these keys:
> 
> label (str)
>   : describes what output was driven by this prediction explanation. For regression
>     projects, it is the name of the target feature. For classification projects, it is the
>     class whose probability increasing would correspond to a positive strength of this
>     prediction explanation.
> feature (str)
>   : the name of the feature contributing to the prediction
> feature_value (object)
>   : the value the feature took on for this row. The type corresponds to the feature
>     (boolean, integer, number, string)
> strength (float)
>   : algorithm-specific explanation value attributed to feature in this row
> 
> `ShapMetadata` dictionary contains these keys:
> 
> shap_remaining_total (float)
>   : The total of SHAP values for features beyond the
> max_explanations
> . This can be
>     identically 0 in all rows, if max_explanations is greater than the number of features
>     and thus all features are returned.
> shap_base_value (float)
>   : the model’s average prediction over the training data. SHAP values are deviations from
>     the base value.
> warnings (dict or None)
>   : SHAP values calculation warnings (e.g., additivity check failures in XGBoost models).
>     Schema described as
> ShapWarnings
> .
> 
> `ShapWarnings` dictionary contains these keys:
> 
> mismatch_row_count (int)
>   : the count of rows for which additivity check failed
> max_normalized_mismatch (float)
>   : the maximal relative normalized mismatch value

> [!NOTE] Examples
> ```
> import datarobot as dr
> 
> # Fetch existing training predictions by their id
> training_predictions = dr.TrainingPredictions.get(project_id, prediction_id)
> 
> # Iterate over predictions
> for row in training_predictions.iterate_rows()
>     print(row.row_id, row.prediction)
> ```

### class datarobot.models.training_predictions.TrainingPredictions

Represents training predictions metadata and provides access to prediction results.

- Variables:

> [!NOTE] Notes
> Each element in `shap_warnings` has the following schema:
> 
> partition_name (str)
>   : the partition used for the prediction record.
> value (object)
>   : the warnings related to this partition.
> 
> The objects in `value` are:
> 
> mismatch_row_count (int)
>   : the count of rows for which additivity check failed.
> max_normalized_mismatch (float)
>   : the maximal relative normalized mismatch value.

> [!NOTE] Examples
> Compute training predictions for a model on the whole dataset
> 
> ```
> import datarobot as dr
> 
> # Request calculation of training predictions
> training_predictions_job = model.request_training_predictions(dr.enums.DATA_SUBSET.ALL)
> training_predictions = training_predictions_job.get_result_when_complete()
> print('Training predictions {} are ready'.format(training_predictions.prediction_id))
> 
> # Iterate over actual predictions
> for row in training_predictions.iterate_rows():
>     print(row.row_id, row.partition_id, row.prediction)
> ```
> 
> List all training predictions for a project
> 
> ```
> import datarobot as dr
> 
> # Fetch all training predictions for a project
> all_training_predictions = dr.TrainingPredictions.list(project_id)
> 
> # Inspect all calculated training predictions
> for training_predictions in all_training_predictions:
>     print(
>         'Prediction {} is made for data subset "{}"'.format(
>             training_predictions.prediction_id,
>             training_predictions.data_subset,
>         )
>     )
> ```
> 
> Retrieve training predictions by id
> 
> ```
> import datarobot as dr
> 
> # Getting training predictions by id
> training_predictions = dr.TrainingPredictions.get(project_id, prediction_id)
> 
> # Iterate over actual predictions
> for row in training_predictions.iterate_rows():
>     print(row.row_id, row.partition_id, row.prediction)
> ```

#### classmethod list(project_id)

Fetch all the computed training predictions for a project.

- Parameters: project_id ( str ) – id of the project
- Return type: A list of TrainingPredictions objects

#### classmethod get(project_id, prediction_id)

Retrieve training predictions on a specified data set.

- Parameters:
- Returns: object which is ready to operate with specified predictions
- Return type: TrainingPredictions

#### iterate_rows(batch_size=None)

Retrieve training prediction rows as an iterator.

- Parameters: batch_size ( Optional[int] ) – maximum number of training prediction rows to fetch per request
- Returns: iterator – an iterator which yields named tuples representing training prediction rows
- Return type: TrainingPredictionsIterator

#### get_all_as_dataframe(class_prefix='class_', serializer='json')

Retrieve all training prediction rows and return them as a pandas.DataFrame.

Returned dataframe has the following structure:
: - row_id : row id from the original dataset
  - prediction : the model’s prediction for this row
  - class_: the probability that the target is this class (only appears for
    classification and multiclass projects)
  - timestamp : the time of the prediction (only appears for out of time validation or
    time series projects)
  - forecast_point : the point in time used as a basis to generate the predictions
    (only appears for time series projects)
  - forecast_distance : how many time steps are between timestamp and forecast_point
    (only appears for time series projects)
  - series_id : he id of the series in a multiseries project
    or None for a single series project
    (only appears for time series projects)

- Parameters:
- Returns: dataframe
- Return type: pandas.DataFrame

#### download_to_csv(filename, encoding='utf-8', serializer='json')

Save training prediction rows into CSV file.

- Parameters:

---
