|
1
|
|
|
''' |
|
2
|
|
|
Utilities and auxiliary functions. |
|
3
|
|
|
|
|
4
|
|
|
:author: Andreas Kanz |
|
5
|
|
|
|
|
6
|
|
|
''' |
|
7
|
|
|
|
|
8
|
|
|
# Imports |
|
9
|
|
|
import pandas as pd |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
def _memory_usage(data): |
|
13
|
|
|
''' |
|
14
|
|
|
Gives the total memory usage in kilobytes. |
|
15
|
|
|
|
|
16
|
|
|
Parameters |
|
17
|
|
|
---------- |
|
18
|
|
|
data: 2D dataset that can be coerced into Pandas DataFrame. If a Pandas DataFrame is provided, the index/column \ |
|
19
|
|
|
information is used to label the plots. |
|
20
|
|
|
|
|
21
|
|
|
Returns |
|
22
|
|
|
------- |
|
23
|
|
|
memory_usage: float |
|
24
|
|
|
|
|
25
|
|
|
''' |
|
26
|
|
|
|
|
27
|
|
|
data = pd.DataFrame(data) |
|
28
|
|
|
memory_usage = round(data.memory_usage(index=True, deep=True).sum()/1024, 2) |
|
29
|
|
|
|
|
30
|
|
|
return memory_usage |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
def _missing_vals(data): |
|
34
|
|
|
''' |
|
35
|
|
|
Gives metrics of missing values in the dataset. |
|
36
|
|
|
|
|
37
|
|
|
Parameters |
|
38
|
|
|
---------- |
|
39
|
|
|
data: 2D dataset that can be coerced into Pandas DataFrame. If a Pandas DataFrame is provided, the index/column \ |
|
40
|
|
|
information is used to label the plots. |
|
41
|
|
|
|
|
42
|
|
|
Returns |
|
43
|
|
|
------- |
|
44
|
|
|
mv_total: float, number of missing values in the entire dataset |
|
45
|
|
|
mv_rows: float, number of missing values in each row |
|
46
|
|
|
mv_cols: float, number of missing values in each column |
|
47
|
|
|
mv_rows_ratio: float, ratio of missing values for each row |
|
48
|
|
|
mv_cols_ratio: float, ratio of missing values for each column |
|
49
|
|
|
''' |
|
50
|
|
|
|
|
51
|
|
|
data = pd.DataFrame(data) |
|
52
|
|
|
mv_rows = data.isna().sum(axis=1) |
|
53
|
|
|
mv_cols = data.isna().sum(axis=0) |
|
54
|
|
|
mv_total = data.isna().sum().sum() |
|
55
|
|
|
mv_rows_ratio = mv_rows/data.shape[1] |
|
56
|
|
|
mv_cols_ratio = mv_cols/data.shape[0] |
|
57
|
|
|
|
|
58
|
|
|
return {'mv_total': mv_total, |
|
59
|
|
|
'mv_rows': mv_rows, |
|
60
|
|
|
'mv_cols': mv_cols, |
|
61
|
|
|
'mv_rows_ratio': mv_rows_ratio, |
|
62
|
|
|
'mv_cols_ratio': mv_cols_ratio} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
def _validate_input_0_1(value, desc): |
|
66
|
|
|
if value < 0 or value > 1: |
|
67
|
|
|
raise ValueError(f'Input value for {desc} is {value} but should be a float in the range 0 <= {desc} <=1.') |
|
68
|
|
|
|