<jupyter_start><jupyter_text>Exercise 01: Matplotlib  1. A. & B. Scatterplots and histograms<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
%matplotlib inline
titanic = sns.load_dataset('titanic')
titanic.head()
fig, ax = plt.subplots(2,2)

# first subplot: fare over age
ax[0,0].set_title('Fare over age')
ax[0,0].set_xlabel('age [y]')
ax[0,0].set_ylabel('fare [$]')
ax[0,0].scatter(titanic['age'],titanic['fare'])

# second subplot: Fare over class
ax[0,1].set_title('Fare over class')
ax[0,1].set_xlabel('class')
ax[0,1].set_ylabel('fare [$]')
ax[0,1].scatter(titanic['pclass'],titanic['fare'])

# third subplot: Fares
ax[1,0].set_title('Fares')
ax[1,0].set_xlabel('fare [$]')
ax[1,0].set_ylabel('counts')
ax[1,0].hist(titanic['fare'])

# fourth subplot: Survivors per class
ax[1,1].set_title('Survivors per class')
ax[1,1].set_xlabel('class')
ax[1,1].set_ylabel('counts')
ax[1,1].hist(titanic['pclass'],bins=3)
ax[1,1].xaxis.set_ticks([1.33,2,2.66])
ax[1,1].set_xticklabels([1,2,3])

fig.tight_layout()

figname = 'titanic_explorative_analysis.pdf'
plt.savefig(figname)<jupyter_output><empty_output><jupyter_text>1. C. Open figure in inkscape<jupyter_code>#! inkscape {figname}<jupyter_output><empty_output><jupyter_text>1. D. Heatmap for survival<jupyter_code>import importlib
import seaborn.apionly
importlib.reload(seaborn.apionly);

survivors = np.zeros((2,3))
male =  titanic.loc[titanic['sex'] == 'male']
female =  titanic.loc[titanic['sex'] == 'female']

decks = ['A','B','C','D','E','F','G']

male_s = []
female_s = []

for deck in decks:
    male_deck = male.loc[male['deck'] == deck]
    male_survived = male_deck.loc[male_deck['survived'] == 1]
    male_survived = len(male_survived)
    if male_survived != 0:
        male_survived = male_survived/len(male_deck)
    male_s.append(male_survived)
    
    female_deck = female.loc[female['deck'] == deck]
    female_survived = female_deck.loc[female_deck['survived'] == 1]
    female_survived = len(female_survived)
    female_survived = female_survived/len(female_deck)
    female_s.append(female_survived)
    
male_s = np.asarray(male_s).reshape((1,len(male_s)))
female_s = np.asarray(female_s).reshape((1,len(female_s)))  
survivors = np.vstack([male_s,female_s])  
    
fig, ax = plt.subplots()
pos = ax.imshow(survivors, interpolation='nearest', cmap='afmhot')
ax.yaxis.set_ticks((0,1))
ax.set_yticklabels(['male','female'])
ax.set_xticklabels(['0','A', 'B', 'C', 'D', 'E', 'F', 'G'])
cbar = fig.colorbar(pos,orientation='horizontal')
cbar.set_label('probability of survival')<jupyter_output><empty_output><jupyter_text>2. A. & B lying with plots  Histogram bins<jupyter_code>import seaborn
importlib.reload(seaborn)

# weird and awkward way to create some sample data for a histogram...
N = 500
num = np.sin(np.linspace(1,50.0,N))*10
num = num.astype(np.int)
num = num - num.min()

values = []
for i in range(1,N):
    nums = [i + np.random.uniform(-0.5,0.5) for n in range(num[i])]
    values.append(nums)

values = np.asarray(values)
new_values = values[0]
for val in values[1:]:
    new_values = np.hstack([new_values, val])

# plot the histogram with different bin numbers
fig, axes = plt.subplots(3,1)

# 100 bins show the full beauty of the distribution
axes[0].set_title('100 bins')
axes[0].hist(new_values, bins=100)
axes[0].set_xticklabels([])

# 50 bins still capture the sinusoidal features
axes[1].set_title('50 bins')
axes[1].hist(new_values, bins=50)
axes[1].set_xticklabels([])

# 8 bins show a nuniform distribution
axes[2].set_title('8 bins')
axes[2].hist(new_values, bins=8)
axes[2].set_xlabel('value')

for ax in axes:
    ax.set_ylabel('counts')<jupyter_output><empty_output><jupyter_text>Axis ranges<jupyter_code># look only at the survivors from classes 1 & 2
titanic = titanic.loc[titanic['pclass'].isin([1,2])]

# set the plot up
fig, axes = plt.subplots(1,2)

for ax in axes:
    ax.set_title('Survivors per class')
    ax.set_xlabel('class')
    ax.set_ylabel('counts')


# plot with two bins and adjust tick positions
for ax in axes:
    ax.hist(titanic['pclass'],bins=2)
    ax.xaxis.set_ticks([1.25,1.75])
    ax.set_xticklabels([1,2])

# adjusting the range on the x-axis is fine and helps to have a clearer
# plot
axes[0].set_xlim((1,2))
axes[1].set_xlim((1,2))

# adjusting the range on the y-axis suggests, that more than double
# the number 
axes[0].set_ylim(170,220)

fig.tight_layout()<jupyter_output><empty_output><jupyter_text>Logarithmic plots<jupyter_code>x = np.random.normal(10000,1000,size=100)
y = np.random.normal(10000,1000,size=100)

fig, axes = plt.subplots(2,2)
axes[0,0].scatter(x,y)
axes[0,0].set_title('no log')
axes[0,1].scatter(x,y)
axes[0,1].set_title('x log')
axes[1,0].scatter(x,y)
axes[1,0].set_title('y log')
axes[1,1].scatter(x,y)
axes[1,1].set_title('x log and y log')

axes[0,0].set_ylabel('$y$')
axes[0,1].set_xscale('log')
axes[1,0].set_yscale('log')
axes[1,0].set_xlabel('$x$')
axes[1,0].set_ylabel('$y$')
axes[1,1].set_xscale('log')
axes[1,1].set_yscale('log')
axes[1,1].set_xlabel('$x$')

fig.tight_layout()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18084066471163246, 'fraction_numerical': 0.038318670576735096, 'mean_word_length': 6.155244755244755, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '288294', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2282, 'n_tokens_neox': 2135, 'n_words': 370, 'path': 'day3/exercise01-solutions.ipynb', 'repo_name': 'JanaLasser/data-science-course', 'size': '5115'}