Implement base MissingValues strategies
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
__pycache__
|
@@ -13,6 +13,7 @@ uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
|
||||
|
||||
if uploaded_file is not None:
|
||||
st.session_state.data = pd.read_csv(uploaded_file)
|
||||
st.session_state.working_data = st.session_state.data
|
||||
st.success("File loaded successfully!")
|
||||
|
||||
|
||||
|
70
frontend/mvstrategy.py
Normal file
70
frontend/mvstrategy.py
Normal file
@@ -0,0 +1,70 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from pandas import DataFrame, Series
|
||||
from pandas.api.types import is_numeric_dtype
|
||||
from typing import Any, Union
|
||||
|
||||
class MVStrategy(ABC):
|
||||
"""A way to handle missing values in a dataframe."""
|
||||
|
||||
@abstractmethod
|
||||
def apply(self, df: DataFrame, label: str, series: Series) -> DataFrame:
|
||||
"""Apply the current strategy to the given series.
|
||||
|
||||
The series is described by its label and dataframe."""
|
||||
return df
|
||||
|
||||
@staticmethod
|
||||
def list_available(series: Series) -> list['MVStrategy']:
|
||||
"""Get all the strategies that can be used."""
|
||||
choices = [DropStrategy(), ModeStrategy()]
|
||||
if is_numeric_dtype(series):
|
||||
choices.extend((MeanStrategy(), MedianStrategy()))
|
||||
return choices
|
||||
|
||||
|
||||
class DropStrategy(MVStrategy):
|
||||
#@typing.override
|
||||
def apply(self, df: DataFrame, label: str, series: Series) -> DataFrame:
|
||||
df.dropna(subset=label, inplace=True)
|
||||
return df
|
||||
|
||||
def __str__(self) -> str:
|
||||
return "Drop"
|
||||
|
||||
|
||||
class PositionStrategy(MVStrategy):
|
||||
#@typing.override
|
||||
def apply(self, df: DataFrame, label: str, series: Series) -> DataFrame:
|
||||
series.fillna(self.get_value(series), inplace=True)
|
||||
return df
|
||||
|
||||
@abstractmethod
|
||||
def get_value(self, series: Series) -> Any:
|
||||
pass
|
||||
|
||||
|
||||
class MeanStrategy(PositionStrategy):
|
||||
#@typing.override
|
||||
def get_value(self, series: Series) -> Union[int, float]:
|
||||
return series.mean()
|
||||
|
||||
def __str__(self) -> str:
|
||||
return "Use mean"
|
||||
|
||||
|
||||
class MedianStrategy(PositionStrategy):
|
||||
#@typing.override
|
||||
def get_value(self, series: Series) -> Union[int, float]:
|
||||
return series.median()
|
||||
|
||||
def __str__(self) -> str:
|
||||
return "Use median"
|
||||
|
||||
|
||||
class ModeStrategy(PositionStrategy):
|
||||
#@typing.override
|
||||
def get_value(self, series: Series) -> Any:
|
||||
return series.mode()[0]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return "Use mode"
|
23
frontend/pages/normalization.py
Normal file
23
frontend/pages/normalization.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import streamlit as st
|
||||
from mvstrategy import MVStrategy
|
||||
|
||||
if "data" in st.session_state:
|
||||
data = st.session_state.data
|
||||
st.session_state.data = data.copy()
|
||||
|
||||
for column, series in data.items():
|
||||
missing_count = series.isna().sum()
|
||||
choices = MVStrategy.list_available(series)
|
||||
option = st.selectbox(
|
||||
f"Missing values of {column} ({missing_count})",
|
||||
choices,
|
||||
index=1,
|
||||
key=f"mv-{column}",
|
||||
)
|
||||
# Always re-get the series to avoid reusing an invalidated series pointer
|
||||
data = option.apply(data, column, data[column])
|
||||
|
||||
st.write(data)
|
||||
st.session_state.working_data = data
|
||||
else:
|
||||
st.error("file not loaded")
|
@@ -5,8 +5,8 @@ import seaborn as sns
|
||||
st.header("Data Visualization")
|
||||
|
||||
|
||||
if "data" in st.session_state:
|
||||
data = st.session_state.data
|
||||
if "working_data" in st.session_state:
|
||||
data = st.session_state.working_data
|
||||
|
||||
st.subheader("Histogram")
|
||||
column_to_plot = st.selectbox("Select Column for Histogram", data.columns)
|
||||
|
Reference in New Issue
Block a user