<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ topic modeling - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ topic modeling - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Tue, 23 Jun 2026 22:45:25 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/topic-modeling/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Topic Modeling Tutorial – How to Use SVD and NMF in Python ]]>
                </title>
                <description>
                    <![CDATA[ In the context of Natural Language Processing (NLP), topic modeling is an unsupervised learning problem whose goal is to find abstract topics in a collection of documents.  Topic Modeling answers the question: "Given a text corpus of many documents, ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/advanced-topic-modeling-how-to-use-svd-nmf-in-python/</link>
                <guid isPermaLink="false">66bb8b17c332a9c775d15b66</guid>
                
                    <category>
                        <![CDATA[ natural language processing ]]>
                    </category>
                
                    <category>
                        <![CDATA[ nlp ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ topic modeling ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Bala Priya C ]]>
                </dc:creator>
                <pubDate>Tue, 21 Feb 2023 18:32:38 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/02/brett-jordan-M3cxjDNiLlQ-unsplash-cover-img.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In the context of Natural Language Processing (NLP), <strong>topic modeling</strong> is an unsupervised learning problem whose goal is to find abstract topics in a collection of documents. </p>
<p><strong>Topic Modeling</strong> answers the question: "Given a text corpus of many documents, can we find the abstract topics that the text is talking about?"</p>
<p>In this tutorial, you’ll:</p>
<ul>
<li>Learn about two powerful matrix factorization techniques - <strong>Singular Value Decomposition (SVD)</strong> and <strong>Non-negative Matrix Factorization (NMF)</strong></li>
<li>Use them to find topics in a collection of documents</li>
</ul>
<p>By the end of this tutorial, you'll be able to build your own topic models to find topics in any piece of text.📚📑 </p>
<p>Let's get started.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><a class="post-section-overview" href="#heading-what-is-topic-modeling">What is Topic Modeling?</a></li>
<li><a class="post-section-overview" href="#heading-tf-idf-score-equation">TF-IDF Score Equation</a></li>
<li><a class="post-section-overview" href="#heading-topic-modeling-using-singular-value-decomposition-svd">Topic Modeling Using Singular Value Decomposition (SVD)</a></li>
<li><a class="post-section-overview" href="#heading-what-is-truncated-svd-or-k-svd">What is Truncated SVD or k-SVD?</a></li>
<li><a class="post-section-overview" href="#heading-topic-modeling-using-non-negative-matrix-factorization-nmf">Topic Modeling Using Non-Negative Matrix Factorization (NMF)</a></li>
<li><a class="post-section-overview" href="#heading-7-steps-to-use-svd-for-topic-modeling">7 Steps to Use SVD for Topic Modeling</a></li>
<li><a class="post-section-overview" href="#heading-how-to-visualize-topics-as-word-clouds">How to Visualize Topics as Word Clouds</a></li>
<li><a class="post-section-overview" href="#heading-how-to-use-nmf-for-topic-modeling">How to Use NMF for Topic Modeling</a></li>
<li><a class="post-section-overview" href="#heading-svd-vs-nmf-an-overview-of-the-differences">SVD vs NMF – An Overview of the Differences</a></li>
</ol>
<h2 id="heading-what-is-topic-modeling">What is Topic Modeling?</h2>
<p>Let's start by understanding what topic modeling is.</p>
<p>Suppose you're given a large text corpus containing several documents. You'd like to know the <strong>key topics</strong> that reside in the given collection of documents without reading through each document.</p>
<p>Topic Modeling helps you distill the information in the large text corpus into a certain number of topics. Topics are groups of words that are <em>similar in context</em> and are indicative of the information in the collection of documents.</p>
<p>The general structure of the Document-Term Matrix for a text corpus containing <code>M</code> documents, and <code>N</code> terms in all, is shown below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/1.png" alt="Image" width="600" height="400" loading="lazy">
<em>Structure of the Document-Term Matrix</em></p>
<p>Let's parse the matrix representation:</p>
<ul>
<li>D1, D2, ..., DM are the M documents.</li>
<li>T1, T2, ..., TN are the N terms</li>
</ul>
<p>To populate the Document-Term Matrix, let’s use the widely-used metric—the TF-IDF Score.</p>
<h2 id="heading-tf-idf-score-equation">TF-IDF Score Equation</h2>
<p>The TF-IDF score is given by the following equation:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>where,</p>
<ul>
<li><code>TF_ij</code> is the number of times the term  <code>Tj</code> occurs in the document  <code>Di</code>.</li>
<li><code>dfj</code> is the number of documents containing the term <code>Tj</code></li>
</ul>
<p>A term that occurs frequently in a particular document, and rarely across the entire corpus has a higher IDF score. </p>
<p>I hope you’ve now gained a cursory understanding of the DTM and the TF-IDF score. Let’s now go over the matrix factorization techniques.</p>
<h2 id="heading-topic-modeling-using-singular-value-decomposition-svd">Topic Modeling Using Singular Value Decomposition (SVD)</h2>
<p>The use of Singular Value Decomposition (SVD) for topic modeling is explained in the figure below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/3.jpeg" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Singular Value Decomposition on the the Document-Term Matrix D gives the following three matrices:</p>
<ul>
<li>The left singular vector matrix <strong>U</strong>. This matrix is obtained by the eigen decomposition of the Gram matrix <strong>D.D_T</strong>—also called the document similarity matrix. The i,j-th entry of the document similarity matrix signifies how similar document <code>i</code> is to document <code>j</code>.</li>
<li>The matrix of singular values <strong>S</strong>, which (values) signify the relative importance of topics.</li>
<li>The right singular vector matrix <strong>V_T</strong>, which is also called the term topic matrix. The topics in the text reside along the rows of this matrix.</li>
</ul>
<p>If you'd like to refresh the concept of eigen decomposition, here's an excellent tutorial by <a target="_blank" href="https://www.youtube.com/c/3blue1brown">Grant Sanderson from 3Blue1Brown</a>. It explains eigenvectors and eigenvalues visually.</p>
<p><a target="_blank" href="https://www.youtube.com/embed/PFDu9oVAE-g">Embedded content</a></p>
<p>It's totally fine if you find the working of SVD a bit difficult to understand. 🙂 For now, you may think of SVD as a black box that operates on your Document-Term Matrix (DTM) and yields 3 matrices, <strong>U, S</strong>, and <strong>V_T</strong>. And the topics reside along the rows of the matrix <strong>V_T</strong>. </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/4.jpeg" alt="Image" width="600" height="400" loading="lazy"></p>
<p><strong>Note</strong>: SVD is also called <strong>Latent Semantic Indexing (LSI).</strong></p>
<h2 id="heading-what-is-truncated-svd-or-k-svd">What is Truncated SVD or k-SVD?</h2>
<p>Suppose you have a text corpus of 150 documents. Would you prefer skimming through 150 different topics that describe the corpus, or would you be happy reading through 10 topics that can convey the content of the corpus?</p>
<p>Well, it's often helpful to fix a small number of topics that best convey the content of the text. And this is what motivates <strong>k-SVD</strong>.</p>
<p>As matrix multiplication requires a lot of computation, it's preferred to choose the <strong>k largest singular values</strong>, and the topics corresponding to them. The working of k-SVD is illustrated below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/5.jpeg" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-topic-modeling-using-non-negative-matrix-factorization-nmf">Topic Modeling Using Non-Negative Matrix Factorization (NMF)</h2>
<p>Non-negative Matrix Factorization (NMF) works as shown below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/6.jpeg" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Non-negative Matrix Factorization acts on the Document-Term Matrix and yields the following:</p>
<ul>
<li>The matrix <strong>W</strong> which is called the <strong>document-topic matrix</strong>. This matrix shows the distribution of the topics across the documents in the corpus.</li>
<li>The matrix <strong>H</strong> which is also called the <strong>term-topic matrix</strong>. This matrix captures the significance of terms across the topics.</li>
</ul>
<p>NMF is easier to interpret as all the elements of the matrices <strong>W</strong> and <strong>H</strong> are now non-negative. So a higher score corresponds to greater relevance.</p>
<p><strong>But how do we get matrices W and H?</strong> </p>
<p>NMF is a <em>non-exact</em> matrix factorization technique. This means that you cannot multiply W and H to get back the original document-term matrix V. </p>
<p>The matrices W and H are initialized randomly. And the algorithm is run iteratively until we find a W and H that minimize the cost function. </p>
<p>The cost function is the Frobenius norm of the matrix <strong>V - W.H</strong>, as shown below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/e2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The Frobenius norm of a matrix A with m rows and n columns is given by the following equation:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/e3.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-7-steps-to-use-svd-for-topic-modeling">7 Steps to Use SVD for Topic Modeling</h2>
<p>1️⃣ To use SVD to get topics, let's first get a text corpus. The following code cell contains a piece of text on <a target="_blank" href="https://en.wikipedia.org/wiki/Computer_programming">computer programming</a>.</p>
<pre><code class="lang-python">text=[<span class="hljs-string">"Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a specific task."</span>,

      <span class="hljs-string">"Programming involves tasks such as: analysis, generating algorithms, profiling algorithms' accuracy and resource consumption, and the implementation of algorithms in a chosen programming language (commonly referred to as coding)."</span>,

      <span class="hljs-string">"The source program is written in one or more languages that are intelligible to programmers, rather than machine code, which is directly executed by the central processing unit."</span>,

      <span class="hljs-string">"The purpose of programming is to find a sequence of instructions that will automate the performance of a task (which can be as complex as an operating system) on a computer, often for solving a given problem."</span>,

      <span class="hljs-string">"Proficient programming thus often requires expertise in several different subjects, including knowledge of the application domain, specialized algorithms, and formal logic."</span>,

      <span class="hljs-string">"Tasks accompanying and related to programming include: testing, debugging, source code maintenance, implementation of build systems, and management of derived artifacts, such as the machine code of computer programs."</span>,

      <span class="hljs-string">"These might be considered part of the programming process, but often the term software development is used for this larger process with the term programming, implementation, or coding reserved for the actual writing of code."</span>,

      <span class="hljs-string">"Software engineering combines engineering techniques with software development practices."</span>,

    <span class="hljs-string">"Reverse engineering is a related process used by designers, analysts and programmers to understand and re-create/re-implement"</span>]
</code></pre>
<p>The text for which you need to find topics is now ready.</p>
<p>2️⃣ The next step is to import the <code>TfidfVectorizer</code> class from scikit-learn's feature extraction module for text data:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> sklearn.feature_extraction.text <span class="hljs-keyword">import</span> TfidfVectorizer
</code></pre>
<p>You'll use the <code>TfidfVectorizer</code> class to get the DTM populated with the TF-IDF scores for the text corpus.</p>
<p>3️⃣ To use <strong>Truncated SVD (k-SVD)</strong> discussed earlier, you need to import the <code>TruncatedSVD</code> class from scikit-learn's <code>decomposition</code> module:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> sklearn.decomposition <span class="hljs-keyword">import</span> TruncatedSVD
</code></pre>
<p>▶ Now that you've imported all the necessary modules, it's time to start your quest for topics in the text.</p>
<p>4️⃣ In this step, you'll instantiate a <code>Tfidfvectorizer</code> object. Let's call it vectorizer.</p>
<pre><code class="lang-python">vectorizer = TfidfVectorizer(stop_words=<span class="hljs-string">'english'</span>,smooth_idf=<span class="hljs-literal">True</span>) 
<span class="hljs-comment"># under the hood - lowercasing,removing special chars,removing stop words</span>
input_matrix = vectorizer.fit_transform(text).todense()
</code></pre>
<p>So far, you've:</p>
<p>☑ collected the text,<br>☑ imported the necessary modules, and<br>☑ obtained the input DTM.</p>
<p>Now you'll proceed with using SVD to obtain topics.</p>
<p>5️⃣ You'll now use the <code>TruncatedSVD</code> class that you imported in step 3️⃣.</p>
<pre><code class="lang-python">svd_modeling= TruncatedSVD(n_components=<span class="hljs-number">4</span>, algorithm=<span class="hljs-string">'randomized'</span>, n_iter=<span class="hljs-number">100</span>, random_state=<span class="hljs-number">122</span>)
svd_modeling.fit(input_matrix)
components=svd_modeling.components_
vocab = vectorizer.get_feature_names()
</code></pre>
<p>6️⃣ Let’s write a function that gets the topics for us.</p>
<pre><code class="lang-python">topic_word_list = []
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_topics</span>(<span class="hljs-params">components</span>):</span> 
  <span class="hljs-keyword">for</span> i, comp <span class="hljs-keyword">in</span> enumerate(components):
    terms_comp = zip(vocab,comp)
  sorted_terms = sorted(terms_comp, key= <span class="hljs-keyword">lambda</span> x:x[<span class="hljs-number">1</span>], reverse=<span class="hljs-literal">True</span>)[:<span class="hljs-number">7</span>]
     topic=<span class="hljs-string">" "</span>
     <span class="hljs-keyword">for</span> t <span class="hljs-keyword">in</span> sorted_terms:
      topic= topic + <span class="hljs-string">' '</span> + t[<span class="hljs-number">0</span>]
     topic_word_list.append(topic)
     print(topic_word_list)
  <span class="hljs-keyword">return</span> topic_word_list
get_topics(components)
</code></pre>
<p>7️⃣ And it's time to view the topics, and see if they make sense. When you call the <code>get_topics()</code> function with the components obtained from SVD as the argument, you'll get a list of topics, and the top words in each of those topics.</p>
<pre><code class="lang-python">Topic <span class="hljs-number">1</span>: 
  code programming process software term computer engineering

Topic <span class="hljs-number">2</span>: 
  engineering software development combines practices techniques used

Topic <span class="hljs-number">3</span>: 
  code machine source central directly executed intelligible

Topic <span class="hljs-number">4</span>: 
  computer specific task automate complex given instructions
</code></pre>
<p>And you have your topics in just 7 steps. Do the topics look good?</p>
<h2 id="heading-how-to-visualize-topics-as-word-clouds">How to Visualize Topics as Word Clouds</h2>
<p>In the previous section, you printed out the topics, and made sense of the topics using the top words in each topic.</p>
<p>Another popular visualization method for topics is the <strong>word cloud</strong>. In a word cloud, the terms in a particular topic are displayed in terms of their <strong>relative significance</strong>. The most important word has the largest font size, and so on.</p>
<pre><code class="lang-python">!pip install wordcloud
<span class="hljs-keyword">from</span> wordcloud <span class="hljs-keyword">import</span> WordCloud
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">4</span>):
  wc = WordCloud(width=<span class="hljs-number">1000</span>, height=<span class="hljs-number">600</span>, margin=<span class="hljs-number">3</span>,  prefer_horizontal=<span class="hljs-number">0.7</span>,scale=<span class="hljs-number">1</span>,background_color=<span class="hljs-string">'black'</span>, relative_scaling=<span class="hljs-number">0</span>).generate(topic_word_list[i])
  plt.imshow(wc)
  plt.title(<span class="hljs-string">f"Topic<span class="hljs-subst">{i+<span class="hljs-number">1</span>}</span>"</span>)
  plt.axis(<span class="hljs-string">"off"</span>)
  plt.show()
</code></pre>
<p>The word clouds for topics 1 through 4 are shown in the image grid below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/wc1.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>Topic Clouds from SVD</em></p>
<p>As you can see, the font-size of words indicate their relative importance in a topic. These word clouds are also called topic clouds.</p>
<h2 id="heading-how-to-use-nmf-for-topic-modeling">How to Use NMF for Topic Modeling</h2>
<p>In this section, you'll run through the same steps as in SVD. You need to first import the <code>NMF</code> class from scikit-learn's <code>decomposition</code> module.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> sklearn.decomposition <span class="hljs-keyword">import</span> NMF
NMF_model = NMF(n_components=<span class="hljs-number">4</span>, random_state=<span class="hljs-number">1</span>)
W = NMF_model.fit_transform(input_matrix)
H = NMF_model.components_
</code></pre>
<p>And then you may call the <code>get_topics()</code> function on the matrix <strong>H</strong> to get the topics.</p>
<pre><code class="lang-python">Topic <span class="hljs-number">1</span>: 
  code machine source central directly executed intelligible

Topic <span class="hljs-number">2</span>: 
  engineering software process development used term combines

Topic <span class="hljs-number">3</span>: 
  algorithms programming application different domain expertise formal

Topic <span class="hljs-number">4</span>: 
  computer specific task programming automate complex given
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/wc2.jpeg" alt="Image" width="600" height="400" loading="lazy">
<em>Topic Clouds from NMF</em></p>
<p>For the given piece of text, you can see that both SVD and NMF give similar topic clouds.</p>
<h2 id="heading-svd-vs-nmf-an-overview-of-the-differences">SVD vs NMF – An Overview of the Differences</h2>
<p>Now, let's put together the differences between these two matrix factorization techniques for topic modeling.</p>
<ul>
<li>SVD is an exact matrix factorization technique – you can reconstruct the input DTM from the resultant matrices.</li>
<li>If you choose to use k-SVD, it's the best possible k-rank approximation to the input DTM.</li>
<li>Though NMF is a non-exact approximation to the input DTM, it's known to capture more diverse topics than SVD.</li>
</ul>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>I hope you enjoyed this tutorial. As a next step, you may spin up your own Colab notebook using the code cells from this tutorial. You only have to plug in the piece of text that you'd like to find topics for, and you'd have your topics and word clouds ready!</p>
<p>Thank you for reading, and happy coding!</p>
<h3 id="heading-references-and-further-reading-on-topic-modeling">References and Further Reading on Topic Modeling</h3>
<ul>
<li><a target="_blank" href="https://www.fast.ai/2019/07/08/fastai-nlp/">A Code-First Approach to Natural Language Processing</a> by fast.ai</li>
<li><a target="_blank" href="https://www.fast.ai/2017/07/17/num-lin-alg/">Computational Linear Algebra</a> by fast.ai</li>
</ul>
<p>Cover Image: Photo by <a target="_blank" href="https://unsplash.com/ja/@brett_jordan?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Brett Jordan</a> on <a target="_blank" href="https://unsplash.com/photos/M3cxjDNiLlQ?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ A Data Scientist’s Guide to Happiness: Findings From the Happy Experiences of 10,000+ Humans ]]>
                </title>
                <description>
                    <![CDATA[ By Jordan Rohrlich Modern life throws a lot at us. We often find ourself struggling to manage anxiety, wrangle responsibilities, adapt to new conditions, and maintain a happy state of mind. But happiness is a noisy space these days. Self help books, ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/a-data-scientists-guide-to-happiness-findings-from-the-happy-experiences-of-10-000-humans-fc02b5c8cbc1/</link>
                <guid isPermaLink="false">66c3425e4f1fc448a3678f74</guid>
                
                    <category>
                        <![CDATA[ Data Science ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Happiness ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ topic modeling ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 23 Apr 2018 22:53:44 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/0*EsFDmp-QfBLa0vBf.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Jordan Rohrlich</p>
<p>Modern life throws a lot at us. We often find ourself struggling to manage anxiety, wrangle responsibilities, adapt to new conditions, and maintain a happy state of mind.</p>
<p>But happiness is a noisy space these days. Self help books, articles, blogs, and meditation apps can’t help everyone, and often increase the mental burden needed to stay content. That’s a serious problem. So, as mental health becomes increasingly vulnerable and solutions become increasingly complex, it’s important to anchor oneself to the fundamentals. <strong>That is, we need to refocus our daily lives on the everyday things that make people happy.</strong></p>
<h3 id="heading-data">Data</h3>
<p>This research dives into a handy dataset that can help shed some light on the fundamentals of happiness. <a target="_blank" href="https://github.com/rit-public/HappyDB">HappyDB</a> is a set of 100,000+ happy experiences gathered through Amazon Mechanical Turk from March to June of 2017. It contains the experiences and demographics from tens of thousands of contributors around the world. Interestingly, some basic text analysis methods can help us learn a lot from this data.</p>
<p>By understanding the emotional intensity and keyword patterns drawn from these happy experiences, HappyDB teaches us two valuable lessons.</p>
<p>You can check out the code for yourself on <a target="_blank" href="https://github.com/jrohrlich/DSGuideToHappy">GitHub</a>.</p>
<h3 id="heading-1-happiness-is-not-conditional-on-demographics">1. Happiness is not conditional on demographics.</h3>
<p>This one is counterintuitive.</p>
<p>Most of us experience a “grass is always greener” effect with respect to happiness. Young people anticipate a happy career and family later on in life. Older folks reminisce about a time when they were young and adventurous. Bachelors yearn for companionship. Couples hope for children.</p>
<p>And, despite knowing this, we all think someone else is happier, or some other stage of our life will bring us more joy. Let’s take a look at the data.</p>
<p><a target="_blank" href="https://web.stanford.edu/class/cs124/lec/sentiment.pdf">Sentiment analysis</a> weighs the emotional intensity of text. Using an R package called “Syuzhet,” I measured the sentiment of these happy experiences to determine how their intensities vary. This created a spectrum of happy experiences that could be broken down by specific demographic groups:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/vtSR9KdR4zPHMmWcHn7v1MPvZ640dX8VRe6M" alt="Image" width="800" height="460" loading="lazy">
<em>Sentiment of Happy Experiences (by gender, family status)</em></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/dBKgVGpc2rWvemRIUZryNLznFVUVlxWVRHuK" alt="Image" width="800" height="460" loading="lazy">
<em>Sentiment of Happy Experiences (by age group)</em></p>
<p>Somewhat surprisingly, there’s little change in the spread of happy experiences across these gender, family, and age demographic groups. Here are the highlights:</p>
<ul>
<li>Overall, the experiences are definitely positive. But the bottom quartile does have negative sentiment (some happy things poetically arise from discomfort and tragedy)</li>
<li>The distributions have high-end tails and fairly limited lower bounds — some experiences are extremely positive, and few are strikingly negative</li>
<li>Self-identifying females have slightly higher sentiment scores than men for most of their experiences (a 0.05–0.1 point difference)</li>
<li>Married parents have slightly higher sentiment scores than bachelors and childless couples for most of their experiences (a 0.05–0.1 point difference)</li>
<li>The quartiles of happy experiences (25th, 50th, and 75th percentiles) across age groups are virtually identical</li>
</ul>
<p><strong>In sum,</strong> <strong>there is no significant difference in the range of happy experiences reported by different demographics.</strong> Although women and parents tend to have marginally more happy experiences to record, the differences on the sentiment scale can’t be taken seriously — they correspond to a fraction of a fraction of a single happy word per experience recorded. That’s a minuscule difference.</p>
<p>This dataset, however, does not include any data fields for race, socioeconomic status, or other identity positions that may materially influence daily experiences. Future happiness research should inspect these relationships closely.</p>
<h3 id="heading-2-happiness-is-determined-by-specific-types-of-experiences">2. Happiness is determined by specific types of experiences.</h3>
<p>It’s easy to think of happiness as a mysterious, ethereal substance that penetrates our experiences in uninterpretable ways. This view espouses a metaphysical understanding of happiness as something beyond human comprehension.</p>
<p>But that’s not very helpful, especially for people who rely on happy and meaningful experiences as the lifeline of their mental health.</p>
<p>Enter Topic Modeling. This method of text analysis (explained <a target="_blank" href="http://blog.echen.me/2011/08/22/introduction-to-latent-dirichlet-allocation/">here</a>; I use R’s “Mallet” package) provides a constructive approach to explaining what HappyDB’s 10,000+ participants find to be happy experiences.</p>
<p>By segmenting the dataset into documents of each respondent’s experiences, then running an LDA topic model to identify groups of commonly occurring keywords, we can begin to isolate distinct types of experiences that bring us happiness. The topics and related keywords can be seen below, in no particular order:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/bSlgdQrleybiwhVeNzAzevuLqtRXteuIFEpQ" alt="Image" width="800" height="619" loading="lazy">
<em>Topic Model Output from 100,922 Happy Experiences</em></p>
<h4 id="heading-time-with-family"><strong>Time with Family</strong></h4>
<p>Seems like a no-brainer. Words like “daughter,” “son,” “husband,” “baby,” “wife,” and “time” seem to show that lots of people reflect very positively on experiences that involve their loved ones. These experiences often involve the most commonplace of settings and derive happiness simply from company and affection.</p>
<p>Try spending more time with loved ones: call your mom, go to your kid’s soccer game. It may pay off more than you think.</p>
<h4 id="heading-getting-paid"><strong>Getting Paid</strong></h4>
<p>Although people don’t like thinking that money relates to happiness, their experiences sure say the opposite. Getting a paycheck, clearing a credit card balance, or giving money to a friend can make people really happy. And the sense of accomplishment and economic security that comes with would definitely explain why.</p>
<h4 id="heading-food"><strong>Food</strong></h4>
<p>People love eating. Cooking a favorite meal, eating out with friends, or gorging on a pint of iced cream in front of the TV can all make someone happy. Good food with friends should definitely play a part in any happy lifestyle.</p>
<h4 id="heading-sleep-time"><strong>Sleep Time</strong></h4>
<p>Surprisingly, people document lots of happy experiences around sleep: cuddling up in bed, going to sleep with a furry friend, waking up to a promising new day, and so on. There’s lots to be happy about, if one takes a moment to reflect at night after a productive day, or in the motivated morning before something exciting.</p>
<h4 id="heading-games-and-competition"><strong>Games and Competition</strong></h4>
<p>Humans are competitive. They love playing video games, watching sports, and doing other things that stoke their biological instinct to dominate. Play a board game with some friends or get excited about your home sports team. Chances are you’ll be happy you did.</p>
<h4 id="heading-achievement-and-education"><strong>Achievement and Education</strong></h4>
<p>After weeks of work, it feels great to finish big enterprises. Finishing a class, graduating from school, or launching a project can all seriously lift a person’s mood. But finishing big undertakings requires a few to start, so go out and start something new! Learning and doing are rewarded handsomely.</p>
<h4 id="heading-celebrating-and-birthdays"><strong>Celebrating and Birthdays</strong></h4>
<p>Obviously, celebrations make people happy (think birthdays, anniversaries, and friendsgivings). People enjoy finding a reason — however important or silly — to meet up with loved ones, get happy about an occasion, and do something to break up a dull weekly routine.</p>
<h4 id="heading-mental-balance-and-introspection"><strong>Mental Balance and Introspection</strong></h4>
<p>The act of tuning into one’s mental state seems to provide a lot of happiness in and of itself. Thinking introspectively about one’s wellbeing, head space, and happiness seem to have positive effects on those very things! Try meditating, reflecting on happy experiences, or just being aware of your mental state — it may be the very thing to help boost it.</p>
<h4 id="heading-spending"><strong>Spending</strong></h4>
<p>Satisfying our material desires, of course, brings lots of people happiness. Finding good deals, finally buying that car or home, and getting something nice for oneself or a loved one all create some sort of happiness. Enjoy responsibly.</p>
<h4 id="heading-weekend-trips"><strong>Weekend Trips</strong></h4>
<p>People like being off work, but enjoy it dramatically more if enjoyed in good company, while doing something different. Go on a trip somewhere, have an outing nearby, or find another novel excuse for spending time with others in new scenery. The data says you certainly won’t regret it.</p>
<h4 id="heading-reading-and-music"><strong>Reading and Music</strong></h4>
<p>Whether bundling up at home with a new book or discovering a song on the bus ride home, lots of people get happy through the simple act of reading or listening. Taking an hour before bed to read something new or skim through Discover Weekly is probably worth the time investment.</p>
<h4 id="heading-decisions"><strong>Decisions</strong></h4>
<p>Decisions also clock in as a big happiness generating activity. It’s exciting to spend time thinking about a big change, decide to do something new, and tell people about it. It leaves a lingering mood boost for lots of folks, too. So make a change you’ve been meaning to for a while; and commit to it!</p>
<h3 id="heading-wrapping-up">Wrapping up</h3>
<p>These twelve categories of experience represent the foundations of daily happiness for tens of thousands of people. Given that humans are more alike than we often give them credit for, the same can likely be said about you.</p>
<p>This method, like any, is imperfect. Some demographics contribute more heavily than others, which may throw curious words into some topics, or may bias the topics that are represented in the model. Textual data is messy and people also don’t think about happiness in crisply defined categories of experience.</p>
<p>But, using these two lessons as a basic structure for understanding positivity in our everyday lives, I think it can help remind us that happiness is never so far off as we may think.</p>
<p>We already know many of these happy topics to be true on some level. But we rarely recognize the power that they have on our mood, and so don’t structure them into our everyday lives as readily as we should.</p>
<p>These categories are empirically-certified mood boosters. They’re happiness slam dunks.</p>
<p>So we should take what we can get. Throw out the self-help handbooks and focus on real happy experiences. You may like what you find.</p>
<p><em>If you found this article helpful, share it with a friend or give some claps ?.</em></p>
<p>See the code for yourself on <a target="_blank" href="https://github.com/jrohrlich/DSGuideToHappy">GitHub</a>!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
