<?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 Ariel Wentworth on Medium]]></title>
        <description><![CDATA[Stories by Ariel Wentworth on Medium]]></description>
        <link>https://medium.com/@ariel_joy?source=rss-b9bb92ed2789------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*mX8DkpMpPbHELnXtRMdaZA.jpeg</url>
            <title>Stories by Ariel Wentworth on Medium</title>
            <link>https://medium.com/@ariel_joy?source=rss-b9bb92ed2789------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 22:18:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ariel_joy/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[K-means Clustering on Ordinal Data]]></title>
            <link>https://medium.com/data-science/k-means-clustering-on-ordinal-data-7222b5ee1693?source=rss-b9bb92ed2789------2</link>
            <guid isPermaLink="false">https://medium.com/p/7222b5ee1693</guid>
            <category><![CDATA[clustering]]></category>
            <category><![CDATA[unsupervised-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Ariel Wentworth]]></dc:creator>
            <pubDate>Mon, 18 May 2020 08:29:38 GMT</pubDate>
            <atom:updated>2020-05-18T15:03:57.598Z</atom:updated>
            <content:encoded><![CDATA[<h4>Using a mapping to uncover the numeric-like behaviors in your data</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WiV60pFWKKGzqKby9a2rdA.jpeg" /><figcaption>Image by <a href="https://pixabay.com/users/8385-8385/?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=259458">Reimund Bertrams</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=259458">Pixabay</a></figcaption></figure><p>K-means clustering. It’s the holy grail of unsupervised learning. And honestly? I understand why… Sure, there’s a bit of an art form to deciding on the <em>number</em> of clusters you should calculate, but by and large it’s borderline magical to sit back and let the algorithm do it’s thing. Even so, there’s one very important caveat: <strong>k-means clustering only works on numerical data</strong>… <em>Right?!</em></p><h3>Well… Maybe</h3><p>In general, attempting to broaden k-means into categorical applications is precarious at best. The most integral part of k-means clustering deals with finding points with the minimal<em> distance</em> between them. How do we define distance amongst categorical variables? How far is an apple from an orange? Are those closer to blueberries or watermelon? In unordered contexts, it just doesn’t make sense to ask these questions.</p><h4>But what if my categorical data is ordered?</h4><p>Wow, I’m so glad you asked. Just as you’ve astutely pointed out, there <em>absolutely</em> are contexts where it seems like there should be a way to answer these questions.</p><p>Recently, I was looking at some data from <a href="https://www.kff.org/coronavirus-covid-19/issue-brief/state-data-and-policy-actions-to-address-coronavirus/#socialdistancing">KFF</a> regarding the statewide social distancing mandates that have been implemented in the United States in response to COVID-19. After some wrangling, here’s what the first few rows looked like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z-OP-luB0ww5ab6HOGhh2A.png" /></figure><p>(Note: If you want to see my whole process for wrangling and analyzing this dataset, you can check out my R code on <a href="https://github.com/ariel-j-w/social-distancing-effectiveness">Github</a>.)</p><p>Consider for instance, the Stay at Home Order attribute. This attribute describes whether or not a state has a Stay at Home order, and which populations are affected by the order. Obviously having no order is different from having a statewide order, which is different from having an order that only applies to high risk groups. And here’s the important part: <em>a statewide order seems “closer” to an order affecting only high risk groups, than no order at all</em>.</p><p>It seems like we <strong>should</strong> be able to answer the questions required for k-means clustering. The natural ordering of our data lends itself to further analysis. Sure, it’s not actually numeric data, but it behaves in a roughly numeric way, so what’s holding us back from treating it as such? In the ubiquitous words of Hannah Montana, life’s what you make it so let’s make it possible to perform k-means clustering on ordinal data! (No? That wasn’t her quote? Shoot.)</p><p>Here’s the gist: in order to make k-means possible on an ordinal dataset, we’re going to define a mapping from our ordinal data into numerical values. Essentially, we assign weights to each factor level, and use those weights to perform our analysis.</p><p>(Reminder: this is a bad idea if your categorical data has no natural ordering. We’re basically <em>creating</em> the concepts of order and magnitude and forcing them upon our dataset. If this doesn’t make sense for your data, you may be interested in <a href="https://www.youtube.com/watch?v=b39_vipRkUo">k-modes clustering</a>.)</p><p>For my data, I had 3 attributes that I wanted to transform. In order to make sure the attributes were equally important during clustering, I made sure that possible weights for each attribute ranged from 0 to 10. The exact selection of 0 and 10 doesn’t matter as much as the idea of <strong>ensuring all attributes have scales of similar magnitude. </strong>This is perhaps the most important thing to keep in mind when defining your mappings.</p><blockquote>Creating scales of similar magnitudes for all attributes is the most important aspect to consider when transforming ordinal data for k-means analysis.</blockquote><p>Ultimately, I defined my mappings as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*D32jexhw-nRjUAEwBrib7Q.png" /><figcaption>Mapping ordinal social distancing variables to numerical values</figcaption></figure><p>Once I had my mapping defined, I performed an entire k-means clustering analysis on my now-numerical variables. Here’s a glimpse into the shape of my transformed data:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZpgTdq2aUftvqYMAKt9yug.png" /><figcaption>Transformed ordinal data, along with clusters identified by k-means</figcaption></figure><p>It seemed to work pretty well: my cluster means were quite distinct from each other, and scatterplots of each of the combinations of the three variables appropriately illuminated the delineation between clusters. (Check out out the code on <a href="https://github.com/ariel-j-w/social-distancing-effectiveness">Github</a> for full details.) When all was said and done, I ended up with the following cluster identifications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zwpu5q-YNeTaiTr5fLx_uw.png" /><figcaption>Classifications of statewide social distancing mandates, identified by k-means clustering of ordinal data</figcaption></figure><h3>It’s an art form, not a science.</h3><p>Defining the mapping from ordinal to numerical values is an extremely subjective process. It depends heavily upon the specifics of the data, and I don’t believe it is possible to give anything more than general advice about how to go about choosing a mapping.</p><p>In some datasets, a simple rank of ordinal data may be the best fit. In other instances, such as our case study here, a more fluid scale may be necessary. There is no one-size-fits-all approach to this method, but I still believe it has the potential to produce highly valuable insights.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7222b5ee1693" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/k-means-clustering-on-ordinal-data-7222b5ee1693">K-means Clustering on Ordinal Data</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>