4.8 Use the Python Interpreter in a Notebook Paragraph

An Oracle Machine Learning notebook supports multiple languages. Each paragraph is associated with a specific interpreter. To run Python commands in a notebook, you must first connect to the Python interpreter. To use OML4Py, you must import the oml module.

In an Oracle Machine Learning notebook, you can add multiple paragraphs, and each paragraph can be connected to different interpreters such as SQL or Python. You identify which interpreter to use by specifying % followed by the interpreter to use: sql, script, r, python, conda, markdown.

This example shows you how to:
  • Connect to a Python interpreter to run Python commands in a notebook
  • Import the Python modules - oml, matplotlib, and numpy
  • Check if the oml module is connected to the Oracle AI Database

Note:

z is a reserved keyword and must not be used as a variable in %python paragraphs in Oracle Machine Learning notebooks.
Assumption: The example assumes that you have created a new notebook named Py Note.
  1. Open the Py Note notebook and click the interpreter bindings icon. View the available interpreter bindings.
  2. To connect to the Python interpreter, type %python
    You are now ready to run Python scripts in your notebook.
  3. To use OML4Py module, you must import the oml module. Type the following Python command to import the oml module, and the click run icon. Alternatively, you can press Shift+Enter keys to run the notebook.
    import oml
  4. To verify if the oml module is connected to the Database, type:
    oml.isconnected()
    Once your notebook is connected, the command returns TRUE. The notebook is now connected to the Python interpreter, and you are ready to run python commands in your notebook.

Example to demonstrate the use of the Python modules - matplotlib and numpy , and use random data to plot two histograms.

  1. Type the following commands to import the modules:
    %python
    import matplotlib.pyplot as plt
    import numpy as np
    • Matplotlib - Python module to render graphs
    • Numpy - Python module for computations
  2. Type the following commands to compute and render the data in two histograms.
    
    list1 = np.random.rand(10)*2.1
    list2 = np.random.rand(10)*3.0
    
    plt.subplot(1,2,1) # 1 line, 2 rows, index nr 1 (first position in the subplot)
    plt.hist(list1)
    plt.subplot(1, 2, 2) # 1 line, 2 rows, index nr 2 (second position in the subplot)
    plt.hist(list2)
    plt.show()
    In this example, the commands import two Python module to compute and render the data in two histograms list1 and list2.
  3. Click Run.
    The output section of the paragraph which contains a charting component displays the results in two histograms - list1 and list2, as shown in the screenshot.

4.8.1 About Oracle Machine Learning for Python

Oracle Machine Learning for Python (OML4Py) is a component of Oracle Autonomous AI Database, which includes Oracle Autonomous AI Lakehouse (ADW), Oracle Autonomous AI Transaction Processing (ATP), and Oracle Autonomous AI JSON Database (AJD). By using Oracle Machine Learning UI notebooks, you can run Python functions on data for data exploration and preparation while leveraging Oracle AI Database as a high-performance computing environment. Oracle Machine Learning User Interface (UI) is available through Autonomous AI Lakehouse (ADW) , Autonomous AI Transaction Processing (ATP) and Autonomous AI JSON Database (AJD) services.

Oracle Machine Learning for Python (OML4Py) makes the open source Python scripting language and environment ready for the enterprise and big data. Designed for problems involving both large and small volumes of data, Oracle Machine Learning for Python integrates Python with Oracle Autonomous AI Database, including its powerful in-database machine learning algorithms, and enables deployment of Python code.

Use Oracle Machine Learning for Python to:
  • Perform data exploration, data analysis, and machine learning using Python leveraging Oracle AI Database as a high performance compute engine
  • Build and evaluate machine learning models and score data using those models from an integrated Python API using in-database algorithms
  • Deploy user-defined Python functions through a REST interface with data-parallel and task-parallel processing

The Python interpreter uses Python 3.13.5 to process Python scripts in Oracle Machine Learning notebooks. To use the interpreter, specify the %python directive at the beginning of the paragraph. The Python interpreter supports the following Python modules:

  • cffi-1.15.1
  • contourpy-1.1.0
  • cryptography-43.0.1
  • cycler-0.10.0
  • fonttools-4.49.0
  • joblib-1.4.2
  • kiwisolver-1.4.5
  • matplotlib-3.8.4
  • numpy-2.0.1
  • oracledb-2.4.1
  • packaging-23.1
  • pandas-2.2.2
  • pillow-10.4.0
  • pycparser-2.21
  • pyparsing-2.4.0
  • python_dateutil-2.8.2
  • pytz-2022.1
  • scikit_learn-1.5.1
  • scipy-1.14.0
  • setuptools-70.0.0
  • six-1.16.0
  • threadpoolctl-3.5.0