avatarHasan Basri Akçay

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3445

Abstract

.groupby</span>(<span class="hljs-selector-attr">[<span class="hljs-string">'Sport'</span>]</span>) <span class="hljs-selector-attr">[<span class="hljs-string">'Age'</span>]</span><span class="hljs-selector-class">.transform</span>(lambda x: x<span class="hljs-selector-class">.fillna</span>(<span class="hljs-built_in">round</span>(x<span class="hljs-selector-class">.mean</span>(), <span class="hljs-number">2</span>))) data_clean<span class="hljs-selector-attr">[<span class="hljs-string">'Height'</span>]</span> = data<span class="hljs-selector-class">.groupby</span>(<span class="hljs-selector-attr">[<span class="hljs-string">'Sport'</span>]</span>)<span class="hljs-selector-attr">[<span class="hljs-string">'Height'</span>]</span><span class="hljs-selector-class">.transform</span>(lambda x: x<span class="hljs-selector-class">.fillna</span>(<span class="hljs-built_in">round</span>(x<span class="hljs-selector-class">.mean</span>(), <span class="hljs-number">2</span>))) data_clean<span class="hljs-selector-attr">[<span class="hljs-string">'Weight'</span>]</span> = data<span class="hljs-selector-class">.groupby</span>(<span class="hljs-selector-attr">[<span class="hljs-string">'Sport'</span>]</span>)<span class="hljs-selector-attr">[<span class="hljs-string">'Weight'</span>]</span><span class="hljs-selector-class">.transform</span>(lambda x: x<span class="hljs-selector-class">.fillna</span>(<span class="hljs-built_in">round</span>(x<span class="hljs-selector-class">.mean</span>(), <span class="hljs-number">2</span>)))</pre></div><h2 id="3c8c">Drop Constant Columns</h2><p id="7df4">Constant columns are weight for the forecasting model and there are no affect on forecasting. So if there is a constant column, we should drop it. We have not a constant column. You can see it in the below.</p><div id="7ba7"><pre>data_clean<span class="hljs-selector-class">.columns</span><span class="hljs-selector-attr">[data_clean.nunique() <= 1]</span> <span class="hljs-function"><span class="hljs-title">Index</span><span class="hljs-params">([], dtype=<span class="hljs-string">'object'</span>)</span></span></pre></div><h2 id="b0e1">Matching Incorrectly Spelt Words</h2><p id="db71">A dataset that created by human, always can have some incorrectly spelt word. Incorrectly spelt words are dangerous for forecasting models because the forecasting model can not realize they are same words. We use fuzzywuzzy library for this situation. We are looking for words that has match score higher than 90.</p><div id="eece"><pre><span class="hljs-attr">team_unique</span> = data_clean[<span class="hljs-string">'Team'</span>].unique()</pre></div><div id="6cf2"><pre>import fuzzywuzzy <span class="hljs-keyword">from</span> fuzzywuzzy import process

<span class="hljs-keyword">for</span> team <span class="hljs-keyword">in</span> team_unique: matches = fuzzywuzzy.process.extract(team, team_unique, <span class="hljs-attribute">limit</span>=2, <span class="hljs-attribute">scorer</span>=fuzzywuzzy.fuzz.token_sort_ratio)
<span class="hljs-keyword">if</span> matches[1][1] > 90: <span class="hljs-built_in">print</span>(team, matches[1])</pre></div><div id="d9f3"><pre><span class="hljs-attribute">Netherlands</span> ('Netherlands-<span class="hljs-number">1</span>', <span class="hljs-number">92</span>) <span class="hljs-attribute">United</span> States ('United States-<span class="hljs-number">1</span>',

Options

<span class="hljs-number">93</span>) <span class="hljs-attribute">Soviet</span> Union ('Soviet Union-<span class="hljs-number">2</span>', <span class="hljs-number">92</span>) <span class="hljs-attribute">Unified</span> Team ('Unified Team-<span class="hljs-number">2</span>', <span class="hljs-number">92</span>) <span class="hljs-attribute">Switzerland</span> ('Switzerland-<span class="hljs-number">1</span>', <span class="hljs-number">92</span>) <span class="hljs-attribute">East</span> Germany ('East Germany-<span class="hljs-number">1</span>', <span class="hljs-number">92</span>) <span class="hljs-attribute">Great</span> Britain ('Great Britain-<span class="hljs-number">1</span>', <span class="hljs-number">93</span>) <span class="hljs-attribute">Switzerland</span>-<span class="hljs-number">1</span> ('Switzerland', <span class="hljs-number">92</span>) <span class="hljs-attribute">Switzerland</span>-<span class="hljs-number">2</span> ('Switzerland', <span class="hljs-number">92</span>)</pre></div><p id="007b">After searching incorrectly spelt words, we fix it by deleting the numbers and punctuation marks.</p><div id="78e4"><pre>data_clean<span class="hljs-selector-attr">[<span class="hljs-string">"Team"</span>]</span> = data_clean<span class="hljs-selector-attr">[<span class="hljs-string">'Team'</span>]</span><span class="hljs-selector-class">.str</span><span class="hljs-selector-class">.replace</span>(<span class="hljs-string">'[^\w\s]'</span>,<span class="hljs-string">''</span>) data_clean<span class="hljs-selector-attr">[<span class="hljs-string">"Team"</span>]</span> = data_clean<span class="hljs-selector-attr">[<span class="hljs-string">'Team'</span>]</span><span class="hljs-selector-class">.str</span><span class="hljs-selector-class">.replace</span>(<span class="hljs-string">'\d+'</span>, <span class="hljs-string">''</span>) data_clean<span class="hljs-selector-attr">[<span class="hljs-string">'Team'</span>]</span> = data_clean<span class="hljs-selector-attr">[<span class="hljs-string">'Team'</span>]</span><span class="hljs-selector-class">.str</span><span class="hljs-selector-class">.strip</span>()</pre></div><p id="890f">👋 Thanks for reading. If you enjoy my work, don’t forget to like, follow me on medium and <a href="https://www.linkedin.com/in/hasan-basri-akcay/">on LinkedIn</a>. It will motivate me in offering more content to the Medium community ! 😊</p><p id="fd3b">Data cleaning part ends here. You can keep reading about data analysis in part 2. The link is <a href="https://medium.com/@hasan.basri.akcay/olympic-medal-numbers-predictions-with-timeseries-part-2-data-analysis-5d5d7e38fc37">Part 2</a>.</p><figure id="a5eb"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*DPIxjwyD_raAqT1EmFcPGA.png"><figcaption></figcaption></figure><h2 id="fb2d">Reference</h2><p id="393c">[1]: <a href="https://www.kaggle.com/heesoo37/120-years-of-olympic-history-athletes-and-results">https://www.kaggle.com/heesoo37/120-years-of-olympic-history-athletes-and-results</a> [2]: <a href="https://www.kaggle.com/hasanbasriakcay/which-country-is-good-at-which-sports-in-olympics">https://www.kaggle.com/hasanbasriakcay/which-country-is-good-at-which-sports-in-olympics</a> [3]: <a href="https://www.kaggle.com/hasanbasriakcay/which-country-is-good-at-which-sports-in-olympics">https://www.kaggle.com/hasanbasriakcay/which-country-is-good-at-which-sports-in-olympics</a></p></article></body>

