Model Prediction

To get recommendations for the new user we will define a new recommend function.

Python3




def recommend(user_df, num_recommendations=5):
  
    # Apply SVD to the feature
    # matrix of the user_df dataframe
    svd_matrixs = svd.transform(user_df)
  
    # Calculate the cosine similarity
    # between the user_df and training set users
    cosine_sim_new = cosine_similarity(svd_matrixs, svd_matrix)
  
    # Get the indices of the top
    # num_recommendations similar users
    sim_scores = list(enumerate(cosine_sim_new[0]))
    sim_scores = sorted(sim_scores,
                        key=lambda x: x[1], reverse=True)
    sim_indices = [i[0] for i in
                   sim_scores[1:num_recommendations+1]]
  
    # Return the user_ids of the recommended users
    return tinder_df['username'].iloc[sim_indices]


Next, we will take input from the user and convert it into a dataframe so that we can use this information to make new predictions.

Python3




user_df = {}
  
# Get user input for numerical columns
user_df['age'] = float(input("Enter age: "))
user_df['status'] = float(input("Enter status: "))
user_df['sex'] = float(input("Enter sex \
              (0 for female, 1 for male): "))
user_df['height'] = float(input("Enter \
                height in inches: "))
user_df['smokes'] = float(input("Enter smokes\
                  (0 for no, 1 for yes): "))
user_df['new_languages'] = float(
    input("Enter number of new \
         languages learned: "))
user_df['body_profile'] = float(input("Enter body \
              profile (0-1)"))
user_df['education_level'] = float(input("Enter \
              education level (1-5): "))
user_df['dropped_out'] = float(
    input("Enter dropped out (0 for no, 1 for yes): "))
user_df['bio'] = [input("Enter bio: ")]
user_df['location_preference'] = float(
    input("Enter location preference (0-2): "))
user_df['num_languages'] = float(input("\
               Enter number of languages known: "))
user_df['drinks_encoded'] = float(input("\
               Enter drinks encoded (0-3): "))
user_df['drugs_encoded'] = float(input("\
                  Enter drugs encoded (0-2): "))
  
# Get user input for one-hot encoded categorical columns
user_df['location_new_york'] = float(
    input("Enter location_new_york (0 or 1): "))
user_df['location_northern_california'] = float(
    input("Enter location_northern_california (0 or 1): "))
user_df['location_southern_california'] = float(
    input("Enter location_southern_california (0 or 1): "))
user_df['job_encoded'] = float(input("\
               Enter job encoded (0-9): "))
user_df['pets_0'] = float(input("\
                Enter pets_0 (0 or 1): "))
user_df['pets_1'] = float(input("\
                  Enter pets_1 (0 or 1): "))
user_df['pets_2'] = float(input("\
               Enter pets_2 (0 or 1): "))
user_df['pets_3'] = float(input("\
                  Enter pets_3 (0 or 1): "))
  
# Convert tfidf matrix to DataFrame
tfidf_df = pd.DataFrame(tfidf.transform(
    user_df['bio']).toarray(), columns=feature_names)
  
# Convert the user input
# dictionary to a Pandas DataFrame
user_df = pd.DataFrame(user_df, index=[0])
user_df.drop("bio", axis=1, inplace=True)
user_df = pd.concat([user_df, tfidf_df], axis=1)


Output:

Enter age: 22
Enter status: 1
Enter sex (0 for female, 1 for male): 1
Enter height in inches: 60
Enter smokes 0 for no, 1 for yes): 0
Enter number of new languages learned: 2
Enter body profile (0-1)1
Enter education level (1-5): 4
Enter dropped out (0 for no, 1 for yes): 1
Enter bio: I am a foodie and traveller. But sometimes like to sit alone in a 
corner and read a good fiction.
Enter location preference (0-2): 2
Enter number of languages known: 2
Enter drinks encoded (0-3): 0
Enter drugs encoded (0-2): 0
Enter location_new_york (0 or 1): 0
Enter location_northern_california (0 or 1): 1
Enter location_southern_california (0 or 1): 0
Enter job encoded (0-9): 4
Enter pets_0 (0 or 1): 0
Enter pets_1 (0 or 1): 0
Enter pets_2 (0 or 1): 0
Enter pets_3 (0 or 1): 0

Call function to print the recommended user.

Python3




print(recommend(user_df))


Output:

23      Ronald Millwood
550        Terry Ostrov
1685       Thomas Moran
1044    Travis Pergande
241       Carol Valente
Name: username, dtype: object

This is a very basic content-based recommender system but there are multiple models which are based on Deep Learning and work really well when provided to the real-world dataset.  



Predict Tinder Matches with Machine Learning

In this article, we are going to make a project on the Tinder Match-Making Recommender system. Most social media platform have their own recommender system algorithms. In our project, which works like Tinder, we are going to make a recommender algorithm that recommends profiles to people based on their similar interests so we will aim to predict the profiles to the user such that the user finds it most interesting out of all and tries to connect with them. We are going to make this project from basic and the steps we are going to follow are as:

Similar Reads

Importing Libraries

We will import all the libraries in one place so that we don’t have to import packages every time we use them. This practice will save us time and reserve our memory space....

Exploratory Data Analysis of the Dataset

...

Data Wrangling

...

Data Manipulation

In exploratory data analysis(EDA), we try to gain essential pieces of information from the dataframe. EDA is considered to be one of the time-consuming parts of a data science project about 75% of our work will be in doing EDA of the dataset. However, we will see next that our effort will get justified in the end....

Data Modelling

...

Model Prediction

...

Contact Us