<?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 Michael Duchak on Medium]]></title>
        <description><![CDATA[Stories by Michael Duchak on Medium]]></description>
        <link>https://medium.com/@michaelkduchak?source=rss-cacbcafa6ba7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*JNANrl7XmbxKpKQcF2TOtw.jpeg</url>
            <title>Stories by Michael Duchak on Medium</title>
            <link>https://medium.com/@michaelkduchak?source=rss-cacbcafa6ba7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 19:03:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@michaelkduchak/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[Predicting Chicago Health Inspection Failures]]></title>
            <link>https://medium.com/@michaelkduchak/predicting-chicago-health-inspection-failures-a9e01788b88e?source=rss-cacbcafa6ba7------2</link>
            <guid isPermaLink="false">https://medium.com/p/a9e01788b88e</guid>
            <category><![CDATA[restaurant]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[plotly]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[chicago]]></category>
            <dc:creator><![CDATA[Michael Duchak]]></dc:creator>
            <pubDate>Thu, 01 Mar 2018 22:36:47 GMT</pubDate>
            <atom:updated>2018-03-01T22:36:47.983Z</atom:updated>
            <content:encoded><![CDATA[<p>While looking though the extensive Chicago Data Portal, I found a file with all 162,000 health inspection data for the past 8 years(2010–2017). My curiosity followed and I wondered if there were any key predictors of whether a restaurant will pass or fail a health inspection. As always, the notebooks for this project can be found on my <a href="https://github.com/mkduchak/chicagohealthinspections">GitHub</a>.</p><p>I did some digging and found that <a href="https://chicago.github.io/food-inspections-evaluation/">another study</a> had been done before on this data. The study identified some key areas for predictability, such as previous failures, avg temperature, crime, sanitation, and most intriguingly the inspector assigned. This being Chicago, you have to wonder if some inspectors palms weren’t getting greasy in their inspections. Unfortunately the data I had access to cannot add anything to that hypothesis. However, I wanted to see if I could replicate any of their findings as well as bring in some other outside information.</p><p>This is where Yelp comes in. I was able to use their API to pull data on around 10,000 unique businesses that had a health inspection within the time frame. I wanted to see if price, rating, or any specific categories of restaurant contributed to the failure rate. When I combined the business information from Yelp and the inspection data, I was left with around 85,000 inspections that I could use for analysis. So what does the data tell us? Can we identify failing restaurants by other characteristics?</p><h3>1.1 Health Inspection Overview</h3><p>There are 3 main results from a Chicago health inspection: Pass, Fail, and Pass with Conditions. There are some other results such as Out of Business, but I have removed them for clarity. Below you can see a breakdown the inspection distribution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*a3qxgPATxoQK15PmKCwvXA.png" /></figure><p>Overall, 21% of all inspections result in a failure. The other 79% were passes. So right off the bat, 1/5 health inspections are failures. So these are more common that people think/expect.</p><p>Next we can look at the type of investigation. I found that investigations came in 3 main types: canvass, complaint, and license.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*K-b5gcts22NkMDci-vTQdQ.png" /></figure><p>To start off, I will be using a similar graph template here, so a brief overview of the above chart. The Y axis has total inspections, the sections in green are the inspections that passed and the sections in orange are the inspections that failed. The red line shows the relative failure rates for each category.</p><p>Canvass inspections are inspections off the street, so an inspector will pick a block or certain area and just do drop in inspections. These are by far the most common type of inspections and have a failure rate of around 18%, so slightly better than the average.</p><p>The second type of inspection is a complaint inspection. These inspections are prompted by a complaint filed with the city health inspectors office. I have also included suspected food poisoning in this case. They have a failure rate of 22% so a little higher than the average.</p><p>License inspections are the inspections carried out to first obtain a business license. So the restaurant cannot legally open for business unless it passes the license inspection. License inspections fail 31% of the time, much higher than the average. For the purposes of this analysis, I have removed the license inspections because they don’t have prior information and generally if they don’t pass this they don’t exist to have a yelp page.</p><p>There is another category to consider within the categories above, the re-inspection. A re-inspection occurs if the prior inspection failed and it will occur around a week or two after the initial failure. Here are the failure rates of re-inspections:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*EJKxr4eNWF4ZoCWCGLOkhA.png" /></figure><p>The failure rate of re-inspections is only 6% from the normal 25%. So having just failed an inspection drops the rate of failure of the next inspection by 19% . This means that when restaurants fail, they do actually make improvements to the restaurant and fix the violations that caused the failure. So interestingly enough, it would seem that a previous failure is a good predictor of a passed inspection, but only for the next immediate inspection.</p><h3>1.2 Yelp Data</h3><p>So now that we have a general understanding of the inspection distribution we can now look at some features from the Yelp data. The first feature to look at is rating:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*Uj429M86HrrWXG-c0jN-pA.png" /></figure><p>This graph shows the yelp rating and the failure rate for those ratings. As expected restaurants with a rating of 1 have the highest failure rate at 22.5%. Interestingly, however, the second highest failure rates are for restaurants with a 3 or 3.5 rating. So aside from 1 start restaurants, the rating is not truly indicative of failure rate until you get over a 4 rating. As you would expect 4 and above correspond to a decrease in fail rate, but only to about 16% which is not too much of an improvement on the average.</p><p>Now lets look at price level:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/975/1*0buqZT3Xq4Qz4J9VoB0Llg.png" /></figure><p>So each number on the X axis is the number of “$” in the price rating. So here you can see that there is really no relationship between price level and failure rate. An expensive restaurant is just as likely to fail as a cheap one.</p><p>The last feature to look at in this analysis is the categories for each restaurant.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*6irxo-oj5KzbAXcBnAUviw.png" /></figure><p>These are the top 15 categories for businesses. The same business can have multiple categories so the totals will add up to much larger than the 85,000 inspections. As you can see, the lowest rates of failures are sandwich shops and fast food restaurants. Fast food is lower than I would expect but this probably because most fast food restaurants have stricter internal cleaning guidelines. Bars are the highest at 24.5%, so you should probably avoid ordering food at places where bar is the main attribute.</p><h3>1.3 Modelling the Data</h3><p>So now that we have a brief idea of the features that can be used to predict health inspection outcomes, we can now look at how to exactly model the data.</p><p>Because this is a classification problem, most classification algorithms require the classes to be balanced. Because my classes are unbalanced, I need to either up sample or down sample. If I down sampled, I would be effectively cutting my data set in half. So instead I will be up sampling the data.</p><p>To up sample, I eventually went with SMOTE (Synthetic Minority Oversampling Technique). SMOTE essentially looks at a points K nearest neighbors and then puts a new point somewhere between the original point and the neighbor to simulate a new data point. This way we can up sample while still being consistent with the existing data and not just randomly choosing points to up sample.</p><p>Once this was complete, I needed to test out some models via cross validation. For the models that used cross validation, I needed to ensure that no over sampled validation points where used in the test set. So ultimately, I re-sampled for each fold in my 10 fold cross validation.</p><p>I tried several models for the classification: KNN, Logistic Regression, SVM, gradient boost, and random forest classification. Overall, random forest classification gave me the best F1 score at 0.47. Which isn’t too great. But it has a better accuracy than guessing. If we are just looking at Recall, then the model has about 0.75 recall, meaning that of the failed inspection the model identified them 75% of the time. This is brought down by my models precision, which of the predicted failures only 33% of them actually failed. So there still is some work to be done.</p><h3>1.4 Next Steps</h3><p>There are a few key directions I can take the rest of the analysis. The previous study mentioned other key demographic indicators as affecting failure rates. Also, I did location matching with the yelp data, so I could also break it down by Chicago neighborhood. I think the most interesting follow, though, are the over 600,000 specific violations and comments on those violations. Due to them being text, I will probably have to use some NLP techniques to look into them, but I am sure there is some further predictions we can add with that information. More to follow.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a9e01788b88e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NBA Player Value vs Draft Position]]></title>
            <link>https://medium.com/@michaelkduchak/nba-player-value-vs-draft-position-e4d020696a71?source=rss-cacbcafa6ba7------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4d020696a71</guid>
            <category><![CDATA[chicago-bulls]]></category>
            <category><![CDATA[nba-draft]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[sports]]></category>
            <category><![CDATA[nba]]></category>
            <dc:creator><![CDATA[Michael Duchak]]></dc:creator>
            <pubDate>Mon, 05 Feb 2018 00:28:58 GMT</pubDate>
            <atom:updated>2018-02-05T00:28:58.224Z</atom:updated>
            <content:encoded><![CDATA[<p>The current Chicago Bulls are updating their roster to get, in the words of the General Manager Gar Forman, “Younger and more athletic”. Essentially they are looking to rebuild the team and move some of their older players to other teams in return for draft picks. For this specific case, I looked at Bulls power forward Nikola “Niko” Mirotic. The 2017–18 season had been Niko’s 4th season with the Chicago Bulls and there have been rumors circulating for months about his eventual trade.</p><p>I started out wanting to see what was the current value Niko was adding to the bulls and then wanted to look at where in the draft we could get comparable value. To do this analysis, I used selenium via python and scraped <a href="https://www.basketball-reference.com/">basketball-reference.com</a>. I pulled player stats (age, height, weight, draft position, etc), seasonal advanced stats and college stats for all drafted players. The code for this can be found on my <a href="https://github.com/mkduchak/luther-project">github</a>.</p><h3>1.0 Determining Player Value</h3><p>The first step is deciding on a metric to assess player value. With sport stats in general, there are no perfect measures of player performance. However, we can at least get to a baseline for comparison using some already created metrics. For comparing NBA players, the best metric right now is probably Value Over Replacement (VORP).</p><p>VORP is calculated first by weighing a player’s stats to get a base level of comparison. That does not account for the level of team a player is on, though. So those stats are then adjusted to simulate the player being on an average team. This is called Box Plus/Minus. But that box plus / minus does not adjust for minutes played. So once we adjust by minutes played, we get Value Over Replacement.</p><p>The idea is that VORP puts all players on a scale of -2 through around 12. Though technically it can go higher than 12, a performance of 12 is quite rare and only been accomplished a few times in history. Most recently Russell Westbrook had a 12 in his 2016–17 MVP season. A -2 VORP is considered a replacement player. That is to say that a player from the G-league could replace them with no impact. It is worth noting that VORP does tend to weigh offense contributions over defensive contributions.</p><h3><strong>1.1 VORP Characteristics</strong></h3><p>So using the data I pulled we can look at the distribution of VORP. Below is the distribution of 40 years of VORP statistics.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/375/1*DYV2pUBsdIU-eBNyScphPg.png" /><figcaption>Distribution of VORP</figcaption></figure><p>Looking at this we can see that VORP does got from -2 to a few better than 12. Overall they center around 0 as the most common score. There is a pretty steep cutoff below 0 as most players that are consistently below this threshold are replaced by players with other potential.</p><p>Also, we can look at the year in the league for each player and compare it with their VORP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*n5Q10lh1whif_bqMbg2UsQ.png" /><figcaption>VORP per Year in League</figcaption></figure><p>For the most part, it takes about 4 or 5 years for the VORP to peak for players. With first years on average having the lowest value. This makes sense as anyone with a VORP score too low in their first year or two will not be in the league for very long. It is also worth noting that as the years increase you find less negative VORP players. This is most likely due to any older player only staying signed as long as they put up at least average numbers.</p><p>So then we can look at Career Average VORP to see who the best players are on average.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/753/1*4OGlJ-97bpfKfUv6KfCd7w.png" /><figcaption>Top 10 Players by Career Avg VORP</figcaption></figure><p>I don’t think anyone would be surprised by this list. Lebron James is the highest due to having no real career decline like Michael Jordan in his last few seasons. But all people on this are either in the Hall of Fame or will be in the Hall of Fame once they finish playing in the league.</p><p>Here is the distribution of the Career Avg VORP. I have also added a line to show where Niko is located on this graph.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*0j40WjL0j0yMEyOQXqXqRQ.png" /></figure><p>The distribution is similar to the distribution of VORP. The whole distribution is shifted downwards a bit. But that is expected, as most players go keep playing even past their prime. This is why the most common career VORPs are actually less than 0. As we saw in the last graph, only players that can continue to play at an above zero value play into later years.</p><p>The is graph also has a line drawn on it to show where Niko is in relation to the other players. In his four seasons he has had VORPs of 1.3, 1.3, 1.0, and 0.7. So overall, he has been better than around 75% of all NBA players, but he is definitely not in all star territory. So this is the ultimate value we want to compare to the draft.</p><h3>1.2 Draft Position Compared to Career Avg VORP</h3><p>So to get back to the premise, we can compare the avg career VORP to the position drafted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*SX6G5ZX6On-9QnPZblVUeA.png" /><figcaption>Career Avg VORP to Draft Number</figcaption></figure><p>Here we can see the distribution of first round draft picks and the avg career VORP each player provided. There are a whole lot of outliers from every draft position. Basically all draft ranks still produced at least one player with over 2 VORP. Also this means that there is a lot of variability within draft positions. Also, as we go up the rank there are even better outliers but overall the distribution remains relatively consistent between 0 and 1, like most other draft positions. So basically, higher draft positions only give you a better likelihood of getting an all star. But looking at the median players drafted by each rank is about the same for most draft positions.</p><h3>1.3 Modeling Attempts</h3><p>I tried doing linear modelling to see if the relationship between draft position and career avg VORP. I included features for height, draft age, and some college stats. When I ran a cross validated lasso regressions, though, any attempts to predict VORP had a larger error than just taking the avg VORP.</p><p>A lot of those issues were caused by the heteroskedasticity of VORP. The variation among draft positions increases at the lower draft positions more than the later draft positions, thus the data is not homoskedastic. To counter this I log transformed VORP, but that didn’t take care of the issue completely and I still was unable to predict anything meaningful from my regressions.</p><p>I used a few other modelling techniques, like adding polynomial regression, adjusting the date ranges and positions and removing outliers via Inter quartile range (IQR). Unfortunately, none really had an affect on the predictability of VORP from the draft position. Essentially, the higher variability makes any concrete predictions invalid. Instead we should look at the probabilities associated with drafting a good player.</p><h3>1.4 Conclusion</h3><p>So where does Niko stack up? As of February 1, 2018, he was traded to the New Orleans Pelicans for their first round pick (and a few of the Pelican’s bad contracts to sweeten the deal). Currently the Pelicans are ranked 13th and the expectation is for them to draft 17th. So here is the distribution of VORP for players drafted 17th.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*Q16yc4bxTYdyTPjWiprZnA.png" /></figure><p>So it looks like it is unlikely the Bulls will get a better player than Niko at 17th place, but it is not set in stone and there is a lot of variability when it comes to drafting player value. So it will be a gamble. The Bulls will get “Younger” but not necessarily more athletic.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4d020696a71" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NYC Turnstile Data]]></title>
            <link>https://medium.com/@michaelkduchak/nyc-turnstile-data-fb5fc7019a21?source=rss-cacbcafa6ba7------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb5fc7019a21</guid>
            <category><![CDATA[mta]]></category>
            <category><![CDATA[nyc]]></category>
            <category><![CDATA[public-transit]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[matplotlib]]></category>
            <dc:creator><![CDATA[Michael Duchak]]></dc:creator>
            <pubDate>Fri, 02 Feb 2018 18:49:47 GMT</pubDate>
            <atom:updated>2018-02-02T18:49:47.332Z</atom:updated>
            <content:encoded><![CDATA[<p>The first project for Metis data science boot camp was for teams to identify key locations and times around NYC subway stations to find where there would be the most foot traffic entering the station. To do this my team looked at the turnstile data logs available freely off of the the MTA data portal. The notebooks for this can be found on <a href="https://github.com/mkduchak/benson-project">github</a>.</p><h3><strong>1.0 Getting and Cleaning Data</strong></h3><p>MTA turnstile requires a lot of clean up to get working files. The files are generated by taking logs of the turnstile status in four hour intervals from each turnstile unit within the city. The logs are then published in weekly intervals.</p><p>The turnstile status does not do the work for you. Instead of just providing the count of rotation in the last for hour interval, it just has an updated state. For example, if at noon the log was at 75000, the output at 4 pm would be 75,570, thus the count for 4 hours was 570 entries. To get around this we ended up compiling all the files in the date range, sorting by unit information, and then using an array based on the index to subtract the previous amount from the current amount. This worked for the most part but there were still a few additional issues to work though.</p><p>Overall, we noticed 3 main problems with this approach:</p><p>1. Once a unit got to the most recent date, then the following turnstile unit would take previous turnstile unit as a starting point. This would lead to wildly inaccurate entry counts in the first period. To get around this, we flagged the first instance of a unit and converted their entry counts to Nan.</p><p>2. Once a turnstile reached a certain amount, they would roll over back to zero. This caused huge turnstile counts for the period due to some turnstile logs getting to over 9 digits. We eliminated these outliers by only taking in logs that were 5 times the interquartile range. This reduced our data set by around 0.3% but it reduced our counts by literally billions.</p><p>3. In about 4% of entry logs, the turnstiles actually counted backwards. I am not entirely sure how this is possible, but it usually coincided with a rollover mentioned in issue 2. The actual counts looked normal, they were just negative, so we just took the absolute value and called it a day.</p><h3>1.1 Results</h3><p>Once these steps were taken we had a better data set to work with. The following were crated with matplotlib and are also in the github.</p><p>The first image just shows the top 10 avg entries per station for a given day. The largest station was Grand Central and followed closely by 34 St Herald Sq. The other three had similar volume but there was a large drop-off between the top five an later ten.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/946/1*PNxKwax10DDM5iPxoKhdig.png" /></figure><p>We then looked at these top 5 stations to see the most busy day and hours for them. The next graph shows the avg entries by weekday for these top 5 stations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/1*lWj7FtJnRlXcspErmeJ3KQ.png" /></figure><p>Once again, Grand Central is has the largest volume during the week. The data here shows that there are distinct trends between station entries on weekdays vs weekends. Grand Central drops to the lowest of the 5 on Saturday and Sunday. This would point to Grand Central being a hub for more commuters vs 34th Herald Sq, which has significantly more volume on the weekend.</p><p>To take a look at the timing during the day, the best way is to look at it from an avg weekday view and and avg weekend view.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*MJTKiF0uxPHg7weYYsDnFg.png" /></figure><p>The time of entries at these 5 stations shift a lot during the day. In the morning, the 42 st Port Authority has the largest entry volume. Grand Central and Herald Sq take the lead as the day progresses however. Times Square has relatively low volume during the day, but it actually has the most volume from 4 to 8 pm.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*giI0b_GICyx6-37C21yECA.png" /></figure><p>On weekends we can see that the station timing is more regular, however it is much smaller than the weekday volume. Weekend volume also starts increasing at a much later hour than weekdays, which is to be expected with fewer people having to commute. For weekends, 34st Herald Sq is by far the largest as is the busiest for almost the entire weekend. The exception to this being from 8 pm to 4 am where 14 st Union Sq. I would guess that this location is home to more bars and night clubs to explain the late volume of traffic.</p><p>Though turnstile data isn’t the easiest to work with, there are a lot of interesting aspects on population movement through the data. I am interested in seeing how stations ratio of entries and exits compares over the course of a day, but that is for another post.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb5fc7019a21" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>