Tutorial
Decorators
@timeit_arg_info_dec
timeit_arg_info_dec
is a decorator that decorates a function when it runs, by printing its used parameters, their arguments, the execution time and the output of that function. This can help e.g. with debugging.
[1]:
from typing import List
from time import sleep
import pandas as pd
from extra_ds_tools.decorators.func_decorators import timeit_arg_info_dec
@timeit_arg_info_dec(round_seconds=1)
def illustrate_decorater(a_number: int,
text: str,
lst: List[int],
df: pd.DataFrame,
either: bool = True,
*args,
**kwargs):
sleep(1)
return "Look how informative!"
illustrate_decorater(42, 'Bob', list(range(100)), pd.DataFrame([list(range(1,10))]), either=False, **{'Even': 'this works!'})
param type_hint default_value arg_type arg_value arg_len
-- ------------- --------------------------- --------------- --------------------------- ------------------------ ---------
0 a_number int int 42
1 text str str Bob 3
2 lst List[int] list [0, 1, 2, .. 7, 98, 99] 100
3 df pandas.core.frame.DataFrame pandas.core.frame.DataFrame (1, 9)
4 either bool True bool False
5 kwarg['Even'] str this works! 11
illustrate_decorater() took 1.0 seconds to run.
Returned:
Look how informative!
[1]:
'Look how informative!'
For the full documentation of timeit_arg_info_dec
click here.
Plots
stripboxplot
stripboxplot
is a plot which combines seaborn’s boxplot and stripplot into one plot and adds extra count information using extra-datascience-tool’s add_counts_to_yticks
and add_counts_to_xticks
.
[4]:
# import libraries
import pandas as pd
import numpy as np
from extra_ds_tools.plots.eda import stripboxplot
from numpy.random import default_rng
# generate data
rng = default_rng(42)
cats = ['Cheetah', 'Leopard', 'Puma']
cats = rng.choice(cats, size=1000)
cats = np.append(cats, [None]*102)
weights = rng.integers(25, 100, size=1000)
weights = np.append(weights, [np.nan]*100)
weights = np.append(weights, np.array([125,135]))
rng.shuffle(cats)
rng.shuffle(weights)
df = pd.DataFrame({'cats': cats, 'weights': weights})
df.head()
[4]:
cats | weights | |
---|---|---|
0 | Cheetah | 86.0 |
1 | Puma | 38.0 |
2 | Puma | 68.0 |
3 | None | NaN |
4 | Puma | 36.0 |
[5]:
# run stripboxplot
fig, ax = stripboxplot(df, 'cats', 'weights')
For the full documentation of stripboxplot
click here.
add_counts_to_xticks
add_counts_to_xticks
is a function which add count statistics of a categorical variable on the a-axis of a plot. If the categorical variable is on the y-axis you can use add_counts_to_yticks
instead.
[1]:
# import libraries
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from extra_ds_tools.plots.format import add_counts_to_xticks
from numpy.random import default_rng
# generate data
rng = default_rng(42)
cats = ['Cheetah', 'Leopard', 'Puma']
cats = rng.choice(cats, size=1000)
cats = np.append(cats, [None]*102)
weights = rng.integers(25, 100, size=1000)
weights = np.append(weights, [np.nan]*100)
weights = np.append(weights, np.array([125,135]))
rng.shuffle(cats)
rng.shuffle(weights)
df = pd.DataFrame({'cats': cats, 'weights': weights})
df.head()
[1]:
cats | weights | |
---|---|---|
0 | Cheetah | 86.0 |
1 | Puma | 38.0 |
2 | Puma | 68.0 |
3 | None | NaN |
4 | Puma | 36.0 |
[2]:
# create e.g. a violinplot
fig, ax = plt.subplots()
sns.violinplot(df, x='cats', y='weights', ax=ax)
[2]:
<AxesSubplot: xlabel='cats', ylabel='weights'>
[3]:
# add counts to xticks
fig, ax = add_counts_to_xticks(fig, ax, df, x_col='cats', y_col='weights')
fig
[3]:
For the full documentation of add_counts_to_xticks
click here.