<?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 Yassir Afif on Medium]]></title>
        <description><![CDATA[Stories by Yassir Afif on Medium]]></description>
        <link>https://medium.com/@yassirafif?source=rss-cdaefc30173a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ApEnv-NNpceOs2Vj</url>
            <title>Stories by Yassir Afif on Medium</title>
            <link>https://medium.com/@yassirafif?source=rss-cdaefc30173a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 10:09:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yassirafif/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[Sentiment analysis of scraped customer reviews using a pre-trained language model.]]></title>
            <link>https://medium.com/@yassirafif/sentiment-analysis-of-scraped-reviews-about-a-major-airlines-company-using-a-pre-trained-bert-model-e64015792e59?source=rss-cdaefc30173a------2</link>
            <guid isPermaLink="false">https://medium.com/p/e64015792e59</guid>
            <category><![CDATA[sentiment-analysis]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[transfer-learning]]></category>
            <category><![CDATA[naturallanguageprocessing]]></category>
            <dc:creator><![CDATA[Yassir Afif]]></dc:creator>
            <pubDate>Sun, 29 Jan 2023 20:20:49 GMT</pubDate>
            <atom:updated>2025-12-19T17:15:10.984Z</atom:updated>
            <content:encoded><![CDATA[<p><strong><em>Analyzing the reviews about a major airline company using advanced machine learning tools.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6RpO7MXOjoE-_v1AGN7BAA.jpeg" /></figure><p>Sentiment analysis, also known as opinion mining, is a powerful tool for understanding customers’ emotions and opinions based on their online reviews. In the digital age, online reviews play a crucial role in the perception of potential customers as they are one of the most used sources of information that customers rely on to learn about a company.</p><p>With the increasing use of social media and online reviews platforms, customers can easily share their opinions about products and services with a wide audience. This makes it crucial for companies to monitor and understand these reviews in order to gain insights into customer opinions, preferences, and concerns. Doing this can help companies identify areas where they need to improve their products or services which in turn will improve customer satisfaction. Sentiment analysis using advanced analytical tools can help companies achieve this goal by quickly and efficiently analyzing large volumes of customer reviews.</p><p>Another benefit of sentiment analysis is that it can help companies identify trends in customer opinions over time. By tracking sentiment over time, companies can examine how their products or services are perceived by customers and identify changes in sentiment that may be caused by new products, marketing campaigns or other events.</p><p>In this article we will apply sentiment analysis to the customer reviews about a major airline company. The reviews were scraped from the airline customer review website “airlinequality.com”.</p><p>To do the analysis we will use a BERT (Bidirectional Encoder Representations from Transformers) model, which is a machine learning model for natural language processing that was developed in 2018 by researchers at Google AI Language and can be used to solve more than 11 linguistic tasks such as sentiment analysis and named entity recognition.</p><p>Conceptually, BERT is an ensemble of 12 encoders of the “Transformer” architecture, with 110 million parameters. It was trained on two tasks: language modeling where 15% of the words were masked and BERT was trained to predict them using context. The second task was next sentence prediction (BERT was trained to predict whether or not the chosen next sentence was likely given the first sentence). As a result of the training process, BERT learns the contextual vector representations of the different words.</p><p>We will use a variation of BERT called RoBERTa, which is different from BERT by the way the masking is done. RoBERTa uses dynamic masking during training, which means that in each epoch a different part of the sequence is masked, whereas BERT uses static masking that consists of applying masking only in the phase of data preparation so the same part of the sentence is masked in each epoch.</p><p>This article will be structured as follows:</p><p>I. Creating the dataset.</p><p>II. Sentiment analysis using RoBERTa.</p><p>III. Data analysis based on the type of seat.</p><ol><li><em>Counting how many cases of each sentiment class we have for each type of seat.</em></li><li><em>Negative and positive word clouds for each type of seat.</em></li><li><em>The type of seat with the highest level of dissatisfaction.</em></li><li><em>The type of seat with the highest level of satisfaction.</em></li></ol><p>IV. Data analysis based on the type of traveler .</p><ol><li><em>The type of traveler with the highest level of dissatisfaction.</em></li><li><em>The type of traveler with the highest level of satisfaction.</em></li></ol><p>V. A Cross-tabulation of the variables “type of seat” and “type of traveler” with an aggregation of the ratings.</p><h4>I. Creating the dataset.</h4><p>To scrape the data we will use the libraries BeautifulSoup and Lxml, the latter will allow us to use the Xpath syntax with BeautifulSoup.</p><p>To create the word cloud visualizations we used the “Wordcloud” library and for the pre-trained models, we used the amazing library “Transformers” provided by HuggingFace.</p><pre>from bs4 import BeautifulSoup<br>from lxml import html<br>import requests<br>from wordcloud import WordCloud<br>from transformers import TFAutoModelForSequenceClassification<br>from transformers import AutoTokenizer<br>import numpy as np<br>import pandas as pd<br>from scipy.special import softmax</pre><p>The Xpath syntax for the different elements that we will use was obtained by inspecting the HTML of the webpage.</p><pre># The Xpath for the different elements that we will scrape<br>rating_xpath = &#39;//div[contains(@itemprop, &quot;reviewRating&quot;)]/span[contains(@itemprop, &quot;ratingValue&quot;)]&#39;<br>header_xpath = &#39;//div[contains(@class, &quot;body&quot;)]/h2[contains(@class, &quot;text_header&quot;)]&#39;<br>rev_date_xpath = &#39;//div[contains(@class, &quot;body&quot;)]//time[contains(@itemprop, &quot;datePublished&quot;)]/@datetime&#39;<br>verfiyed_xpath = &#39;//div[contains(@class, &quot;text_content&quot;)]/strong/a/em&#39;<br>review_cont_xpath = &#39;//article[contains(@itemprop, &quot;review&quot;)]//div[contains(@class, &quot;text_con&quot;)]&#39;<br>type_of_traveler_xpath = &#39;//tr/td[contains(@class, &quot;review-rating-header type_of_traveller &quot;)]/following-sibling::td/text()&#39;<br>type_of_seat_xpath = &#39;//tr/td[contains(@class, &quot;review-rating-header cabin_flown &quot;)]/following-sibling::td/text()&#39;<br>route_path_xpath = &#39;//tr/td[contains(@class, &quot;review-rating-header route &quot;)]/following-sibling::td/text()&#39;<br>date_flight_xpath = &#39;//tr/td[contains(@class, &quot;review-rating-header date_flown &quot;)]/following-sibling::td/text()&#39;</pre><p>Let’s get the data.</p><pre>ratings = []<br>headers = []<br>rev_date = []<br>verif = []<br>reviews = []<br>type_of_trav = []<br>type_of_seat = []<br>routes = []<br>flt_date = []<br>complete_pages = []<br><br>for i in range(1, 36):<br>    URL =&quot;https://www.airlinequality.com/airline-reviews/british-airways/page/{}/?sortby=post_date%3ADesc&amp;pagesize=100&quot;.format(i)<br><br>    page = requests.get(URL)<br>    tree = html.fromstring(page.content)<br>    <br>    # Getting the ratings<br>    rt = tree.xpath(rating_xpath)<br>    <br>    # Getting the headers<br>    hd = tree.xpath(header_xpath)<br>    <br>    # Getting the dates<br>    dt_rv = tree.xpath(rev_date_xpath)<br>    <br>    #  Getting verifications<br>    vr_f = tree.xpath(verfiyed_xpath)<br>    <br>    # Getting the review content<br>    cont = list(tree.xpath(review_cont_xpath)[0])<br>    <br>    # Getting type of traveler <br>    trav_tp = tree.xpath(type_of_traveler_xpath)<br>    <br>    # Getting type of seat<br>    seat_tp = tree.xpath(type_of_seat_xpath)<br>    <br>    # Getting the routes <br>    route = tree.xpath(route_path_xpath)<br>    <br>    # Getting flight date<br>    fl_dt = tree.xpath(date_flight_xpath)<br>    <br>    rev = []<br>    <br>    # Code for cleaning the reviews.<br>    for j in tree.xpath(review_cont_xpath)[0].xpath(&#39;//text()&#39;):<br>        if &#39; | &#39; in j and &#39;{&quot;@context&#39; not in j and &#39;[]&#39; not in j and &#39;Air Travel Ratings &amp;&#39; not in j:<br>            rev.append(j)<br>    # Code to make sure we get only complete reviews with no missing data. <br>    if len(rt)==len(rev) and len(rt)==len(route) and len(rt)==len(trav_tp):<br>        ratings = ratings + rt<br>        headers = headers + hd<br>        rev_date = rev_date + dt_rv<br>        verif = verif + vr_f<br>        reviews = reviews + rev<br>        type_of_trav = type_of_trav + trav_tp<br>        type_of_seat = type_of_seat + seat_tp<br>        routes = routes + route<br>        flt_date = flt_date + fl_dt<br>        complete_pages.append(i)</pre><pre># Getting the ratings <br>ratings_ct = []<br>for i in range(len(ratings)):<br>    ratings_ct.append(int(ratings[i].text))</pre><pre># Getting the headers<br>headers_ct = []<br>for i in range(len(headers)):<br>    headers_ct.append(str(headers[i].text).strip(&#39;&quot;&quot;&#39;))</pre><pre># Getting the reviews <br>reviews_ct = []<br>for i in range(len(reviews)):<br>    reviews_ct.append(reviews[i].strip(&#39; | &#39;))</pre><pre>df_BA = pd.DataFrame({&#39;ratings&#39;:ratings_ct, &#39;headers&#39;:headers_ct, &#39;review_date&#39;:rev_date, &#39;review&#39;:reviews_ct, &#39;traveler_type&#39;:type_of_trav, &#39;seat_type&#39;:type_of_seat, &#39;routes&#39;:routes, &#39;flight_date&#39;:flt_date})<br>df_BA</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nupNHyhH8EWW2_rFCn0Vzg.jpeg" /></figure><h4>II. Sentiment analysis using RoBERTa.</h4><p>After creating the dataset, let’s load the model. This is a roBERTa-base model trained on ~58M tweets and fine-tuned for sentiment analysis.</p><pre>weights = f&quot;cardiffnlp/twitter-roberta-base-sentiment&quot;<br>tok = AutoTokenizer.from_pretrained(weights)<br>model = TFAutoModelForSequenceClassification.from_pretrained(weights)<br>model.save_pretrained(weights)</pre><p>Let’s write a function to predict the classes using the model.</p><pre>def predict_class(text, model_output):<br>    &quot;&quot;&quot; <br>    A function to output the probability score for a given sentiment or the sentiment class.<br>    <br>    Inputs:<br>    ---------<br>    <br>     - text : The text for which we want to compute the sentiment score or the class<br>     - model_output : One of the following strings to get their probability score [&#39;positive&#39;, &#39;negative&#39;, &#39;neutral&#39;]<br>                      or &#39;class&#39; to the get the predicted sentiment.<br>    Outputs :<br>    ---------<br>    <br>    - Probability score for the sentiment in question or the sentiment class.<br>    <br>    &quot;&quot;&quot;<br>    # Encoding the text.<br>    enc = tok(text, return_tensors=&#39;tf&#39;, padding=&#39;max_length&#39;)<br>    # Computing the output.<br>    output = model(**enc)<br>    # Getting the probabilities for the three classes.<br>    prob = softmax(output[0][0])<br><br>    if model_output == &#39;positive&#39;:<br>        return prob[-1]<br>    elif model_output == &#39;neutral&#39;:<br>        return prob[1]<br>    elif model_output == &#39;negative&#39;:<br>        return prob[0]<br>    elif model_output == &#39;class&#39;:<br>        return np.argmax(prob)</pre><p>Let’s predict the sentiment class for our reviews. The model will only use the first 512 elements of each sequence to make predictions.</p><pre>df_BA[&#39;sent_class&#39;] = [predict_class(i[0:510], &#39;class&#39;) for i in list(df_BA[&#39;review&#39;].values)]</pre><p>After predicting the sentiment for each review in our dataset, let’s now evaluate the performance of the model using the ratings.</p><pre># Evaluating the performance of the model.<br>df_BA.groupby([&#39;sent_class&#39;])[&#39;ratings&#39;].mean()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*Tu1Ewy5MLKWGjFLCfUXQlw.png" /></figure><p>We can see that the model did a decent job in classifying a large number of positive and negative reviews, but did not do a good job in distinguishing neutral classes.</p><p>Let’s investigate the ratings of the reviews that were classified as neutral reviews.</p><pre># investigate the ratings of the neutral class<br>df_BA[df_BA[&quot;sent_class&quot;]== 1][&#39;ratings&#39;].describe()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*O3F8ocBoac2jn1qXWyTbww.png" /></figure><p>We can see that we have at least two examples that were incorrectly classified as neutral, one example with a rating of 10 (positive) and another one with a rating of 1 (negative).</p><p>Let’s fix the mistakes of the model by setting the value of sent_class to 0 (negative) for examples that have a rating less than 5 and were classified as neutral by the model and to 2 (positive) for the examples that have a rating greater than 5 and were classified as neutral.</p><pre>df_BA.loc[(df_BA[&quot;ratings&quot;] &lt; 5) &amp; (df_BA[&#39;sent_class&#39;]==1), &#39;sent_class&#39;] = 0 <br>df_BA.loc[(df_BA[&quot;ratings&quot;] &gt; 5) &amp; (df_BA[&#39;sent_class&#39;]==1), &#39;sent_class&#39;] = 2<br># Evaluating the performance of the model after the correction of the mistakes done by the model.<br>df_BA.groupby([&#39;sent_class&#39;])[&#39;ratings&#39;].mean() </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*7Y2E3aGC3Vn-AtKjZ659mw.png" /></figure><p>Now, we can see that the average rating for the neutral class is 5 which is the most representative rating for a neutral review.</p><p>Let’s count the exemples for each sentiment class.</p><pre># Replacing the values of the &#39;sent_class&#39; column<br>dict_ = {0:&#39;negative&#39;,<br>        1: &#39;neutral&#39;,<br>        2: &#39;positive&#39;}<br>df_BA[&#39;sentiment&#39;] = df_BA[&#39;sent_class&#39;].replace(dict_)<br><br># Counting the cases for each sentiment<br>sns.countplot(x=&#39;sentiment&#39;, data=df_BA)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/389/1*3A5rPf-BS9lzE0MumYbPyQ.png" /></figure><h4>III. Data analysis based on the “type of seat”.</h4><ol><li><em>Counting how many cases of each sentiment class we have for each type of seat.</em></li></ol><pre># the case of the business class<br>sns.countplot(x=&#39;sentiment&#39;, data=df_BA[df_BA[&#39;seat_type&#39;] == &#39;Business Class&#39;], order=[&#39;negative&#39;, &#39;positive&#39;, &#39;neutral&#39;])<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/389/1*ko4P-pSgGytfHdq4pB9txQ.png" /></figure><pre># The case of the economy class<br>sns.countplot(x=&#39;sentiment&#39;, data=df_BA[df_BA[&#39;seat_type&#39;] == &#39;Economy Class&#39;], order=[&#39;negative&#39;, &#39;positive&#39;, &#39;neutral&#39;])<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/389/1*85qVjXgje-_rFilvhTe4KQ.png" /></figure><pre># The case of the premium economy class<br>sns.countplot(x=&#39;sentiment&#39;, data=df_BA[df_BA[&#39;seat_type&#39;] == &#39;Premium Economy&#39;], order=[&#39;negative&#39;, &#39;positive&#39;, &#39;neutral&#39;])<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/382/1*lUO67yjztZ8juDeK1gtaVA.png" /></figure><pre># The case of the first class<br>sns.countplot(x=&#39;sentiment&#39;, data=df_BA[df_BA[&#39;seat_type&#39;] == &#39;First Class&#39;], order=[&#39;negative&#39;, &#39;positive&#39;, &#39;neutral&#39;])<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/382/1*WcHRfFbbxuGFpNPGZDmoLw.png" /></figure><p>From the plots we can see that the first class is the only class where the number of positive reviews is higher than the number of negative reviews.</p><p><em>2. Negative and positive word clouds for each type of seat.</em></p><p>Let’s create the word cloud visualizations for each type of seat. This will help us get an idea about the sources of satisfaction and dissatisfaction for the customers of this airline company.</p><pre># Word cloud for the business class <br># Negative sentiment.<br>bc_df = df_BA[(df_BA[&#39;seat_type&#39;] == &#39;Business Class&#39;) &amp; (df_BA[&#39;sentiment&#39;] == &#39;negative&#39;) ][&#39;review&#39;]<br>words = &#39; &#39;.join(list(bc_df.values))<br>wordcloud = WordCloud(background_color=&#39;white&#39;).generate(words)<br>plt.imshow(wordcloud)<br>plt.axis(&#39;off&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*oqe-RsUaH0KFAUKhPE4Rbg.png" /></figure><pre># Word cloud for the business class <br># Positive sentiment.<br>bc_df = df_BA[(df_BA[&#39;seat_type&#39;] == &#39;Business Class&#39;) &amp; (df_BA[&#39;sentiment&#39;] == &#39;positive&#39;) ][&#39;review&#39;]<br>words = &#39; &#39;.join(list(bc_df.values))<br>wordcloud = WordCloud(background_color=&#39;white&#39;).generate(words)<br>plt.imshow(wordcloud)<br>plt.axis(&#39;off&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*LohgJvHPVpkbYOr851aJRg.png" /></figure><pre># Wordcloud for economy class <br># negative sentiment.<br>bc_df = df_BA[(df_BA[&#39;seat_type&#39;] == &#39;Economy Class&#39;) &amp; (df_BA[&#39;sentiment&#39;] == &#39;negative&#39;) ][&#39;review&#39;]<br>words = &#39; &#39;.join(list(bc_df.values))<br>wordcloud = WordCloud(background_color=&#39;white&#39;).generate(words)<br>plt.imshow(wordcloud)<br>plt.axis(&#39;off&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*UPuHx3I2Mb6Uen2gvj5XZA.png" /></figure><pre># Wordcloud for economy class <br># positive sentiment.<br>bc_df = df_BA[(df_BA[&#39;seat_type&#39;] == &#39;Economy Class&#39;) &amp; (df_BA[&#39;sentiment&#39;] == &#39;positive&#39;) ][&#39;review&#39;]<br>words = &#39; &#39;.join(list(bc_df.values))<br>wordcloud = WordCloud(background_color=&#39;white&#39;).generate(words)<br>plt.imshow(wordcloud)<br>plt.axis(&#39;off&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*SW3bzKkBNs2bJiNcYCfULA.png" /></figure><pre># Wordcloud for premium economy <br># negative sentiment.<br>bc_df = df_BA[(df_BA[&#39;seat_type&#39;] == &#39;Premium Economy&#39;) &amp; (df_BA[&#39;sentiment&#39;] == &#39;negative&#39;) ][&#39;review&#39;]<br>words = &#39; &#39;.join(list(bc_df.values))<br>wordcloud = WordCloud(background_color=&#39;white&#39;).generate(words)<br>plt.imshow(wordcloud)<br>plt.axis(&#39;off&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*erRk6JR_1Eu8HPT13D4yqg.png" /></figure><pre># Wordcloud for premium economy <br># positive sentiment.<br>bc_df = df_BA[(df_BA[&#39;seat_type&#39;] == &#39;Premium Economy&#39;) &amp; (df_BA[&#39;sentiment&#39;] == &#39;positive&#39;) ][&#39;review&#39;]<br>words = &#39; &#39;.join(list(bc_df.values))<br>wordcloud = WordCloud(background_color=&#39;white&#39;).generate(words)<br>plt.imshow(wordcloud)<br>plt.axis(&#39;off&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*3omz3ibL-0TVl34Dc6GP4Q.png" /></figure><pre># Wordcloud for first class <br># negative sentiment.<br>bc_df = df_BA[(df_BA[&#39;seat_type&#39;] == &#39;First Class&#39;) &amp; (df_BA[&#39;sentiment&#39;] == &#39;negative&#39;) ][&#39;review&#39;]<br>words = &#39; &#39;.join(list(bc_df.values))<br>wordcloud = WordCloud(background_color=&#39;white&#39;).generate(words)<br>plt.imshow(wordcloud)<br>plt.axis(&#39;off&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*r1tEt6LDT7X4Rj3i6UspXg.png" /></figure><pre># Wordcloud for first class <br># positive sentiment.<br>bc_df = df_BA[(df_BA[&#39;seat_type&#39;] == &#39;First Class&#39;) &amp; (df_BA[&#39;sentiment&#39;] == &#39;positive&#39;) ][&#39;review&#39;]<br>words = &#39; &#39;.join(list(bc_df.values))<br>wordcloud = WordCloud(background_color=&#39;white&#39;).generate(words)<br>plt.imshow(wordcloud)<br>plt.axis(&#39;off&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*yZXkfzfsnA0XAKT87Gb61w.png" /></figure><p><em>3. The type of seat with the highest level of dissatisfaction.</em></p><p>In this part we will analyze negative reviews in order to find the type of seat with the highest level of dissatisfaction. In our calculation we will normalize the counts in order to neutralize the effect of the number of examples in each class.</p><pre># Getting the dataframe<br>neg_df = pd.DataFrame(df_BA[df_BA.sentiment == &#39;negative&#39;].groupby([&#39;seat_type&#39;])[&#39;sentiment&#39;].count()).reset_index()<br><br># normalizing the counts to neutralize the effect of the number of examples for each class<br><br># Getting the number of examples for the economy class<br>num_econ = df_BA[df_BA[&#39;seat_type&#39;]==&#39;Economy Class&#39;].shape[0]<br><br># Getting the number of examples for the business class<br>num_bus = df_BA[df_BA[&#39;seat_type&#39;]==&#39;Business Class&#39;].shape[0]<br><br># Getting the number of examples for the first class<br>num_first = df_BA[df_BA[&#39;seat_type&#39;]==&#39;First Class&#39;].shape[0]<br><br># Getting the number of examples for the premium economy<br>num_prem = df_BA[df_BA[&#39;seat_type&#39;]==&#39;Premium Economy&#39;].shape[0]<br><br># normlized count for business class<br>neg_df.loc[(neg_df[&quot;seat_type&quot;] == &#39;Business Class&#39;), &#39;normalized_count&#39;] = neg_df.loc[(neg_df[&quot;seat_type&quot;] == &#39;Business Class&#39;), &#39;sentiment&#39;]/num_bus<br><br># normlized count for economy class<br>neg_df.loc[(neg_df[&quot;seat_type&quot;] == &#39;Economy Class&#39;), &#39;normalized_count&#39;] = neg_df.loc[(neg_df[&quot;seat_type&quot;] == &#39;Economy Class&#39;), &#39;sentiment&#39;]/num_econ<br><br># normlized count for first class<br>neg_df.loc[(neg_df[&quot;seat_type&quot;] == &#39;First Class&#39;), &#39;normalized_count&#39;] = neg_df.loc[(neg_df[&quot;seat_type&quot;] == &#39;First Class&#39;), &#39;sentiment&#39;]/num_first<br><br># normlized count for premium economy <br>neg_df.loc[(neg_df[&quot;seat_type&quot;] == &#39;Premium Economy&#39;), &#39;normalized_count&#39;] = neg_df.loc[(neg_df[&quot;seat_type&quot;] == &#39;Premium Economy&#39;), &#39;sentiment&#39;]/num_prem<br><br><br># Dropping the old column<br>neg_df.drop(&#39;sentiment&#39;, axis=1, inplace=True)</pre><pre># Plotting the results. <br>sns.catplot(x=&#39;seat_type&#39;, y=&#39;normalized_count&#39;, data=neg_df, kind=&#39;bar&#39;)<br>plt.title(&#39;The level of unsatisfaction per type of seat&#39;)<br>plt.xlabel(&#39;The type of the seat&#39;)<br>plt.ylabel(&#39;The normalized count&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*ka4grE94FX1AeqUGFYCHVA.png" /></figure><p>The visual above shows us that the class with the highest level of dissatisfaction is the “economy class”.</p><p><em>4. The type of seat with the highest level of satisfaction.</em></p><p>Let’s do the same thing, but this time to find the type of seat with the highest level of satisfaction.</p><pre><br># Getting the dataframe<br>pos_df = pd.DataFrame(df_BA[df_BA.sentiment == &#39;positive&#39;].groupby([&#39;seat_type&#39;])[&#39;sentiment&#39;].count()).reset_index()<br><br># normalizing using the number of examples for each class<br><br># normlized count for business class<br>pos_df.loc[(pos_df[&quot;seat_type&quot;] == &#39;Business Class&#39;), &#39;normalized_count&#39;] = pos_df.loc[(pos_df[&quot;seat_type&quot;] == &#39;Business Class&#39;), &#39;sentiment&#39;]/num_bus<br><br># normlized count for economy class<br>pos_df.loc[(pos_df[&quot;seat_type&quot;] == &#39;Economy Class&#39;), &#39;normalized_count&#39;] = pos_df.loc[(pos_df[&quot;seat_type&quot;] == &#39;Economy Class&#39;), &#39;sentiment&#39;]/num_econ<br><br># normlized count for first class<br>pos_df.loc[(pos_df[&quot;seat_type&quot;] == &#39;First Class&#39;), &#39;normalized_count&#39;] = pos_df.loc[(pos_df[&quot;seat_type&quot;] == &#39;First Class&#39;), &#39;sentiment&#39;]/num_first<br><br># normlized count for premium economy <br>pos_df.loc[(pos_df[&quot;seat_type&quot;] == &#39;Premium Economy&#39;), &#39;normalized_count&#39;] = pos_df.loc[(pos_df[&quot;seat_type&quot;] == &#39;Premium Economy&#39;), &#39;sentiment&#39;]/num_prem<br><br><br># Dropping the old column<br>pos_df.drop(&#39;sentiment&#39;, axis=1, inplace=True)<br><br></pre><pre># Plotting the results.<br>sns.catplot(x=&#39;seat_type&#39;, y=&#39;normalized_count&#39;, data=pos_df, kind=&#39;bar&#39;)<br>plt.title(&#39;The level of satisfaction per type of seat&#39;)<br>plt.xlabel(&#39;The type of the seat&#39;)<br>plt.ylabel(&#39;The normalized count&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*35v1tcuAHLJp2vbeRxo3Mw.png" /></figure><p>The two visuals show us that the most appreciated class is the “first class”, it has the lowest level of dissatisfaction and the highest level of satisfaction.</p><h4>IV. Data analysis based on the type of traveler .</h4><ol><li><em>The type of traveler with the highest level of dissatisfaction.</em></li></ol><p>After analyzing the data based on the type of seat, let’s analyze it to find the type of seat with the highest level of dissatisfaction.</p><pre># Getting the dataframe<br>neg_df_tr = pd.DataFrame(df_BA[df_BA.sentiment == &#39;negative&#39;].groupby([&#39;traveler_type&#39;])[&#39;sentiment&#39;].count()).reset_index()<br><br># normalizing using the number of examples for each type of traveler<br><br># Getting the number of examples for the solo leisure type<br>num_solo_l = df_BA[df_BA[&#39;traveler_type&#39;]==&#39;Solo Leisure&#39;].shape[0]<br><br># Getting the number of examples for the couple leisure type<br>num_couple_l = df_BA[df_BA[&#39;traveler_type&#39;]==&#39;Couple Leisure&#39;].shape[0]<br><br># Getting the number of examples for the business type<br>num_bus = df_BA[df_BA[&#39;traveler_type&#39;]==&#39;Business&#39;].shape[0]<br><br># Getting the number of examples for the family leisure type<br>num_fam_l = df_BA[df_BA[&#39;traveler_type&#39;]==&#39;Family Leisure&#39;].shape[0]<br><br># normlized count for solo leisure<br>neg_df_tr.loc[(neg_df_tr[&quot;traveler_type&quot;] == &#39;Solo Leisure&#39;), &#39;normalized_count_trav&#39;] = neg_df_tr.loc[(neg_df_tr[&quot;traveler_type&quot;] == &#39;Solo Leisure&#39;), &#39;sentiment&#39;]/num_solo_l<br><br># normlized count for couple leisure<br>neg_df_tr.loc[(neg_df_tr[&quot;traveler_type&quot;] == &#39;Couple Leisure&#39;), &#39;normalized_count_trav&#39;] = neg_df_tr.loc[(neg_df_tr[&quot;traveler_type&quot;] == &#39;Couple Leisure&#39;), &#39;sentiment&#39;]/num_couple_l<br><br># normlized count for Business<br>neg_df_tr.loc[(neg_df_tr[&quot;traveler_type&quot;] == &#39;Business&#39;), &#39;normalized_count_trav&#39;] = neg_df_tr.loc[(neg_df_tr[&quot;traveler_type&quot;] == &#39;Business&#39;), &#39;sentiment&#39;]/num_bus<br><br># normlized count for Family Leisure <br>neg_df_tr.loc[(neg_df_tr[&quot;traveler_type&quot;] == &#39;Family Leisure&#39;), &#39;normalized_count_trav&#39;] = neg_df_tr.loc[(neg_df_tr[&quot;traveler_type&quot;] == &#39;Family Leisure&#39;), &#39;sentiment&#39;]/num_fam_l<br><br>neg_df_tr</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/1*0--NDJcm5AX6ok6PsBey2Q.jpeg" /></figure><pre># Plotting the results.<br>sns.catplot(x=&#39;traveler_type&#39;, y=&#39;normalized_count_trav&#39;, data=neg_df_tr, kind=&#39;bar&#39;)<br>plt.title(&#39;The level of unsatisfaction per type of traveler&#39;)<br>plt.xlabel(&#39;The type of the traveler&#39;)<br>plt.ylabel(&#39;The normalized count&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*4lNGYDx-DC86kZPQdFkRlw.png" /></figure><p>From the visual, we can notice that the “Business” traveler has the highest level of dissatisfaction.</p><p><em>4. The type of traveler with the highest level of satisfaction.</em></p><p>Let’s do the same thing to find the type of traveler with the highest level of satisfaction.</p><pre># Getting the dataframe<br>pos_df_tr = pd.DataFrame(df_BA[df_BA.sentiment == &#39;positive&#39;].groupby([&#39;traveler_type&#39;])[&#39;sentiment&#39;].count()).reset_index()<br><br># normlized count for solo leisure<br>pos_df_tr.loc[(pos_df_tr[&quot;traveler_type&quot;] == &#39;Solo Leisure&#39;), &#39;normalized_count_trav&#39;] = pos_df_tr.loc[(pos_df_tr[&quot;traveler_type&quot;] == &#39;Solo Leisure&#39;), &#39;sentiment&#39;]/num_solo_l<br><br># normlized count for couple leisure<br>pos_df_tr.loc[(pos_df_tr[&quot;traveler_type&quot;] == &#39;Couple Leisure&#39;), &#39;normalized_count_trav&#39;] = pos_df_tr.loc[(pos_df_tr[&quot;traveler_type&quot;] == &#39;Couple Leisure&#39;), &#39;sentiment&#39;]/num_couple_l<br><br># normlized count for Business<br>pos_df_tr.loc[(pos_df_tr[&quot;traveler_type&quot;] == &#39;Business&#39;), &#39;normalized_count_trav&#39;] = pos_df_tr.loc[(pos_df_tr[&quot;traveler_type&quot;] == &#39;Business&#39;), &#39;sentiment&#39;]/num_bus<br><br># normlized count for Family Leisure <br>pos_df_tr.loc[(pos_df_tr[&quot;traveler_type&quot;] == &#39;Family Leisure&#39;), &#39;normalized_count_trav&#39;] = pos_df_tr.loc[(pos_df_tr[&quot;traveler_type&quot;] == &#39;Family Leisure&#39;), &#39;sentiment&#39;]/num_fam_l<br><br></pre><pre># Plotting the result.<br>sns.catplot(x=&#39;traveler_type&#39;, y=&#39;normalized_count_trav&#39;, data=pos_df_tr, kind=&#39;bar&#39;)<br>plt.title(&#39;The level of satisfaction per type of traveler&#39;)<br>plt.xlabel(&#39;The type of the traveler&#39;)<br>plt.ylabel(&#39;The normalized count&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*pxv9zG9ph5CAlbpftR5JKQ.png" /></figure><p>From the two visuals above, we can see that the most satisfied type of traveler is the “Family Leisure” traveler. They have the lowest level of dissatisfaction and the highest level of satisfaction.</p><h4>V. A Cross-tabulation of the variables “type of seat” and “type of traveler” with an aggregation of the ratings.</h4><p>Let’s do a cross-tabulation using these two variables and the rating as an aggregation variable with the objective to gain more insights about the level of satisfaction expressed by the customers of this airline company.</p><pre>cross_tb = pd.crosstab(index=df_BA[&#39;traveler_type&#39;], columns=df_BA[&#39;seat_type&#39;], values=df_BA[&#39;ratings&#39;],<br>           aggfunc=&#39;median&#39;)<br>cross_tb</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/836/1*LR9hJlfoRfcxjx58yVUtng.jpeg" /></figure><pre>cross_tb.plot(kind=&#39;bar&#39;)<br>plt.title(&#39;Average rating of the different classes by the different types of travelers&#39;)<br>plt.xlabel(&#39;Type of traveler&#39;)<br>plt.ylabel(&#39;Average rating&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*Ca0LdfGD8uHzDAHgfD2N5g.png" /></figure><p>From the cross table and the visual we can see that :</p><ul><li>Travelers of type “business” are very unsatisfied with all types of seats.</li><li>The first class is poorly rated by travelers of type “family leisure” and highly rated by travelers of types “solo leisure” and “couple leisure”.</li><li>The “family leisure” type of travelers appreciate the “business class”.</li><li>The “solo leisure” travelers give on average a decent rating for all types of seats, except the “economy class”.</li></ul><h4>Conclusion:</h4><p>In conclusion, pre-trained Large Language models can be an effective tool for doing sentiment analysis of customer reviews. By utilizing a dataset of scraped reviews we were able to demonstrate the ability of these models to accurately classify the sentiment of customer reviews. This could be useful for airlines to gain a better understanding of customer satisfaction and identify areas where they can improve their services.</p><p>References :</p><ul><li><a href="https://huggingface.co/cardiffnlp/twitter-roberta-base-sentiment">https://huggingface.co/cardiffnlp/twitter-roberta-base-sentiment</a></li><li>Devlin, Jacob; Chang, Ming-Wei; Lee, Kenton; Toutanova, Kristina (11 October 2018). “BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding”. arXiv:1810.04805</li><li>Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov( 26 Jul 2019) . RoBERTa: A Robustly Optimized BERT Pretraining Approach arXiv:1907.11692</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e64015792e59" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Projecting Customer Lifetime Value using the BG/NBD and the Gamma-Gamma models.]]></title>
            <link>https://medium.com/@yassirafif/projecting-customer-lifetime-value-using-the-bg-nbd-and-the-gamma-gamma-models-9a937c60fe7f?source=rss-cdaefc30173a------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a937c60fe7f</guid>
            <category><![CDATA[marketing]]></category>
            <category><![CDATA[customer-lifetime-value]]></category>
            <category><![CDATA[advanced-analytics]]></category>
            <category><![CDATA[clv]]></category>
            <category><![CDATA[marketing-analytics]]></category>
            <dc:creator><![CDATA[Yassir Afif]]></dc:creator>
            <pubDate>Sat, 28 Jan 2023 20:44:08 GMT</pubDate>
            <atom:updated>2024-02-02T15:35:04.567Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong><em>A case study of predicting Customer Lifetime Value using the famous probabilistic models BG/NBD and Gamma-Gamma.</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*su8L50fLfUawYUwvso1XLQ.jpeg" /></figure><p>Marketing analytics is a large sub-field of marketing that uses advanced quantitative techniques to track, measure and analyze the marketing performance of an organization or an entity, It gives companies the ability to identify trends and patterns in consumer behavior and make data-driven decisions to optimize marketing campaigns and identify the best opportunities for growth.</p><p>One of the major areas in this field is the area of Customer Lifetime Value (CLV) modeling. The concept of CLV was first introduced in 1988 by Robert Shaw and Merlin Stone in their book titled Database Marketing: Strategy and Implementation. It can be defined as the present value of the future cash flows attributed to the customer during his/her entire relationship with the company(Faris et al).</p><p>Understanding and correctly calculating the CLV can help companies identify the most profitable customers, which in turn can facilitate the task of resource allocation when trying to retain customers. In this case high valuable customers deserve more attention and more resources than less valuable ones. CLV prediction can also help companies to budget and forecast future sales and revenue.</p><p>One of the most powerful tools for predicting customer lifetime value in non-contractual settings is the BG/NBD model (combined with the Gamma-Gamma model), also known as the Beta-Geometric/Negative Binomial Distribution model. It was created and introduced by Peter Fader , Bruce Hardie and Ka Lok Lee in their famous article published in 2005 in the prestigious journal « Marketing Science » , and titled : “Counting Your Customers” the Easy Way: An Alternative to the Pareto/NBD Model.</p><p>The BG/NBD model is an integrated probabilistic model that describes two aspects of consumer behavior, the buying behavior and the churn behavior. This is done by using a combination of the following probability distributions:</p><ul><li>The Poisson distribution to model transactions and the exponential distribution to model the time between transactions.</li><li>Because different consumers have different buying behaviors, The BG/NBD model uses the Gamma distribution to model the variation in the buying behavior across the population. Note that the combination of the Poisson/Gamma distributions is known as the negative binomial distribution (NBD) and this is where the name of the model comes from.</li><li>After each purchase, a customer makes a decision whether to remain a customer or to churn. According to the creators of the model, this behavior can be modeled using the shifted geometric distribution.</li><li>The Beta distribution is used to describe the variation in the churn probability across the population.</li></ul><p>To calculate the CLV, we need to predict the expected value of purchases using the Gamma-Gamma model.</p><p>According to the creators of the model, the value of a given customer’s transaction follows a gamma distribution and the heterogeneity between customers follows similarly a gamma distribution. The use of the gamma distribution in both cases is the reason why the model is called Gamma-Gamma.</p><p>The combination of the BG/NBD model and the Gamma-Gamma model can, not only, predict the CLV of each customer, but also has the ability to provide us with accurate answers to the following questions :</p><ul><li>Which customers are still customers and which ones of them will order again in the next period.</li><li>The number of orders each customer will place and the average order value of each customer’s order.</li></ul><p>In this article we will demonstrate how to implement these models using the Lifetimes library and a dataset provided by a renowned UK based online retailer. After predicting the CLV for each customer we will segment the customers using the KMeans algorithm before using the resulting segments to analyze the frequency and the monetary metrics.</p><h4>Importing the packages and preparing the data.</h4><p>We will use Seaborn and matplotlib for visualizing the data, Pandas for loading and cleaning the data and the Lifetimes library for the models and functions that we will use.</p><pre>import pandas as pd<br>import numpy as np<br>import matplotlib.pyplot as plt<br>import seaborn as sns<br>from sklearn.cluster import KMeans<br>from sklearn.metrics import mean_squared_error<br>from lifetimes import BetaGeoFitter<br>from lifetimes import GammaGammaFitter<br>from lifetimes.utils import calibration_and_holdout_data<br>from lifetimes.utils import summary_data_from_transaction_data<br>from lifetimes.plotting import plot_calibration_purchases_vs_holdout_purchases<br>from decimal import Decimal <br>import datetime as dt<br><br></pre><pre>df = pd.read_csv(&#39;OnlineRetail.csv&#39;)<br>df.drop(&#39;Unnamed: 0&#39;, inplace=True, axis=1)<br>df</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X8nJqXktvuXBgvEDZNbbsw.jpeg" /><figcaption>The raw data</figcaption></figure><p>Let’s verify the existence of NaN values.</p><pre>df.isna().sum()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*Qg77dY4qE-3_WwbHQYkI5A.png" /></figure><p>We need to drop these NaN values.</p><pre>df.dropna(inplace=True)</pre><p>We have some negative values in the quantity column that we need to eliminate.</p><pre># Dropping rows with negative quantity.<br>df = df[~df[&#39;Quantity&#39;] &lt; 0]<br>df.info()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*YLEl3bBhI1_P6dCYHRV8QQ.png" /></figure><p>Let’s modify the datatypes of the different columns, we set the types of “InvoiceNo”’, “CustomerID” and “StockCode”’ to string type because they may contain characters other than integers. We set the type of the “UnitPrice” column to decimal because it is the most appropriate data type for monetary values in Python.</p><pre># Setting data types <br>df[&#39;InvoiceNo&#39;] = df[&#39;InvoiceNo&#39;].astype(&#39;str&#39;)<br>df[&#39;InvoiceDate&#39;] = pd.to_datetime(df[&#39;InvoiceDate&#39;])<br>df[&#39;CustomerID&#39;] = df[&#39;CustomerID&#39;].astype(&#39;str&#39;)<br>df[&#39;Description&#39;] = df[&#39;Description&#39;].astype(&#39;str&#39;)<br>df[&#39;StockCode&#39;] = df[&#39;StockCode&#39;].astype(&#39;str&#39;)<br>df[&#39;Country&#39;] = df[&#39;Country&#39;].astype(&#39;str&#39;)<br>df[&#39;UnitPrice&#39;] = df[&#39;UnitPrice&#39;].apply(Decimal)</pre><h4>Computing the RFM metrics :</h4><p>The RFM framework of thinking, which stands for Recency, Frequency, and Monetary Value, is a widely used tool in marketing to analyze customer behavior and identify high-value customers. The model is based on the idea that customers who have recently made a purchase, made an important number of purchases during their relationship with the company, and spent a significant amount of money are more valuable to the company than those who have not.</p><p>In order to create the summary data (Recency, frequency and monetary) for each customer we need to compute the monetary value of the transactions.</p><pre># Creating the monetary value of the transactions (quantity * price)<br>df[&#39;Monetary&#39;] = df[&#39;Quantity&#39;] * df[&#39;UnitPrice&#39;]<br>df[&#39;Monetary&#39;] = df[&#39;Monetary&#39;].apply(Decimal) </pre><p>Now, we can compute the summary data using the Lifetimes method, “summary_data_from_transaction_data”.</p><pre># Computing the summary data (Recency, Frequency, monetary and tenure)<br>df_rfmt = summary_data_from_transaction_data(transactions = df, <br>                                         customer_id_col = &#39;CustomerID&#39;, <br>                                         datetime_col = &#39;InvoiceDate&#39;, <br>                                         monetary_value_col = &#39;Monetary&#39;)<br>df_rfmt.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*UE2nI_rZCCV8OrLW6SBtyg.jpeg" /><figcaption>Summary data</figcaption></figure><ul><li>The recency variable is a measure of the time elapsed since the customer’s last purchase.</li><li>The frequency corresponds to the number of orders placed by a customer.</li><li>The monetary metric shows the total revenue generated by each customer.</li><li>Tenure, or T, represents how long a given customer has been with the company.</li></ul><h4>Fitting and testing the BG/NBD model.</h4><p>The first step is to get a calibration period and an observation (holdout) period for each customer. The first period will be used to fit the model and the second period will be used to test the model.</p><pre># size of the data <br>diff_time = df[&#39;InvoiceDate&#39;].max() - df[&#39;InvoiceDate&#39;].min() <br>diff_time</pre><p>We have 373 days of data, we will use 200 days to fit the model and 173 days to test the model.</p><pre># Getting the ending date of the calibration period. <br>end_date_cal = df[&#39;InvoiceDate&#39;].min() + dt.timedelta(days=200)<br>end_date_obs = end_date_cal + (diff_time - dt.timedelta(days=200))</pre><p>Now, we can split our dataset using the Lifetimes method “calibration_and_holdout_data”.</p><pre>df_rfmt_cal = calibration_and_holdout_data(transactions=df, <br>                                          customer_id_col=&quot;CustomerID&quot;,<br>                                          datetime_col = &quot;InvoiceDate&quot;, <br>                                          calibration_period_end=end_date_cal,<br>                                          observation_period_end= end_date_obs)<br>df_rfmt_cal</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*6ZCMfPac54eSzVPwlEwHQQ.jpeg" /></figure><p>The BG/NBD model uses the L2 regularization. To find the best L2 coefficient we will use “Grid Search”.</p><pre>l2_coefs = [0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]<br>l2_list = []<br>rmse_list = []<br>for coef in l2_coefs :<br>    # Fitting the model using the calibration dataset.<br>    model = BetaGeoFitter(penalizer_coef=coef)<br>    model.fit(df_rfmt_cal[&#39;frequency_cal&#39;], <br>        df_rfmt_cal[&#39;recency_cal&#39;], <br>        df_rfmt_cal[&#39;T_cal&#39;])<br>    # Predicting the frequency for the holdout period for all customers. <br>    pred_freq = pd.DataFrame(model.predict(df_rfmt_cal[&#39;duration_holdout&#39;], <br>                                 df_rfmt_cal[&#39;frequency_cal&#39;], df_rfmt_cal[&#39;recency_cal&#39;], df_rfmt_cal[&#39;T_cal&#39;]), columns=[&#39;pred_frequency&#39;]).reset_index()<br>    # Merging the two dataframes and dropping NaN values. <br>    new_df = df_rfmt_cal.reset_index().merge(pred_freq, on=&#39;CustomerID&#39;).dropna()<br><br>    # Computing the rmse score <br>    rmse_score = np.sqrt(mean_squared_error(new_df[&#39;frequency_holdout&#39;],new_df[&#39;pred_frequency&#39;]))<br>    l2_list.append(coef)<br>    rmse_list.append(rmse_score)<br><br># Getting the results <br>resl = pd.DataFrame(np.array(rmse_list), columns=[&#39;rmse_score&#39;])\<br>             .merge(pd.DataFrame(np.array(l2_list), columns=[&#39;L2 coefs&#39;]), right_index=True, left_index=True)<br>resl</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/293/1*4z1cAdGNyG1NAPiclLGjkA.jpeg" /></figure><p>We fit the model with the best L2 coefficient which is 0.80.</p><pre># Fitting the model using the calibration dataset.<br>model = BetaGeoFitter(penalizer_coef=0.80)<br>model.fit(df_rfmt_cal[&#39;frequency_cal&#39;], <br>        df_rfmt_cal[&#39;recency_cal&#39;], <br>        df_rfmt_cal[&#39;T_cal&#39;])</pre><p>We can inspect the performance of the model visually using the method “plot_calibration_purchases_vs_holdout_purchases” which compares the predicted purchases(the frequency values predicted by the model) to the holdout purchases (the frequency values not seen by the model).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*h0bndMWfotuIBMvVWqLizQ.png" /></figure><p>From the plot we can see that the model is doing a good job predicting the frequencies in the holdout dataset.</p><h4>Predicting the next six months CLV for each customer.</h4><p>After fitting the model, the first thing to do is to predict the number of purchases that each customer will make in the next six months . To do this we will use the method; “conditional_expected_number_of_purchases_up_to_time” .</p><pre># Predicting the number of purchases in the next 180 days for all customers.<br>df_rfmt[&#39;predicted_purchases&#39;] = model.conditional_expected_number_of_purchases_up_to_time(180, <br>                                                                                      df_rfmt[&#39;frequency&#39;], <br>                                                                                      df_rfmt[&#39;recency&#39;], <br>                                                                                      df_rfmt[&#39;T&#39;])<br>df_rfmt.dropna(inplace=True)<br># Getting rid of negative values.<br>df_rfmt = df_rfmt[df_rfmt[&#39;monetary_value&#39;]&gt;0]<br>df_rfmt</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*Yyyo2FJzdFRcvYBmpW5PTA.jpeg" /></figure><p>The next thing to do is to fit the Gamma-Gamma model which we will use to predict the spending of each customer in the next six months.</p><pre># Fitting the GammaGamma model <br><br>gg_model = GammaGammaFitter()<br>gg_model.fit(df_rfmt[&#39;frequency&#39;], df_rfmt[&#39;monetary_value&#39;])</pre><p>Now, we can predict the spending using the Gamma-Gamma model’s method ; “conditional_expected_average_profit”.</p><pre>df_rfmt[&#39;pred_monetary&#39;] = gg_model.conditional_expected_average_profit(<br>        df_rfmt[&#39;frequency&#39;],<br>        df_rfmt[&#39;monetary_value&#39;])<br>df_rfmt</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6XzSE1LwxUgtmgMZd8XLUQ.jpeg" /></figure><p>Finally, let’s compute the CLV for the next six months for each customer using the method ; “ customer_lifetime_value”.</p><pre># Predicting the CLV.<br>df_rfmt[&#39;CLV&#39;] = gg_model.customer_lifetime_value(<br>    model,<br>    df_rfmt[&#39;frequency&#39;],<br>    df_rfmt[&#39;recency&#39;],<br>    df_rfmt[&#39;T&#39;],<br>    df_rfmt[&#39;monetary_value&#39;],<br>    time = 6,# In months <br>    )<br>df_rfmt </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VuVB7mCLGvJBrclX1FJvDA.jpeg" /></figure><h4>Customer segmentation using the KMeans algorithm.</h4><p>The next step in our case study is to cluster the customers using the generated data and the clustering algorithm KMeans. the first thing to do is to find the optimal number of clusters for our dataset. To achieve this we will use the “Elbow” method which will be implemented using the “KElbowVisualizer” from the Yellowbrick library.</p><pre>from yellowbrick.cluster import KElbowVisualizer<br><br><br># Instantiate the clustering model and visualizer<br>km_model = KMeans()<br>visualizer = KElbowVisualizer(km_model, k=(2,10))<br><br>visualizer.fit(df_rfmt)        # Fit the data to the visualizer<br>visualizer.show()        # Finalize and render the figure<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*STpu6K1MrmEeI2vSJWdVEA.png" /></figure><p>We can see that the optimal number of clusters or the elbow value is 4.</p><p>Note that the distortion score used to find the optimal number of clusters is the sum of squared errors, given that the error for a given point is the distance between this point and the centroid to which it was assigned.</p><p>We can now fit the KMeans model with the optimal number of clusters.</p><pre>km_model = KMeans(n_clusters=4)<br>km_model.fit(df_rfmt)<br># Creating a new column called cluster whose values are the corresponding cluster for each point.<br>df_rfmt[&#39;cluster&#39;] = km_model.labels_</pre><p>Let’s group by clusters to verify if there is a difference between the clusters in terms of CLV.</p><pre># Grouping by clusters<br>df_clusters = df_rfmt.groupby([&#39;cluster&#39;])[&#39;CLV&#39;]\<br>                     .agg([&#39;mean&#39;, &quot;count&quot;])\<br>                     .reset_index()<br><br>df_clusters.columns = [&quot;cluster&quot;, &quot;avg_CLV&quot;, &quot;n_customers&quot;]<br><br>df_clusters[&#39;perct_customers&#39;] = (df_clusters[&#39;n_customers&#39;]/df_clusters[&#39;n_customers&#39;]\<br>                                  .sum())*100<br>df_clusters </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/641/1*QayEf1MiJVMZUhVdsCCxPw.png" /></figure><p>We can notice from the table that cluster 1 which represents 0.149 % of the customer base, has the highest average CLV which is equal to 37242.6£, followed by cluster 3 with an average CLV of 16019.64£, then we have cluster 2 in the third place with an average CLV of 4537.56£ followed by cluster 0 (97.86% of the customer base) with 395.08£ as an average CLV.</p><p>Let’s rename this clusters “Diamond”, “Gold”, “Sivler” and “Bronze” respectively.</p><pre>df_rfmt[&#39;customer_category&#39;] = df_rfmt[&#39;cluster&#39;]\<br>.replace({3:&quot;Gold&quot;, 1:&quot;Diamond&quot;, 2:&quot;Silver&quot;, 0:&quot;Bronze&quot;})</pre><p>Let’s visualize the differences in terms of CLV between the four groups.</p><pre># Grouping by customer category<br>df_cat = pd.DataFrame(df_rfmt.groupby([&#39;customer_category&#39;])[&#39;CLV&#39;]\<br>                      .agg(&#39;mean&#39;)).reset_index()<br><br><br># Code source : https://www.geeksforgeeks.org/how-to-annotate-bars-in-barplot-with-matplotlib-in-python/<br># Defining the plot size<br>plt.figure(figsize=(8, 8))<br> <br># Defining the values for x-axis, y-axis<br># and from which dataframe the values are to be picked<br>plots = sns.barplot(x=&quot;customer_category&quot;, y=&quot;CLV&quot;, data=df_cat)<br> <br># Iterating over the bars one-by-one<br>for bar in plots.patches:<br>   <br>  # Using Matplotlib&#39;s annotate function and<br>  # passing the coordinates where the annotation shall be done<br>  # x-coordinate: bar.get_x() + bar.get_width() / 2<br>  # y-coordinate: bar.get_height()<br>  # free space to be left to make graph pleasing: (0, 8)<br>  # ha and va stand for the horizontal and vertical alignment<br>    plots.annotate(format(bar.get_height(), &#39;.2f&#39;),<br>                   (bar.get_x() + bar.get_width() / 2,<br>                    bar.get_height()), ha=&#39;center&#39;, va=&#39;center&#39;,<br>                   size=15, xytext=(0, 8),textcoords=&#39;offset points&#39;)<br><br>plt.xlabel(&quot;Customer category&quot;, size=14)<br> <br># Setting the label for y-axis<br>plt.ylabel(&quot;CLV&quot;, size=14)<br> <br># Setting the title for the graph<br>plt.title(&quot;CLV per category&quot;)<br> <br># Finally showing the plot<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/513/1*Q9u-yxGCpj20NgeyCBgXtA.png" /></figure><p>let’s compute the contribution of each category to the total CLV of the next six months and visualize the results</p><pre>df_cat[&quot;contribution_to_CLV&quot;] = df_cat[&#39;CLV&#39;]/df_cat[&#39;CLV&#39;].sum()*100<br><br># Code source : https://www.geeksforgeeks.org/how-to-annotate-bars-in-barplot-with-matplotlib-in-python/<br># Defining the plot size<br>plt.figure(figsize=(8, 8))<br> <br># Defining the values for x-axis, y-axis<br># and from which dataframe the values are to be picked<br>plots = sns.barplot(x=&quot;customer_category&quot;, y=&quot;contribution_to_CLV&quot;, data=df_cat)<br> <br># Iterating over the bars one-by-one<br>for bar in plots.patches:<br>   <br>  # Using Matplotlib&#39;s annotate function and<br>  # passing the coordinates where the annotation shall be done<br>  # x-coordinate: bar.get_x() + bar.get_width() / 2<br>  # y-coordinate: bar.get_height()<br>  # free space to be left to make graph pleasing: (0, 8)<br>  # ha and va stand for the horizontal and vertical alignment<br>    plots.annotate(format(bar.get_height(), &#39;.2f&#39;),<br>                   (bar.get_x() + bar.get_width() / 2,<br>                    bar.get_height()), ha=&#39;center&#39;, va=&#39;center&#39;,<br>                   size=15, xytext=(0, 8),textcoords=&#39;offset points&#39;)<br><br>plt.xlabel(&quot;Customer category&quot;, size=12)<br><br># Setting the label for y-axis<br>plt.ylabel(&quot;contribution to CLV in %&quot;, size=12)<br> <br># Setting the title for the graph<br>plt.title(&quot;The contribution of each category to the total CLV of the next six months&quot;)<br> <br># Finally showing the plot<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*n6W3f03f7pfbqV_MFK9eCw.png" /></figure><p>We can see from the bar plot that “Diamond” and “Gold” customers contribute to more than 90% of the total CLV of the next six months even though they represent less than 1% of the customer base.</p><p>and the contribution of “Bronze” customers is less than 1% of the total CLV, even though they represent more than 97% of the customer base.</p><h4>Analyzing the frequency metric.</h4><p>Let’s start by investigating the distribution of the metric.</p><pre>sns.displot(df_rfmt[&#39;frequency&#39;])<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/1*Yn1FvFqcSZwxmkmUNdLFVQ.png" /></figure><p>We’ll now investigate the frequency for each customer category.</p><pre>df_freq = pd.DataFrame(df_rfmt.groupby([&#39;customer_category&#39;])[&#39;frequency&#39;]\<br>                              .mean()\<br>                              .reset_index())<br><br><br># Code source : https://www.geeksforgeeks.org/how-to-annotate-bars-in-barplot-with-matplotlib-in-python/<br># Defining the plot size<br>plt.figure(figsize=(8, 8))<br> <br># Defining the values for x-axis, y-axis<br># and from which dataframe the values are to be picked<br>plots = sns.barplot(x=&quot;customer_category&quot;, y=&quot;frequency&quot;, data=df_freq)<br> <br># Iterating over the bars one-by-one<br>for bar in plots.patches:<br>   <br>  # Using Matplotlib&#39;s annotate function and<br>  # passing the coordinates where the annotation shall be done<br>  # x-coordinate: bar.get_x() + bar.get_width() / 2<br>  # y-coordinate: bar.get_height()<br>  # free space to be left to make graph pleasing: (0, 8)<br>  # ha and va stand for the horizontal and vertical alignment<br>    plots.annotate(format(bar.get_height(), &#39;.2f&#39;),<br>                   (bar.get_x() + bar.get_width() / 2,<br>                    bar.get_height()), ha=&#39;center&#39;, va=&#39;center&#39;,<br>                   size=15, xytext=(0, 8),textcoords=&#39;offset points&#39;)<br><br>plt.xlabel(&quot;Customer category&quot;, size=14)<br> <br># Setting the label for y-axis<br>plt.ylabel(&quot;Frequency&quot;, size=14)<br> <br># Setting the title for the graph<br>plt.title(&quot;Frequency per category&quot;)<br> <br># Finally showing the plot<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*C3PjK4De7Z2M2qBm4ykMZg.png" /></figure><p>We can see that “Gold” customers have the highest mean frequency.</p><p>By analyzing the data we found that 90% of the customers made less than 10 purchases. With the following visualizations we will try to find which customer categories are omnipresent in this group of customers and which ones are not.</p><pre># Getting the number of customers per category for those with a frequency less than 10.<br><br>df_freq_2 = pd.DataFrame(df_rfmt[df_rfmt[&#39;frequency&#39;] &lt; 10]\<br>                        .groupby([&#39;customer_category&#39;])[&#39;customer_category&#39;]\<br>                        .agg(&#39;count&#39;))<br><br>df_freq_2.columns=[&#39;n_customers&#39;]<br><br>df_freq_2 = df_freq_2.reset_index()<br><br><br># Code source : https://www.geeksforgeeks.org/how-to-annotate-bars-in-barplot-with-matplotlib-in-python/<br># Defining the plot size<br>plt.figure(figsize=(8, 8))<br> <br># Defining the values for x-axis, y-axis<br># and from which dataframe the values are to be picked<br>plots = sns.barplot(x=&quot;customer_category&quot;, y=&quot;n_customers&quot;, data=df_freq_2)<br> <br># Iterating over the bars one-by-one<br>for bar in plots.patches:<br>   <br>  # Using Matplotlib&#39;s annotate function and<br>  # passing the coordinates where the annotation shall be done<br>  # x-coordinate: bar.get_x() + bar.get_width() / 2<br>  # y-coordinate: bar.get_height()<br>  # free space to be left to make graph pleasing: (0, 8)<br>  # ha and va stand for the horizontal and vertical alignment<br>    plots.annotate(format(bar.get_height(), &#39;.2f&#39;),<br>                   (bar.get_x() + bar.get_width() / 2,<br>                    bar.get_height()), ha=&#39;center&#39;, va=&#39;center&#39;,<br>                   size=15, xytext=(0, 8),textcoords=&#39;offset points&#39;)<br><br>plt.xlabel(&quot;Customer category&quot;, size=14)<br> <br># Setting the label for y-axis<br>plt.ylabel(&quot;number of customers&quot;, size=14)<br><br># Setting the title for the graph<br>plt.title(&quot;Nombre of customers per category for those with frequency less than 10&quot;)<br> <br># Finally showing the plot<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*aD0udkQulCtw9M4bdd5wsQ.png" /></figure><pre># Getting the number of customers per category for those with a frequency greater than 10.<br><br>df_freq_1 = pd.DataFrame(df_rfmt[df_rfmt[&#39;frequency&#39;] &gt; 10]\<br>                        .groupby([&#39;customer_category&#39;])[&#39;customer_category&#39;]\<br>                        .agg(&#39;count&#39;))<br><br>df_freq_1.columns = [&#39;n_customers&#39;]<br><br>df_freq_1 = df_freq_1.reset_index()<br><br><br># Code source : https://www.geeksforgeeks.org/how-to-annotate-bars-in-barplot-with-matplotlib-in-python/<br># Defining the plot size<br>plt.figure(figsize=(8, 8))<br> <br># Defining the values for x-axis, y-axis<br># and from which dataframe the values are to be picked<br>plots = sns.barplot(x=&quot;customer_category&quot;, y=&quot;n_customers&quot;, data=df_freq_1)<br> <br># Iterating over the bars one-by-one<br>for bar in plots.patches:<br>   <br>  # Using Matplotlib&#39;s annotate function and<br>  # passing the coordinates where the annotation shall be done<br>  # x-coordinate: bar.get_x() + bar.get_width() / 2<br>  # y-coordinate: bar.get_height()<br>  # free space to be left to make graph pleasing: (0, 8)<br>  # ha and va stand for the horizontal and vertical alignment<br>    plots.annotate(format(bar.get_height(), &#39;.2f&#39;),<br>                   (bar.get_x() + bar.get_width() / 2,<br>                    bar.get_height()), ha=&#39;center&#39;, va=&#39;center&#39;,<br>                   size=15, xytext=(0, 8),textcoords=&#39;offset points&#39;)<br><br>plt.xlabel(&quot;Customer category&quot;, size=14)<br> <br># Setting the label for y-axis<br>plt.ylabel(&quot;number of customers&quot;, size=14)<br><br># Setting the title for the graph<br>plt.title(&quot;Nombre of customers per category for those with frequency higher than 10&quot;)<br> <br># Finally showing the plot<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*gAgZuFQIdj2OmEET8hp17A.png" /></figure><p>As we can see from the plots above, the distribution of the frequency variable is so skewed towards the left, with 90 % of customers having a frequency less than 10.</p><p>There are no “Diamond” customers with a frequency less than 10, so this means that all “Diamond” customers made more than 10 purchases during the given period. On average they made more than 25 purchases (see the Frequency per category plot).</p><p>Six out of seven “Gold” customers made more than 10 purchases in the given period, on average, they made more than 39 purchases.</p><h4>Analyzing the monetary value.</h4><p>Let’s start by examining the distribution of this variable.</p><pre>sns.displot(df_rfmt[&#39;monetary_value&#39;])<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/1*rTWjkDmiQ6ibnAXY9No3qw.png" /></figure><p>We will now explore the monetary value for each customer category.</p><pre>df_mon = pd.DataFrame(df_rfmt.groupby([&#39;customer_category&#39;])[&#39;monetary_value&#39;].mean().reset_index())<br><br># Code source : https://www.geeksforgeeks.org/how-to-annotate-bars-in-barplot-with-matplotlib-in-python/<br># Defining the plot size<br>plt.figure(figsize=(8, 8))<br> <br># Defining the values for x-axis, y-axis<br># and from which dataframe the values are to be picked<br>plots = sns.barplot(x=&quot;customer_category&quot;, y=&quot;monetary_value&quot;, data=df_mon)<br> <br># Iterating over the bars one-by-one<br>for bar in plots.patches:<br>   <br>  # Using Matplotlib&#39;s annotate function and<br>  # passing the coordinates where the annotation shall be done<br>  # x-coordinate: bar.get_x() + bar.get_width() / 2<br>  # y-coordinate: bar.get_height()<br>  # free space to be left to make graph pleasing: (0, 8)<br>  # ha and va stand for the horizontal and vertical alignment<br>    plots.annotate(format(bar.get_height(), &#39;.2f&#39;),<br>                   (bar.get_x() + bar.get_width() / 2,<br>                    bar.get_height()), ha=&#39;center&#39;, va=&#39;center&#39;,<br>                   size=15, xytext=(0, 8),textcoords=&#39;offset points&#39;)<br><br>plt.xlabel(&quot;Customer category&quot;, size=14)<br> <br># Setting the label for y-axis<br>plt.ylabel(&quot;average monetary value&quot;, size=14)<br> <br># Setting the title for the graph<br>plt.title(&quot;average monetary value per category&quot;)<br> <br># Finally showing the plot<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*aNoGgdFREqgDAF0HvPcGVw.png" /></figure><pre># Getting the number of customers per category for those with a monetary value greater than the 80th percentile.<br><br># Getting the 80th percentile <br>perct = df_rfmt[&#39;monetary_value&#39;].quantile(q=0.8)<br><br>df_mon_1 = pd.DataFrame(df_rfmt[df_rfmt[&#39;monetary_value&#39;] &gt; perct]\<br>                        .groupby([&#39;customer_category&#39;])[&#39;customer_category&#39;]\<br>                        .agg(&#39;count&#39;))<br><br>df_mon_1.columns = [&#39;n_customers&#39;]<br><br>df_mon_1 = df_mon_1.reset_index()<br><br><br># Code source : https://www.geeksforgeeks.org/how-to-annotate-bars-in-barplot-with-matplotlib-in-python/<br># Defining the plot size<br>plt.figure(figsize=(8, 8))<br> <br># Defining the values for x-axis, y-axis<br># and from which dataframe the values are to be picked<br>plots = sns.barplot(x=&quot;customer_category&quot;, y=&quot;n_customers&quot;, data=df_mon_1)<br> <br># Iterating over the bars one-by-one<br>for bar in plots.patches:<br>   <br>  # Using Matplotlib&#39;s annotate function and<br>  # passing the coordinates where the annotation shall be done<br>  # x-coordinate: bar.get_x() + bar.get_width() / 2<br>  # y-coordinate: bar.get_height()<br>  # free space to be left to make graph pleasing: (0, 8)<br>  # ha and va stand for the horizontal and vertical alignment<br>    plots.annotate(format(bar.get_height(), &#39;.2f&#39;),<br>                   (bar.get_x() + bar.get_width() / 2,<br>                    bar.get_height()), ha=&#39;center&#39;, va=&#39;center&#39;,<br>                   size=15, xytext=(0, 8),textcoords=&#39;offset points&#39;)<br><br>plt.xlabel(&quot;Customer category&quot;, size=14)<br> <br># Setting the label for y-axis<br>plt.ylabel(&quot;number of customers&quot;, size=14)<br> <br># Setting the title for the graph<br>plt.title(&quot;Nombre of customers per category for those with a monetary value greater than the 80th percentile.&quot;)<br> <br># Finally showing the plot<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/1*F-tlTwsp_5M05DypGkzlYw.png" /></figure><pre># Getting the number of customers per category for those with a monetary value less than the 80th percentile.<br><br>df_mon_2= pd.DataFrame(df_rfmt[df_rfmt[&#39;monetary_value&#39;] &lt; perct]\<br>                        .groupby([&#39;customer_category&#39;])[&#39;customer_category&#39;]\<br>                        .agg(&#39;count&#39;))<br><br>df_mon_2.columns = [&#39;n_customers&#39;]<br><br>df_mon_2 = df_mon_2.reset_index()<br><br><br># Code source : https://www.geeksforgeeks.org/how-to-annotate-bars-in-barplot-with-matplotlib-in-python/<br># Defining the plot size<br>plt.figure(figsize=(8, 8))<br> <br># Defining the values for x-axis, y-axis<br># and from which dataframe the values are to be picked<br>plots = sns.barplot(x=&quot;customer_category&quot;, y=&quot;n_customers&quot;, data=df_mon_2)<br> <br># Iterating over the bars one-by-one<br>for bar in plots.patches:<br>   <br>  # Using Matplotlib&#39;s annotate function and<br>  # passing the coordinates where the annotation shall be done<br>  # x-coordinate: bar.get_x() + bar.get_width() / 2<br>  # y-coordinate: bar.get_height()<br>  # free space to be left to make graph pleasing: (0, 8)<br>  # ha and va stand for the horizontal and vertical alignment<br>    plots.annotate(format(bar.get_height(), &#39;.2f&#39;),<br>                   (bar.get_x() + bar.get_width() / 2,<br>                    bar.get_height()), ha=&#39;center&#39;, va=&#39;center&#39;,<br>                   size=15, xytext=(0, 8),textcoords=&#39;offset points&#39;)<br><br>plt.xlabel(&quot;Customer category&quot;, size=14)<br> <br># Setting the label for y-axis<br>plt.ylabel(&quot;number of customers&quot;, size=14)<br> <br># Setting the title for the graph<br>plt.title(&quot;Nombre of customers per category for those with a monetary value less than the 80th percentile.&quot;)<br> <br># Finally showing the plot<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*KiNjjXA_kXgyojjEcgAB4Q.png" /></figure><p>The distribution plot shows a distribution highly skewed towards the left, meaning that a large number of customers spent small amounts of money and few customers spent relatively high amounts of money during the given period.</p><p>The 80th percentile is 211£, meaning that 80 % of the customers spent less than 211£ during the given period. 100% of these customers with a spending less than 211£ are “Bronze” and “Silver” customers.</p><p>“Diamond” and “Gold” customers spent more than the 80th percentile, with an average spending of 2355.36£ for “Gold” customers and an average spending of 2772£ for “Diamond” customers (see the average spending per category plot).</p><h4>Conclusion :</h4><p>In conclusion, the BG/NBD and Gamma-Gamma models are powerful tools for predicting customer lifetime value in a non-contractual setting. By using a real-world dataset from an online retailer, we were able to demonstrate the effectiveness of these models in providing valuable information about customer behavior and buying patterns. This information can be used to develop efficient marketing strategies to retain and attract high-value customers.</p><p>References :</p><ul><li>Fader, P.S., Hardie, B.G. and Lee, K.L., 2005. “Counting your customers” the easy way: An alternative to the Pareto/NBD model. Marketing science, 24(2), pp.275–284.</li><li>Fader, P.S. and Hardie, B.G., 2013. The Gamma-Gamma model of monetary value.</li><li>Farris, Paul W.; Neil T. Bendle; Phillip E. Pfeifer; David J. Reibstein (2010). Marketing Metrics: The Definitive Guide to Measuring Marketing Performance. Upper Saddle River, New Jersey: Pearson Education,</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a937c60fe7f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>