mirror of
https://github.com/guezoloic/millesima-ai-engine.git
synced 2026-03-28 18:03:47 +00:00
ajout: restructuration de la cleaning
This commit is contained in:
160
src/cleaning.py
Normal file → Executable file
160
src/cleaning.py
Normal file → Executable file
@@ -1,103 +1,87 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from pandas import DataFrame, to_numeric, get_dummies
|
|
||||||
|
|
||||||
SCORE_COLS = ["Robert", "Robinson", "Suckling"]
|
from typing import cast, override
|
||||||
|
from pandas import DataFrame, read_csv, to_numeric, get_dummies
|
||||||
|
|
||||||
|
|
||||||
def display_info(df: DataFrame, name: str = "DataFrame") -> None:
|
class Cleaning:
|
||||||
"""
|
def __init__(self, filename) -> None:
|
||||||
Affiche un résumé du DataFrame
|
self._vins: DataFrame = read_csv(filename)
|
||||||
-la taille
|
#
|
||||||
-types des colonnes
|
self.SCORE_COLS: list[str] = [
|
||||||
-valeurs manquantes
|
c for c in self._vins.columns if c not in ["Appellation", "Prix"]
|
||||||
-statistiques numériques
|
]
|
||||||
"""
|
#
|
||||||
print(f"\n===== {name} =====")
|
for col in self.SCORE_COLS:
|
||||||
|
self._vins[col] = to_numeric(self._vins[col], errors="coerce")
|
||||||
|
|
||||||
print(f"Shape : {df.shape[0]} lignes × {df.shape[1]} colonnes")
|
def getVins(self) -> DataFrame:
|
||||||
|
return self._vins.copy(deep=True)
|
||||||
|
|
||||||
print("\nTypes des colonnes :")
|
@override
|
||||||
print(df.dtypes)
|
def __str__(self) -> str:
|
||||||
|
"""
|
||||||
|
Affiche un résumé du DataFrame
|
||||||
|
- la taille
|
||||||
|
- types des colonnes
|
||||||
|
- valeurs manquantes
|
||||||
|
- statistiques numériques
|
||||||
|
"""
|
||||||
|
return (
|
||||||
|
f"Shape : {self._vins.shape[0]} lignes x {self._vins.shape[1]} colonnes\n\n"
|
||||||
|
f"Types des colonnes :\n{self._vins.dtypes}\n\n"
|
||||||
|
f"Valeurs manquantes :\n{self._vins.isna().sum()}\n\n"
|
||||||
|
f"Statistiques numériques :\n{self._vins.describe().round(2)}\n\n"
|
||||||
|
)
|
||||||
|
|
||||||
print("\nValeurs manquantes :")
|
def drop_empty_appellation(self) -> Cleaning:
|
||||||
print(df.isna().sum())
|
self._vins = self._vins.dropna(subset=["Appellation"])
|
||||||
|
return self
|
||||||
|
|
||||||
print("\nStatistiques numériques :")
|
def _mean_score(self, col: str) -> DataFrame:
|
||||||
print(df.describe().round(2))
|
"""
|
||||||
|
Calcule la moyenne d'une colonne de score par appellation.
|
||||||
|
- Convertit les valeurs en numériques, en remplaçant les non-convertibles par NaN
|
||||||
|
- Calcule la moyenne par appellation
|
||||||
|
- Remplace les NaN résultants par 0
|
||||||
|
|
||||||
|
"""
|
||||||
|
means = self._vins.groupby("Appellation", as_index=False)[col].mean()
|
||||||
|
means = means.rename(
|
||||||
|
columns={col: f"mean_{col}"}
|
||||||
|
) # pyright: ignore[reportCallIssue]
|
||||||
|
return cast(DataFrame, means.fillna(0))
|
||||||
|
|
||||||
def drop_empty_appellation(df: DataFrame) -> DataFrame:
|
def _mean_robert(self) -> DataFrame:
|
||||||
|
return self._mean_score("Robert")
|
||||||
|
|
||||||
return df.dropna(subset=["Appellation"])
|
def _mean_robinson(self) -> DataFrame:
|
||||||
|
return self._mean_score("Robinson")
|
||||||
|
|
||||||
|
def _mean_suckling(self) -> DataFrame:
|
||||||
|
return self._mean_score("Suckling")
|
||||||
|
|
||||||
def mean_score(df: DataFrame, col: str) -> DataFrame:
|
def fill_missing_scores(self) -> Cleaning:
|
||||||
"""
|
"""
|
||||||
Calcule la moyenne d'une colonne de score par appellation.
|
Remplacer les notes manquantes par la moyenne
|
||||||
- Convertit les valeurs en numériques, en remplaçant les non-convertibles par NaN
|
des vins de la même appellation.
|
||||||
- Calcule la moyenne par appellation
|
"""
|
||||||
- Remplace les NaN résultants par 0
|
for element in self.SCORE_COLS:
|
||||||
|
means = self._mean_score(element)
|
||||||
|
self._vins = self._vins.merge(means, on="Appellation", how="left")
|
||||||
|
|
||||||
|
mean_col = f"mean_{element}"
|
||||||
|
self._vins[element] = self._vins[element].fillna(self._vins[mean_col])
|
||||||
|
|
||||||
"""
|
self._vins = self._vins.drop(columns=["mean_" + element])
|
||||||
tmp = df[["Appellation", col]].copy()
|
return self
|
||||||
|
|
||||||
tmp[col] = to_numeric(tmp[col], errors="coerce")
|
def encode_appellation(self, column: str = "Appellation") -> Cleaning:
|
||||||
|
"""
|
||||||
# moyenne par appellation
|
Remplace la colonne 'Appellation' par des colonnes indicatrices
|
||||||
means = tmp.groupby("Appellation", as_index=False)[col].mean()
|
"""
|
||||||
|
appellations = self._vins[column].astype(str).str.strip()
|
||||||
means[col] = means[col].fillna(0)
|
appellation_dummies = get_dummies(appellations)
|
||||||
|
self._vins = self._vins.drop(columns=[column])
|
||||||
means = means.rename(columns={col: f"mean_{col}"})
|
self._vins = self._vins.join(appellation_dummies)
|
||||||
|
return self
|
||||||
|
|
||||||
def mean_robert(df: DataFrame) -> DataFrame:
|
|
||||||
return mean_score(df, "Robert")
|
|
||||||
|
|
||||||
|
|
||||||
def mean_robinson(df: DataFrame) -> DataFrame:
|
|
||||||
return mean_score(df, "Robinson")
|
|
||||||
|
|
||||||
|
|
||||||
def mean_suckling(df: DataFrame) -> DataFrame:
|
|
||||||
return mean_score(df, "Suckling")
|
|
||||||
|
|
||||||
|
|
||||||
def fill_missing_scores(df: DataFrame) -> DataFrame:
|
|
||||||
"""
|
|
||||||
Remplacer les notes manquantes par la moyenne
|
|
||||||
des vins de la même appellation.
|
|
||||||
"""
|
|
||||||
df_copy = df.copy()
|
|
||||||
df_copy["Appellation"] = df_copy["Appellation"].astype(str).str.strip()
|
|
||||||
|
|
||||||
for score in SCORE_COLS:
|
|
||||||
df_copy[score] = to_numeric(df_copy[score], errors="coerce")
|
|
||||||
|
|
||||||
temp_cols: list[str] = []
|
|
||||||
|
|
||||||
for score in SCORE_COLS:
|
|
||||||
mean_df = mean_score(df_copy, score)
|
|
||||||
mean_name = f"mean_{score}"
|
|
||||||
temp_cols.append(mean_name)
|
|
||||||
|
|
||||||
df_copy = df_copy.merge(mean_df, on="Appellation", how="left")
|
|
||||||
df_copy[score] = df_copy[score].fillna(df_copy[mean_name])
|
|
||||||
|
|
||||||
df_copy = df_copy.drop(columns=temp_cols)
|
|
||||||
return df_copy
|
|
||||||
|
|
||||||
|
|
||||||
def encode_appellation(df: DataFrame, column: str = "Appellation") -> DataFrame:
|
|
||||||
"""
|
|
||||||
Remplace la colonne 'Appellation' par des colonnes indicatrices
|
|
||||||
"""
|
|
||||||
df_copy = df.copy()
|
|
||||||
|
|
||||||
appellations = df_copy[column].astype(str).str.strip()
|
|
||||||
|
|
||||||
appellation_dummies = get_dummies(appellations)
|
|
||||||
|
|
||||||
df_copy = df_copy.drop(columns=[column])
|
|
||||||
|
|
||||||
return df_copy.join(appellation_dummies)
|
|
||||||
@@ -1,64 +1,68 @@
|
|||||||
import pandas as pd
|
|
||||||
import pytest
|
import pytest
|
||||||
from pandas import DataFrame
|
from pandas import DataFrame
|
||||||
|
from unittest.mock import patch, mock_open
|
||||||
from cleaning import (
|
from cleaning import Cleaning
|
||||||
SCORE_COLS,
|
|
||||||
drop_empty_appellation,
|
|
||||||
mean_score,
|
|
||||||
fill_missing_scores,
|
|
||||||
encode_appellation,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def df_raw() -> DataFrame:
|
def cleaning_raw() -> Cleaning:
|
||||||
return pd.DataFrame({
|
"""
|
||||||
"Appellation": ["Pauillac", "Pauillac ", "Margaux", None, "Pomerol", "Pomerol"],
|
"Appellation": ["Pauillac", "Pauillac ", "Margaux", None , "Pomerol", "Pomerol"],
|
||||||
"Robert": ["95", None, "bad", 90, None, None],
|
"Robert": ["95" , None , "bad" , 90 , None , None ],
|
||||||
"Robinson": [None, "93", 18, None, None, None],
|
"Robinson": [None , "93" , 18 , None , None , None ],
|
||||||
"Suckling": [96, None, None, None, 91, None],
|
"Suckling": [96 , None , None , None , 91 , None ],
|
||||||
"Prix": ["10.0", "11.0", "20.0", "30.0", "40.0", "50.0"],
|
"Prix": ["10.0" , "11.0" , "20.0" , "30.0", "40.0" , "50.0" ],
|
||||||
})
|
"""
|
||||||
|
csv_content = """Appellation,Robert,Robinson,Suckling,Prix
|
||||||
|
Pauillac,95,,96,10.0
|
||||||
|
Pauillac ,,93,,11.0
|
||||||
|
Margaux,bad,18,,20.0
|
||||||
|
,90,,,30.0
|
||||||
|
Pomerol,,,91,40.0
|
||||||
|
Pomerol,,,,50.0
|
||||||
|
"""
|
||||||
|
m = mock_open(read_data=csv_content)
|
||||||
|
with patch("builtins.open", m):
|
||||||
|
return Cleaning("donnee.csv")
|
||||||
|
|
||||||
|
|
||||||
def test_drop_empty_appellation(df_raw: DataFrame):
|
def test_drop_empty_appellation(cleaning_raw: Cleaning) -> None:
|
||||||
out = drop_empty_appellation(df_raw)
|
out = cleaning_raw.drop_empty_appellation().getVins()
|
||||||
assert out["Appellation"].isna().sum() == 0
|
assert out["Appellation"].isna().sum() == 0
|
||||||
assert len(out) == 5
|
assert len(out) == 5
|
||||||
|
|
||||||
|
|
||||||
def test_mean_score_zero_when_no_scores(df_raw: DataFrame):
|
def test_mean_score_zero_when_no_scores(cleaning_raw: Cleaning) -> None:
|
||||||
out = drop_empty_appellation(df_raw)
|
out = cleaning_raw.drop_empty_appellation()
|
||||||
m = mean_score(out, "Robert")
|
m = out._mean_score("Robert")
|
||||||
assert list(m.columns) == ["Appellation", "mean_Robert"]
|
assert list(m.columns) == ["Appellation", "mean_Robert"]
|
||||||
|
pomerol_mean = m.loc[m["Appellation"].str.strip() == "Pomerol", "mean_Robert"].iloc[
|
||||||
# Pomerol n'a aucune note Robert => moyenne doit être 0
|
0
|
||||||
pomerol_mean = m.loc[m["Appellation"].str.strip() == "Pomerol", "mean_Robert"].iloc[0]
|
]
|
||||||
assert pomerol_mean == 0
|
assert pomerol_mean == 0
|
||||||
|
|
||||||
|
|
||||||
def test_fill_missing_scores(df_raw: DataFrame):
|
def test_fill_missing_scores(cleaning_raw: Cleaning):
|
||||||
out = drop_empty_appellation(df_raw)
|
cleaning_raw._vins["Appellation"] = cleaning_raw._vins["Appellation"].str.strip()
|
||||||
filled = fill_missing_scores(out)
|
|
||||||
|
|
||||||
# plus de NaN dans les colonnes de scores
|
cleaning_raw.drop_empty_appellation()
|
||||||
for col in SCORE_COLS:
|
filled = cleaning_raw.fill_missing_scores().getVins()
|
||||||
|
for col in cleaning_raw.SCORE_COLS:
|
||||||
assert filled[col].isna().sum() == 0
|
assert filled[col].isna().sum() == 0
|
||||||
|
|
||||||
assert filled.loc[1, "Robert"] == 95.0
|
pauillac_robert = filled[filled["Appellation"] == "Pauillac"]["Robert"]
|
||||||
|
assert (pauillac_robert == 95.0).all()
|
||||||
# pas de colonnes temporaires mean_*
|
|
||||||
for col in SCORE_COLS:
|
|
||||||
assert f"mean_{col}" not in filled.columns
|
|
||||||
|
|
||||||
|
|
||||||
def test_encode_appellation(df_raw: DataFrame):
|
def test_encode_appellation(cleaning_raw: Cleaning):
|
||||||
out = drop_empty_appellation(df_raw)
|
cleaning_raw._vins["Appellation"] = cleaning_raw._vins["Appellation"].str.strip()
|
||||||
filled = fill_missing_scores(out)
|
|
||||||
encoded = encode_appellation(filled)
|
|
||||||
|
|
||||||
# la colonne texte disparaît
|
out = (
|
||||||
assert "Appellation" not in encoded.columns
|
cleaning_raw.drop_empty_appellation()
|
||||||
assert "Pauillac" in encoded.columns
|
.fill_missing_scores()
|
||||||
assert encoded.loc[0, "Pauillac"] == 1
|
.encode_appellation()
|
||||||
|
.getVins()
|
||||||
|
)
|
||||||
|
assert "Appellation" not in out.columns
|
||||||
|
assert "Pauillac" in out.columns
|
||||||
|
assert int(out.loc[0, "Pauillac"]) == 1
|
||||||
|
|||||||
Reference in New Issue
Block a user