Olympic Medal Numbers Predictions with Time Series, Part 1: Data Cleaning

Fbprophet, Darts, AutoTS, Arima, Sarimax and Monte Carlo Simulation

This is a historical dataset on the modern Olympic Games, including all the Games from Athens 1896 to Rio 2016. This data was scraped from www.sports-reference.com in May 2018. We found this dataset in kaggle and the link is https://www.kaggle.com/heesoo37/120-years-of-olympic-history-athletes-and-results.

Data Cleaning

Firstly dataset should be clean for forecasting. General data cleaning techniques are handling missing value, outlier, constant columns and wrong spelled words.

Impute Missing Values

data.isnull().sum()
ID             0
Name           0
Sex            0
Age         9474
Height     60171
Weight     62875
Team           0
NOC            0
Games          0
Year           0
Season         0
City           0
Sport          0
Event          0
Medal     231333
dtype: int64

There are missing value in four different columns. They are age, height, weight and medal. Medal column can has missing value because all participant can not win a medal. But in this work, we interest only rows that is has a medal. So we will drop the missing rows in the medal columns.

data_clean = data_clean.dropna(subset=['Medal'])

Now we can impute the missing value in age, height and weight with mean value but all sport types has different age, height and weight distribution. For example weightlifting and basketball height distribution. So we impute the missing value with mean value that is in own sport type.

data_clean['Age'] = data.groupby(['Sport'])
['Age'].transform(lambda x: x.fillna(round(x.mean(), 2)))
data_clean['Height'] = data.groupby(['Sport'])['Height'].transform(lambda x: x.fillna(round(x.mean(), 2)))
data_clean['Weight'] = data.groupby(['Sport'])['Weight'].transform(lambda x: x.fillna(round(x.mean(), 2)))

Drop Constant Columns

Constant columns are weight for the forecasting model and there are no affect on forecasting. So if there is a constant column, we should drop it. We have not a constant column. You can see it in the below.

data_clean.columns[data_clean.nunique() <= 1]
Index([], dtype='object')

Matching Incorrectly Spelt Words

A dataset that created by human, always can have some incorrectly spelt word. Incorrectly spelt words are dangerous for forecasting models because the forecasting model can not realize they are same words. We use fuzzywuzzy library for this situation. We are looking for words that has match score higher than 90.

team_unique = data_clean['Team'].unique()
import fuzzywuzzy
from fuzzywuzzy import process

for team in team_unique:
    matches = fuzzywuzzy.process.extract(team, team_unique, 
limit=2, scorer=fuzzywuzzy.fuzz.token_sort_ratio)                
    if matches[1][1] > 90:
        print(team, matches[1])
Netherlands ('Netherlands-1', 92)
United States ('United States-1', 93)
Soviet Union ('Soviet Union-2', 92)
Unified Team ('Unified Team-2', 92)
Switzerland ('Switzerland-1', 92)
East Germany ('East Germany-1', 92)
Great Britain ('Great Britain-1', 93)
Switzerland-1 ('Switzerland', 92)
Switzerland-2 ('Switzerland', 92)

After searching incorrectly spelt words, we fix it by deleting the numbers and punctuation marks.

data_clean["Team"] = data_clean['Team'].str.replace('[^\w\s]','')
data_clean["Team"] = data_clean['Team'].str.replace('\d+', '')
data_clean['Team'] = data_clean['Team'].str.strip()

👋 Thanks for reading. If you enjoy my work, don’t forget to like, follow me on medium and on LinkedIn. It will motivate me in offering more content to the Medium community ! 😊

Data cleaning part ends here. You can keep reading about data analysis in part 2. The link is Part 2.

Reference

[1]: https://www.kaggle.com/heesoo37/120-years-of-olympic-history-athletes-and-results [2]: https://www.kaggle.com/hasanbasriakcay/which-country-is-good-at-which-sports-in-olympics [3]: https://www.kaggle.com/hasanbasriakcay/which-country-is-good-at-which-sports-in-olympics

Time Series Forecasting
Data Science
Time Series Analysis
Time Series Data
Databulls
Recommended from ReadMedium