Implementing Entity Resolution with Python Record Linkage

Daisy McLogan
Daisy McLogan February 26, 2024

I'm a customer Data Architect at Census, and I help our customers implement best practices when it comes to cleaning, transforming, and activating their data.

Entity Resolution (ER) is a critical data management process used to identify, link, and merge records that correspond to the same real-world entities across different databases or within a single database.In the context of a B2C retail company, Entity Resolution helps consolidate customer information from multiple sources, ensuring data accuracy and consistency. This process is paramount for improving data quality and trust, enabling businesses to achieve a unified or customer 360, view of their customers. By accurately identifying unique customers, companies can enhance personalized marketing strategies, improve customer service, and make informed business decisions. Entity Resolution lays the foundation for robust data governance, ensuring that data across the organization is accurate, consistent, and actionable.

 

The Problem at Hand

For this tutorial, we will look at Entity Resolution at a fictional B2C retail company specializing in luggage and travel gear, akin to Away.com; we face the challenge of entity resolution in our customer database. We aim to identify our best customers and eliminate duplicate records, streamlining our marketing efforts and enhancing customer service. To tackle this, we will leverage the RecordLinkage Python package, a powerful library for linking and deduplicating records.

Python record linkage

Implementation

Step 1: The Shape of the Data

First, let's explore the data model in our unioned customer table. Our data includes columns such as first_name, last_name, city, country, internal_id, MAID, braze_id, and shopify_id. These fields will be instrumental in our entity resolution process.

Example Records:


import pandas as pd

# Load the dataset
df = pd.read_csv('/mnt/data/customers_updated.csv')

# Display the first few records
df[['first_name', 'last_name', 'city', 'country', 'internal_id', 'MAID', 'braze_id', 'shopify_id']].head()

 

Step 2: Blocking & Building Index

For our problem, we'll employ blocking and comparison techniques to identify potential duplicates efficiently. Blocking is a technique used in record linkage to efficiently identify duplicate records within or across datasets. The primary goal of blocking is to reduce the computational complexity of the matching process by limiting the number of comparisons that need to be made. Let's delve deeper into how blocking works and why it's important in record linkage.

  1. Segmentation: The dataset is divided into smaller, more manageable blocks or segments based on certain criteria or key attributes. These attributes are usually selected because they highly indicate a match (e.g., last name, birth date, ZIP code for person records).
  2. Comparison Within Blocks: Instead of comparing every record with every other record in the dataset (a process that can be highly time-consuming and computationally expensive), comparisons are made only within these blocks. This significantly reduces the number of comparisons that need to be performed.
  3. Criteria for Blocking: The criteria or key attributes chosen for blocking are crucial. They should be such that true matches will likely end up in the same block but without making the blocks too large. This balance is vital for the effectiveness of blocking.

Importance of Blocking

Blocking makes it possible to scale record linkage processes to larger datasets, which would be impractical to handle using exhaustive comparison methods. Blocking enhances record linkage by minimizing comparisons, thus accelerating the process for large datasets. It boosts efficiency, and when executed properly, it can also maintain or enhance accuracy by concentrating comparisons where necessary. This method enables the scalability of record linkage to handle larger datasets, which would be cumbersome with exhaustive comparison techniques.

Challenges in Blocking

  • Selection of Blocking Keys: Choosing the right attributes for blocking is crucial and challenging. The keys must ensure that true matches will likely fall into the same block without making the blocks too unwieldy.
  • Block Size: The efficiency gains are minimized if blocks are too large. Conversely, if they are too small, there's a risk of missing true matches that don't share the blocking key attributes.
  • Data Quality: Blocking efficiency also depends on the quality of the data, especially the attributes used for blocking. Variations or errors in these attributes can lead to missed matches.
import recordlinkage

# Create an indexer and specify the blocking variable
indexer = recordlinkage.Index()
indexer.block('country')

# Index the DataFrame
candidate_links = indexer.index(df)

Step 3: Implementing RecordLinkage

After identifying candidate pairs through blocking, we proceed to compare these pairs on multiple attributes. The RecordLinkage package offers a variety of comparison methods, including exact matches, string similarity, numerical comparisons, and more. Among these, the choice of method depends on the nature of the data being compared.

Choosing the Comparison Method:

For our customer data, focusing on first_name, last_name, city, and country, we apply both exact matching and string similarity comparisons. The rationale behind selecting a particular method lies like the data:

  • Exact Matching: Used for city and country fields where matches are expected to share identical values, making it straightforward and highly reliable for identifying duplicates.
  • String Similarity: Particularly for first_name and last_name fields, where minor variations in spelling can occur due to typos or alternate spellings. Here, we employ the Jaro-Winkler similarity method.

Why Jaro-Winkler?

The Jaro-Winkler method is particularly suited for short strings such as names, where it excels in accounting for common typographical errors and variations. It improves upon the basic Jaro similarity by giving more weight to a shared prefix, assuming that discrepancies are more likely to occur at the end of strings. This characteristic makes it highly effective for name comparisons where prefixes are often correct, even in minor errors or variations.

The decision to set a threshold of 0.85 for the Jaro-Winkler comparisons was made to strike a balance between recall and precision. A threshold of 0.85 ensures that only records with a high degree of similarity are considered matches, reducing the likelihood of false positives while still capturing a significant portion of true matches.

Alternative Comparison Methods:

