<jupyter_start><jupyter_text>**A walk through olympic history:**This is a historical dataset on the modern Olympic Games, including all the Games from Athens 1896 to Rio 2016. This data was scrapped from www.sports-reference.com in May 2018. Note that the Winter and Summer Games were held in the same year up until 1992. After that, they staggered them such that Winter Games occur on a four year cycle starting with 1994, then Summer in 1996, then Winter in 1998, and so on. A common mistake people make when analyzing this data is to assume that the Summer and Winter Games have always been staggered.Methodology used:The analysis will follow CRISP-DM approach. CRISP-DM stands for Cross Industry Standard Process for Data Mining. It is a standard robust technique that provides a structured approach to planning a data mining project.The given below are the processes it follow:Business UnderstandingData UnderstandingPrepare DataData ModellingEvaluation of ResultDeployment of Model1. Business Understanding:This data set contain the history of 120 years in olympics .It consist of both winter and summer olypmicsThe Data is so diverse that lot of insights can be extracted out of it. But our agenda will be revolving around below mentioned questions:What is the average/maximum/minimum age of atheletes ?What was participation of atheletes throught years in both the olympics  ?which country is the performer in all time in 120 years ?Can you find the average age of USA athelete which had won a medal ?The summary of our analysis can be found at this  Analysis result2. Data Understanding:Data Understanding means know your data well enough to answer all those query for which analysis is being carried out. The data can be huge and more sometime that may not be necessary for our analysis. The best approach to gather data is that we should collect it in entirity and then we can mine into it according to the needs.The used for the analysis Olympic data set . It can be found at this link.https://www.kaggle.com/biphili/india-and-the-world-at-the-olympic-games/dataIt has two .csv files:athletes_events.csv : This data contain the athelete Partcipated in olympics.noc_regions.csv:This data contains NOC data which had participatedImporting basic libraries to be used in the analysis :<jupyter_code>import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import pandas as pd
import  seaborn  as   sns
df = pd.read_csv('athlete_events.csv')
df.head()
# Let's investigate more about the dataset

df.shape
# Let's remove those records having all columns missing.
df.dropna(how='all',inplace=True)<jupyter_output><empty_output><jupyter_text>Let's see total columns name and how many records each column has. It is the best way to see if any missing values are there in each columns.<jupyter_code>df.info()
# check columns with no missing values
set(df.isna().sum()[df.isna().sum()==0].keys())

round((df.isnull().sum()/df.shape[0])*100,2)

# Let's find out the columns having more than 50% record missing. If exist, we will remove the column.
df.columns[round((df.isnull().sum()/df.shape[0])*100,2)>50]<jupyter_output><empty_output><jupyter_text>We cannot fill medal value as number of athelete has not won any medals.  Q1 What is the average/maximum/minimum age/hieght/weight of atheletes ? **AVG HIEGHT WEIGHT AND AGE OF ATHLETES**<jupyter_code>df[['Age','Height','Weight']].describe()<jupyter_output><empty_output><jupyter_text>Average age was 25.5,Weight 70.7,Height 175.3<jupyter_code># now lets find out average age of male female participated in olympic
sns.barplot(x='Sex',y='Age',data=df)<jupyter_output>D:\anaconda3\lib\site-packages\scipy\stats\stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval<jupyter_text>So average age men is 25.5 and women was 24<jupyter_code>#Lets find the whether more men participated or women
sns.countplot(x='Sex',data=df)
plt.title('Male VS Female participation in Olympic',size=10,color='red')
plt.savefig('participation2.jpg')<jupyter_output><empty_output><jupyter_text>So more men participated as compare to the womens<jupyter_code>#lets plot the age gorup of men and women
g = sns.FacetGrid(data=df,col='Sex')
g.map(plt.hist,'Age')<jupyter_output><empty_output><jupyter_text>So maximum number of men belong to 20-25 age group and women belong to 20-30 age group **CORELATION BETWEEN HIEGH AND WEIGHT**<jupyter_code>f,ax=plt.subplots(figsize=(15,10))
sns.kdeplot(df['Weight'].dropna(),color='r',label='Weight')
sns.kdeplot(df['Height'].dropna(),color='b',label='Height')<jupyter_output>D:\anaconda3\lib\site-packages\scipy\stats\stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval<jupyter_text>**AGE OF ATHLETES WON MEDALS IN DIFFERENT SEASON**<jupyter_code>g = sns.FacetGrid(df, col="Season",row ="Medal",hue='Sex')

g = g.map(plt.scatter, "Year", "Age").add_legend()<jupyter_output><empty_output><jupyter_text>Maximum number youngster belong to 20-30 age group had won medal  What was participation of atheletes throught years in both the olympics  ?<jupyter_code>#lets plot the barplot
WOLRD =df.groupby('Games').count()['ID']
fig, ax = plt.subplots(figsize=(15,5))

B =WOLRD.plot.bar(figsize=(15,8))
B.set_xticklabels(labels=df['Games'],rotation=90)

