<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shubham Saxena</title>
    <description>The latest articles on DEV Community by Shubham Saxena (@s9k96).</description>
    <link>https://dev.to/s9k96</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F535076%2F1e7ed473-9255-4e93-9163-3d32bdef237b.jpg</url>
      <title>DEV Community: Shubham Saxena</title>
      <link>https://dev.to/s9k96</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/s9k96"/>
    <language>en</language>
    <item>
      <title>How to Judge your Recommendation System Model ?</title>
      <dc:creator>Shubham Saxena</dc:creator>
      <pubDate>Tue, 09 Feb 2021 16:17:52 +0000</pubDate>
      <link>https://dev.to/s9k96/how-to-judge-your-recommendation-system-model-3p8</link>
      <guid>https://dev.to/s9k96/how-to-judge-your-recommendation-system-model-3p8</guid>
      <description>&lt;p&gt;There are many metrics to judge a classical machine learning model like Accuracy, Precision, Recall, F1 etc, but they cant be directly applied to a recommendation system model. &lt;br&gt;
So in this post I'm writing about the metrics we can use to judge a recommendation system model. &lt;/p&gt;

&lt;h3&gt;
  
  
  Recommended Vs Relevant Items
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Recommended items:&lt;/strong&gt; Items which our model predicted for a given user. 
We can get these items from the model predictions for a given user.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Relevant items:&lt;/strong&gt; Items in which the user is actually interested in. These can be obtained from several methods like:

&lt;ol&gt;
&lt;li&gt;Items on which the user has clicked. &lt;/li&gt;
&lt;li&gt;Items which the user has searched for.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;The goal of a Recommendation model is simply to maximize the intersection between recommended and relevant items for all users.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here are some metrics we can use for checking the performance of our model:&lt;/p&gt;

&lt;h4&gt;
  
  
  Precision @K:
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;How many relevant items are present in top k recommendations?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For eg. P@5 would be calculated by taking top 5 recommendations from the system for each user and checking how many are relevant ones. &lt;br&gt;
If 3 items are relevant then P@5 = 3/5 = 0.6. &lt;/p&gt;

&lt;h4&gt;
  
  
  Average Precision @K:
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;The mean of P@i for i = 1..K&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For eg, AP@3 = SUM(P@1 + P@2 + P@3)/3. This will be calculated for each user. &lt;/p&gt;

&lt;h4&gt;
  
  
  Mean Average Precision @K:
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Mean of AP@K for all users.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For eg, MAP@3 = SUM(AP@1 + AP@2 + AP@3)/3&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;These metrics are straightforward to implement, also can be obtained from &lt;a href="https://github.com/benhamner/Metrics/blob/master/Python/ml_metrics/average_precision.py"&gt;here&lt;/a&gt;.&lt;br&gt;
Happy Learning !&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>todayilearned</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Calculating Run Time of a function using Python Decorators</title>
      <dc:creator>Shubham Saxena</dc:creator>
      <pubDate>Fri, 01 Jan 2021 11:32:49 +0000</pubDate>
      <link>https://dev.to/s9k96/calculating-run-time-of-a-function-using-python-decorators-148o</link>
      <guid>https://dev.to/s9k96/calculating-run-time-of-a-function-using-python-decorators-148o</guid>
      <description>&lt;p&gt;Many times we need to check the running time of a function, so that we can see which functions are taking a lot of time and improve them. &lt;br&gt;
In this post I'm writing about a simple but elegant way to do this using Python Decorators. &lt;/p&gt;

&lt;h3&gt;
  
  
  But, What is a Decorator?
&lt;/h3&gt;

&lt;p&gt;Decorators are a design pattern in python that lets us modify the functionality of an object without changing its structure. Here I'll use this to modify a function to also print the time taken by it. &lt;/p&gt;

&lt;p&gt;Lets see how I'm using this to track function run time: &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Target Function:
&lt;/h3&gt;

&lt;p&gt;Suppose we have a basic function which reads a csv and returns the pd.DataFrame. Now depending on the size of the csv, it can take some time to run.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F103288112-2b35b300-4a0a-11eb-89e1-7d13598487de.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F103288112-2b35b300-4a0a-11eb-89e1-7d13598487de.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Decorator Function:
&lt;/h3&gt;

&lt;p&gt;Here we can define the function &lt;code&gt;calc_runtime&lt;/code&gt; which will act as a decorator for calculating runtime. This function will take a function &lt;code&gt;f&lt;/code&gt; as input and will return an updated version of the same function. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F103288177-54eeda00-4a0a-11eb-859e-16c0ac4fe4d6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F103288177-54eeda00-4a0a-11eb-859e-16c0ac4fe4d6.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Using the Decorator over target function:
&lt;/h3&gt;

&lt;p&gt;As the decorator is ready, we can simply call this over any function to print its runtime. Here we are calling over our target function like this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F103288303-94b5c180-4a0a-11eb-9a07-acc3eeaad4f7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F103288303-94b5c180-4a0a-11eb-9a07-acc3eeaad4f7.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically the &lt;code&gt;load_csv&lt;/code&gt; gets passed on as an argument to the &lt;code&gt;calc_runtime&lt;/code&gt;, there we handle its arguments by using &lt;code&gt;args&lt;/code&gt;&lt;br&gt;
and &lt;code&gt;kwargs&lt;/code&gt; easily. Finally we return the value to the calling space. &lt;/p&gt;

