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