Last time, we looked at how to leverage the SAP HANA R integration which opens the door to about 11,000 packages. So, if you feel like the built-in libraries (APL & PAL) don't offer what you need or if you feel like doing something your way too, now you can!
I know that I promised to dive into the TensorFlow integration last time I publish (3 weeks ago).
But due to technical difficulties on my NUC and some of the Virtual Machines I run, which I had to rebuild a couple of times (yes, can still mess up a system if you don't follow instructions), I decided to postpone this piece until I can figure out an easy path to setup the TensorFlow Serving ModelServer on a SUSE or Red Hat system, and provide an interesting but simple model examples.
You have probably realized that Jupyter was initially designed to run Python script (and is actually built using a series of python modules).
This means that you can use the SAP HANA Python driver to connect and consume your data. But would require you to write Python code using the Python Database API.
What if I can tell you that you could build a Jupyter Notebook that runs SQL with using the Python Database API?
import sqlalchemy
%reload_ext sql
%config SqlMagic.displaylimit = 5
hxe_connection = 'hana://ML_USER:Welcome18Welcome18@hxehost:39015';
%sql $hxe_connection
%sql select * FROM M_DATABASE;
result = _
print(result)
import pandas as pd
import matplotlib.pyplot as plt
from sqlalchemy import create_engine
engine = create_engine('hana://ML_USER:Welcome18Welcome18@localhost:39015')
iris_db = pd.read_sql_query("select * from R_DATA.IRIS",engine)
target_names = iris_db.Species.unique()
feature_names = list(iris_db.drop('Species', axis=1).columns.tolist())
fig, axes = plt.subplots(figsize=(20,10), nrows=2, ncols=2)
fig.suptitle("Iris Frequency Histogram", fontsize=16)
colors= ['red', 'green', 'blue']
for i, ax in enumerate(axes.flat):
for idx_label, color in zip(range(len(target_names)), colors):
ax.hist(
iris_db.loc[iris_db.Species == target_names[idx_label], feature_names[i]],
label=target_names[label],
color=color
)
ax.set_xlabel(feature_names[i])
ax.set_ylabel('Frequency')
axes[1,1].legend(loc='upper right')
plt.show()
library(ggplot2)
library(grid)
library(gridExtra)
library("RJDBC")
jdbcDriver <- JDBC(driverClass="com.sap.db.jdbc.Driver", "/usr/sap/hdbclient/ngdbc.jar")
jdbcConnection <- dbConnect(
jdbcDriver,
"jdbc:sap://localhost:39015/?autocommit=false",
"ML_USER",
"Welcome18Welcome18"
)
iris <- dbGetQuery(jdbcConnection, "select * from R_DATA.IRIS")
sl <- ggplot(data=iris, aes(x=Sepal.Length)) +
geom_histogram(binwidth=0.2, aes(fill=Species)) +
xlab("Sepal Length") +
theme(legend.position="none") +
ylab("Frequency")
sw <- ggplot(data=iris, aes(x=Sepal.Width )) +
geom_histogram(binwidth=0.2, aes(fill=Species)) +
xlab("Sepal Width" ) +
theme(legend.position="none") +
ylab("Frequency")
pl <- ggplot(data=iris, aes(x=Petal.Length)) +
geom_histogram(binwidth=0.2, aes(fill=Species)) +
xlab("Petal Length") +
theme(legend.position="none") +
ylab("Frequency")
pw <- ggplot(data=iris, aes(x=Petal.Width )) +
geom_histogram(binwidth=0.2, aes(fill=Species)) +
xlab("Petal Width") +
theme(legend.position="none") +
ylab("Frequency")
grid.arrange(sl, sw, pl, pw, ncol = 2, nrow = 2, top = textGrob("Iris Frequency Histogram"))
dbDisconnect(jdbcConnection)
import pandas as pd
import matplotlib.pyplot as plt
from sqlalchemy import create_engine
engine = create_engine('hana://ML_USER:Welcome18Welcome18@localhost:39015')
iris_db = pd.read_sql_query("select * from R_DATA.IRIS",engine)
target_names = iris_db.Species.unique()
feature_names = list(iris_db.drop('Species', axis=1).columns.tolist())
feature_len = len(iris.feature_names)
target_len = len(iris.target_names)
fig, axes = plt.subplots(
figsize=(20,10),
nrows=dim,
ncols=dim,
sharex='col',
sharey='row'
)
fig.suptitle("Edgar Anderson's Iris Data", fontsize=16)
plot_colors = ['blue', 'white', 'red']
for x in range(feature_len):
for y in range(feature_len):
ax = axes[x,y]
if x == y:
ax.text(0.5, 0.5,
feature_names[x],
transform=ax.transAxes,
horizontalalignment='center',
verticalalignment='center',
fontsize=16
)
else:
for idx_class, color in zip(range(target_len), plot_colors):
idx = np.where(iris_db.Species == idx_class)
ax.scatter(
iris_db.loc[iris_db.Species == target_names[idx_class], feature_names[x]],
iris_db.loc[iris_db.Species == target_names[idx_class], feature_names[y]],
c=color,
cmap=plt.cm.RdYlBu,
label=target_names[idx_class],
edgecolor='black',
s=40
)
plt.show()
library("RJDBC")
jdbcDriver <- JDBC(driverClass="com.sap.db.jdbc.Driver", "/usr/sap/hdbclient/ngdbc.jar")
jdbcConnection <- dbConnect(
jdbcDriver,
"jdbc:sap://localhost:39015/?autocommit=false",
"ML_USER",
"Welcome18Welcome18"
)
iris_db <- dbGetQuery(jdbcConnection, "select * from R_DATA.IRIS")
# repalce text value by color
iris_db$SpeciesColor[iris_db$Species == "setosa"] <- "blue"
iris_db$SpeciesColor[iris_db$Species == "versicolor"] <- "white"
iris_db$SpeciesColor[iris_db$Species == "virginica"] <- "red"
pairs(
iris_db[1:4],
main = "Edgar Anderson's Iris Data",
pch = 21,
bg = unclass(iris_db$SpeciesColor)
)
dbDisconnect(jdbcConnection)
(Remember sharing && giving feedback is caring!)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
21 | |
16 | |
13 | |
12 | |
11 | |
10 | |
7 | |
7 | |
7 | |
7 |