ax.set_xlabel('Olympic Games', size=14, color="GREEN")
ax.set_ylabel('Number of Athletes Participated', size=10, color="GREEN")
ax.set_title(' Athletes in each type of  Olympic game', size=15, color="BLUE")
plt.savefig('participation.jpg')<jupyter_output><empty_output><jupyter_text>As we cans see rise in atheletes participated in both the olympic games  **NO OF ATHELETES PARTICIPATED IN SUMMER OLYMPICS**<jupyter_code>def barplot_athlete(games,title):
   
    
    """ Function for plotting abar graph for the given dimension name on the dataset. 
        This function is used by some question they require same type of analysis to be done. 
        So, I have reused the code by defining function for data preprocessing as well as ploting at one one go.
    
    INPUT - 
    games : for which game we are going to plot summer or winter 
    title : Name of title that we wanted to plot

    OUTPUT - bar chart showing the total count per dimension of games
    """
    ASummer =games.groupby('Games').count()['ID']
    
    fig, ax = plt.subplots(figsize=(15,5))

    C =ASummer.plot.bar(figsize=(15,4))
    B.set_xticklabels(labels=games['Games'],rotation=90)


    ax.set_ylabel('Number of Athletes Participated', size=10, color="GREEN")
    ax.set_title(title, size=15, color="BLUE")    
        
    
            
         
   
    

#lets first create a table to build a plot for summer s
Summer=df[df.Season.notnull()]#take the non null values so that its doeesnt count none values
Summer_olympics=Summer[Summer.Season=='Summer']
Summer_olympics.head()
ASummer =Summer_olympics.groupby('Games').count()['ID']
barplot_athlete(Summer_olympics,' Athletes in Summer  Olympic game ')
# for winters 
Winter=df[df.Season.notnull()]
Winter_olympics=Winter[Winter.Season=='Winter']
barplot_athlete(Winter_olympics,'Athletes in Winter  Olympic game')<jupyter_output><empty_output><jupyter_text>In summer olympic number athelete participated from summer 2000-2016 remains constant   which country is the performer in all time in 120 years ?<jupyter_code>def  medalplot_games(data,title):
     """ Function for plotting abar graph for the given dimension name on the dataset. 
        This function is used by some question they require same type of analysis to be done. 
        So, I have reused the code by defining function for data preprocessing as well as ploting at one one go.
    
    INPUT - 
    data : for which game we are going to plot summer or winter 
    title : Name of title that we wanted to plot

    OUTPUT - bar chart showing the total count per dimension of games
    """
    # Top 3 Countries  in Olympics All time
    plt.title(title,size=18, color="Purple")
    plt.subplot(3,1,1)
    Gold_Medal  = data[data.Medal ==  "Gold"].Team.value_counts().head(3)
    Gold_Medal.plot(kind='bar',rot=0,figsize=(15, 10))
    plt.ylabel("Gold Medals")
    plt.subplot(3,1,2)
    silver_medal = data[data.Medal == "Silver"].Team.value_counts().head(3)
    silver_medal.plot(kind='bar',rot=0,figsize=(15, 10))
    plt.ylabel("Silver Medals")
    plt.subplot(3,1,3)
    bronze_medal = data[data.Medal == "Bronze"].Team.value_counts().head(3)
    bronze_medal.plot(kind='bar',rot=0,figsize=(15, 10))
    plt.ylabel("Bronze Medals")
    plt.savefig('medal.jpg')
## Top 3 Countries  in Olympics All time
medalplot_games(df,'Best Performer in all Olympics')<jupyter_output><empty_output><jupyter_text>clearly USA Leaves everyone behind Lets do sepaerately for Summer and Winter olympics<jupyter_code>medalplot_games(Summer_olympics,'Best Performer in Summer Olympics')<jupyter_output><empty_output><jupyter_text>USA IS CHAMPION IN SUMMER OLYMPICS<jupyter_code>medalplot_games(Winter_olympics,'Best Performer in Winter Olympics')<jupyter_output><empty_output><jupyter_text>Clearly Canada won more gold medala in winter olympics **USA PERFORMANCE IN SUMMER OLYMPICS**<jupyter_code>#Maximum medal won in which year
USA_Summer=Summer_olympics[Summer_olympics.Medal.notnull()]
USA_Summer_Olympics=USA_Summer[USA_Summer.Team=='United States']
USA_Summer_Olympics.head()
USASummer =USA_Summer_Olympics.groupby('Games').count()['Medal']
fig, ax = plt.subplots(figsize=(15,5))

D=USASummer.plot.bar(figsize=(15,4))
D.set_xticklabels(labels=USA_Summer_Olympics['Games'],rotation=90)

ax.set_xlabel('Summer Olympic Games', size=14, color="GREEN")
ax.set_ylabel('Number of Medals won', size=10, color="GREEN")
ax.set_title(' Performance of USA  in Summer  Olympic game', size=15, color="BLUE")<jupyter_output><empty_output><jupyter_text>For USA summer 2012 was best olympic games in 120 years<jupyter_code># USA IN winter olympics
USA_Winter=Winter_olympics[Winter_olympics.Medal.notnull()]
USA_Winter_Olympics=USA_Winter[USA_Winter.Team=='United States']
USA_Winter_Olympics.head()
USAWinter =USA_Winter_Olympics.groupby('Games').count()['Medal']
fig, ax = plt.subplots(figsize=(15,5))

