import hana_ml.dataframe as dataframe
conn = dataframe.ConnectionContext(userkey='MYHC')
conn.connection.isconnected()
import pandas as pd
df_data = pd.read_csv('USEDCARS.csv', sep=',')
df_remote = dataframe.create_dataframe_from_pandas(connection_context=conn,
pandas_df=df_data,
table_name='USEDCARS',
force=True,
drop_exist_tab=False,
replace=False)
df_remote = df_remote.filter('PRICE >= 1000 AND PRICE <= 20000 and HP >= 50 AND HP <= 300 and YEAROFREGISTRATION >= 1995 AND YEAROFREGISTRATION <= 2015')
df_remote.save('V_USEDCARS', table_type='VIEW', force=True)
df_remote.head(5).collect()
import os, datetime
import hana_ml.dataframe as dataframe
from hana_ml.algorithms.pal.unified_regression import UnifiedRegression
from hana_ml.algorithms.pal.model_selection import GridSearchCV
from hana_ml.model_storage import ModelStorage
from ai_core_sdk.tracking import Tracking
from ai_core_sdk.models import Metric
# Print external IP address
from requests import get
ip = get('https://api.ipify.org').text
print(f'My public IP address is: {ip}')
if os.getenv('KUBERNETES_SERVICE_HOST'):
# Code is executed in AI Core
print('Hello from Python in Kubernetes')
hana_address = os.getenv('HANA_ADDRESS')
print('The SAP HANA address is: ' + hana_address)
conn = dataframe.ConnectionContext(address=os.getenv('HANA_ADDRESS'),
port=int(os.getenv('HANA_PORT')),
user=os.getenv('HANA_USER'),
password=os.getenv('HANA_PASSWORD'))
else:
# File is executed locally
print('Hello from Python that is NOT in Kubernetes')
conn = dataframe.ConnectionContext(userkey='MYHC')
print('Connection to SAP HANA established: ' + str(conn.connection.isconnected()))
# Point to the training data in SAP HANA
df_remote = conn.table('V_USEDCARS')
# Train the Regression model
ur_hgbt = UnifiedRegression(func='HybridGradientBoostingTree')
gscv = GridSearchCV(estimator=ur_hgbt,
param_grid={'learning_rate': [0.001, 0.01, 0.1],
'n_estimators': [5, 10, 20, 50],
'split_threshold': [0.1, 0.5, 1]},
train_control=dict(fold_num=3,
resampling_method='cv',
random_state=1,
ref_metric=['rmse']),
scoring='rmse')
gscv.fit(data=df_remote,
key= 'CAR_ID',
label='PRICE',
partition_method='random',
partition_random_state=42,
output_partition_result=True,
build_report=False)
r2 = ur_hgbt.statistics_.filter("STAT_NAME = 'TEST_R2'").select('STAT_VALUE').collect().iloc[0, 0]
print('The model has been trained. R2: ' + str(r2))
# If executed on AI Core, store model metrics with the execution
aic_connection = Tracking()
aic_connection.log_metrics(
metrics = [
Metric(
name= "R2", value= float(r2), timestamp=datetime.datetime.now(datetime.timezone.utc)),
]
)
# Save the model
model_storage = ModelStorage(connection_context=conn)
ur_hgbt.name = 'Car price regression'
model_storage.save_model(model=ur_hgbt)
print('The model has been saved')
hana_ml==2.17.23071400
ai_core_sdk===1.22.3
shapely==1.8.0
jinja2==3.1.2
urllib3==1.26.16
requests==2.29.0
cryptography==39.0.1
IPython==8.14.0
# Specify which base layers (default dependencies) to use
# You may find more base layers at https://hub.docker.com/
FROM python:3.9
#
# Creates directory within your Docker image
RUN mkdir -p /app/src/
#
# Copies file from your Local system TO path in Docker image
COPY main.py /app/src/
COPY requirements.txt /app/src/
#
# Installs dependencies within you Docker image
RUN pip3 install -r /app/src/requirements.txt
#
# Enable permission to execute anything inside the folder app
RUN chgrp -R 65534 /app && \
chmod -R 777 /app
docker build --no-cache -t YOURUSERHERE/hana-ml-car-price-training:01 .
docker push YOURUSERHERE/hana-ml-car-price-restapi-inference:01
apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
name: hana-ml-car-price-training # Executable ID (max length 64 lowercase-hyphen-separated), please modify this to any value if you are not the only user of your SAP AI Core instance. Example: `first-pipeline-1234`
annotations:
scenarios.ai.sap.com/name: "HANA ML Car Price"
scenarios.ai.sap.com/description: "Estimate the price of a use car with SAP HANA Machine Learning"
executables.ai.sap.com/name: "HANA ML Car Price - Training Workflow"
executables.ai.sap.com/description: "Train the model in SAP HANA"
labels:
scenarios.ai.sap.com/id: "hana-ml-car-price" # The scenario ID to which the serving template belongs
ai.sap.com/version: "0.0.1"
spec:
imagePullSecrets:
- name: docker-registry-secret-XYZ # your docker registry secret
entrypoint: mypipeline
templates:
- name: mypipeline
container:
image: docker.io/YOURUSERHERE/hana-ml-car-price-training:01 # Your docker image name
command: ["/bin/sh", "-c"]
args:
- "python /app/src/main.py"
env:
- name: HANA_ADDRESS # Name of the environment variable that will be available in the image
valueFrom:
secretKeyRef:
name: hanacred # Name of the generic secret created in SAP AI Core for the Resource Group
key: address # Name of the key from the JSON string that is saved as generic secret
- name: HANA_PORT
valueFrom:
secretKeyRef:
name: hanacred
key: port
- name: HANA_USER
valueFrom:
secretKeyRef:
name: hanacred
key: user
- name: HANA_PASSWORD
valueFrom:
secretKeyRef:
name: hanacred
key: password
import base64
def encode_base64(message):
message_bytes = message.encode('ascii')
base64_bytes = base64.b64encode(message_bytes)
base64_message = base64_bytes.decode('ascii')
return base64_message
address_enc = encode_base64('YOURENDPOINT')
port_enc = encode_base64('443')
user_enc = encode_base64('YOURUSER')
password_enc = encode_base64('YOURPASSWORD')
print(json.dumps(
{"address":f'{address_enc}',
"port":f'{port_enc}',
"user":f'{user_enc}',
"password":f'{password_enc}'}
))
df_remote_topredict = conn.table('USEDCARS')
df_remote_predicted = gscv.predict(df_remote_topredict, key='CAR_ID')
df_remote_predicted.save('USEDCARS_PREDICTED', force=True)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
27 | |
24 | |
19 | |
13 | |
12 | |
11 | |
10 | |
8 | |
7 | |
6 |