Unveiling Customer Communities: Harnessing Social Graphs for Community Detection with RelationalAI’s Snowflake Native App

What is RelationalAI?

RelationalAI is the industry’s first relational knowledge graph coprocessor for the Data Cloud, enabling users to easily apply an expanding set of AI techniques to their data, where it already lives. With RelationalAI, organizations can enhance their data with meaning and build a common model of their business, simplifying computing and powering better decisions.

Follow us as we show how RelationalAI’s native app, running on Snowpark Container Services, can run advanced graph analytics workloads to develop a deeper understanding of our customers. We will extract social communities from regular transactions and do this analysis within the Data Cloud.

Let’s demonstrate RelationalAI’s capabilities through an example. TastyBytes is a fictitious food truck company, created by Snowflake. Their dataset offers a variety of data points, including customers that ate at particular food trucks and what they ordered. A way to visualize this data is through the diagram below.

Transactions link Customers to Trucks

While TastyBytes does not provide any direct connections between customers, we often notice co-workers and friends lining up together at our food trucks. Wouldn’t it be great to augment this data by inferring a social graph of our customer base? What if we could market to specific communities based on this social graph? What if, upon receiving a negative review by a customer, we could extend our response to the social circles of that customer, engaging not only with them but also with their friends? Graph analytics will enable us to do these things, similar to the techniques employed by platforms such as Facebook and LinkedIn, which utilize social graphs to enhance customer engagement and facilitate growth.

Continue reading to understand how we can transform the transaction data into a customer-to-customer graph using RelationalAI. Let’s dive in.

With RelationalAI’s native app, running on Snowpark Container Services, we can use our favorite Python development tool to materialize a knowledge graph and run our analysis. All that’s required is the RelationalAI package.

import relationalai as rai

The RelationalAI package is a declarative query builder that lets you model relationships between entities in your data cloud. In a model, entities are represented as objects, rather than rows in a table, and fields are represented as object properties. The properties of objects and relationships between them are stored in the model as rules.

Let’s see how the above works within the TastyBytes dataset.

Step 1: Use transactions to construct a customer social graph

We can create customer-to-customer relationships by applying logic that mirrors the behavior of individuals meeting friends or colleagues for a meal. Normally, you make plans to all show up at a certain time and probably stand in line together. However, it’s common for someone in the group to arrive later. Therefore, grouping people solely based on sequential order at the food truck isn’t practical. Instead, it’s more logical to create connections between two transactions if they occur at the same food truck within a 20-minute time window. This approach intuitively captures groups of friends or colleagues who eat together. Let’s delve into the code that accomplishes this.

# Find transactions occurring within a 20 mins time window and with matching truck IDs but different customer IDs
with model.rule():

t1 = Transaction()
t2 = Transaction()

t1.truck_id == t2.truck_id
t1.customer_id != t2.customer_id
rel.abs(t1.order_ts_seconds - t2.order_ts_seconds)<=1200

t1.set(connected=t2)

Given as input the transactions graph above, the output is the following:

Now we have a Customer to Customer graph, showing an edge for every time a pair of Customers ordered within 20 minutes of each other.

Notice how we transformed the transaction data to a customer-to-customer graph. Edges represent relationships between customers who share a meal at the same truck during similar time frames. The more frequently you eat with your friends or colleagues, the more connections you’ll observe between yourselves. Also, notice that we captured people who just happen to be standing in line before or after you. Those connections are called spurious connections and we can further process the data to remove them. We filter out edges with a weight of one, indicating instances where customers coincidentally visited the same truck simultaneously without any social connection.

# Find RelevantConnections between customers, store the number of times we see this connection as the edge weight
with model.rule():

t = Transaction()
total_connections = aggregates.count(
t, per=[t.customer_id, t.connected.customer_id]
)

total_connections > 1

RelevantConnection.add(
customer_1=Customer(customer_id=t.customer_id),
customer_2=Customer(customer_id=t.connected.customer_id),
total_connections=total_connections,
)

The result is the following:

Converted Customer to Customer graph with edge weights.

Step 2: Run community detection on top of the customer-to-customer graph

Next, we use the RelationalAI object graph and attach our model to it by executing the command:

community_graph = Graph(model, undirected=True)

Then, we write a rule that takes all of the relevant connections and uses those to create edges in our graph object. This will implicitly also add nodes. We add the number of total connections as the weight attribute. In RelationalAI, we support operations over both weighted and unweighted graphs.

# Create an undirected graph from the provided model
community_graph = Graph(model, undirected=True)
# Add edges to the graph between customers / Nodes will be added automatically
with model.rule():

connection = RelevantConnection()
community_graph.edges.add(
connection.customer_1,
connection.customer_2,
weight=connection.total_connections,
)

Once we have added our edges and nodes, we are ready to run community detection over the graph. We run Louvain by executing community_graph.compute.louvain(customer) on our customer-to-customer graph.

# Run community detection using Louvain, store the community id as an attribute
with model.rule():
customer = Customer()
community_id = community_graph.compute.louvain(customer)
customer.set(community_id=community_id)

The result of the calculation is an assignment of every node to a community.

Example communities include:

Our communities we calculated from the Louvain algorithm

In the above example, we can see that two communities were formed. By identifying distinct clusters within our customer base, we gain invaluable insights into their preferences, behaviors, and connections.

Here is an snapshot of the actual graph using the TastyBytes dataset:

TastyBytes Communities

Thanks to Snowflake’s Snowpark Container Services, RelationalAI’s native app can easily scale to support graphs with millions of nodes and edges. And after you run your graph analysis, you can also add the results back to your Snowflake table so that everyone on your team can benefit from your customer social graph!

What’s next?

Community detection is just the tip of the iceberg. With scalable graph analytics, the potential is limitless. From identifying influential customers to delivering personalized group recommendations, you can unlock a wealth of insights to supercharge your marketing efforts and other initiatives to enhance customer engagement. Dive into the world of graph analytics and explore the full spectrum of opportunities today!

For a more in depth walkthrough, see this link

--

--