Machine learning models, such as classification and regression models, capture the relationships and patterns between features in a training dataset and can be applied to similar data in the future for prediction. Model training can be time-consuming, so it is desirable to store or persist a model for future use without retraining it.
In Python Machine Learning Client for SAP HANA (hana-ml), we provide a model storage class for model persistence across classification, regression, clustering, and time-series models. In this blog post, you will learn:
ModelStorage class allows users to save, load, list, and delete models. Internally, a model is stored in two parts:
Some important methods and descriptions are listed below:
Algorithms that have predict and transform functions are supported by Model Storage. Part of the supported algorithm list is as follows:
All source code uses the Python Machine Learning Client for SAP HANA (hana-ml).
The first step is to establish a connection to SAP HANA. Once the connection context is created, hana_ml can push the required time-series processing to SAP HANA for in-database execution. A minimal example is shown below:
import hana_ml
from hana_ml import dataframe
conn = dataframe.ConnectionContext(address='AAA.BBB.CCC.DDD', port=XXX, user='username', password='password')Replace ‘AAA.BBB.CCC.DDD’, ‘XXX’, ‘username’, and ‘password’ with your SAP HANA instance details.
A simple self-made dataset is used to demonstrate model storage for classification. The data is stored in SAP HANA tables called DATA_TBL_FIT and DATA_TBL_PREDICT. Let's have a look at the dataset.
df_fit = conn.table('DATA_TBL_FIT')
df_predict = conn.table('DATA_TBL_PREDICT')
print(df_fit.collect())
print(df_predict.collect())The result is shown below:
ID OUTLOOK TEMP HUMIDITY WINDY CLASS
0 0 Sunny 75 70.0 Yes Play
1 1 Sunny 77 90.0 Yes Do not Play
2 2 Sunny 85 79.0 No Do not Play
...
11 11 Rain 75 80.0 No Play
12 12 Rain 68 80.0 No Play
13 13 Rain 70 96.0 No Play
ID OUTLOOK TEMP HUMIDITY WINDY
0 0 Overcast 75 -10000.0 Yes
1 1 Rain 78 70.0 Yes
2 2 Sunny -10000 78.0 Yes
3 3 Sunny 69 70.0 Yes
4 4 Rain 74 70.0 Yes
5 5 Rain 70 70.0 Yes
6 6 *** 70 70.0 YesTrain the model with the UnifiedClassification function and various algorithms: 'MLP', 'NaiveBayes', 'LogisticRegression', 'decisiontree', 'HybridGradientBoostingTree', 'RandomDecisionTree', and 'SVM':
from hana_ml.algorithms.pal.unified_classification import UnifiedClassification
from hana_ml.model_storage import ModelStorage
ms = ModelStorage(conn)
classification_algorithms = ['MLP', 'NaiveBayes', 'LogisticRegression', 'decisiontree',
'HybridGradientBoostingTree', 'RandomDecisionTree','SVM']
dt_param = dict(algorithm='c45')
mlp_param = dict(hidden_layer_size=(10,), activation='TANH', output_activation='TANH',
training_style='batch', max_iter=1000, normalization='z-transform',
weight_init='normal', thread_ratio=1)
for name in classification_algorithms:
if name == 'decisiontree':
algorithm = UnifiedClassification(func = name, **dt_param)
elif name == 'MLP':
algorithm = UnifiedClassification(func = name, **mlp_param)
else:
algorithm = UnifiedClassification(func = name)
if name == 'LogisticRegression':
algorithm.fit(data=df_fit, key='ID', class_map0='Play', class_map1='Do not Play')
else:
algorithm.fit(data=df_fit, key='ID')
algorithm.name = name
algorithm.version = 1
ms.save_model(model=algorithm, if_exists='replace')Use the list_models function to list all models, and we can see that all models with their names, versions, and other information are shown in a table:
ms.list_models()The model list is shown below:

Let's select one model, 'SVM', and load it for prediction:
new_model = ms.load_model(name='SVM', version =1)
type(new_model)Output:
hana_ml.algorithms.pal.unified_classification.UnifiedClassificationThe type of new_model is an object of UnifiedClassification. We can use this object for prediction:
res = new_model.predict(df_predict, key='ID')
print(res.collect())The result:
ID SCORE CONFIDENCE REASON_CODE
0 0 Play 0.296441 None
1 1 Play 0.505984 None
2 2 Play 0.296441 None
3 3 Play 0.595937 None
4 4 Play 0.635761 None
5 5 Do not Play 0.248283 None
6 6 Do not Play 0.313294 NoneFor example, if we want to delete the model 'SVM':
ms.delete_model(name='SVM', version=1)
ms.list_models()Output:

We could also clean up all models at once:
ms.clean_up()In this blog, we described what model storage in hana-ml is and how to use its core methods. Using a simple classification example, we showed how to save trained models, list available versions, reload a selected model for prediction, and remove models when they are no longer needed. These capabilities help streamline machine learning workflows in SAP HANA by making trained models easier to reuse, manage, and maintain.
Product Documentation:
Other Related Blog Posts:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 36 | |
| 34 | |
| 29 | |
| 28 | |
| 26 | |
| 26 | |
| 25 | |
| 23 | |
| 23 | |
| 22 |