Model

class previsionio.model.Model(_id, uc_id, uc_version, name=None, **other_params)

Bases: previsionio.api_resource.ApiResource

A Model object is generated by Prevision AutoML plateform when you launch a use case. All models generated by Prevision.io are deployable in our Store

With this Model class, you can select the model with the optimal hyperparameters that responds to your buisiness requirements, then you can deploy it as a real-time/batch endpoint that can be used for a web Service.

Parameters:
  • _id (str) – Unique id of the model
  • uc_id (str) – Unique id of the usecase of the model
  • uc_version (str, int) – Version of the usecase of the model (either an integer for a specific version, or “last”)
  • name (str, optional) – Name of the model (default: None)
chart()

Return chart analysis information for a model.

Returns:Chart analysis results
Return type:dict
Raises:PrevisionException – Any error while fetching data from the platform or parsing the result
cross_validation

Get model’s cross validation dataframe.

Returns:Cross-validation dataframe
Return type:pd.Dataframe
deploy() → previsionio.deployed_model.DeployedModel

(Not Implemented yet) Deploy the model as a REST API app.

Keyword Arguments:
 {enum} -- it can be 'model', 'notebook', 'shiny', 'dash' or 'node' application (app_type) –
Returns:Path of the deployed application
Return type:str
feature_importance

Return a dataframe of feature importances for the given model features, with their corresponding scores (sorted by descending feature importance scores).

Returns:Dataframe of feature importances
Return type:pd.DataFrame
Raises:PrevisionException – Any error while fetching data from the platform or parsing the result
hyperparameters

Return the hyperparameters of a model.

Returns:Hyperparameters of the model
Return type:dict
predict(df, confidence=False) → pandas.core.frame.DataFrame

Make a prediction in a Scikit-learn blocking style.

Warning

For large dataframes and complex (blend) models, this can be slow (up to 1-2 hours). Prefer using this for simple models and small dataframes or use option use_best_single = True.

Parameters:
  • df (pd.DataFrame) – A pandas dataframe containing the testing data
  • confidence (bool, optional) – Whether to predict with confidence estimator (default: False)
Returns:

Prediction results dataframe

Return type:

pd.DataFrame

predict_from_dataset(dataset, confidence=False, dataset_folder=None) → pandas.core.frame.DataFrame

Make a prediction for a dataset stored in the current active [client] workspace (using the current SDK dataset object).

Parameters:
  • dataset (Dataset) – Dataset resource to make a prediction for
  • confidence (bool, optional) – Whether to predict with confidence values (default: False)
  • dataset_folder (Dataset, None) – Matching folder dataset resource for the prediction, if necessary
Returns:

Prediction results dataframe

Return type:

pd.DataFrame

predict_from_dataset_name(dataset_name, confidence=False) → pandas.core.frame.DataFrame

Make a prediction for a dataset stored in the current active [client] workspace (referenced by name).

Parameters:
  • dataset_name (str) – Name of the dataset to make a prediction for (if there is more than one dataset having the given name, the first one will be used)
  • confidence (bool, optional) – Whether to predict with confidence values (default: False)
Returns:

Prediction results dataframe

Return type:

pd.DataFrame

predict_single(confidence=False, explain=False, **predict_data)

Make a prediction for a single instance. Use predict_from_dataset_name() or predict methods to predict multiple instances at the same time (it’s faster).

Parameters:
  • confidence (bool, optional) – Whether to predict with confidence values (default: False)
  • explain (bool, optional) – Whether to explain prediction (default: False)
  • **predict_data – Features names and values (without target feature) - missing feature keys will be replaced by nans

Note

You can set both confidence and explain to true.

Returns:Dictionary containing the prediction result

Note

The prediction format depends on the problem type (regression, classification, etc…)

Return type:dict
wait_for_prediction(predict_id)

Wait for a specific prediction to finish.

Parameters:predict_id (str) – Unique id of the prediction to wait for
class previsionio.model.ClassificationModel(_id, uc_id, name=None, **other_params)

Bases: previsionio.model.Model

A model object for a (binary) classification usecase, i.e. a usecase where the target is categorical with exactly 2 modalities.

