<jupyter_start><jupyter_text>9. HistogramsHistograms are a good way to look at a given distribution.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
# creating some random data taken from a normal distribution
# centered in 0 with a standard deviation of 1
data1 = np.random.normal(size=1000)
fig, ax = plt.subplots(figsize=(10, 8))
ax.hist(data1, cumulative=True);<jupyter_output><empty_output><jupyter_text>Multiple parameters of the histogram can of course be changed.For example the colour, whether it is cumulative or not, filled or not, the number of bins, [...] Be careful with histograms, they are best suited to look at continuous datasets.Weird things can happen with discrete datasets, especially when keeping the default parameters.Look at the following example (note that the [binomial distribution](https://en.wikipedia.org/wiki/Binomial_distribution) is a discrete distribution):<jupyter_code>np.random.seed(0)
data2 = np.random.binomial(8, .2, size=1000)
fig, ax = plt.subplots(figsize=(6, 5))
ax.hist(data2)<jupyter_output><empty_output><jupyter_text>One way to deal with that is to have as many bins as you have observations, have a range that span the range of your data and reduce the size of your bins to show the discrete nature of your data:<jupyter_code>fig, ax = plt.subplots(figsize=(6, 5))
data_range = np.min(data2), np.max(data2) +1
nb_bins = data_range[1] - data_range[0]
ax.hist(data2, bins=nb_bins, range=data_range, rwidth=.8, align='left');<jupyter_output><empty_output><jupyter_text>To look at two different distributions, histograms can do the job but they might not be the best<jupyter_code>np.random.seed(0)
data3 = np.random.gamma(2, size=1000)
fig, ax = plt.subplots(figsize=(6, 5))
ax.hist([data1, data3], label=('data1', 'data2'))
ax.legend()<jupyter_output><empty_output><jupyter_text>Different histogram type can be chosen with the `histtype` parameter:<jupyter_code>fig, ax = plt.subplots(figsize=(6, 5))
ax.hist([data1, data3], label=('data1', 'data2'), histtype='step')
ax.legend()
fig, ax = plt.subplots(figsize=(6, 5))
ax.hist([data1, data3], label=('data1', 'data2'), histtype='barstacked')
ax.legend()<jupyter_output><empty_output><jupyter_text>Moreover, the more distributions you add, the worse visible it gets:<jupyter_code>np.random.seed(2)
mean = np.random.uniform(-10, 10, size=5)
std = np.random.uniform(1, 5, size=5)
data = np.random.normal(loc=mean, scale=std, size=(1000, 5))
labels = [f'Mean: {m:.2f}, std: {s:.2f}' for m, s in zip(mean, std)]
fig, ax = plt.subplots(figsize=(6, 5))
ax.hist(data, label=labels)
ax.legend()
fig, ax = plt.subplots(figsize=(6, 5))
ax.hist(data, histtype='step', label=labels)
ax.legend()
fig, ax = plt.subplots(figsize=(6, 5))
ax.hist(data, histtype='barstacked', label=labels)
ax.legend()<jupyter_output><empty_output><jupyter_text>Exercise 2:Plot the following cumulative histograms (note that to plot the infinite bins you need to use `plot` and not `hist`):![png](Resources/exercise_2.png)<jupyter_code>## Write your solution here<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13504718516108039, 'fraction_numerical': 0.026358607224210868, 'mean_word_length': 6.704260651629073, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 1, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '275509', 'license': 'MIT', 'n_tokens_mistral': 1115, 'n_tokens_neox': 1044, 'n_words': 310, 'path': '2-Data-handling-and-visu/09-Histograms.ipynb', 'repo_name': 'GuignardLab/CenTuri-Course-2022', 'size': '3073'}