<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jigar Parmar on Medium]]></title>
        <description><![CDATA[Stories by Jigar Parmar on Medium]]></description>
        <link>https://medium.com/@parmarjigar4?source=rss-1b4461ed3371------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Jigar Parmar on Medium</title>
            <link>https://medium.com/@parmarjigar4?source=rss-1b4461ed3371------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 17:38:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@parmarjigar4/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Kaggle: Instacart Market Basket Analysis]]></title>
            <link>https://parmarjigar4.medium.com/kaggle-instacart-market-basket-analysis-9148608cdf18?source=rss-1b4461ed3371------2</link>
            <guid isPermaLink="false">https://medium.com/p/9148608cdf18</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[kaggle]]></category>
            <dc:creator><![CDATA[Jigar Parmar]]></dc:creator>
            <pubDate>Wed, 18 Nov 2020 09:54:49 GMT</pubDate>
            <atom:updated>2020-11-23T20:23:47.765Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uL4o8ocQK--z0dca8WkY_Q.png" /></figure><h3>Table of Contents</h3><ul><li>Introduction</li><li>Business Problem</li><li>Machine Learning Problem</li><li>Data Explaination</li><li>Exploratory Data Analysis</li><li>Feature Engineering</li><li>Machine Learning Models</li><li>References</li></ul><h3>Introduction</h3><p>Instacart is an American company that operates a grocery delivery and pick-up service in the United States and Canada. The company offers its services via a website and mobile app. The service allows customers to order groceries from participating retailers with the shopping being done by a personal shopper.</p><p>Their service model is like a personal shopper would pick, pack, and deliver the order within the customer’s designated time frame within one hour or up to five days in advance. They also have other premium plans where users can sign up and can get some more privilege on the deliveries.</p><h3>Business Problem</h3><p>There are lots of people who are using this platform for their day-to-day basis for ordering home groceries straight to their door. Unlike any other shop, this platform also has its regular customers who often purchase from this platform.</p><p>The aim insta-cart had in this problem statement was to make it easy for the customer to buy the item which he / she orders frequently in a given period of time, which indeed saves the time of the customer to search for that respective product and re-order it.</p><p>The problem does sound like product recommendation, but it’s different from recommending similar products. Here based on previous purchases we are trying to recommend the products which the user might “buy again” when the user comes to the platform again</p><h3>Machine Learning Problem</h3><p>The data science team of Insta-cart here wants the Machine Learning<br>Engineers or the Researchers to come up with new methods to suggest the<br>probability of the product the user is likely to “buy again”</p><p>The dataset consists of 6 files, each containing different information of the<br>insta-cart. It contains the information about the products, the aisle to which<br>the product is kept, what product was reordered, after how many days did<br>the user come to shop etc.</p><p>The dataset is divided into 3 parts Prior, Train, and Test.</p><p>Prior order contains the information of the previous orders from the users, and the last order from the user is either added in Train or Test set. The number of orders from the user range from 4 to 100, there are almost 50K products and 3M orders from the users</p><p>The dataset is not balanced in terms of ‘reordered’, Neither the number of<br>orders of a given product are the same as others. Here there is also a<br>chance that the customer may order nothing from his / her previously<br>ordered products. So ‘None’, can also be an answer to a user’s next<br>purchase. Thus we should also consider None as a different product along<br>with others.</p><h3>Data Explaination</h3><p>Here we are provided with 6 tables</p><ul><li>Aisles</li><li>Department</li><li>Prior orders</li><li>Train orders</li><li>Orders</li><li>Products</li></ul><p>All the tables are related with each other as displayed below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0c0vK-sbU8OqObWFs7imHw.png" /></figure><p>Explaination of few terms:<br><strong>add_to_cart_order: </strong>while shopping what is the order when product is added.<br><strong>reordered</strong>: if the product was reordered or not.<br><strong>order_dow</strong>: Day of week when the order was made.<br><strong>order_hour_of_day</strong>: Hour of day when the order was made.<br><strong>days_since_prior_order</strong>: After how many days the user came to shop since last order.</p><p>The data contains around 50k products and purchase history of 206209 users.<br>As mentioned above, dataset is divided into 3 parts.<br>Count of Prior orders are 3214874 which will be used to create features. Count of train orders are 131209 and Test orders are 75000.</p><p>The orders made by the user range from 4–100.</p><h3>Exploratory Data Analysis</h3><p>Lets be a Dora and explore the data.</p><p>What time of day people prefer to shop mostly?<br>Answer: Between 10am-4pm</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/539/1*MH_kcJDwo5d4zdIkBpyvMQ.png" /></figure><p>After how many days most user tend to come back for shopping?<br>Answer: Mostly after 7 days or 30 days.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/538/1*phJsI7NlauNov0g6cgx56Q.png" /></figure><p>Which Deparment has most sold products?<br>Answer: Dairy Eggs, Produce</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/1*RcV2M8YZmwJJ5O0-M4D2Dg.png" /></figure><p>What is the basket size when the user checks out?<br>Answer: 4–6</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*pZbQ1XX-znGBXL4SAMmoGA.png" /></figure><p>Check if the the most reordered item is the item which is first kept by the user in the cart</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/485/1*efq7wU0tsvVNFAzsBKgc4A.png" /></figure><p>Which product is reordered mostly every hour?<br>Answer: Banana</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*gHXlkX9ozLSUpAWVUiIx6Q.png" /></figure><p>Which product is reordered every day?<br>Answer: Banana</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*ET4rwuesDgDYXtaQ1_fJfg.png" /></figure><h3>Feature Engineering</h3><p>I created 4 types of features.</p><ol><li><strong>User Features</strong>: What is user like?</li><li><strong>Product Features</strong>: What is the product like?</li><li><strong>User x Product Features</strong>: How does User feel about the product?</li><li><strong>Datetime features</strong>: Day and time of item purchased by the user.</li></ol><h3>User Features</h3><ul><li>How long is the user using instacart for shopping.</li><li>Average days difference of users visit.</li><li>Time of the visit by the user</li><li>Average basket size when the user checksout.</li><li>Distinct products purchased by the user.</li><li>How often the user reorders an item.</li></ul><h3>Product Features</h3><ul><li>How many users purchased this product.</li><li>How often a product is reordered.</li><li>Average position in the cart.</li><li>What is the streak of the product ordered.</li><li>Product’s aisle reorder ratio.</li><li>Product’s Department reorder ratio.</li></ul><h3>User x Product Features</h3><ul><li>How often the user reorderd a particular product.</li><li>After how many days user purchases a particular product.</li><li>Average Day of Week for purchased product.</li><li>Average cart position of the product by thejmk user.</li><li>Mean Hour of the product purchased by the user.</li></ul><p>In all, at the end I had 78 features for each ordered product.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/594/1*eZCDcukI8n50zF173dSvVQ.png" /><figcaption>Feature importance</figcaption></figure><p>To explain some top features:</p><ul><li><strong>user_reorder_ratio</strong>: How often the user reorders products out of all the products purchased till now.</li><li><strong>user_mean_days_prior_order</strong>: Mean of after how many days the user comes to shop.</li><li><strong>user_average_basket: </strong>Size of the basket when user checkouts.</li><li><strong>user_distinct_products:</strong> How many distinct products have been purchased by the user till now.</li><li><strong>prod_user_uniq_reordered: </strong>How many user reordered a particular product.</li><li><strong>user_order_starts_at: </strong>How long the user has been a customer of the insta-cart.</li><li><strong>dep_reordered_ratio: </strong>From what department there is most reordered product.</li><li><strong>up_order_rate: </strong>What is the rate of a product bought by the user. i.e how frequent a product is bought by user.</li></ul><h3>Machine Learning Models</h3><p>For training the model, we already had the train set and test set given by the insta-cart.</p><p>At first I had 92 features, Initially when I loaded all the 92 to XGBoost my threshold based F1Score was 0.28 which was very low So to improve it I decided to get the feature importance which can help me to reduce the dimensionality of the data points. To compute this I performed cross-validation using the LightGBM and XGBoost models. I got better results with LightGBM.</p><p>I printed the feature importance of all the features I had and took 78 features out of that.</p><p>For my first cut approach I tried XGBoost Model by which I reached the on the top 20% of the Leaderboard score on the kaggle.</p><p>Later I also tried the same data with the LightGBM which improved a score by some decimals.</p><p>So, I stacked those models for which I wrote a custom stacking ensemble as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65b7a9d352ff90399b3b51d86b061f94/href">https://medium.com/media/65b7a9d352ff90399b3b51d86b061f94/href</a></iframe><p>Flowchart of how Fit method works</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UmSWB6KudFFC7zQA3LDxLA.png" /></figure><p><strong>Explaination of predict method</strong></p><p>The test data is passed through all the trained models. and addition is done of all the probabilities. At last it is divided by self.loop to get the mean score of the probability.</p><p>This helped me to score 0.40377 on the public Leaderboard score which gives me a rank of 144 out of 2622 thus coming in the top 6% of the leaderboard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/929/1*n0zufbBgVKUHqDft3LwRKA.png" /></figure><h3>References</h3><p>[1] <a href="https://www.kaggle.com/c/instacart-market-basket-analysis/discussion/37016">https://www.kaggle.com/c/instacart-market-basket-analysis/discussion/37016</a></p><p>[2]<a href="https://medium.com/kaggle-blog/instacart-market-basket-analysis-feda2700cded">https://medium.com/kaggle-blog/instacart-market-basket-analysis-feda2700cded</a></p><p>[3]<a href="https://www.kaggle.com/mmueller/f1-score-expectation-maximization-in-o-n">https://www.kaggle.com/mmueller/f1-score-expectation-maximization-in-o-n</a></p><p>[4]<a href="https://www.kaggle.com/c/instacart-market-basket-analysis/discussion/38100">https://www.kaggle.com/c/instacart-market-basket-analysis/discussion/38100</a></p><p>[5] <a href="https://www.appliedaicourse.com/">https://www.appliedaicourse.com</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9148608cdf18" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MetaCost: A cost-sensitive approach for Imbalanced Dataset]]></title>
            <link>https://parmarjigar4.medium.com/metacost-a-cost-sensitive-approach-for-imbalanced-dataset-6f849d8fdf76?source=rss-1b4461ed3371------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f849d8fdf76</guid>
            <category><![CDATA[imbalanced-data]]></category>
            <category><![CDATA[metacost]]></category>
            <category><![CDATA[machinelerning]]></category>
            <dc:creator><![CDATA[Jigar Parmar]]></dc:creator>
            <pubDate>Fri, 22 May 2020 14:59:25 GMT</pubDate>
            <atom:updated>2020-05-22T14:59:25.445Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey Folks, recently I came to problem where I have to make some classification with almost 400k data in which there were almost 400 unique classes ( target variable), the target variable were obviously highly imbalanced.</p><p>There are some methods to handle these imbalanced data, I am mentioning the methods which I tried for my work:</p><ol><li>Using <strong>class_weight=`balanced`</strong> parameter if the classifier supports</li><li>Using <a href="https://imbalanced-learn.readthedocs.io/en/stable/generated/imblearn.over_sampling.SMOTE.html">imblearn’s SMOTE</a>.</li></ol><p>There were some disadvantages of both the approaches, as I had lots of data and highly imbalanced target values, Approach 1 took hell lot of the time, the reason I think is that as I have set the parameter class_weight=balanced, here it tries to balance the data mostly by up-sampling the minority classes and then training and predicting. The disadvantage of SMOTE was it worked well with Binary Classification but not good with Multi-Class Classification and I stated above I had around 400 unique class labels.</p><p>This is when I came across the <a href="https://homes.cs.washington.edu/~pedrod/papers/kdd99.pdf">MetaCost Algorithm</a> which is a cost-sensitive classifier.</p><h3>What is Cost-Sensitive Learning?</h3><p>We have seen in most of the classifier we try to minimize the Error between the predicted label and the actual label but, Cost-Sensitive Learning takes the misclassification costs into consideration. The goal of this type of learning is to minimize the total cost and to pursue a high accuracy of classifying examples into a set of known classes.</p><p>For instance, in binary classification task a confusion matrix has four values FP (False Positive), FN (False Negative), TP (True Positive), TN (True Negative). Here for TP and TN it seems reasonable to not put a cost because the values predicted are correct as per actual values, what values matter are cost for misclassification i.e for FP and FN. Here we represent the Cost <em>C(i, j)</em> where <em>j</em> is the actual class and <em>i</em> is the predicted class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/445/1*ttepnpzPiEVU2oVt--03Hg.png" /></figure><p>These misclassification cost values can be given by domain experts, or learned via other approaches. In cost-sensitive learning, it is usually assume that such a cost matrix is given and known. For multiple classes, the cost matrix<br>can be easily extended by adding more rows and more columns.</p><h3>Deep Understanding of MetaCost algorithm and its working</h3><p>So, you might be wondering how MetaCost helps for Imbalanced Classification.</p><p>Pseudo code of its algo according to its research paper is as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*RlPtAKp-jgB8oA9saq5RtQ.png" /></figure><p>S = Training Set, L = Classifier</p><p>m = number of resamples to generate</p><p>n = number of samples in each resample (n &lt; S)</p><p>p = True if L supports class probabilities</p><p>q = True if all resamples are used for each example</p><p>MetaCost uses a concept of Bagging Ensemble where we take <em>n </em>random data points with replacement and create a subset of the sample and fit this resampled data into a model M, we repeat this <em>m </em>times. Thus, at the end we will have <em>m</em> number of Models where Mᵢ model correspond to Sᵢ sub-sample.</p><p>Here now we loop over the entire Training set. Now check if <em>q </em>is True. if yes, it means we take all the <em>m </em>trained models else we take only those models in which the <em>x </em>query point is not present in Sᵢ Sample. For instance out of 30 samples lets say the <em>x </em>data point was not present in S¹, S², s²¹, S¹⁷ sub samples then we only take the models corresponding to these sub-samples.</p><p>After getting the models on the basis of the <em>q </em>we now compute the the probabilities if the classifier supports, meaning if the Classifier is able to give the probability values of a query point <em>x </em>belonging to each class. so if the value of p=True, we calculate the probability of point <em>x </em>given the <em>m </em>models else we just put 1 for the predicted class.</p><p>Once we get the probability of query point <em>x </em>by all the <em>m </em>models we multiply those probability values with the cost matrix we have and take the minimum value out of it. Here as the cost of misclassification will be high, and we are taking the minimum of the dot product, the chances are less for the rarer class to be classified.</p><p>Thus here there are less chances for the rarest model to be misclassified due to cost-matrix we have, because here we are trying to minimise the cost of misclassification.</p><p>At the end of this loop as we have done all the things for all of the data in training points we would have a relabelled data for the same training set.</p><p>Now we take L classifier and S training set with new labelled data and train the classifier and return that trained Model.</p><p>I hope this helps you to get the basic idea of how MetaCost works and how the cost-sensitive learning helps to penalise the mis-classification of the rarest class in the imbalanced data.</p><h4>Reference and other sources</h4><p>[1] <a href="https://homes.cs.washington.edu/~pedrod/papers/kdd99.pdf">https://homes.cs.washington.edu/~pedrod/papers/kdd99.pdf</a></p><p>[2] <a href="https://cling.csd.uwo.ca/papers/cost_sensitive.pdf">https://cling.csd.uwo.ca/papers/cost_sensitive.pdf</a></p><p>[3] <a href="https://medium.com/rv-data/how-to-do-cost-sensitive-learning-61848bf4f5e7">https://medium.com/rv-data/how-to-do-cost-sensitive-learning-61848bf4f5e7</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f849d8fdf76" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>