<?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 Roland Jeannier on Medium]]></title>
        <description><![CDATA[Stories by Roland Jeannier on Medium]]></description>
        <link>https://medium.com/@rtjeannier?source=rss-b83fd79d4147------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DItcRniz6IKmLddXP3WoUw.jpeg</url>
            <title>Stories by Roland Jeannier on Medium</title>
            <link>https://medium.com/@rtjeannier?source=rss-b83fd79d4147------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 23:08:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rtjeannier/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[Using PCA to Visualize High Dimensional Data]]></title>
            <link>https://medium.com/@rtjeannier/using-pca-to-visualize-high-dimensional-data-6ff028c911c5?source=rss-b83fd79d4147------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ff028c911c5</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[big-data]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[dimensionality-reduction]]></category>
            <dc:creator><![CDATA[Roland Jeannier]]></dc:creator>
            <pubDate>Fri, 08 Sep 2017 20:45:11 GMT</pubDate>
            <atom:updated>2017-09-09T04:41:26.500Z</atom:updated>
            <content:encoded><![CDATA[<p>Often we are working with datasets with hundreds or even thousands of features. While having all this information is great it is extremely difficult to get an sense of what that our data actually looks like. One useful trick (that doesn’t always work) is to use principal component analysis to reduce our dataset to less in number, but larger in significance components.</p><p>I recently worked with some NLP data that ultimately had over 200,000 features. There is obviously no way for me to see how this data is distributed. So I performed PCA and broke my data into its five most informative components. The resulting plots are below. The red dots represent class of my ‘productive’ documents, the blue dots represent the ‘unproductive’ documents.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*kHwbujbDawqr6c2El8PRGQ.png" /><figcaption>You can visibly see the cluster of red data</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*r2veGPQkvS4pp2Ejlesd1g.png" /><figcaption>Again the red cluster shows in the middle</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*lGZztKhGNSmi191PMUZCzg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*8nH7Jx9LtWUBArzwQABwhw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*cb2sM9AkSWOlkc4jkTRVeQ.png" /></figure><p>By the 5th component we being to lose a lot of our useful information. 200,000 features expressed in 5 visualizations. Not too bad.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ff028c911c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Police Shootings by Race]]></title>
            <link>https://medium.com/@rtjeannier/police-shootings-by-race-a0478bb26cc4?source=rss-b83fd79d4147------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0478bb26cc4</guid>
            <category><![CDATA[blacklivesmatter]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[gun-violence]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[racism]]></category>
            <dc:creator><![CDATA[Roland Jeannier]]></dc:creator>
            <pubDate>Fri, 08 Sep 2017 17:06:12 GMT</pubDate>
            <atom:updated>2017-09-08T18:02:02.611Z</atom:updated>
            <content:encoded><![CDATA[<p>I came across the <a href="https://www.washingtonpost.com/graphics/national/police-shootings-2017/">Fatal Force</a> dataset hosted by the Washington Post that tracks all deaths do to police shootings in a given year (dating back to 2015). I was curious what the distribution of police shootings looked like when aggregated by race. Below are some visualizations that may provide some insight into the hotly contested topic of police brutality.</p><p>First, I wanted to see what races were most frequently the victims of fatal police shootings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*C84DchWVBMwVFFU2siuIPA.png" /></figure><p>As we can see, white individuals are the largest demographic shot. But this is not particularly surprising given that the majority of Americans are white. Let’s adjust our results for the relative population of the respective races.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/1*3EP9nDlwotr_tUyQ-feg3g.png" /></figure><p>Now we start to see the racial bias really shine through. But I was curious, the dataset does not give the income status of the victims, how would our results look if we were to adjust these results by the respective poverty rate of each race?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*E8eL3iwEoZL7Pp8_tjta_A.png" /></figure><p>I’m not a political scientist, so I’m not sure how to interpret these preliminary results, but it seems the issue of police shootings may have an underlying economic issue that is deeper than just race.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0478bb26cc4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What’s the point of PCA anyway?]]></title>
            <link>https://medium.com/@rtjeannier/whats-the-point-of-pca-anyway-279cf0ef0683?source=rss-b83fd79d4147------2</link>
            <guid isPermaLink="false">https://medium.com/p/279cf0ef0683</guid>
            <category><![CDATA[feature-engineering]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[apc]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-visualization]]></category>
            <dc:creator><![CDATA[Roland Jeannier]]></dc:creator>
            <pubDate>Fri, 08 Sep 2017 13:41:53 GMT</pubDate>
            <atom:updated>2018-01-31T15:16:41.444Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qh7oe2AlY-X8Pv7_oM403g.png" /></figure><p>Principle Component Analysis (PCA) is one of the buzz words people love throwing around in the Data Science community. For those of you not familiar with PCA I recommend reading this <a href="https://stats.stackexchange.com/questions/2691/making-sense-of-principal-component-analysis-eigenvectors-eigenvalues">stackexchange post</a>. Also this <a href="https://medium.com/towards-data-science/a-one-stop-shop-for-principal-component-analysis-5582fb7e0a9c">blog</a> post by Matt Brems is a great resource for an even deeper understanding.</p><p>But at a high level, PCA is a technique to organize your data so that your information is stored in orthogonal (independent) features that encapsulate as much variance in a each dimension as possible. So what do we use it for?</p><h3>1. Dimensionality Reduction</h3><p>The number one reason to use PCA is to reduce the dimensionality of your data. Pretty much all other uses of PCA stem off of this key concept. How much you reduce your dimensionality is dependent upon the use case you are dealing with. In general one defines the number of features they wish to reduce there data into and a software package does the rest. A second approach is to determine how much information you are willing to lose and calculate the minimum number of components that will contain the rest.</p><h3>2. Increasing Performance</h3><p>The most obvious benefit of dimensionality reduction is that computationally intensive modeling approaches can perform in less time. A common trade off might be reducing the number of features by 50% but only losing 5% of your models predictive power. In production environments this is often a viable trade.</p><h3>3. Visualizing Higher Dimensional Data</h3><p>One of the less obvious benefits of dimensionality reduction is the ability to visualize relationships of high dimensional data is 2D or 3D space. By reducing 1000 features into its 3 most important components, one can potentially visually inspect their data to see if clusters exist. When trying to perform unsupervised clustering on a data set this is a useful trick to keep in mind.</p><h3>4. Obscure Data</h3><p>Another interesting use for PCA is to obscure ones data set. If you are working with sensitive information you can preserve 100% of the underlying structure while removing all direct interpretability. For example a dataset of names, salaries, sex, martial status, and ages can be converted into a dataset of names and four other seemingly meaningless features. If you are trying to predict if someone is likely to vote off of those 5 features, but don’t want to share those real numbers with the public, you can use PCA to create 5 different features that predict exactly as well (potentially even better depending on the model).</p><h3>5. Create Independent Features</h3><p>Lastly, one of the requirements for linear regression is that your features be linearly independent. This can sometimes be an issue when the data you wish to use was not collected with this requirement in mind. Nonetheless, PCA can be used to create new features that describe the underlying relationship identically as well, but they will all be independent of each other.</p><p>So there we have it, PCA can be useful in a number of situations. If you can think of a creative use for PCA not described here let me know in the comments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=279cf0ef0683" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimum approach for iterating over a DataFrame]]></title>
            <link>https://medium.com/@rtjeannier/pandas-101-cont-9d061cb73bfc?source=rss-b83fd79d4147------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d061cb73bfc</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[tips-and-tricks]]></category>
            <category><![CDATA[pandas]]></category>
            <dc:creator><![CDATA[Roland Jeannier]]></dc:creator>
            <pubDate>Wed, 06 Sep 2017 06:35:06 GMT</pubDate>
            <atom:updated>2017-09-08T18:04:27.172Z</atom:updated>
            <content:encoded><![CDATA[<p>In this session I am going to be talking about iterating over rows in a Pandas DataFrame. We are going to use the Titanic dataset that was used in the <a href="https://medium.com/@rtjeannier/pandas-101-fbb5bf86a9bc">previous post</a>. For this example we are going to examine different ways in which we can replace null values in a column. But all of these approaches can be abstracted into more general use cases. Let’s get started!</p><h3>1) pd.iterrows()</h3><p>Many newcomers to Pandas rely on the convenience of the <a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.iterrows.html">iterrows</a> function when iterating over a DataFrame. But this is a terrible habit! If you have used iterrows in the past and you are reading this now; don’t use it again! Let me show you why.</p><pre>def test_iterrow(df):<br>    for (i, row) in df.iterrows():<br>        val = row[&#39;Age&#39;]<br>        if math.isnan(val):<br>                row[&#39;Age&#39;] = -1<br>%timeit test_iterrow(titanic_df)</pre><pre>Output: 10 loops, best of 3: 73.1 ms per loop</pre><p>73.1 ms! That may not seem like a long time, but we are dealing with very little data (1300 rows). There are much better approaches to solving this same problem as we will discover below.</p><h3>2) pd.loc[]/pd.iloc[]</h3><p>A much better and almost just as simple approach for iterating over a DataFrame is the <a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html">loc[]</a> and <a href="http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.DataFrame.iloc.html">iloc[]</a> functionality. How does this approach compare?</p><pre>def test_loc(df):<br>    for i in df.index:<br>        val = df.loc[i,&#39;Age&#39;]<br>        if math.isnan(val):<br>            df.loc[i,&#39;Age&#39;] = -1<br>%timeit test_loc(titanic_df)</pre><pre>Output: 100 loops, best of 3: 10.5 ms per loop</pre><p>10.5 ms! That is more than 7 times faster than using iterrows. While this is a good approach we can still do better.</p><h3>3) pd.get_value()/pd.set_value()</h3><p>The Pandas <a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.get_value.html">get_value()</a> and <a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.set_value.html">set_value()</a> functions are slightly lesser known and a little more nuanced than the more popular loc/iloc functionality. It actually turns out loc/iloc call the get_value/set_value functions underneath the hood. How does this approach stack up against the competition?</p><pre>def test_get_value(df):<br>    for i in df.index:<br>        val = df.get_value(i,&#39;Age&#39;)<br>        if math.isnan(val):<br>            df.set_value( i,&#39;Age&#39;,-1)<br>%timeit test_get_value(titanic_df)</pre><pre>Output: 100 loops, best of 3: 3.15 ms per loop</pre><p>Now that’s more like it! More than twice as fast as using the loc[] feature and 20 times faster than the terrible, horrible, no good, very bad iterrows() function, get_value is my preferred approach for iterating down the rows of a DataFrame. This method is just as flexible as all of the above approaches, allowing use to access any column at a given row by name, but doesn’t sacrifice much of anything in terms of efficiency. However, if we are willing to sacrifice flexibility we can get even better performance out of our code.</p><h3>4) pd.apply()</h3><p>Using <a href="http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.DataFrame.apply.html">pd.apply()</a> is not always trivial, but when mastered it is incredibly useful. The apply() method can be called on a single Pandas Series (as I will be showing in this example) or an entire DataFrame. Let’s see how it performs when we try to replicate the logic of our above methods.</p><pre>def helper(a):<br>    if math.isnan(a):<br>        return -1<br>    else: return a</pre><pre>%timeit titanic_df.Age.apply(helper)</pre><pre>Output: 1000 loops, best of 3: 475 µs per loop</pre><p>Wow, 0.475 ms! Now that’s efficiency. Unlike our previous approaches, apply() requires us to create a helper method. We then pass this method as an argument so that it can be called on each item in the Pandas object you are applying the function to. When you need to make dynamic changes with multiple nested if statements effecting the values across many columns of a single row, apply() will not be as useful. But, when your functionality is simple (like in this example) then using apply() is likely the most efficient approach.</p><p>So there we have it. Four different ways of solving the same problem. While apply is the most efficient approach in this example, I often find myself using get_value() as my default approach. After I have my code functioning I will sometimes come back and refactor using apply(), but more times than not the performance difference between the two is not significant enough to matter.</p><p>Also, I am aware that this example is quite silly because Pandas has a built in function that can replace null values with incredible efficiency. If we simply call <a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html">fillna()</a>…</p><pre>%timeit titanic_df.Age.fillna(-1)</pre><pre>Output: 10000 loops, best of 3: 73.7 µs per loop</pre><p>we get efficiency that will be unmatched by any alternative approach. So for those of you looking for a speedy way to replace null values in your data, use the built in functionality first. It works extremely well.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d061cb73bfc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[EDA in Pandas]]></title>
            <link>https://medium.com/@rtjeannier/pandas-101-fbb5bf86a9bc?source=rss-b83fd79d4147------2</link>
            <guid isPermaLink="false">https://medium.com/p/fbb5bf86a9bc</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[tips-and-tricks]]></category>
            <category><![CDATA[pandas]]></category>
            <category><![CDATA[kaggle]]></category>
            <category><![CDATA[titanic]]></category>
            <dc:creator><![CDATA[Roland Jeannier]]></dc:creator>
            <pubDate>Wed, 06 Sep 2017 03:08:12 GMT</pubDate>
            <atom:updated>2017-09-08T17:56:16.159Z</atom:updated>
            <content:encoded><![CDATA[<p>Over the next few blogs I am going to be covering some helpful tips and tricks for using Pandas.</p><p>When working with a new dataset in Pandas there are always three things I check first:</p><ul><li>What columns have null values and how many?</li><li>How many unique values do I have in a column (and is this what I would expect)?</li><li>What are the datatypes for a column? And if is there more than one datatype is this going to be a problem(this is often overlooked and can lead to unexpected errors)?</li></ul><p>Well, these questions can be answered in no time at all with the helpful function listed below!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dae45c3d009be59e622a544b4526783d/href">https://medium.com/media/dae45c3d009be59e622a544b4526783d/href</a></iframe><p>Let’s quickly see it in action. I’ll use the famous titanic dataset readily available on <a href="https://www.kaggle.com/c/titanic">Kaggle</a>.</p><pre>import pandas as pd</pre><pre># loading our data<br>titanic_train = pd.read_csv(‘~/titantic/train.csv’)<br>titanic_test = pd.read_csv(‘~/titantic/test.csv’)</pre><pre>titanic_df = pd.concat([titanic_train, titanic_test], axis=0)<br>eda_helper(titanic_df)</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01602be238c2a4d25708adbeeb6dc929/href">https://medium.com/media/01602be238c2a4d25708adbeeb6dc929/href</a></iframe><p>Stay tuned for more helpful tips and tricks with Pandas!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fbb5bf86a9bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Combining Datasets with Fuzzy Matching]]></title>
            <link>https://medium.com/@rtjeannier/combining-data-sets-with-fuzzy-matching-17efcb510ab2?source=rss-b83fd79d4147------2</link>
            <guid isPermaLink="false">https://medium.com/p/17efcb510ab2</guid>
            <category><![CDATA[baseball]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-analysis]]></category>
            <dc:creator><![CDATA[Roland Jeannier]]></dc:creator>
            <pubDate>Thu, 17 Aug 2017 08:25:25 GMT</pubDate>
            <atom:updated>2017-08-18T01:48:44.426Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iU9MQs0eK4D5WMnQeJzv7Q.jpeg" /><figcaption>This guy likes fuzzy matching!</figcaption></figure><p>I have a baseball dataset with every pitch thrown in the 2016 MLB season. I also have a dataset of the salaries of most of the pitchers in the 2016 MLB season. I want to combine these two datasets and examine how much a pitch costs.</p><p>I have all the information I need, I just need to join the two datasets on player name and…</p><p>Shoot! My player names do not line up perfectly. And there is definitely some players who I am simply missing salary information for. I’ll have to comb through every row of missing data and manually check to see if names like ‘Chris Lee’ should have mapped to something like ‘Christopher Lee’. This will take hours. Unless there is a better way…</p><p>Often times when trying to combine datasets, the columns you want to match on do not intersect perfectly. Hopefully the only problem is capitalization or special characters being different, and this can be corrected with a little bit of clean up code. But a lot of the time that’s just not case. When trying to join “NY Yankees” on “New York Yankees” no amount of preprocessing is going to make those two values align (at least without jeopardizing the integrity of your other matches). This is where <a href="https://en.wikipedia.org/wiki/Fuzzy_matching_(computer-assisted_translation)">fuzzy matching</a> can save the day.</p><p>In general terms fuzzy matching is that art of linking text to similar text. There are various ways to do it, but in this example we are going to use an algorithm derived from <a href="https://en.wikipedia.org/wiki/Levenshtein_distance">Levenshtein distances</a> with the help of <a href="http://chairnerd.seatgeek.com/fuzzywuzzy-fuzzy-string-matching-in-python/">FuzzyWuzzy</a> a python package developed by SeatGeek.</p><p>Let’s go take a closer look at our baseball example. We can attempt to merge our two data frames together. We already know there is going to be missing data. Let’s see what it looks like for pitchers with over a 1000 pitches.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08dabd48077833c5ed8a58e90a9d16c6/href">https://medium.com/media/08dabd48077833c5ed8a58e90a9d16c6/href</a></iframe><p>Okay, we have 12 pitchers with no salary data. At this point we have no way of knowing if this is because these pitchers are missing from our salary dataset or if there is something different about the spelling of their names. At first glance a lot of these names look suspect. ‘CC Sabathia’ could easily be ‘C.C. Sabathia’. There are multiple nicknames like ‘Joe’, ‘Mike’, ‘Nick’, and ‘Tommy’ that could easily be short for something different in our salary dataset. Why don’t we see what happens if we use FuzzyWuzzy to try and find some approximate matches?</p><p>We can start by writing a helpful method. This takes in a name and compares it to all the names in a list. It returns the best match with the highest score. We can set an minimum score to ensure we don’t return matches below some predetermined value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a97b626cda4090465b3fb9083cd97c5/href">https://medium.com/media/8a97b626cda4090465b3fb9083cd97c5/href</a></iframe><p>Now we can try to use our method to find if we have any decent matches (I went with a minimum score of 75 for a threshold after a little trial and error).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94787996a1006287c897aa927f12c798/href">https://medium.com/media/94787996a1006287c897aa927f12c798/href</a></iframe><p>Not too bad! We seem to have all of our missing data lining up without any false matches. I went back to check the original dataset and it seems I do not have salary data for Jose Fernandez in the 2016 season, which explains why no match was found.</p><p>Just for fun, let’s up the difficulty and see what happens when I try to match with more obscure pitchers. I’ll take all pitchers with at least 500 pitches thrown. Remember the more obscure the pitcher the less likely I am to have their salary in my dataset.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/942a3395681958afa1145922b12e3bec/href">https://medium.com/media/942a3395681958afa1145922b12e3bec/href</a></iframe><p>We can see the missing data beginning to creep in. Nonetheless, all of the values we did match seem accurate, and that is what is most important!</p><p>Fuzzy matching is a great way to combine datasets with uncooperative columns, but it is not full proof. When using it, I recommend holding onto the scores of your matches so you can always go back and filter your dataset later.</p><p>Let me know if you have any creative uses for fuzzy matches in the comments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17efcb510ab2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>