Training user data - Content Base Music Recommendation - 2

Table of contents

Hello All, This blog is about how I trained the user data on the bases of the music to listen by the user

Basic idea

We first find the difference between the values of the song and the user values. Then scale it according to the rating (feedback) of the user. Then add them (The answer must be in the range 0-1 or 0-100). Then according we make changes in the user data, hence train the user data.

Let's take an example to understand and then see the code.

We are going to use 3 fields for music (danceability, energy, popularity). Data for the music has been collected from Spotify. Same data for the user have been generated

Example

  • user=[0.799,0.784,33]
  • song=[0.799,0.684,70]

Consider these as the values of (danceability, energy, popularity) given to us, and according we have to update the user data

Let's Start

  • Find the percentage difference in the values of song w.r.t user.

Consider Energy level

0.684 is 87.24% of 0.784 =>-12.76% is the percentage difference in the values

  • We then scale the percentage difference according to the rating provided by the user.

The reason to do the same is to get the positive impact of the song on the user.

In the above example, the percentage difference is -12.76. So if the user rates the song 4/5 which means it has 80% impact on the user hence we multiple -12.76 with 0.8

=>-12.76 x 0.8 = -10.208%

  • Now we change the user values accordingly to the songs listened by the user hence train the data.

    Code

import tensorflow as tf

user=[0.799,0.784,33]
song=[0.799,0.684,70]
variation=[0.0,0.0,0.0]

user_data=tf.constant(user)
song_data=tf.constant(song)
variation_data=tf.constant(variation)
rating=0.8


print(user_data)
percentage_div=tf.divide(song_data,user_data)#find percentage
print(percentage_div)
percentage_div=tf.subtract(percentage_div,1)# (-100% to make 0 if completely match)
print(percentage_div)
variation_data=tf.add(variation_data, percentage_div)
print(variation_data)
variation_data=tf.multiply(variation_data,rating)
print(variation_data)
user_data=tf.add(user_data,variation_data)
print(user_data)
Output
tf.Tensor([ 0.799  0.784 33.   ], shape=(3,), dtype=float32)
tf.Tensor([1.         0.87244904 2.121212  ], shape=(3,), dtype=float32)
tf.Tensor([ 0.         -0.12755096  1.121212  ], shape=(3,), dtype=float32)
tf.Tensor([ 0.         -0.12755096  1.121212  ], shape=(3,), dtype=float32)
tf.Tensor([ 0.         -0.10204077  0.8969696 ], shape=(3,), dtype=float32)
tf.Tensor([ 0.799      0.6819592 33.89697  ], shape=(3,), dtype=float32)

#need to keep variation data in the range not there in this code

Thank you.

Did you find this article valuable?

Support Vinayak tech Blog by becoming a sponsor. Any amount is appreciated!