SAP HANA ML Python Client (hana-ml)
Related Skills
- sap-dependency-security: Use for secure dependency pinning and upgrade workflows in Python/auxiliary tooling used alongside HANA ML stacks
When to Use This Skill
Use this skill when building machine learning workflows with the hana-ml Python client, using PAL/APL algorithms, querying HANA DataFrames, training or scoring models in-database, using AutoML, visualizing model output, or troubleshooting Python-to-HANA ML connections.
Common Issues
| Issue |
First check |
| Connection fails |
Verify HANA host, port, TLS/encryption, user privileges, and network allowlists. |
| PAL/APL algorithm missing |
Confirm the HANA system has the required AFL/PAL/APL libraries installed and licensed. |
| DataFrame collection is slow |
Push filtering/projection into HANA and avoid collecting large frames into Python. |
Package Version: 2.22.241011
Last Verified: 2025-11-27
Table of Contents
Installation & Setup
pip install hana-ml
Requirements: Python 3.8+, SAP HANA 2.0 SPS03+ or SAP HANA Cloud
Quick Start
Connection & DataFrame
from hana_ml import ConnectionContext
# Connect
conn = ConnectionContext(
address='<hostname>',
port=443,
user='<username>',
password='<password>',
encrypt=True
)
# Create DataFrame
df = conn.table('MY_TABLE', schema='MY_SCHEMA')
print(f"Shape: {df.shape}")
df.head(10).collect()
PAL Classification
from hana_ml.algorithms.pal.unified_classification import UnifiedClassification
# Train model
clf = UnifiedClassification(func='RandomDecisionTree')
clf.fit(train_df, features=['F1', 'F2', 'F3'], label='TARGET')
# Predict & evaluate
predictions = clf.predict(test_df, features=['F1', 'F2', 'F3'])
score = clf.score(test_df, features=['F1', 'F2', 'F3'], label='TARGET')
APL AutoML
from hana_ml.algorithms.apl.classification import AutoClassifier
# Automated classification
auto_clf = AutoClassifier()
auto_clf.fit(train_df, label='TARGET')
predictions = auto_clf.predict(test_df)
Model Persistence
from hana_ml.model_storage import ModelStorage
ms = ModelStorage(conn)
clf.name = 'MY_CLASSIFIER'
ms.save_model(model=clf, if_exists='replace')
Core Libraries
PAL (Predictive Analysis Library)
- 100+ algorithms executed in-database
- Categories: Classification, Regression, Clustering, Time Series, Preprocessing
- Key classes:
UnifiedClassification, UnifiedRegression, KMeans, ARIMA
- See:
references/PAL_ALGORITHMS.md for complete list
APL (Automated Predictive Library)
- AutoML capabilities with automatic feature engineering
- Key classes:
AutoClassifier, AutoRegressor, GradientBoostingClassifier
- See:
references/APL_ALGORITHMS.md for details
DataFrames
- Lazy evaluation - builds SQL until
collect() called
- In-database processing for optimal performance
- See:
references/DATAFRAME_REFERENCE.md for complete API
Visualizers
- EDA plots, model explanations, metrics
- SHAP integration for model interpretability
- See:
references/VISUALIZERS.md for 14 visualization modules
Common Patterns
Train-Test Split
from hana_ml.algorithms.pal.partition import train_test_val_split
train, test, val = train_test_val_split(
data=df,
training_percentage=0.7,
testing_percentage=0.2,
validation_percentage=0.1
)
Feature Importance
# APL models
importance = auto_clf.get_feature_importances()
# PAL models
from hana_ml.algorithms.pal.preprocessing import FeatureSelection
fs = FeatureSelection()
fs.fit(train_df, features=features, label='TARGET')
Pipeline
from hana_ml.algorithms.pal.pipeline import Pipeline
from hana_ml.algorithms.pal.preprocessing import Imputer, FeatureNormalizer
pipeline = Pipeline([
('imputer', Imputer(strategy='mean')),
('normalizer', FeatureNormalizer()),
('classifier', UnifiedClassification(func='RandomDecisionTree'))
])
Best Practices
- Use lazy evaluation - Operations build SQL without execution until
collect()
- Leverage in-database processing - Keep data in HANA for performance
- Use Unified interfaces - Consistent APIs across algorithms
- Save models - Use
ModelStorage for persistence
- Explain predictions - Use SHAP explainers for interpretability
- Monitor AutoML - Use
PipelineProgressStatusMonitor for long-running jobs
Bundled Resources
Reference Files
references/DATAFRAME_REFERENCE.md (479 lines)
- ConnectionContext API, DataFrame operations, SQL generation
references/PAL_ALGORITHMS.md (869 lines)
- Complete PAL algorithm reference (100+ algorithms)
- Classification, Regression, Clustering, Time Series, Preprocessing
references/APL_ALGORITHMS.md (534 lines)
- AutoML capabilities, automated feature engineering
- AutoClassifier, AutoRegressor, GradientBoosting classes
references/VISUALIZERS.md (704 lines)
- 14 visualization modules (EDA, SHAP, metrics, time series)
- Plot types, configuration, export options
references/SUPPORTING_MODULES.md (626 lines)
- Model storage, spatial analytics, graph algorithms
- Text mining, statistics, error handling
Error Handling
from hana_ml.ml_exceptions import Error
try:
clf.fit(train_df, features=features, label='TARGET')
except Error as e:
print(f"HANA ML Error: {e}")
Documentation
1---
2name: sap-hana-ml
3description: |
4 SAP HANA Machine Learning Python Client (hana-ml) development skill.
5
6 Use when: Building ML solutions with SAP HANA's in-database machine learning
7 using Python hana-ml library for PAL/APL algorithms, DataFrame operations,
8 AutoML, model persistence, and visualization.
9
10 Keywords: hana-ml, SAP HANA, machine learning, PAL, APL, predictive analytics,
11 HANA DataFrame, ConnectionContext, classification, regression, clustering,
12 time series, ARIMA, gradient boosting, AutoML, SHAP, model storage
13license: GPL-3.0
14metadata:
15 source: https://skillmd.com/skills/secondsky/sap-hana-ml
16 maintainer: "Eduard Jiglau"
17 maintainer_email: "hello@sap-ai-skills.com"
18 website: "https://sap-ai-skills.com"
19 version: "2.4.1"
20 last_verified: 2025-11-27
21 package_version: 2.22.241011
22---
23
24# SAP HANA ML Python Client (hana-ml)
25
26## Related Skills
27
28- **sap-dependency-security**: Use for secure dependency pinning and upgrade workflows in Python/auxiliary tooling used alongside HANA ML stacks
29
30## When to Use This Skill
31
32Use this skill when building machine learning workflows with the `hana-ml` Python client, using PAL/APL algorithms, querying HANA DataFrames, training or scoring models in-database, using AutoML, visualizing model output, or troubleshooting Python-to-HANA ML connections.
33
34## Common Issues
35
36| Issue | First check |
37|-------|-------------|
38| Connection fails | Verify HANA host, port, TLS/encryption, user privileges, and network allowlists. |
39| PAL/APL algorithm missing | Confirm the HANA system has the required AFL/PAL/APL libraries installed and licensed. |
40| DataFrame collection is slow | Push filtering/projection into HANA and avoid collecting large frames into Python. |
41
42**Package Version**: 2.22.241011
43**Last Verified**: 2025-11-27
44
45## Table of Contents
46
47- [Installation & Setup](#installation--setup)
48- [Quick Start](#quick-start)
49- [Core Libraries](#core-libraries)
50- [Common Patterns](#common-patterns)
51- [Best Practices](#best-practices)
52- [Bundled Resources](#bundled-resources)
53
54---
55
56## Installation & Setup
57
58```bash
59pip install hana-ml
60```
61
62**Requirements**: Python 3.8+, SAP HANA 2.0 SPS03+ or SAP HANA Cloud
63
64---
65
66## Quick Start
67
68### Connection & DataFrame
69
70```python
71from hana_ml import ConnectionContext
72
73# Connect
74conn = ConnectionContext(
75 address='<hostname>',
76 port=443,
77 user='<username>',
78 password='<password>',
79 encrypt=True
80)
81
82# Create DataFrame
83df = conn.table('MY_TABLE', schema='MY_SCHEMA')
84print(f"Shape: {df.shape}")
85df.head(10).collect()
86```
87
88### PAL Classification
89
90```python
91from hana_ml.algorithms.pal.unified_classification import UnifiedClassification
92
93# Train model
94clf = UnifiedClassification(func='RandomDecisionTree')
95clf.fit(train_df, features=['F1', 'F2', 'F3'], label='TARGET')
96
97# Predict & evaluate
98predictions = clf.predict(test_df, features=['F1', 'F2', 'F3'])
99score = clf.score(test_df, features=['F1', 'F2', 'F3'], label='TARGET')
100```
101
102### APL AutoML
103
104```python
105from hana_ml.algorithms.apl.classification import AutoClassifier
106
107# Automated classification
108auto_clf = AutoClassifier()
109auto_clf.fit(train_df, label='TARGET')
110predictions = auto_clf.predict(test_df)
111```
112
113### Model Persistence
114
115```python
116from hana_ml.model_storage import ModelStorage
117
118ms = ModelStorage(conn)
119clf.name = 'MY_CLASSIFIER'
120ms.save_model(model=clf, if_exists='replace')
121```
122
123---
124
125## Core Libraries
126
127### PAL (Predictive Analysis Library)
128- **100+ algorithms** executed in-database
129- Categories: Classification, Regression, Clustering, Time Series, Preprocessing
130- **Key classes**: `UnifiedClassification`, `UnifiedRegression`, `KMeans`, `ARIMA`
131- See: `references/PAL_ALGORITHMS.md` for complete list
132
133### APL (Automated Predictive Library)
134- **AutoML capabilities** with automatic feature engineering
135- **Key classes**: `AutoClassifier`, `AutoRegressor`, `GradientBoostingClassifier`
136- See: `references/APL_ALGORITHMS.md` for details
137
138### DataFrames
139- **Lazy evaluation** - builds SQL until `collect()` called
140- **In-database processing** for optimal performance
141- See: `references/DATAFRAME_REFERENCE.md` for complete API
142
143### Visualizers
144- **EDA plots**, model explanations, metrics
145- **SHAP integration** for model interpretability
146- See: `references/VISUALIZERS.md` for 14 visualization modules
147
148---
149
150## Common Patterns
151
152### Train-Test Split
153```python
154from hana_ml.algorithms.pal.partition import train_test_val_split
155
156train, test, val = train_test_val_split(
157 data=df,
158 training_percentage=0.7,
159 testing_percentage=0.2,
160 validation_percentage=0.1
161)
162```
163
164### Feature Importance
165```python
166# APL models
167importance = auto_clf.get_feature_importances()
168
169# PAL models
170from hana_ml.algorithms.pal.preprocessing import FeatureSelection
171fs = FeatureSelection()
172fs.fit(train_df, features=features, label='TARGET')
173```
174
175### Pipeline
176```python
177from hana_ml.algorithms.pal.pipeline import Pipeline
178from hana_ml.algorithms.pal.preprocessing import Imputer, FeatureNormalizer
179
180pipeline = Pipeline([
181 ('imputer', Imputer(strategy='mean')),
182 ('normalizer', FeatureNormalizer()),
183 ('classifier', UnifiedClassification(func='RandomDecisionTree'))
184])
185```
186
187---
188
189## Best Practices
190
1911. **Use lazy evaluation** - Operations build SQL without execution until `collect()`
1922. **Leverage in-database processing** - Keep data in HANA for performance
1933. **Use Unified interfaces** - Consistent APIs across algorithms
1944. **Save models** - Use `ModelStorage` for persistence
1955. **Explain predictions** - Use SHAP explainers for interpretability
1966. **Monitor AutoML** - Use `PipelineProgressStatusMonitor` for long-running jobs
197
198---
199
200## Bundled Resources
201
202### Reference Files
203- **`references/DATAFRAME_REFERENCE.md`** (479 lines)
204 - ConnectionContext API, DataFrame operations, SQL generation
205
206- **`references/PAL_ALGORITHMS.md`** (869 lines)
207 - Complete PAL algorithm reference (100+ algorithms)
208 - Classification, Regression, Clustering, Time Series, Preprocessing
209
210- **`references/APL_ALGORITHMS.md`** (534 lines)
211 - AutoML capabilities, automated feature engineering
212 - AutoClassifier, AutoRegressor, GradientBoosting classes
213
214- **`references/VISUALIZERS.md`** (704 lines)
215 - 14 visualization modules (EDA, SHAP, metrics, time series)
216 - Plot types, configuration, export options
217
218- **`references/SUPPORTING_MODULES.md`** (626 lines)
219 - Model storage, spatial analytics, graph algorithms
220 - Text mining, statistics, error handling
221
222---
223
224## Error Handling
225
226```python
227from hana_ml.ml_exceptions import Error
228
229try:
230 clf.fit(train_df, features=features, label='TARGET')
231except Error as e:
232 print(f"HANA ML Error: {e}")
233```
234
235---
236
237## Documentation
238
239- **Official Docs**: [https://help.sap.com/doc/1d0ebfe5e8dd44d09606814d83308d4b/2.0.07/en-US/hana_ml.html](https://help.sap.com/doc/1d0ebfe5e8dd44d09606814d83308d4b/2.0.07/en-US/hana_ml.html)
240- **PyPI Package**: [https://pypi.org/project/hana-ml/](https://pypi.org/project/hana-ml/)