RecordLinkage offers several other comparison methods, each suited for different types of data and comparison needs:

  1. Levenshtein (Edit Distance): Measures the minimum number of edits needed to change one string into the other. This method is useful for longer strings where more complex edits (insertions, deletions, substitutions) are common.
  2. Soundex: A phonetic algorithm for indexing names by sound, as pronounced in English. Soundex is beneficial when dealing with names that might have different spellings but sound similar, aiding in matching records with phonetic similarities.
  3. Cosine Similarity: Useful for comparing longer texts or strings where the overall structure and common terms are more important than the exact sequence of characters. This method computes the cosine of the angle between two vectors in a multi-dimensional space, reflecting the similarity in their text composition.
  4. Damerau-Levenshtein: Similar to Levenshtein but also considers transpositions of two adjacent characters as a single edit. This is particularly useful for typos where characters are accidentally swapped.
  5. Hamming Distance: Measures the difference between two strings of equal length by counting the number of positions at which the corresponding symbols differ. It’s most appropriate for fixed-length strings where the difference is expressed as a simple count of mismatches.

Each of these methods has its applications, depending on the nature of the data and the specific requirements of the entity resolution task. The choice of the Jaro-Winkler method in our context was guided by its effectiveness in handling the variations and errors typically found in first and last names within customer records.

Back to the code, we can append the following to our script.

# Initialize the comparison object
compare = recordlinkage.Compare()

# Add comparison tasks
compare.exact('city', 'city', label='city')
compare.string('first_name', 'first_name', method='jarowinkler', threshold=0.85, label='first_name')
compare.string('last_name', 'last_name', method='jarowinkler', threshold=0.85, label='last_name')

# Execute the comparison
features = compare.compute(candidate_links, df)

In this step, we've meticulously tailored our comparison methods to align with the characteristics of our data, utilizing exact matching for geolocation attributes and the Jaro-Winkler similarity for personal names. This dual approach, underpinned by strategic thresholding, ensures a robust and sensitive mechanism for identifying potential duplicates, laying the groundwork for effective entity resolution.

Step 4: Highlighting the Output

The output of the RecordLinkage process is a DataFrame containing pairs of indices from our original dataset that are likely to represent the same entity based on the criteria we set. Each pair is accompanied by a similarity score for each comparison field, reflecting the degree of match according to the Jaro-Winkler method.

let's examine an output derived from our customer dataset.

 

internal_id first_name last_name city country MAID braze_id shopify_id
1 John Doe New York USA 12345 abcde zyxwv
2 Jon Doe New York USA 12345 fghij zyxwv
3 Jane Smith Los Angeles USA 67890 klmno stuvwx

 


# Classification
matches = features[features.sum(axis=1) > 2] # Assuming a simple threshold
print(matches.head())

After running our RecordLinkage process, we find that records 1 and 2 are identified as a match with the following scores:

  • First Name Similarity: 0.90
  • Last Name Similarity: 1.00
  • City Similarity: 1.00
  • Country Similarity: 1.00

These scores indicate a high likelihood that records 1 and 2 represent the same individual, John Doe, despite the slight variation in the spelling of the first name ("John" vs. "Jon"). The high similarity scores across all compared fields, especially the perfect matches in last_name, city, and country, reinforce this conclusion.

Identifying such matches allows us to take several important actions to enhance the quality of our customer data:

  1. Merging Records: We can merge duplicate records into a single, comprehensive record, ensuring that all customer interactions, preferences, and transaction history are consolidated. This would involve combining the unique identifiers (MAID, braze_id, shopify_id) and any other relevant information from the matching records.
  2. Data Cleaning: The process helps identify and correct inconsistencies in our dataset, such as misspellings or variations in name entries.
  3. Enhanced Customer Insights: By resolving duplicates and creating more accurate customer profiles, we can better understand customer behavior, preferences, and value, leading to more targeted and effective marketing strategies.

Conclusion and Recommendations

Through this implementation, we've streamlined our customer database, enhancing our ability to engage effectively with our clientele. The RecordLinkage package has proven to be an invaluable tool for our entity resolution needs, offering a flexible and efficient approach to identifying and merging duplicate records.

For further exploration, readers are encouraged to delve into the advanced features of the RecordLinkage package, such as machine learning classifiers for match decision-making. Additionally, continuously updating and refining the entity resolution process is crucial as new data becomes available, ensuring ongoing data quality and reliability.

Looking to implement Entity Resolution and don't want to write code or deal with running your script on schedule? Contact us and explore Census Entity Resolution. The Warehouse native Entity Resolution🚀

Related articles

Tutorials
Transforming data Before Syncing with Census Datasets
Transforming data Before Syncing with Census Datasets

The Problem: Your good data is always one request away. Your data team built some great data models in your warehouse; it could be with dbt, or could be plain ol’ SQL — the typical Accounts, Contacts, and Teams golden models. Now you work with another vendor. Maybe a third-party enrichment provider writes open job listings and description data for your warehouse, like Sumble.com. Or perhaps you have an enriched target accounts list generated in another marketing tool.

Product News
Census Datasets: The first step toward collaborative data transformation
Census Datasets: The first step toward collaborative data transformation

Late nights, long hours, and a constant string of tickets and feedback are the reality for most data and IT teams today. As every company’s appetite for data grows, technical teams are forced to scale up support to ensure that the right data lands in the right place. But it doesn’t stop there. Data teams are expected to provide actionable insights, comprehensive data governance, and compliant datasets for their entire organization while juggling new technologies, unclear expectations, and an ever-growing volume of data. Data teams are overwhelmed, business teams are confused and anxious, and everyone is spending more time discussing processes and procedures — and less time innovating.

Product News
Introducing Embedded Reverse ETL Syncs, the future of SaaS integrations
Introducing Embedded Reverse ETL Syncs, the future of SaaS integrations

Looking for a demo? Click here to jump to it on this page.