Model

class previsionio.model.Model(_id: str, usecase_version_id: str, project_id: str, model_name: str = None, deployable: bool = False, **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
  • usecase_version_id (str) – Unique id of the usecase version of the model
  • name (str, optional) – Name of the model (default: None)
classmethod from_id(_id: str) → Union[previsionio.model.RegressionModel, previsionio.model.ClassificationModel, previsionio.model.MultiClassificationModel, previsionio.model.TextSimilarityModel]

Get a usecase from the platform by its unique id.

Parameters:
  • _id (str) – Unique id of the usecase to retrieve
  • version (int, optional) – Specific version of the usecase to retrieve (default: 1)
Returns:

Fetched usecase

Return type:

BaseUsecaseVersion

Raises:

PrevisionException – Any error while fetching data from the platform or parsing result

hyperparameters

Return the hyperparameters of a model.

Returns:Hyperparameters of the model
Return type:dict
predict(df: pandas.core.frame.DataFrame, confidence: bool = False, prediction_dataset_name: str = None) → 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: previsionio.dataset.Dataset, confidence: bool = False, dataset_folder: previsionio.dataset.Dataset = None) → previsionio.prediction.ValidationPrediction

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 object

Return type:

pd.DataFrame

class previsionio.model.ClassicModel(_id: str, usecase_version_id: str, project_id: str, model_name: str = None, deployable: bool = False, **other_params)

Bases: previsionio.model.Model

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
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
predict_single(data: Dict, confidence: bool = False, explain: bool = False)

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:
  • data (dict) – Features names and values (without target feature) - missing feature keys will be replaced by nans
  • confidence (bool, optional) – Whether to predict with confidence values (default: False)
  • explain (bool, optional) – Whether to explain prediction (default: False)

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
class previsionio.model.ClassificationModel(_id, usecase_version_id, **other_params)

Bases: previsionio.model.ClassicModel

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: float = 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
class previsionio.model.MultiClassificationModel(_id: str, usecase_version_id: str, project_id: str, model_name: str = None, deployable: bool = False, **other_params)

Bases: previsionio.model.ClassicModel

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)
class previsionio.model.RegressionModel(_id: str, usecase_version_id: str, project_id: str, model_name: str = None, deployable: bool = False, **other_params)

Bases: previsionio.model.ClassicModel

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)
class previsionio.model.TextSimilarityModel(_id: str, usecase_version_id: str, project_id: str, model_name: str = None, deployable: bool = False, **other_params)

Bases: previsionio.model.Model

predict(df: pandas.core.frame.DataFrame, queries_dataset_content_column: str, top_k: int = 10, queries_dataset_matching_id_description_column: str = None, prediction_dataset_name: str = None) → Optional[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
  • queries_dataset_content_column (str) – Content queries column name
  • top_k (integer) – Number of the nearest description to predict
  • queries_dataset_matching_id_description_column (str) – Matching id description column name
Returns:

Prediction results dataframe

Return type:

pd.DataFrame

predict_from_dataset(queries_dataset: previsionio.dataset.Dataset, queries_dataset_content_column: str, top_k: int = 10, queries_dataset_matching_id_description_column: str = None) → previsionio.prediction.ValidationPrediction

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
  • queries_dataset_content_column (str) – Content queries column name
  • top_k (integer) – Number of the nearest description to predict
  • queries_dataset_matching_id_description_column (str) – Matching id description column name
Returns:

Prediction object

Return type:

pd.DataFrame