<?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 Hakan Okay on Medium]]></title>
        <description><![CDATA[Stories by Hakan Okay on Medium]]></description>
        <link>https://medium.com/@hokay18?source=rss-daa09b78a176------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*FIRZfQHlf8khSBZmCaj0Yg.jpeg</url>
            <title>Stories by Hakan Okay on Medium</title>
            <link>https://medium.com/@hokay18?source=rss-daa09b78a176------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 16:45:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hokay18/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[RFM and CLTV to Know Your Customers Better]]></title>
            <link>https://medium.com/@hokay18/using-rfm-and-cltv-for-customer-segmentation-dd16084c66df?source=rss-daa09b78a176------2</link>
            <guid isPermaLink="false">https://medium.com/p/dd16084c66df</guid>
            <category><![CDATA[rfm-analysis]]></category>
            <category><![CDATA[crm]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[cltv-analysis]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Hakan Okay]]></dc:creator>
            <pubDate>Sun, 02 Jan 2022 10:46:48 GMT</pubDate>
            <atom:updated>2022-01-04T11:22:32.252Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IbupPOhoiFSllVcV-pAmng.png" /><figcaption>Source: Author</figcaption></figure><p>RFM and CLTV are two methods commonly utilized to analyze customer value. CLTV or CLV represents the amount of money a customer is expected to spend in your business during their lifetime and can be used to optimize your marketing efforts. RFM is commonly used for segmenting marketing strategies for different segments. CLTV helps you determine how much money you should spend acquiring new customers and retaining existing ones.</p><p>This article will walk you through how to use Pandas and Lifetimes modules to perform RFM and CLTV analyses in Python. The main focus will be CLTV, and we will create and add our RFM segments to our data so we can interpret our analysis of CLTV more effectively.</p><h3>Let’s take a look at our data</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*PWf2x-JH4_NsH82T" /><figcaption>Photo by <a href="https://unsplash.com/@egnaro?utm_source=medium&amp;utm_medium=referral">Rick Mason</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>For today’s analysis, we will use Online Retail 2 dataset. This data set contains all of the transactions recorded for an online retailer based and registered in the UK between 2009–12–01 and 2011–12–09. The retailer specializes in all-occasion gift items. Most of the retailer’s customers are wholesalers. The dataset has the following attributes:</p><ul><li>InvoiceNo: Invoice number. Nominal. A 6-digit integral number uniquely assigned to each transaction. If this code starts with the letter ‘C’, it indicates a cancellation.</li><li>StockCode: Product (item) code. Nominal. A 5-digit integral number uniquely assigned to each distinct product.</li><li>Description: Product (item) name. Nominal.</li><li>Quantity: The quantities of each product (item) per transaction. Numeric.</li><li>InvoiceDate: Invoice date and time. Numeric. The day and time when a transaction was generated.</li><li>UnitPrice: Unit price. Numeric. Product price per unit in sterling (£).</li><li>CustomerID: Customer number. Nominal. A 5-digit integral number uniquely assigned to each customer.</li><li>Country: Country name. Nominal. The name of the country where a customer resides.</li></ul><p>We will focus on purchases from UK customers between 2010–2011. Findings from Exploratory Data Analysis are as follows:</p><ul><li>· There are <strong>485123 </strong>observation units, 8 attributes.</li><li>· There are <strong>130802 </strong>null values in Customer ID.</li><li>· There are <strong>3844 </strong>unique products.</li><li>· There are <strong>9288 </strong>returns.</li><li>· There are a lot of outliers and negative values in <strong>Quantity </strong>and <strong>Price </strong>columns.</li><li>· Most seen product in data set <em>White Hanging Heart T-Light Holder.</em></li><li>· The item that generated the most revenue is <em>Paper Craft, Little Birdie.</em></li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdatapane.com%2Fu%2Fhokay%2Freports%2FVkGyql3%2Fdf-raw%2Fembed%2F&amp;display_name=Datapane&amp;url=https%3A%2F%2Fdatapane.com%2Fu%2Fhokay%2Freports%2FVkGyql3%2Fdf-raw%2F&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fdatapane-files-prod%2Fdp%2Fthumbnails%2F1e455fbb-937e-4ad2-bf7d-1f0dabbb3edb.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=datapane" width="960" height="700" frameborder="0" scrolling="no"><a href="https://medium.com/media/977c00fb0bfbb349ccbe74ea3b9bcbf4/href">https://medium.com/media/977c00fb0bfbb349ccbe74ea3b9bcbf4/href</a></iframe><h3>Preparing the data for analysis</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_uIqcxSQUHgjp_fq" /><figcaption>Photo by <a href="https://unsplash.com/@markuswinkler?utm_source=medium&amp;utm_medium=referral">Markus Winkler</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>First, we drop null values on CustomerID. For outliers, since there are a lot of them, we don’t want to just drop all of them. Instead, we replace our outliers with ±1.5*IQR. Then we create our TotalPrice column which we will use to calculate our Monetary.</p><p>As stated in Lifetimes module documentation, we have to put our data in a certain shape which consists of 4 columns and we will use CustomerIDas our index.</p><ul><li>Frequency: Number of repeat purchases the customer has made.</li><li>T: Customer’s first purchase and the end of the period under study. We will use weekly age on our analysis.</li><li>Recency: The duration between a customer’s first purchase and their latest purchase.</li><li>Monetary: Average value of a given customer’s purchases.</li></ul><p>The last InvoiceDate available in the dataset is 2011–12–09. We have to select a proper analysis date since there have been 10 years since the last entry and then start preparing our columns. We will calculate customer lifetime(tenure) on a weekly scale that’s why we are dividing our recency and T columns by 7.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63ef4abc53a47117c1ebc9fd132d909d/href">https://medium.com/media/63ef4abc53a47117c1ebc9fd132d909d/href</a></iframe><p>After cleaning and preparation this is how our data looks like:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdatapane.com%2Fu%2Fhokay%2Freports%2F9ArDxM7%2Fcltv-df%2Fembed%2F&amp;display_name=Datapane&amp;url=https%3A%2F%2Fdatapane.com%2Fu%2Fhokay%2Freports%2F9ArDxM7%2Fcltv-df%2F&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fdatapane-files-prod%2Fdp%2Fthumbnails%2F152ea5a5-e060-44c9-855d-05f62bcf1742.png&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=datapane" width="960" height="700" frameborder="0" scrolling="no"><a href="https://medium.com/media/8dfebc275d1fc947c6e5589bde538ff1/href">https://medium.com/media/8dfebc275d1fc947c6e5589bde538ff1/href</a></iframe><h3>Theory and Modelling</h3><p>Before we dive into modelling, I want to talk about the theory behind our models. As stated before CLTV is the monetary value that a customer will bring to the company during the relationship and communication with a company. The most basic definition would be:</p><pre>CTLV = Number of Transactions * Average Order Value </pre><p>Since we are trying to predict our CLTV using probability with time projection we have to use 2 different models. The first one will be the Beta Geometric / Negative Binomial distribution model (BG/NBD) which will calculate the <strong>Expected Number of Transactions</strong>. And the second one will be Gamma Gamma Submodel which will calculate the <strong>Average Order Value</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2RQ-VHe4z5BcTDKbg87nIg.png" /><figcaption>Source: Author</figcaption></figure><p>For modelling, we first fit our Frequency, Recency and T columns to BG/NBD model and Frequency, Monetary columns to our Gamma Gamma Submodel. Then we call the customer_lifetime_valuefunction on our Gamma model with BG/NBD as the first parameter. We also state our prediction length in months using time parameter. We state the frequency we used to calculate our Tenure which in this case is ‘W’ for weekly. After calculation, the values are still hard to interpret so we should put them on a scale. Using MinMaxScaler from the scikit-learn module we put our CLTV predictions on a 0 to 1 scale.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c40b3a671b21a153fdec919277bd6378/href">https://medium.com/media/c40b3a671b21a153fdec919277bd6378/href</a></iframe><p>This is how our data looks like after calculating and scaling CLTV:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdatapane.com%2Fu%2Fhokay%2Freports%2FBAmpowk%2Fcltv-final%2Fembed%2F&amp;display_name=Datapane&amp;url=https%3A%2F%2Fdatapane.com%2Fu%2Fhokay%2Freports%2FBAmpowk%2Fcltv-final%2F&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fdatapane-files-prod%2Fdp%2Fthumbnails%2Fa422681b-7a45-40c5-b49c-89845837e98f.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=datapane" width="960" height="700" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e6a80868d321657de22b4f7b277918e/href">https://medium.com/media/5e6a80868d321657de22b4f7b277918e/href</a></iframe><h3>RFM (Recency, Frequency, Monetary)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eNBQb-aXDD4zcf6D" /><figcaption>Photo by <a href="https://unsplash.com/@amyshamblen?utm_source=medium&amp;utm_medium=referral">Amy Shamblen</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>For better interpretation, we will add RFM segments to our CLTV table. This way we can compare our results from both methods. Let’s first create our RFM metrics, scores and segments.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c798e8eafd7c7e17f0b86e4187ff2b0/href">https://medium.com/media/2c798e8eafd7c7e17f0b86e4187ff2b0/href</a></iframe><p>This is how our RFM results look like:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdatapane.com%2Fu%2Fhokay%2Freports%2FJ35lD0A%2Frfm%2Fembed%2F&amp;display_name=Datapane&amp;url=https%3A%2F%2Fdatapane.com%2Fu%2Fhokay%2Freports%2FJ35lD0A%2Frfm%2F&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fdatapane-files-prod%2Fdp%2Fthumbnails%2F5e5cba3b-8ed8-47b5-bc28-2baec8718191.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=datapane" width="960" height="700" frameborder="0" scrolling="no"><a href="https://medium.com/media/833aec0647f36b57221a2eb9ec05b50a/href">https://medium.com/media/833aec0647f36b57221a2eb9ec05b50a/href</a></iframe><p>And this is the distribution of our RFM segments:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8wRNfH25tgnEmukq1hv-tQ.png" /><figcaption>Source: Author</figcaption></figure><h3>Bringing results together</h3><p>Adding our RFM segments to our CLTV will give us a more solid foundation for our interpretation. We will also add 1 month and 12 months projections to our table which will lead us to see some interesting findings of the effect of monetary on CLTV modelling.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fb06b7557c2cac0e16bfd648bb5a754/href">https://medium.com/media/7fb06b7557c2cac0e16bfd648bb5a754/href</a></iframe><p>This is how our data looks like after adding RFM segments, 1-month CLTV and 12-months CLTV projections:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdatapane.com%2Fu%2Fhokay%2Freports%2FE7PXrY7%2Ffinal-triple%2Fembed%2F&amp;display_name=Datapane&amp;url=https%3A%2F%2Fdatapane.com%2Fu%2Fhokay%2Freports%2FE7PXrY7%2Ffinal-triple%2F&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fdatapane-files-prod%2Fdp%2Fthumbnails%2F4dfe5ccd-53e3-487b-a9bc-dd5c20d988dc.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=datapane" width="960" height="700" frameborder="0" scrolling="no"><a href="https://medium.com/media/e53ddc008a859008094ad34d544f6baf/href">https://medium.com/media/e53ddc008a859008094ad34d544f6baf/href</a></iframe><h3>CLTV Segmentation</h3><p>Since we try to create better segmentation and marketing strategies based on RFM and CLTV we will also create segments based on our CLTV. ‘A’ for our best customers and ‘D’ for the worst.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d68e2943454fd71f61a2b9a94f09ada/href">https://medium.com/media/5d68e2943454fd71f61a2b9a94f09ada/href</a></iframe><p>Our final table looks like this:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdatapane.com%2Fu%2Fhokay%2Freports%2FaAMZX63%2Ffinal-segmented%2Fembed%2F&amp;display_name=Datapane&amp;url=https%3A%2F%2Fdatapane.com%2Fu%2Fhokay%2Freports%2FaAMZX63%2Ffinal-segmented%2F&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fdatapane-files-prod%2Fdp%2Fthumbnails%2F51488a00-86ac-42b6-91e3-77919ad8fb96.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=datapane" width="960" height="700" frameborder="0" scrolling="no"><a href="https://medium.com/media/ade55c5a82f093792d993047de01da51/href">https://medium.com/media/ade55c5a82f093792d993047de01da51/href</a></iframe><h3>Discussion</h3><p>I will discuss and interpret the results based on Pareto Principle. The <strong>Pareto principle</strong> states that for many outcomes, roughly 80% of consequences come from 20% of causes. In our case, we expect to see 80% of our income generated from our top 20% of customers. If we take a look at our segments from both analyses:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fdatapane.com%2Fu%2Fhokay%2Freports%2FO7vZKl7%2Ftop20rfmclv%2Fembed%2F&amp;display_name=Datapane&amp;url=https%3A%2F%2Fdatapane.com%2Fu%2Fhokay%2Freports%2FO7vZKl7%2Ftop20rfmclv%2F&amp;image=https%3A%2F%2Fstorage.googleapis.com%2Fdatapane-files-prod%2Fdp%2Fthumbnails%2Ff9062353-a467-4445-be79-790eec70ffc6.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=datapane" width="960" height="700" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed06cc272a5374ccb44b6efc8576c2c5/href">https://medium.com/media/ed06cc272a5374ccb44b6efc8576c2c5/href</a></iframe><p>Most of our Champions, Loyal Customers and Potential Loyalists belong to A segment from our CLTV segmentation which makes sense. And if we look at how much value they brought to the business we can see Champions, Loyal Customers are generating roughly %80 of our income. For our top customers in these segments I would recommend the following:</p><ul><li>Organize loyalty programs.</li><li>Advertise Limited Edition products.</li><li>Provide special discounts.</li><li>Tune recommendation systems.</li><li>Pay attention to feedbacks of these groups.</li><li>Provide special discounts and free shipments etc.</li></ul><h3>Caveats</h3><p>Some important notes should be taken when using CLTV.</p><ul><li>The calculation represents a single period of time in which you performed the analysis.</li><li>The churn rate comes from the whole audience, not personal.</li><li>The profit comes from the whole audience, not personal.</li><li>Total price is the dominant factor and crushes the frequency effect. It means if 2 customers brought the same amount, the frequency of their purchases doesn’t matter. This effect loses its dominance for predictions made for longer time periods. This effect can be seen on customers 16000 and 15601. If we look at 1-month predictions customer 16000 has higher CLTV than 15601 because of its monetary. But high frequency starts to show its effect on the longer-term and results in a higher CLTV for customer 15601 in the long run.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5bc09f7d4346ade4b128ef3e9c735bd1/href">https://medium.com/media/5bc09f7d4346ade4b128ef3e9c735bd1/href</a></iframe><p>And since this is a model, there should be some metrics to evaluate the performance. To create Frequency/Recency matrix which computes the expected number of transactions an artificial customer is to make in the next time period, given her recency and frequency:</p><pre><strong>from</strong> <strong>lifetimes.plotting</strong> <strong>import</strong> plot_frequency_recency_matrix<br>plot_frequency_recency_matrix(bgf)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*leGz40QzsOUPhd1E9XoYEw.png" /><figcaption>Source: lifetimes.readthedocs.io</figcaption></figure><p>And to compare your data versus artificial data simulated with your fitted model’s parameters:</p><pre><strong>from</strong> <strong>lifetimes.plotting</strong> <strong>import</strong> plot_period_transactions<br>plot_period_transactions(bgf)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*ZG0X4n4X_S75jdm8BMZNnQ.png" /><figcaption>Source: lifetimes.readthedocs.io</figcaption></figure><h3>Conclusion</h3><p>CLTV helps you determine how much money you can afford to spend acquiring new customers and retaining existing ones and RFM can be used to segment your customers to better target your marketing efforts. All the efforts to perform these analyses have only one goal which is to make better business decisions based on data. Using CLTV and RFM simultaneously and interpreting data based on both analyses can help businesses grow.</p><p>Performing these analyses using Python and Lifetimes modules is not the most important and complicated part. Knowing your business, having domain knowledge and using that knowledge to produce useful results from these analyses is the key to a business’s success.</p><h4>References</h4><ul><li><a href="https://lifetimes.readthedocs.io/en/latest/index.html">Measuring users is hard. Lifetimes makes it easy. - lifetimes 0.11.2 documentation</a></li><li><a href="https://www.linkedin.com/company/veribilimiokulu/mycompany/">Veri Bilimi Okulu | LinkedIn</a></li><li><a href="https://www.linkedin.com/company/miuul/">Miuul | LinkedIn</a></li><li><a href="https://github.com/h-okay/Projects/tree/master/CRM%20Analytics/CLTV">Projects/CRM Analytics/CLTV at master · h-okay/Projects</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd16084c66df" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[You Don’t Need Machine Learning for Eveything: Rule-Based Classification]]></title>
            <link>https://python.plainenglish.io/rule-based-classification-for-customer-segmentation-and-income-estimation-with-python-159744b1a3ce?source=rss-daa09b78a176------2</link>
            <guid isPermaLink="false">https://medium.com/p/159744b1a3ce</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[streamlit]]></category>
            <category><![CDATA[pandas]]></category>
            <category><![CDATA[data-analysis]]></category>
            <dc:creator><![CDATA[Hakan Okay]]></dc:creator>
            <pubDate>Mon, 20 Dec 2021 15:14:51 GMT</pubDate>
            <atom:updated>2021-12-28T08:35:01.777Z</atom:updated>
            <content:encoded><![CDATA[<h3>You Don’t Need Machine Learning for Everything: Rule-Based Classification</h3><h4>Customer Segmentation and Income Estimation</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JAJ-BCYLtqcQQ4aMdzAq5g.gif" /><figcaption>Customer segmentation</figcaption></figure><p>Customer segmentation is the process by which you divide your customers up based on <strong>common characteristics</strong>. This allows marketers to <strong>engage </strong>with each customer in the most effective way or make an <strong>estimation </strong>for their new customers based on previous data.</p><p>There’s one more term we’re going to use a lot today and that’s <strong>persona</strong>. A persona is a fictional character created to represent a user type that might use a site, brand, or product similarly.</p><h3>Segmentation Workflow</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P1okvWO88FvEiiJ_tQzksw.gif" /><figcaption>Segmentation workflow</figcaption></figure><p>Let’s say you run a mobile app business. You can collect user data such as which <strong>country </strong>users are from, what <strong>device </strong>they are using, their <strong>gender</strong>, age,<strong> </strong>and how much <strong>money </strong>they spend using your app. After you’ve had a good amount of data, you can process this data and create <strong>segmentations </strong>and <strong>personas </strong>based on your previous or current users. After this point, you can use this knowledge to get a fast <strong>estimation </strong>about your new users, how they are likely to behave and use your application. You can use different marketing for your product based on your user’s segmentation to increase your profits.</p><h3>Creating Personas</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BAzxx-iIShL18av5E6YO0Q.gif" /><figcaption>Data characteristics and data manipulation</figcaption></figure><p>To be able to create accurate personas collecting the data is not enough. First, we have to process the data to a form that holds information in categories. And since we are collecting user data based on their purchases, there may be duplicates in our data. The same customer can make 2 purchases and appear as 2 different customers.</p><h3>Exploring and Processing the Data</h3><p>Let’s go through exploring and processing the data step by step. Let’s start with <strong>exploratory data analysis(EDA)</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/776b9c8b489c307382f4ba141e41d274/href">https://medium.com/media/776b9c8b489c307382f4ba141e41d274/href</a></iframe><p>EDA gave us a quick overview of our data can be seen below.</p><ul><li>There are <strong>2</strong> unique sources, <strong>6 </strong>unique prices, and <strong>6</strong> unique countries available in the dataset.</li><li>The minimum age is <strong>15</strong> while the maximum is <strong>66</strong>.</li><li><strong>Brazil </strong>is the country with the most<strong> money spent </strong>while <strong>Turkey </strong>has the highest money spent <strong>per person</strong>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3oWFaOeGdHugkR34M-CCwQ.gif" /><figcaption>Exploratory data analysis</figcaption></figure><p>Based on our EDA we can create an <strong>aggregated </strong>dataframe, sort the values <strong>by price in descending</strong> order and reset the dataframe’s index to fix possible <strong>level issues</strong> later on. We will call this dataframe <strong>agg_df</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac7ba6e5f279df75107332687c342000/href">https://medium.com/media/ac7ba6e5f279df75107332687c342000/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vwd3z40EWOBrJYQ8YzXRsQ.gif" /><figcaption>Aggregated dataframe</figcaption></figure><p>This aggregated dataframe still <strong>doesn’t hold information in categories</strong> and can’t be generalized and used for estimation. It still suffers from the same problem we discussed earlier. Unless we don’t put in the <strong>exact age</strong> available in the dataset we won’t get an estimate. To fix this do the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f72567880a0e8c4ca719dbab96c41f90/href">https://medium.com/media/f72567880a0e8c4ca719dbab96c41f90/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wy-yUUU9AkT-82h0BtsK4A.gif" /><figcaption>Age categories</figcaption></figure><p>Now we will put the information in our columns in an easily <strong>reproducible </strong>format.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae66123d642e5546aada8a7716597ab0/href">https://medium.com/media/ae66123d642e5546aada8a7716597ab0/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QL7AC2scFG6oOjYP4bUyTQ.gif" /><figcaption>Concatenated columns</figcaption></figure><p>After grouping up and concatenating our columns there’s still a major problem. Since we hold information in categories called <strong>personas</strong>, there will be duplicates.</p><p>For example, there will be a <strong>FRA_ANDROID_FEMALE_24_30 </strong>persona for every entry available between ages 24 to 30. To fix this we group by <strong>customers_level_based </strong>column and get the <strong>mean </strong>price for the category.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/818b4db5ad57c8a8135b7a23f992ec86/href">https://medium.com/media/818b4db5ad57c8a8135b7a23f992ec86/href</a></iframe><p>Since we <strong>created our personas</strong> and <strong>removed duplicates</strong>, now we can create our <strong>segments</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87334504fdebd39f45724a101aad65a2/href">https://medium.com/media/87334504fdebd39f45724a101aad65a2/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K7j9eFGZKQmhqnDNDZy-zA.gif" /><figcaption>Creating segments</figcaption></figure><p>Now we have ourselves <strong>rule-based classified data</strong> that we can use to get quick about our new user’s estimated spending and segment information.</p><h3><strong>Deployment</strong></h3><p>We have a couple of options from here on how we want to deploy this data to be used easily. We can create a simple <strong>user interface</strong> or we can create a <strong>python script</strong> to run on the command line or we can deploy this data in the format of a <strong>web application</strong>. I preferred going for a web application because of its accessibility and easy-to-use. I used <strong>Streamlit </strong>to deploy and host the project. You can check it out here:</p><p><a href="https://share.streamlit.io/h-okay/level_based/index.py">https://share.streamlit.io/h-okay/level_based/index.py</a></p><p>Thank you for reading. I hope you enjoyed it!<br>Feel free to <strong>contact me</strong> for any feedback.</p><p><strong>I </strong>❤️<strong> VBO</strong></p><p>References:</p><ul><li><a href="https://www.linkedin.com/company/veribilimiokulu/mycompany/">Veri Bilimi Okulu | LinkedIn</a></li><li><a href="https://www.linkedin.com/company/miuul/">Miuul | LinkedIn</a></li></ul><p><em>More content at </em><a href="http://plainenglish.io/"><em>plainenglish.io</em></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><em>free weekly newsletter</em></a><em>. Get exclusive access to writing opportunities and advice in our </em><a href="https://discord.gg/GtDtUAvyhW"><em>community Discord</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=159744b1a3ce" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/rule-based-classification-for-customer-segmentation-and-income-estimation-with-python-159744b1a3ce">You Don’t Need Machine Learning for Eveything: Rule-Based Classification</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[List Comprehensions in Python]]></title>
            <link>https://python.plainenglish.io/list-comprehensions-in-python-ab6dd4a54b57?source=rss-daa09b78a176------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab6dd4a54b57</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[comprehension]]></category>
            <category><![CDATA[python3]]></category>
            <dc:creator><![CDATA[Hakan Okay]]></dc:creator>
            <pubDate>Thu, 16 Dec 2021 13:26:19 GMT</pubDate>
            <atom:updated>2021-12-22T07:31:16.135Z</atom:updated>
            <content:encoded><![CDATA[<h4>What are list comprehensions and how to use them</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TdAns3x9QkDOyOXa" /><figcaption>Photo by <a href="https://unsplash.com/@jstrippa?utm_source=medium&amp;utm_medium=referral">James Harrison</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>List comprehensions are an elegant way to define and create lists based on existing lists. It might seem like this same output can be achieved using a simple for loop. Then why would we want to use a list comprehension?</p><ul><li>List comprehensions are generally more compact and faster than normal functions and loops for creating a list.</li><li>They are easier to read and debug if they are properly used. We should avoid writing very long list comprehensions in one line to ensure that code is user-friendly.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3rwoCZbwVy5lGwOpXNXi-Q.gif" /><figcaption>Comparison of creating a new list with regular for loop vs. list comprehension</figcaption></figure><p>Now let’s dive into how we can use them. I color-coded the important parts to make it easier to see how we can get the same result as a regular <em>for</em> loop using list comprehensions.</p><ul><li>Blue: The output you want to have in your list</li><li>Yellow: <em>for</em> loops to go through every element in iterative objects.</li><li>Red: <em>if</em> statements.</li><li>Green: <em>else</em> statements.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ReyJ6f3OWc70zQJ7zPlR7g.gif" /><figcaption>Basic list comprehension usage</figcaption></figure><pre>new_salaries = []<br>for salary in salaries:<br>    new_salaries.append(salary)</pre><pre>################################</pre><pre>new_salaries = [salary for salary in salaries]</pre><p>The two expressions above will give us the same result. It will go through every element in the salaries list and add them to a new list called new_salaries. Using list comprehensions on this example allowed us to accomplish the same task writing only a one-line, easily-readable code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ii3IJkaIQMdI9FKRnNyoLQ.gif" /><figcaption>List comprehension with if condition</figcaption></figure><pre>new_salaries = []<br>for salary in salaries:<br>    if salary &gt;= 3000:<br>        new_salaries.append(salary)</pre><pre>################################</pre><pre>new_salaries = [salary for salary in salaries if salary &gt;= 3000]</pre><p>To use conditional logic with list comprehensions there are some differences in syntax depending on what we want to accomplish. If we are going to use an <em>if</em> condition only, we can add the expression just after the loop part of the comprehension. In this example, we are adding salaries equal to or higher than 3000 to our new list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cOW0CgpOj9SQNfOVCi6n5w.gif" /><figcaption>List comprehension with if and else conditions</figcaption></figure><pre>new_salaries = []<br>for salary in salaries:<br>    if salary &gt;= 3000:<br>        new_salaries.append(salary)<br>    else:<br>        new_salaries.append(salary + 500)</pre><pre>################################</pre><pre>new_salaries = [salary if salary &gt;= 3000 else salary + 500 for salary in salaries]</pre><p>If we want to use an else statement in our comprehension, the syntax differs a bit. In this situation, we first state the output we want from our if statement, followed by the statement itself, then we add our else statement followed by the output we want to get from our else statement. And lastly, we add our for loop to the end of our list. In this example, we leave salaries equal to or higher than 3000 just as they are, and we’re giving a 500 raise to the rest.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZLiUU4wKfmeMfp8k7wPbTw.gif" /><figcaption>List comprehension with outer loops</figcaption></figure><pre>new_salaries = []<br>for department in departments:<br>    for salary in department:<br>        if salary &gt;= 3000:<br>        new_salaries.append(salary)<br>    else:<br>        new_salaries.append(salary + 500)</pre><pre>################################</pre><pre>new_salaries = [salary if salary &gt;= 3000 else salary + 500 for salary in department for department in departments]</pre><p>Let’s imagine we were working on a single department this whole time. Now we have to get working on other departments too. Now we have a list of lists called departments and in every element of departments, we have salaries of a certain department. If we want to access the salaries we have to go by order. First, we select a department, then go through every salary in that department and calculate new salaries depending on the current salaries.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*isBEdKHTrJFXGT8WkDFA-w.gif" /><figcaption>Adding multiple if conditions</figcaption></figure><p>We can add multiple if conditions one after another in list comprehensions. But it doesn’t mean we should. One of the main points of using list comprehensions is their readability.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TUZcvr9boxClq6aUjtH_1g.gif" /><figcaption>Some caveats</figcaption></figure><p>We went through what are list comprehensions, why and when they are useful, and how to use them. But there are some caveats we should consider when we decide to use them.</p><ul><li>The main point of using list comprehensions is producing readable code. We should avoid using very long list comprehensions.</li><li>Remember that every list comprehension can be rewritten in for loop, but every for loop can’t be rewritten in the form of list comprehension.</li><li>Be careful using list comprehensions for nested loops. What’s most important is to write code that can easily be read and modified.</li><li>While working with large datasets consider using generators instead of comprehensions. A <strong>generator</strong> doesn’t create a single, large data structure in memory, but instead returns an iterable. Your code can ask for the next value from the iterable as many times as necessary or until you’ve reached the end of your sequence, while only storing a single value at a time.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*k3WGmZ4K9tVyqorlEJhmdw.gif" /></figure><p>Thank you for reading!</p><p>I ❤️ VBO</p><p><em>You can reach the official documentation about list comprehensions here:</em></p><p><a href="https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions">5. Data Structures</a></p><p><em>More content at </em><a href="http://plainenglish.io/"><em>plainenglish.io</em></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><em>free weekly newsletter here</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab6dd4a54b57" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/list-comprehensions-in-python-ab6dd4a54b57">List Comprehensions in Python</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An Introduction to Virtual Environments for Beginners]]></title>
            <link>https://python.plainenglish.io/virtual-environments-1d09041771d?source=rss-daa09b78a176------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d09041771d</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[virtual-environment]]></category>
            <dc:creator><![CDATA[Hakan Okay]]></dc:creator>
            <pubDate>Tue, 14 Dec 2021 11:42:14 GMT</pubDate>
            <atom:updated>2021-12-22T07:32:27.988Z</atom:updated>
            <content:encoded><![CDATA[<h4>What are Virtual Environments</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bKMt1khV7DAtwTho" /><figcaption>Photo by <a href="https://unsplash.com/@mitchel3uo?utm_source=medium&amp;utm_medium=referral">Mitchell Luo</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Have you ever wondered why your code works on your laptop fine, but not on your desktop? Or a colleague sent you their code, the code worked fine on their end but not yours? Although there are many factors that might cause this issue, today we will talk about one of the possible factors. Today we’re going to talk about <strong>Virtual Environments</strong>.</p><p>Virtual environments are isolated spaces for your projects to live on. This means that each project can have its own <strong>dependencies</strong>, regardless of what dependencies every other project has.</p><p>You want these spaces because someone trying to run your code might face an error if they are using different versions of the modules you used. Because of the regular updates to popular packages, something might change along the road and cause this issue. Or let’s say you want to review your project after 2 years and years of development on your favorite modules will probably cause you some problems trying to run your old code because of the incompatibility issues caused by your updated modules. So to fix this you can just write down every module you used to a text file or you can use a <strong>virtual environment tool</strong> to produce <strong>easily sharable</strong> virtual environment files.</p><p>Although there are more options to use, 4 virtual environment tools are the most popular and widely used. These tools are called <strong>venv</strong>, <strong>virtualenv</strong>, <strong>pipenv, </strong>and <strong>conda</strong>. Today we will focus on Conda.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xIRIdPsNaa1eiwvtIAUr1g.gif" /></figure><p>While <strong>venv</strong> and <strong>virtualenv </strong>use <strong>pip </strong>as their package manager, <strong>pipenv </strong>and <strong>conda </strong>can be used for both <strong>managing packages</strong> and <strong>virtual environments</strong>.</p><h3>Setting Up Anaconda</h3><blockquote>You can get Anaconda from → <a href="https://www.anaconda.com/">https://www.anaconda.com/</a></blockquote><p>Working with Anaconda is pretty straightforward. Get the installer for your operating system, install Anaconda, run Anaconda Prompt and you’re good to go!</p><h3>Creating and working with virtual environments</h3><p>Let’s imagine creating a virtual environment is like creating an empty sealed box. You will put the books you read inside and the box’s contents will be isolated from other books in your room and keep the place clean and tidy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rfz0vF3WfMnkFVLYv8Iv6A.gif" /><figcaption>Creating a virtual env using Anaconda</figcaption></figure><p>To create this box using Anaconda Prompt we write down:</p><pre>conda create -n my_env</pre><p>We created the box but it is still sealed. If we wanna put the books inside, first we have to open the box.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0swCN80wcCAjqdll6i_5eQ.gif" /><figcaption>Activating a virtual environment using Anaconda</figcaption></figure><p>To open the box using Anaconda Prompt we write down:</p><pre>conda activate my_env</pre><p>Now that the box is open we can put our books inside freely.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KOBO6jDadHESxafDGdKL0g.gif" /><figcaption>Installing packages to an Anaconda virtual environment</figcaption></figure><p>To put our books inside using Anaconda Prompt we write down:</p><pre>conda install python<br>conda install numpy<br>conda install pandas</pre><p>or we can just add the name of the books one after another with a single space between:</p><pre>conda install python numpy pandas</pre><p>and if we have different prints of the same book and we want to put a specific one into the box, using Anaconda Prompt we write down:</p><pre>conda install pandas=1.2.1</pre><p>From time to time you may want to know which books you have inside the box.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1CcV_kU9y4tmt4DzIRh37g.gif" /><figcaption>Listing installed packages in an Anaconda virtual environment</figcaption></figure><p>To see every book you have you use:</p><pre>conda list</pre><p>And what happens when you want to replace your old book with a new print?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ec39MB9r33VnNJX9Y8DDNA.gif" /><figcaption>Updating packages inside an Anaconda Environment</figcaption></figure><p>To replace your book with the new print you use:</p><pre>conda update package_name</pre><p>or</p><pre>conda upgrade package_name</pre><p>Let’s say you no longer want to keep a book inside your box.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zlwb0xqgrhAyVwwqCJ93nA.gif" /><figcaption>Removing packages from an Anaconda virtual environment</figcaption></figure><p>To achieve this, using Anaconda Prompt you can write:</p><pre>conda remove package_name</pre><p>You studied all the books inside and have all the knowledge you need for a certain subject. Now a friend of yours wants to study the same subject as exactly as you did and keeps confusing himself. He’s probably trying to learn from different prints. You want to help your friend out so you want to send the contents of your box to him.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*989DKkoIMrbbmciylnpEkA.gif" /><figcaption>Exporting a yaml file of virtual environment for later use</figcaption></figure><p>You can write down the contents to send to your friend using:</p><pre>conda env export &gt; environment.yaml</pre><p>And after your friend gets the contents he can use it as a shopping list to get the books:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KMdzjeLo62qcjdAJkTzlMw.gif" /><figcaption>Importing a virtual environment using Anaconda</figcaption></figure><pre>conda env create -f environment.yaml</pre><p>After you know you will no longer need the box and have already written down what you had inside to a notebook you may want to make the box disappear from existence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QRSr3j6_SU-HM-R1HpBi-Q.gif" /><figcaption>Deleting a virtual environment using Anaconda</figcaption></figure><p>To achieve this:</p><pre>conda env remove -n my_env</pre><p>Today we’ve talked about virtual environments, some of their use cases, why they are useful and how can we create and manage our own virtual environments.</p><p>This might be an unorthodox way to explain how virtual environments work but I really think this is easier to get a fast and good grasp of the concept.</p><p><em>Thank you for reading. I hope you enjoyed it.</em></p><p><strong>I </strong>❤️<strong> VBO</strong></p><p><em>More content at </em><a href="http://plainenglish.io/"><em>plainenglish.io</em></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><em>free weekly newsletter here</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d09041771d" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/virtual-environments-1d09041771d">An Introduction to Virtual Environments for Beginners</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>