E=USAWinter.plot.bar(figsize=(15,4))
E.set_xticklabels(labels=USA_Winter_Olympics['Games'],rotation=90)

ax.set_xlabel('Winter Olympic Games', size=14, color="GREEN")
ax.set_ylabel('Number of Medal won', size=10, color="GREEN")
ax.set_title(' Performance of USA  in Winter Olympic game', size=15, color="BLUE")<jupyter_output><empty_output><jupyter_text>Clearly USA had best 1998 winter olympics  **Medals won By USA in Summer  Olympics**<jupyter_code>USA_Summer_Olympics.head()
sns.countplot(x='Medal',data=USA_Summer_Olympics)
USA_Summer2=Summer_olympics[Summer_olympics.Team.notnull()]
USA_Summer_ololympics2=USA_Summer2[USA_Summer2.Team=='United States']
print('The youngest age athlete  of  USA in Summer Olympics is:' ,USA_Summer_ololympics2.Age.min())
print('The average age of athletes of USA in Summer Olympics is:',USA_Summer_ololympics2.Age.mean())
print('The oldest  Age of athlete of USA in Summer Olympics is:',USA_Summer_ololympics2.Age.max())<jupyter_output>The youngest age athlete  of  USA in Summer Olympics is: 12.0
The average age of athletes of USA in Summer Olympics is: 26.226537896283855
The oldest  Age of athlete of USA in Summer Olympics is: 97.0<jupyter_text>Can you find the average age of USA athelete which had won a medal ? AVG AGE OF ATHELETE WON MEDAL FOR USA IN SUMMER OLYMPICS<jupyter_code>Bsl=USA_Summer_Olympics.pivot_table(values='Age',index='Medal',columns='Year')
f, ax = plt.subplots(figsize=(15, 5))
sns.heatmap(Bsl,annot=True, linewidths=0.05,cmap="coolwarm")
ax.set_xlabel('Summer Game Year', size=14, color="Purple")
ax.set_ylabel('Medal', size=14, color="purple")
ax.set_title(' Avg Age of USA  Athelete won Medal in Summer  Olympic games', size=18, color="Purple")
plt.savefig('summer.jpg')<jupyter_output><empty_output><jupyter_text>Clearly Average age was 25 when USA won any medal in Summer olympics ATHLETES PARTICIPATION IN WINTER OLYMPICS<jupyter_code>print('The youngest Age of athlete  of In winter Olympic  is:' ,Winter_olympics.Age.min())
print('The average age of athletes of Winter  Olympic id:',Winter_olympics.Age.mean())
print('The Age of  oldest athlete of Winter Olypic is',Winter_olympics.Age.max())<jupyter_output>The youngest Age of athlete  of In winter Olympic  is: 11.0
The average age of athletes of Winter  Olympic id: 25.0391474554154
The Age of  oldest athlete of Winter Olypic is 58.0<jupyter_text>**Medal Distrubution of USA IN WINTER OLYPMPICS**<jupyter_code>USA_Winter_Olympics.head()
sns.countplot(x='Medal',data=USA_Winter_Olympics)
#lets find out age of athelete for USA
USA_Winter2=Winter_olympics[Winter_olympics.Team.notnull()]
USA_Winter_Olympics2=USA_Winter2[USA_Winter2.Team=='United States']
print('The youngest age of athlete  of  USA in Winter Olympics is:' ,USA_Winter_Olympics2.Age.min())
print('The average age of athletes of USA Winter Olympics is:',USA_Winter_Olympics2.Age.mean())
print('The oldest Age of athlete of USA Winter Olympics is :',USA_Winter_Olympics2.Age.max())<jupyter_output>The youngest age of athlete  of  USA in Winter Olympics is: 14.0
The average age of athletes of USA Winter Olympics is: 24.67871123155676
The oldest Age of athlete of USA Winter Olympics is : 51.0<jupyter_text>Avg Age of USA  Athelete won Medal in Winter  Olympic games<jupyter_code>asl=USA_Winter_Olympics.pivot_table(values='Age',index='Medal',columns='Year')
f, ax = plt.subplots(figsize=(20, 3))
sns.heatmap(asl,annot=True, linewidths=0.05,cmap="coolwarm")
ax.set_xlabel('Winter Game Year', size=14, color="Purple")
ax.set_ylabel('Medal', size=14, color="purple")
ax.set_title(' Avg Age of USA  Athelete won Medal in Winter  Olympic games', size=18, color="Purple")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10209751354354772, 'fraction_numerical': 0.021461314071398806, 'mean_word_length': 5.833887043189368, 'pattern_counts': {'":': 0, '<': 76, '<?xml version=': 0, '>': 77, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '601680', 'license': 'CNRI-Python', 'n_tokens_mistral': 5029, 'n_tokens_neox': 4583, 'n_words': 1476, 'path': 'Olympics DataVisualization.ipynb', 'repo_name': 'AshishDhadwal/Write-a-Data-Science-Blog-post', 'size': '14398'}