Chapter 8. Saving your model as an independent Python function
Turn your data model into an independent Python function so that you can run it outside your notebook server environment and use it in intelligent applications.
Prerequisites
- You have access to the JupyterLab interface.
- You have developed a prediction model in a Jupyter notebook.
- Your Jupyter notebook is saved in a Git repository that was created from the Red Hat OpenShift Data Science sample S2I application repositories.
Procedure
-
In JupyterLab, create a new
prediction.pyfile. Edit the
prediction.pyfile to define apredictfunction based on the prediction model in your Jupyter notebook.- Include only the code required to make the prediction. For example, you do not need to import libraries that only related to rendering plots in your Jupyter notebook.
-
If any new packages are required to run your prediction, update the contents of the
requirements.txtfile and runpip install -r requirements.txtto install the new packages.
Test that you can run the independent Python function from your notebook by calling the function in a new notebook cell, for example:
from prediction import predict predict(data)
Verification
-
The
predictfunction runs correctly and returns the expected output when called from the notebook cell.
Additional resources
8.1. Installing Python packages on your notebook server
You can install Python packages that are not part of the default notebook server image by adding the package and the version to a requirements.txt file and then running the pip install command in a notebook cell.
You can also install packages directly, but Red Hat recommends using a requirements.txt file so that the packages stated in the file can be easily re-used across different notebooks. In addition, using a requirements.txt file is also useful when using a S2I build to deploy a model.
Prerequisites
- Log in to Jupyter and open a notebook.
Procedure
Create a new text file using one of the following methods:
- Click + to open a new launcher and click Text file.
- Click File → New → Text File.
Rename the text file to
requirements.txt.- Right-click on the name of the file and click Rename Text. The Rename File dialog opens.
-
Enter
requirements.txtin the New Name field and click Rename.
Add the packages to install to the
requirements.txtfile.altair
You can specify the exact version to install by using the
==(equal to) operator, for example:altair==4.1.0
NoteRed Hat recommends specifying exact package versions to enhance the stability of your notebook server over time. New package versions can introduce undesirable or unexpected changes in your environment’s behavior.
To install multiple packages at the same time, place each package on a separate line.
Install the packages in
requirements.txtto your server using a notebook cell.Create a new cell in your notebook and enter the following command:
!pip install -r requirements.txt
- Run the cell by pressing Shift and Enter.
ImportantThis installs the package on your notebook server, but you must still run the
importdirective in a code cell to use the package in your code.import altair
Verification
-
Confirm that the packages in
requirements.txtappear in the list of packages installed on the notebook server. See Viewing Python packages installed on your notebook server for details.