&lt;p&gt;By using this kind of decorators, its straightforward to change the return value of a function, or print some additional layers over a function. &lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>todayilearned</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to? : *args and **kwargs in Python</title>
      <dc:creator>Shubham Saxena</dc:creator>
      <pubDate>Sat, 26 Dec 2020 16:03:52 +0000</pubDate>
      <link>https://dev.to/s9k96/how-to-args-and-kwargs-in-python-nkg</link>
      <guid>https://dev.to/s9k96/how-to-args-and-kwargs-in-python-nkg</guid>
      <description>&lt;p&gt;Python offers several ways to pass on arguments to a function body. &lt;br&gt;
Suppose we are making a function that returns the area of a rectangle. We can write the basic code like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F101327234-6bcf6000-3894-11eb-8757-749a1ffcc788.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F101327234-6bcf6000-3894-11eb-8757-749a1ffcc788.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This given function will calculate area of a given rectangle given its height and width. &lt;/p&gt;

&lt;p&gt;Now, if we want to make this function generic to find hyper volume of a cuboid given its n dimensions, we have two ways to move forward:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Change the function to take n dimensions as arguments: &lt;code&gt;calculate_volume(x1, x2, x3 ... xn)&lt;/code&gt;, this seems really messy and wont even work on n-1 dimensions. &lt;/li&gt;
&lt;li&gt;Change the function to take a list of n dimensions as a single argument:&lt;code&gt;calculate_volume([x1, x2, x3, ... xn])&lt;/code&gt;, this seems much better, although we will have to pass on a list of dimensions which isn't really that nice. &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Understanding *args:
&lt;/h2&gt;

&lt;p&gt;The important part here is the &lt;code&gt;*&lt;/code&gt;, and not the &lt;code&gt;args&lt;/code&gt;. We can use any variable name according to the function use case.&lt;br&gt;
Now let's use this to solve our current problem:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F101330500-af2bcd80-3898-11eb-8a8c-05855d21031e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F101330500-af2bcd80-3898-11eb-8a8c-05855d21031e.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As the &lt;code&gt;dimensions&lt;/code&gt; argument can take any number of arguments, we can now pass several dimensions in it to return the volume. &lt;/p&gt;

&lt;p&gt;We can further improve this logically by using both fixed and extended arguments :&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F101333935-3aa75d80-389d-11eb-8e7b-5d5fdd5c52c4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F101333935-3aa75d80-389d-11eb-8e7b-5d5fdd5c52c4.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, as a valid shape will be having at least one dimension, so we can pass it as &lt;code&gt;first_dimension&lt;/code&gt; and all other dimensions in &lt;code&gt;extra_dimensions&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding **kwargs
&lt;/h2&gt;

&lt;p&gt;This is used for variable length keyword arguments. Use case is a function that where we dont know the arguments or we need to pass on all the arguments to some other function. &lt;br&gt;
It is basically a dictionary that can hold all the keyword arguments together and we can simply iterate over its key-value pair to access them. &lt;br&gt;
Example:&lt;br&gt;
Here I'm making an automatic html generator which generates the tag given all its attributes. We dont need to worry about handling all the keyword arguments, we simply pass them in **tags.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F103154854-9be98f00-47c0-11eb-813a-d0b9fa15f102.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F103154854-9be98f00-47c0-11eb-813a-d0b9fa15f102.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;'&amp;lt; img src = "mountain.jpg" width = "500" height = "600" &amp;gt;'&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Combining everything:
&lt;/h2&gt;

&lt;p&gt;One real life use for these are in arguments forwarding.&lt;br&gt;
Imagine we are working on a big project with several functions, and now we want to monitor the time taken for each function in the code. &lt;br&gt;
Basic way would be to go to function definition and encapsulate it with in &lt;code&gt;start_time&lt;/code&gt; and &lt;code&gt;end_time&lt;/code&gt;, then logging out the difference as time taken. This will require heavy code change and is not advisable at all. &lt;/p&gt;

&lt;p&gt;Another way would be to make a generic trace function that will take any other function as argument and returns the time taken by it. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F101368040-aead2a00-38cc-11eb-9ad2-0e8229505577.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F101368040-aead2a00-38cc-11eb-9ad2-0e8229505577.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the &lt;code&gt;trace&lt;/code&gt; function is generic in nature, it doesn't care about the arguments of &lt;code&gt;f&lt;/code&gt;. We can simply pass them as args and kwargs without changing the function definition. &lt;/p&gt;

&lt;p&gt;For example, here we have a function that calculates the exponent value given 2 numbers (which of course has been modified for this example)&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F101369202-05ffca00-38ce-11eb-8bcf-da6ba31c633f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F13352807%2F101369202-05ffca00-38ce-11eb-8bcf-da6ba31c633f.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although we can do this in a much elegant way, that I'll continue in the next post. &lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>todayilearned</category>
    </item>
  </channel>
</rss>
