Configuring the Python Machine-Learning Client for SAP HANA

Objective

After completing this lesson, you will be able to install the hana-ml package and establish a reliable connection context to SAP HANA Cloud for the machine learning tasks.

Accessing and Exploring Data in SAP HANA

To build time series forecasting models using the SAP HANA Predictive Analysis Library (PAL) from Python, you need to first set up your local Python environment with the necessary libraries and establish a secure connection to your SAP HANA Cloud instance. The hana-ml library enables this integration by allowing you to access SAP HANA data and run SAP HANA PAL algorithms directly from your Python script.

Technical Prerequisites

To use time series models in SAP HANA, you need the following:

  • Instance of SAP HANA Cloud with ScriptServer (APL, PAL enabled)
  • Python hana-ml package 2.24.25051600, other packages and dependencies installed
  • Basic Structured Query Language (SQL) knowledge (highly desirable)

Setting Up the Environment

Before implementing time series models in SAP HANA, it is essential to set up the required environment. This includes installing the necessary Python libraries, establishing a connection to SAP HANA Cloud, and configuring authentication settings.

The following cell loads/imports diverse modules needed for the current demo. As you may know, a module can be considered to be the same as a code library.

Code Snippet
12
import hana_ml print(hana_ml.__version__)

2.24.25051600

Code Snippet
123456789101112131415161718
#importing other packages used import pandas as pd import matplotlib.pyplot as plt import time import numpy as np import os import json from dotenv import load_dotenv load_dotenv() from hana_ml import dataframe from hana_ml.visualizers.unified_report import UnifiedReport from hana_ml.algorithms.pal.tsa.additive_model_forecast import AdditiveModelForecast from hana_ml.algorithms.pal.random import binomial from hana_ml.algorithms.pal.partition import train_test_val_split %matplotlib inline

SAP HANA Cloud Database Connection

With the Python machine learning client for SAP HANA (hana-ml), connections are always related to an SAP HANA DataFrame. Furthermore, for SAP HANA Cloud, we need to use an encrypted connection.

Learners should have the SAP HANA system address and the credentials to access it because they need to be provided to instantiate class hana_mI.dataframe.ConnectionContext; it represents a connection to an SAP HANA database instance [1].

Code Snippet
123456789101112131415
hana_address = os.getenv('hana_address', "<your_hana_address>") hana_port = int(os.getenv('hana_port', 443)) hana_user = os.getenv('hana_user', "<your_hana_user>") hana_password = os.getenv('hana_password', "<your_hana_password>") hana_encrypt = os.getenv('hana_encrypt', 'True').lower() == 'true' hana_schema = os.getenv('HANA_SCHEMA', 'My_Schema') conn = dataframe.ConnectionContext(address=hana_address, port=hana_port, user=hana_user, password=hana_password, encrypt=True, sslValidateCertificate=False, current_schema=hana_schema) conn.connection.isconnected()

True

References