Parameters:
  • _id (str) – Unique id of the model
  • uc_id (str) – Unique id of the usecase of the model
  • uc_version (str, int) – Version of the usecase of the model (either an integer for a specific version, or “last”)
  • name (str, optional) – Name of the model (default: None)
get_dynamic_performances(threshold=0.5)

Get model performance for the given threshold.

Parameters:threshold (float, optional) – Threshold to check the model’s performance for (default: 0.5)
Returns:Model classification performance dict with the following keys:
  • confusion_matrix
  • accuracy
  • precision
  • recall
  • f1_score
Return type:dict
Raises:PrevisionException – Any error while fetching data from the platform or parsing the result
optimal_threshold

Get the value of threshold probability that optimizes the F1 Score.

Returns:Optimal value of the threshold (if it not a classification problem it returns None)
Return type:float
Raises:PrevisionException – Any error while fetching data from the platform or parsing the result
predict_proba(df, confidence=False)

Make a prediction in a Scikit-learn blocking style and return probabilities.

Warning

For large dataframes and complex (blend) models, this can be slow (up to 1-2 hours). Prefer using this for simple models and small dataframes or use option use_best_single = True.

Parameters:
  • df (pd.DataFrame) – A pandas dataframe containing the testing data
  • confidence (bool, optional) – Whether to predict with confidence estimator (default: False)
Returns:

(Formatted) prediction results dataframe

Return type:

pd.DataFrame

predict_single(confidence=False, explain=False, **predict_data)

Make a prediction for a single instance.

Parameters:
  • confidence (bool, optional) – Whether to predict with confidence values (default: False)
  • explain (bool, optional) – Whether to explain prediction (default: False)
  • **predict_data – Features names and values (without target feature) - missing feature keys will be replaced by nans

Note

You can set both confidence and explain to true.

Returns:Predictions probability, predictions class, predictions confidence and predictions explanation
Return type:tuple
class previsionio.model.MultiClassificationModel(_id, uc_id, uc_version, name=None, **other_params)

Bases: previsionio.model.Model

A model object for a multi-classification usecase, i.e. a usecase where the target is categorical with strictly more than 2 modalities.

Parameters:
  • _id (str) – Unique id of the model
  • uc_id (str) – Unique id of the usecase of the model
  • uc_version (str, int) – Version of the usecase of the model (either an integer for a specific version, or “last”)
  • name (str, optional) – Name of the model (default: None)
predict(df, confidence=False)

Make a prediction in a Scikit-learn blocking style.

Warning

For large dataframes and complex (blend) models, this can be slow (up to 1-2 hours). Prefer using this for simple models and small dataframes or use option use_best_single = True.

Parameters:
  • df (pd.DataFrame) – A pandas dataframe containing the testing data
  • confidence (bool, optional) – Whether to predict with confidence estimator (default: False)
Returns:

(Formatted) prediction results dataframe

Return type:

pd.DataFrame

predict_proba(df, confidence=False)

Make a prediction in a Scikit-learn blocking style and return probabilities.

Warning

For large dataframes and complex (blend) models, this can be slow (up to 1-2 hours). Prefer using this for simple models and small dataframes or use option use_best_single = True.

Parameters:
  • df (pd.DataFrame) – A pandas dataframe containing the testing data
  • confidence (bool, optional) – Whether to predict with confidence estimator (default: False)
Returns:

(Formatted) prediction results dataframe

Return type:

pd.DataFrame

class previsionio.model.RegressionModel(_id, uc_id, uc_version, name=None, **other_params)

Bases: previsionio.model.Model

A model object for a regression usecase, i.e. a usecase where the target is numerical.

Parameters:
  • _id (str) – Unique id of the model
  • uc_id (str) – Unique id of the usecase of the model
  • uc_version (str, int) – Version of the usecase of the model (either an integer for a specific version, or “last”)
  • name (str, optional) – Name of the model (default: None)
predict(df, confidence=False)

Make a prediction in a Scikit-learn blocking style.

Warning

For large dataframes and complex (blend) models, this can be slow (up to 1-2 hours). Prefer using this for simple models and small dataframes or use option use_best_single = True.

Parameters:
  • df (pd.DataFrame) – A pandas dataframe containing the testing data
  • confidence (bool, optional) – Whether to predict with confidence estimator (default: False)
Returns:

Prediction results dataframe

Return type:

pd.DataFrame