How I Built My Own Dating App Algorithm | Towards Data Science

Almost every dating app on the market right now have some form of a dating algorithm. However, these algorithms are usually not publicly available for public scrutiny. Also, the companies behind them probably wouldn’t want their secret formula to be exposed to competitors. But, we can still try and figure out how a potential dating algorithm is made.
In the past, I have delved into this subject numerous times. I even built a simple dating app using machine learning clustering algorithms:
towardsdatascience.com
It was great exercise in using unsupervised machine learning and the resulting dating algorithm was not too bad! It was a great experience but I thought it could use some improvements.
In this article, I won’t be strictly using machine learning but instead will opt for a simpler approach using Data Science tools and some recommendation system techniques. The logic of this algorithm is simple — it will be based on the idea of the wisdom of the crowd. This means the algorithm will find users just like you, aggregate the interactions of those like-minded users, then finally recommend profiles you may like based on the like-minded users’ opinions and interactions.
I’ll walk you through how I created this dating algorithm using mainly Python and Pandas. But before I begin, I have to list several prerequisites and assumptions about this dating algorithm:
This dating algorithm is working under an already established platform. Meaning users and user interactions already exist.
This established platform onboards users with a quick and simple questionnaire. Similar to filling in questions on a dating profile.
The platform works similar to other popular dating apps with right and left swipes indicating likes between profiles.
Straight men and women will be the only users on this platform for now. Other preferences can potentially be added later.
And that’s it! We can now move on to the coding aspect and feel free to code-along if you wish to create your own dating algorithm!
Sign up for a Medium Membership here to gain unlimited access and support content like mine! With your support I earn a small portion of the membership fee. Thanks!
Since our dating algorithm only works with an already established set of data, we’ll need to manufacture that data with random values. We could make more complex datasets that mimic real world dating profiles but that’s not necessary for now.
towardsdatascience.com
First, let’s import the following libraries:
import pandas as pdimport randomimport numpy as np
For this algorithm, there will be 3 data tables or DataFrames. One table for men, one table for women, and one table showing how they interacted. The tables for men and women will contain 5 columns representing the questions and answers from the “profile questionnaire” and their user IDs. The questions and answers will just be represented as a simple “Q1 to Q5” and the answers as “A to E”.
The ratings or interaction table will display 3 different values. These values represent the resulting interactions between an M and W user:
1 represents corresponding right swipes between two users resulting in a successful match.
0 represents a left swipe from either user resulting in no match.
“unseen” represents two users that have yet to see one another.
Now that we have data to play around with, we can continue to the fun part — building the dating algorithm. This dating algorithm will work in the following sequence:
Find an M user that hasn’t yet seen a lot of W profiles.Find other similar M users based on the chosen M user’s dating profile answers.Rank the similar M users from most to least similar.Include only the most similar M users. In our case it will just be the top 10 most similar.Get the top 10’s interactions with all the W users that our chosen M user hasn’t yet seen.Aggregate those interaction values from the top 10 in order to find the cumulative values.Use those cumulative values to predict the potential interaction between the unseen W users and our chosen M user.
Find an M user that hasn’t yet seen a lot of W profiles.
Find other similar M users based on the chosen M user’s dating profile answers.
Rank the similar M users from most to least similar.
Include only the most similar M users. In our case it will just be the top 10 most similar.
Get the top 10’s interactions with all the W users that our chosen M user hasn’t yet seen.
Aggregate those interaction values from the top 10 in order to find the cumulative values.
Use those cumulative values to predict the potential interaction between the unseen W users and our chosen M user.
There are additional steps afterwards such as sorting the cumulative values for each W user but that isn’t too important right now. The above sequence will be the blueprint for our matchmaking algorithm. Also, this sequence can also be applied for W users as well.
Now that we have the blueprint, let’s get to coding. In order to find the M user with the most “unseen” values in the ratings table, run this code:
m_user = ratings.T.apply(pd.Series.value_counts).T.sort_values(    by="unseen",     ascending=False).iloc[0]
Next, we’ll find and save a list of all the W users that this M user has yet to see:
m_nrate = ratings.T[ratings.T[m_user.name]=="unseen"].index
Now we can find all the similar M users by using the variables we created above. But first, we’ll have to do some light data preprocessing. We’ll need to categorize the answers in the Men’s Table so we can use their corresponding numerical values to calculate correlation:
n_men = men.apply(lambda x: x.cat.codes)
Resulting in the transformed DataFrame:
With this table, we can calculate the correlation scores with Pandas.
m_sim = n_men.T.corrwith(    n_men.T[m_user.name]).sort_values(    ascending=False)[1:11]
Running this code will result in the table on the left showing the most similar users to our chosen M user. The most similar users have a correlation score of 1.0 and the least similar users will have a score of 0. By default, Pandas uses the “Pearson” method to calculate correlation.
With the Top 10 similar users known, let’s find their interaction values for each of the W users our chosen M user hasn’t yet seen:
msim_rate = ratings.loc[list(m_sim.index)][m_nrate]
So it appears that even with the top 10 similar users, there are still some “unseen” values. This can be a future issue that can be potentially resolved by adding more similar users than just the top 10. However, for the purposes of this project, we will continue with what we have.
We can now aggregate these values to find what the overall value was for each W user. To do so, there are multiple ways we can calculate the cumulative values. Let’s keep it simple and just find the average, frequency, and median results:
# Man predictionsm_predict = pd.DataFrame()# Replacing the unseen values with NaNs for calculation purposesmsim_rate.replace(    "unseen",     np.nan,     inplace=True)# Averagem_predict['avg'] = msim_rate.mean()# Frequencym_predict['freq'] = msim_rate.mode().T[0]# Medianm_predict['median'] = msim_rate.median()
On the left, you should see the resulting DF of values from the cumulation of the Top 10’s interaction values.
From here, we can choose which calculation best represents the overall sentiment from the top 10 similar users — the average, frequency, or median?
The average appears to be the best because of its wide range of values. We can use this to sort the W users to find which ones were the most consistently liked amongst the top 10. It can also serve as a match/compatibility score for our chosen M user. For example:
Let’s say you were the chosen user. If the value is 0.80, then there is an 80% chance you will like this profile because 8 out of the 10 users most similar to you liked this profile.
With the decision to use the average value, we now have our finished dating algorithm! In this instance, we only applied the algorithm to an existing user. But, how would we apply it to a new user on the platform?
Now that we have laid out the code for our dating algorithm, let’s apply it to a new user! This new user will be onboarded with the same questionnaire as the existing users. So we are basically just adding them to the existing dataset.
I’ve already constructed a function that uses the code and sequence of our recently created dating algorithm:
Given that our new user will be introduced to the platform by answering a few questions, we’ll just use random answers for them. Afterwards, we’ll run the above function and then our new user will be able to see which users they will most likely match with:
ans = ['A', 'B', 'C', 'D', 'E']# Randomly picking answersnew_man_answers = random.choices(ans, k=5)# Running the functionrecs = matchMan(    men,     women,     ratings,     new_man_answers,     num_sim=10)# Finding the top 20 most potentially compatiblerecs[:20]
Running the function above and using the same tables as before, the new user will be presented with these recommended W users sorted by the average score. We can see the score decrease further down the list, which means the algorithm is working.
Now these W users can be shown with the scores to inform the new M user of their potential match compatibility. Or, they can be shown without the scores and the new user will just see these profiles as their top recommended matches. The decision of how these W users are presented is ultimately up to the people in charge of the dating platform.
We have successfully constructed a simple dating algorithm without the use of any machine learning models. However, there are steps we can take to potentially improve this dating algorithm. For instance, we’ll need to validate our results by comparing predicted scores with the actual scores for the existing users. From there we can make changes to improve the algorithm such as including more similar users than just the top 10 or even weighting the interaction values.
I’ve completed some basic validation processes in my code on Github — see below. However, any validation done here will be pointless because our existing datasets are all random.
Another potential issue to be aware of is the scale. What happens when the amount of users on the platform (or even the onboarding questions) increases to a point where the algorithm begins to slow down? How do we compensate for that? These are things that need to be considered as we improve upon this algorithm.
All in all, building this algorithm was a great exercise in coding and utilizing some recommendation system techniques. If you wish to continue improving upon this existing algorithm, feel free to check out my code on Github. I hope you enjoyed this article and check out my other articles if you wish!