<?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 Andy Yu on Medium]]></title>
        <description><![CDATA[Stories by Andy Yu on Medium]]></description>
        <link>https://medium.com/@andyyu_68202?source=rss-8f6bf0f4e9b2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*psyn6RppL8_NyMdA.</url>
            <title>Stories by Andy Yu on Medium</title>
            <link>https://medium.com/@andyyu_68202?source=rss-8f6bf0f4e9b2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 08:50:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@andyyu_68202/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[Predicting churn for Sparkify (a music streaming service)]]></title>
            <link>https://medium.com/data-science/predicting-churn-for-sparkify-a-music-streaming-service-59f2fd46b3a6?source=rss-8f6bf0f4e9b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/59f2fd46b3a6</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[churn-prediction]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[pyspark]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Andy Yu]]></dc:creator>
            <pubDate>Thu, 14 May 2020 05:03:39 GMT</pubDate>
            <atom:updated>2020-05-20T15:56:03.018Z</atom:updated>
            <content:encoded><![CDATA[<h3>Predict user churn for Sparkify (a digital music service)</h3><h4>Building a churn prediction model with PySpark.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*l6ukY_v43LK9LB9fjV2f0g.png" /><figcaption>Sparkify is a fictional music streaming service created by Udacity</figcaption></figure><p>Sparkify is a fictional digital music service, created by Udacity to simulate real-world companies such as Spotify or Pandora. On Sparkify, users can play songs with free plan or premium subscription plan, which offers advanced functionalities and is ad-free. Users can upgrade, downgrade, or cancel their services at any time. Data is generated whenever a user interacts with the services, including playing songs, adding them to playlists, rating them with a thumbs up or down, adding a friend, logging in or out, upgrade or downgrade, etc. As the rule of thumb that it is more costly to acquire a new customer than to retain an existing one, keeping customers satisfied and identifying those users who are at risk of canceling the services are high priority tasks for the service company.</p><p>Therefore, the purpose of this project is to analyze user activity logs and build a classifier to identify users who are likely to churn — canceled the Sparkify music streaming service. Furthermore, considering reality overwhelming streaming data, I only use a small set of data (98MB) of full Sparkify data (12GB) for data exploration and model development. The final model is built by using Spark so it is scalable to run on a distributed clustering environment.</p><h3>Why does the user churn?</h3><p>In the hypothesis, users have more interaction with fewer frictions should be satisfied and active. Thus, it will be our first idea to look at the data, do churns users behave differently from existing subscribers?</p><p>Before starting to answer the business questions, let’s explore what’s available in the log.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9q6miPCNHsjcaVC9y0XhWQ.png" /><figcaption>Raw data sample</figcaption></figure><p>Here I depict the main features I will use to slice and dice:</p><ul><li><strong><em>userId</em></strong>: user identifier</li><li><strong><em>sessionId</em></strong>: session to which the log belongs to</li><li><strong><em>song</em></strong>: a song played by each user</li><li><strong><em>registration</em></strong>: user registration timestamp</li><li><strong><em>ts</em></strong>: timestamp of a given event</li><li><strong><em>page</em></strong>: The pages visited by a user.</li><li><strong><em>level</em></strong>: free or paid</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*chR_DZVgQ4eGVWPJZSQVdg.png" /><figcaption>Page field shows activities users can do on Sparkify</figcaption></figure><p>Lastly, we need to define <strong>churn</strong> and label churn users accordingly. The churn is defined as users who have click <strong>cancellation confirmation </strong>page.</p><p>Now we are about to slice and dice to see whether churns users engage less than existing users.</p><p>Below we deep dive into certain dimensions to see if there is a difference between churn group and other users.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/385/1*AW-TCBNv6YZ45sbjWP8yWg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/1*Px3Ud-GeTY5G4AQ7vsFSfA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*ZADOvZ343VxIQqFaOkH7MA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/393/1*_GuK9ayDLTNMhJ68BAP9Ag.png" /></figure><p>From above charts, we can summarize:</p><ol><li>Churn users have less engagement on Sparkify.</li><li>Paid user churn more. It might be because the paid services are not good enough, or advanced features cause frustration to users.</li><li>Churn users have fewer social connections (friends), which decreases their loyalty on the platform.</li></ol><h3>Build a machine learning model to detect churn</h3><h4>Features Engineering</h4><p>We prepare the features based on the insights. For detail of the engineering process, please check my <a href="https://github.com/andypwyu/churn_prediction_for_Sparkify/blob/master/Sparkify.ipynb">Github notebook</a>.</p><h4>Create new features</h4><ol><li>Total songs listened</li><li>Number of thumbs up</li><li>Number of thumbs up</li><li>Total time since registration (Total lifetime)</li><li>Average songs played per session</li><li>Number of songs added to playlist</li><li>Total number of friends</li><li>Downgrade (categorical variables)</li></ol><h4>Set target variable</h4><ol><li>Churn</li></ol><h4>Modeling and Evaluation</h4><p>Last, we vectorized all features to a vector, and, most importantly, then standardized to avoid one feature with larger scale dominant the whole model.</p><p>The data set is split to 60% for training and 40% for test. Every model will get Accuracy and F1 score against the test set. However, since the churned users are a fairly small subset, I will use only F1 score as the metric to optimize models.</p><p>Here I explore three models with hyperparameters tuning information below:</p><ul><li>Random Forest: numTrees[10, 20], maxDepth[10, 20]</li><li>Gradient Boosted Trees: maxIter[10, 20], maxDepth[10, 20]</li><li>Support Vector Machine: maxIter[10, 20]</li></ul><p>Performance result ranking:</p><p>1. Random Forest: F1 score 0.746, Accuracy 0.781<br>2. Gradient Boosted Trees: F1 score 0.731, Accuracy 0.734<br>3. Support Vector Machine: F-1 score of 0.685, Accuracy 0.781</p><p>As we optimized based on F1, Random Forest is the best model.</p><h4>Feature Importance from the best model</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*m7IfjtYCcqIAGrBaP3FPmQ.png" /></figure><p>From the chart, we can see total lifetime actually plays a very important role to detect churns. But this may raise a debate that by the fact that those who churned naturally had shorter period of time using the service. Nevertheless, it could also be explained that, as long as users keep active a longer period of time on the platform, they are not easy to churn since they are used to the streaming services. Moreover, total friends matters as we hypothesized users who with more social connections on the platform have higher loyalty. Total songs listened and average songs played per session measure the engagement and activeness of users also help indicate whether the user would churn or not. Downgrade is not effective to predict churn, it might because users still stay in their free account though they are not happy with the paid plan.</p><h3>Conclusion and Future Improvement</h3><p>In this project, I implemented a model to predict customer churn for Sparkify music streaming services. I explore the data set to get some ideas of what features might be useful and create features for the modeling process. Here I evaluate 3 models: Random Forest (RF), Gradient Boosted Trees (GBT), and Support Vector Machine (SVM). The tuned RF model is able to achieve about F1 score 0.746, which still outperforms the baseline model — predicting everyone not churn, with F1 score of 0.685, though its performance is actually inflated due to the imbalance of data set. Based on F1 score, the final model has 9% improvement compared to the baseline model.</p><p>The model performance could be better and more reliable with larger data size since there are only 175 unique users in the small data set. Therefore, the next step will be moving the selected model on cloud and setting up Spark clusters, such as <a href="https://aws.amazon.com/emr/">Amazon EMR</a> or <a href="https://www.ibm.com/cloud/watson-studio">IBM Watson Studio</a>, to process real-life full data set leveraging distributed computing resources.</p><p>The features can also be improved if considering more factors or adding more domain knowledge. It evolves as the company grows its user base and excavates more insights from data.</p><p>For more details about this project, please check my Github available <a href="https://github.com/andypwyu/churn_prediction_for_Sparkify">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59f2fd46b3a6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/predicting-churn-for-sparkify-a-music-streaming-service-59f2fd46b3a6">Predicting churn for Sparkify (a music streaming service)</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Will Customers Buy the Products in their Cart?]]></title>
            <link>https://medium.com/data-science/will-customers-buy-the-products-in-their-cart-b8ac5e30f3?source=rss-8f6bf0f4e9b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/b8ac5e30f3</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[ecommerce]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[predictive-analytics]]></category>
            <dc:creator><![CDATA[Andy Yu]]></dc:creator>
            <pubDate>Mon, 17 Feb 2020 14:48:51 GMT</pubDate>
            <atom:updated>2020-02-20T03:37:05.896Z</atom:updated>
            <content:encoded><![CDATA[<h4>Using XGB Classifier to predict if a customer will eventually make a purchase once they add the item to cart.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WKr5tRzykYjW22XZ.jpeg" /><figcaption>Picture Source: <a href="https://www.thesouthafrican.com/lifestyle/black-friday-deals-in-south-africa-tips-2019/">https://www.thesouthafrican.com/lifestyle/black-friday-deals-in-south-africa-tips-2019/</a></figcaption></figure><p>Understanding what are the key drivers of customers’ buying behavior is always the holy grail for the eCommerce industry. The knowledge can be used to improve the shopping process and eventually result in higher sales and customer satisfaction. In this project, I choose the 2019-November data from <a href="https://www.kaggle.com/mkechinov/ecommerce-behavior-data-from-multi-category-store">Kaggle — eCommerce behavior data from a multi-category store</a> to demonstrate how I analyze and build a basic prediction model through XGBoost.</p><h3>Know your customers</h3><p>Before we devoted ourselves to feature engineering and building models, it’s always good to step back and have an <strong>EDA</strong> (exploratory data analysis). Usually, we will found some useful information helping later process — data preparation, feature engineering, and modeling.</p><p>The data looks like below, for details of each attribute please check out <a href="https://www.kaggle.com/mkechinov/ecommerce-behavior-data-from-multi-category-store">here</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/991/1*ru5RLgH280KLXXRItLi33g.png" /><figcaption>Data source: <a href="https://rees46.com/">REES46 Marketing Platform</a> for Kaggle</figcaption></figure><p>After checking the basic descriptive analysis, there are some questions might be interesting to explore further. Knowing the data set contains time, product information, and price, I am thinking of a few business questions I am able to get an answer from the current data set.</p><ol><li><strong>Daily traffic in November?</strong></li></ol><p>Total customer visits in November is 3,696,117 but it’s unlikely they came to the site evenly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/839/1*LhR8mh8YeTZdNVgpf7661w.png" /></figure><p>We can see a big spike around Nov 16th and 17th, and I guess it would be an on-sell event. This can be further proved if we check the daily price trend for the price of a product (ex: <em>product_id</em> = <em>1003461, Xiaomi </em>or <em>1005115, Apple</em>), and you would always a lower price for the product during the 16th and 17th.</p><p><strong>2. What product categories and brands are the most popular?</strong></p><p>The top 5 categories people view is ‘<em>electronics.smartphone</em>’, ‘<em>electronics.video.tv</em>’, ‘<em>computers.notebook</em>’, ‘<em>electronics.clocks</em>’, and ‘<em>apparel.shoes</em>’, as below treemap. When I check the brand from those purchase event data, the most popular brands are <em>Samsung</em>, <em>Apple</em>, <em>Xiaomi</em>, and other brands of electronics. It’s apparent that the majority of customers come for shopping electronics. Hence, it would be an open strategic question for the managers whether they should concentrate on this particular category instead of being a multi-category store, or would it be beneficial to include other categories? In case this phenomenon can be observed from other month’s data as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/989/1*-uMUoV_MmGqZEHm4oN4__w.png" /><figcaption>Treemap of top 30 categories</figcaption></figure><p><strong>3. Are the customers&#39; purchase journey just like the typical funnel (view =&gt; cart =&gt; purchase)?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*pl6nT1DWhdUdPB39QBYJXQ.png" /></figure><p>While the pie chart shows us only a small portion of people really add the item into the cart but it is roughly 30% (1.36% divided by 4.49%) purchase conversion once the item was put into the cart by customers. Though each step of the conversion funnel can be optimized, in this project, I focus on the cart-to- purchase conversion.</p><h3>Will the customer purchases the products while adding them to the shopping cart?</h3><p>Now I am going to build the prediction model. For this use case, I only use “<em>Cart”</em> and “<em>Purchase”</em> data. Furthermore, I also re-engineer the data structure by introducing a few new features:</p><ul><li>category_code_level1: category</li><li>category_code_level2: sub-category</li><li>event_weekday: weekday of the event</li><li>activity_count: number of activities in that session, including all event type</li><li>is_purchased: whether the item put in the cart is purchased</li></ul><p>With two more features, ‘<em>brand</em>’ and ‘<em>price</em>’, from raw data, it’s good to go. If you are interested in data cleaning and feature engineering process, please check the code <a href="https://github.com/andypwyu/eCommerce-behavior/blob/master/eCommerce%20behavior%20data%20from%20multi%20category%20store.ipynb">here</a>.</p><p><strong>Modeling</strong></p><p>In my analysis, I use XGB classifier. XGBoost is an implementation of gradient boosted decision trees with good performance. Considering the amount of data, I also random downsample the raw data (500,000 records for each class: purchase and not purchase) to avoid the issue of class imbalance. I aim to get a quick look result and then ponder on the next iteration of model improvement and tuning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1013/1*Nv_iqU9hJsuC00YyjQ1xJg.png" /></figure><p>Fbeta shows 0.68 and Recall is 0.74, it’s okay for the first simple model. However, I am more curious about what feature plays an important role to predict purchase.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*aR63lKu1lUE7uFrXyYiyhw.png" /></figure><p>Feature_importance gives us some insight, event_weekday and activity_count seem to dominate the prediction, according to the data set. When reviewing the daily traffic, I can see a few increases during the weekend and holidays. It makes sense that customers spend more time during these days and make purchases. However, it might also because of some implicit reason, such as a promotion, as I mentioned before — People buy on a particular date because of the price rather than their habit. To verify this hypothesis, I need more context for the store. Activity_count is a metric that I count total event records for a particular user session, serving as a proxy of customer engagement. Therefore, interactions between customers and the site really show the significance. It might be because customers usually spending time comparing their items and gathering information to make buying decisions. If in such case, making UI friendly or introducing some features like “the lowest price guarantee”, or “other people also look at” to help customer expedite their buy decision process will eventually drive up the conversion.</p><h3>Conclusion and Future Work</h3><p>Looking at the result, now we know that customer activity on the site is the key driver to decide whether a customer would buy from the analysis, we can reconsider our strategy to further develop our model. For example, to enhance the prediction capability by exploring personal purchase experience, we might need clickstream data for more event type, like which component customer clicks. We might also need to acquire customers’ profile data so that we can cluster the type of customers accordingly, which will help improve the product recommendation system. Since the purchase behavior is very personal, to have data from various facets allows us to better analyze what characteristics and features are important to work on.</p><p>Apart from acquiring more data points, it could be useful to try other classifiers and tuning parameters for the model. To scale this, it would be better to build a training pipeline in which we experiment faster and pick the best performance result for our next model. In my other <a href="https://github.com/andypwyu/finding_donors/blob/master/finding_donors.ipynb">project — Finding donors</a>, I showcase how to build the pipeline so that you try many different classifiers and parameter settings efficiently.</p><p>Once the model is ready to predict customer purchase, it can be used as an actionable marketing tool, in which we have win-back approaches while customers seem to change their minds — the real business value we can drive from the prediction!</p><p>To see more about this analysis, see the link to my Github available <a href="https://github.com/andypwyu/eCommerce-behavior/blob/master/eCommerce%20behavior%20data%20from%20multi%20category%20store.ipynb">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b8ac5e30f3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/will-customers-buy-the-products-in-their-cart-b8ac5e30f3">Will Customers Buy the Products in their Cart?</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>