File size: 1,312 Bytes
a55ae21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from typing import Any
import numpy as np
import pandas as pd
from typing import List

import matplotlib.pyplot as plt
import seaborn as sns


class Eda:
    def missing_values_table(self, df: pd.DataFrame):
        mis_val = df.isnull().sum()
        mis_val_percent = 100 * mis_val / len(df)
        mis_val_table = pd.concat([mis_val, mis_val_percent], axis=1)
        mis_val_table.columns = ["Missing Values", "% of Total Values"]
        mis_val_table = (
            mis_val_table[mis_val_table["% of Total Values"] != 0]
            .sort_values("% of Total Values", ascending=False)
            .round(1)
        )
        print(
            f"The selected dataframe has {df.shape[1]} columns and {mis_val_table.shape[0]} columns with missing values."
        )
        return mis_val_table

    def detect_outliers_boxplot(df: pd.DataFrame, features: List[str], fac=1.5):
        if isinstance(features, str):
            features = [features]

        q1 = df.loc[:, features].quantile(0.25)
        q3 = df.loc[:, features].quantile(0.75)
        iqr = q3 - q1
        lower_threshold = q1 - fac * iqr
        upper_threshold = q3 + fac * iqr

        outliers = (
            (df[features] < lower_threshold) | (df[features] > upper_threshold)
        ).any(axis=1)
        return outliers