Implement preprocessing module for P2
This commit is contained in:
parent
f49764bf41
commit
b6b7ffc409
|
@ -1,16 +1,13 @@
|
||||||
from pandas import read_csv
|
from pandas import DataFrame, read_csv
|
||||||
from sklearn.model_selection import KFold
|
|
||||||
from sklearn.preprocessing import LabelEncoder
|
|
||||||
|
|
||||||
|
|
||||||
def replace_values(df):
|
def replace_values(df) -> DataFrame:
|
||||||
columns = ["BI-RADS", "Margin", "Density", "Age"]
|
for column in df.columns:
|
||||||
for column in columns:
|
|
||||||
df[column].fillna(value=df[column].mean(), inplace=True)
|
df[column].fillna(value=df[column].mean(), inplace=True)
|
||||||
return df
|
return df
|
||||||
|
|
||||||
|
|
||||||
def process_na(df, action):
|
def process_na(df, action) -> DataFrame:
|
||||||
if action == "drop":
|
if action == "drop":
|
||||||
return df.dropna()
|
return df.dropna()
|
||||||
elif action == "fill":
|
elif action == "fill":
|
||||||
|
@ -22,28 +19,25 @@ def process_na(df, action):
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
|
||||||
def encode_columns(df):
|
def filter_dataframe(df) -> DataFrame:
|
||||||
label_encoder = LabelEncoder()
|
relevant_columns = [
|
||||||
encoded_df = df.copy()
|
"HORA",
|
||||||
encoded_df["Shape"] = label_encoder.fit_transform(df["Shape"])
|
"DIASEMANA",
|
||||||
encoded_df["Severity"] = label_encoder.fit_transform(df["Severity"])
|
"COMUNIDAD_AUTONOMA",
|
||||||
return encoded_df
|
"ISLA",
|
||||||
|
"TOT_HERIDOS_LEVES",
|
||||||
|
"TOT_HERIDOS_GRAVES",
|
||||||
|
"TOT_VEHICULOS_IMPLICADOS",
|
||||||
|
"TOT_MUERTOS",
|
||||||
|
"TIPO_VIA",
|
||||||
|
"LUMINOSIDAD",
|
||||||
|
"FACTORES_ATMOSFERICOS",
|
||||||
|
]
|
||||||
|
filtered_df = df.filter(items=relevant_columns)
|
||||||
|
return filtered_df
|
||||||
|
|
||||||
|
|
||||||
def split_train_target(df):
|
def parse_data(source, action) -> DataFrame:
|
||||||
train_data = df.drop(columns=["Severity"])
|
|
||||||
target_data = df["Severity"]
|
|
||||||
return train_data, target_data
|
|
||||||
|
|
||||||
|
|
||||||
def split_k_sets(df):
|
|
||||||
k_fold = KFold(shuffle=True, random_state=42)
|
|
||||||
return k_fold.split(df)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_data(source, action):
|
|
||||||
df = read_csv(filepath_or_buffer=source, na_values="?")
|
df = read_csv(filepath_or_buffer=source, na_values="?")
|
||||||
processed_df = process_na(df=df, action=action)
|
processed_df = process_na(df=df, action=action)
|
||||||
encoded_df = encode_columns(df=processed_df)
|
return processed_df
|
||||||
test_data, target_data = split_train_target(df=encoded_df)
|
|
||||||
return test_data, target_data
|
|
||||||
|
|
Loading…
Reference in New Issue