Run the Post-Processing Steps

Perform the following post-processing steps once the model is trained:
  1. Save the model in a datastore.

    The following code save the model in an OML datastore as part of the post-processing step. The Spatial Pipeline is stored in the datastore named sai_regressor_ds, containing a dictionary with object names and Python objects.

    import oml
     
    oml.ds.save({'spatial_error_pipeline': spatial_error_pipeline}, 
       'sai_regressor_ds', description='some description', overwrite=True)
  2. Load the model from a datastore.

    Use the oml.ds.load function to load the model from a datastore into Python for predictions by specifying the name of the datastore and the name of the Python object with the trained model.

    ds_objs = oml.ds.load('sai_regressor_ds', objs=['spatial_error_pipeline'], to_globals=False)
    error_model_loaded = ds_objs['spatial_error_pipeline']
  3. Create and store a user-defined Python function that makes predictions with the trained model given a prediction set.

    The following code creates a Python user-defined function (UDF) that loads the trained model from a datastore and uses it to make predictions with a given prediction set. The UDF is then registered with OML using oml.script.create.

    udf = """def error_model_pipeline_predict_(X):
        import oml   
        ds_objs = oml.ds.load('sai_regressor_ds', objs=['spatial_error_pipeline'], to_globals=False)   
        error_model_pipeline = ds_objs['spatial_error_pipeline']   
        pred = error_model_pipeline.predict(X)   
        return pred.tolist()"""   
        
    oml.script.create("errorModelPipelinePredict", udf, is_global=True, overwrite=True)
  4. Run a Python UDF with SQL.

    The following code uses pyqEval to run the Python UDF errorModelPipelinePredict in SQL by passing the X parameter consisting of a single observation.

    select *
        from table(pyqEval(
            par_lst => '{"X": [[30.6005898, 12.1, 342200.000, 0.8]]}',
            out_fmt => 'JSON',
            scr_name => 'errorModelPipelinePredict'
            )
        );

    The Moran’s I statistic is positive and significant, confirming the presence of spatial dependence in the target variable.

    The response shows estimated median income for the given observation.

    NAME	VALUE
    	[[67428.20461759513]]