hana_ml
2.6: connected data, or graphs.04 Graph.ipynb
in JupyterLab.import pandas as pd
from hana_ml import dataframe as dfh
hana_cloud_endpoint="<uuid>.hana.trial-eu10.hanacloud.ondemand.com:443"
hana_cloud_host, hana_cloud_port=hana_cloud_endpoint.split(":")
cchc=dfh.ConnectionContext(port=hana_cloud_port,
address=hana_cloud_host,
user='HANAML',
password='Super$ecr3t!',
encrypt=True
)
cchc.connection.isconnected()
dfh_ports=cchc.table("PORTS", geo_cols={"POINT_LON_LAT_GEO":"4326"})
dfh_routes=cchc.table("ROUTES")
hana_ml
package includes the new module hana_ml.graph.hana_graph
.import hana_ml.graph.hana_graph
AIRROUTES_DFH
-- represented by the Python variable hgws_airroutes
-- from existing tables represented by HANA DataFrames using create_hana_graph_from_vertex_and_edge_frames()
method. We need to provide all the same parameters as if we would use SQL statement.hgws_airroutes = (
hana_ml.graph.hana_graph
.create_hana_graph_from_vertex_and_edge_frames(
connection_context=cchc,
workspace_name='AIRROUTES_DFH',
vertices_hdf=dfh_ports,
vertex_key_column="ID",
edges_hdf=dfh_routes,
edge_key_column="ID",
edge_source_column="FROM", edge_target_column="TO"
)
)
print("SQL for Vertices: {}\nSQL for Edges: {}"
.format(hgws_airroutes.vertices_hdf.select_statement,
hgws_airroutes.edges_hdf.select_statement))
PORTS_VIEW
and ROUTES_VIEW
were created on the database side on top of existing column tables PORTS
and ROUTES
.hgws_airroutes.vertices_hdf.filter("CODE='WRO'").collect()
313
in this example, then I can call vertices()
method that will return a Pandas dataframe already.hgws_airroutes.vertices(vertex_key=313)
hgws_airroutes.vertices(
vertex_key=(hgws_airroutes
.vertices_hdf.filter("CODE='WRO'").select('ID')
.collect().values[0][0])
)
edges()
for the same node...hgws_airroutes.edges(vertex_key=313).head(3)
direction='INCOMING')
property to it.hgws_airroutes.edges(vertex_key=313, direction='INCOMING').head(3)
edges()
method.hgws_airroutes.source(edge_key=hgws_airroutes.edges(
vertex_key=313,
direction='INCOMING').head(1).ID.values[0])
edges()
method gives us a way to traverse paths, it is more convenient to use neighbors()
method to discover nodes within N-degrees of separation.dfp_nei_wro=hgws_airroutes.neighbors(start_vertex=313)
print("Number of immediate neighbors: {}".format(len(dfp_nei_wro.index)))
display(dfp_nei_wro)
min_depth=0
to include the starting node too.dfp_nei_wro=hgws_airroutes.neighbors(
start_vertex=313,
min_depth=0,
include_edges=True
).edges()
display(dfp_nei_wro)
len(dfp_nei_wro[dfp_nei_wro['FROM']==313].ID)
!pip install networkx
import matplotlib.pyplot as plt
import networkx as nx
nx.__version__
plt.rcParams["figure.figsize"] = [26, 12]
nx_graph_wro = nx.from_pandas_edgelist(
dfp_nei_wro,
source="FROM", target="TO"
)
nx.draw_networkx(nx_graph_wro)
nx.draw_networkx(nx_graph_wro)
a few times.WRO
airport.nx_graph_wro = nx.from_pandas_edgelist(
dfp_nei_wro.query("FROM==313"),
source="FROM",
target="TO"
)
nx.draw_networkx(nx_graph_wro)
pos_geo=nx.shell_layout(nx_graph_wro)
nx.draw_networkx(nx_graph_wro, pos=pos_geo)
X
and Y
coordinates for nodes rendered for display.hgws_airroutes.vertices(vertex_key=313)[['LON','LAT']].values[0]
for x in pos_geo.keys():
pos_geo[x]=hgws_airroutes.vertices(vertex_key=x)[['LON','LAT']].values[0]
nx.draw_networkx(nx_graph_wro, pos=pos_geo)
217
is Reykjavik's Keflavik International Airport or 146
is Tel Aviv's Ben Gurion International Airport.You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
11 | |
10 | |
9 | |
8 | |
6 | |
6 | |
6 | |
5 | |
5 |