Table of contents

Load a model in a notebook or script

In a Watson Studio Local notebook or job, the environment variable DSX_PROJECT_DIR is set and can be used to find the model. If it is not, use the absolute path of the model.

In the following examples, {model_path} refers to the actual model path relative from the project path, for example, "models/BrakeEventClassifier/1/model".

Load Spark model

import os
from pyspark.ml import Pipeline, Model, PipelineModel
model_path = os.getenv("DSX_PROJECT_DIR")+{model_path}
model_rf = PipelineModel.load(model_path)

Load caret model in R

modelPath <- paste(Sys.getenv("DSX_PROJECT_DIR"),{model_path},sep="")
model<-readRDS(modelPath)

Load pickle model

import os, pickle
model_path = os.getenv("DSX_PROJECT_DIR")+{model_path}
loaded_model = pickle.load(open(model_path, 'rb'))

Load Joblib model

import os
from sklearn.externals import joblib
model_path = os.getenv("DSX_PROJECT_DIR")+{model_path}
loaded_model = pickle.load(open(model_path, 'rb'))

Load XGboost model

import os, xgb
model_path = os.getenv("DSX_PROJECT_DIR")+{model_path}
loaded_model = xgb.Booster()
loaded_model.load_model(model_path)

Load keras model:

from keras.models import load_model
import os
model_path = os.getenv("DSX_PROJECT_DIR")+{model_path}
model = load_model(model_path)