mirror of
https://github.com/guezoloic/millesima_projetS6.git
synced 2026-03-28 19:13:42 +00:00
ajout : aout des tests test_cleaning.py
This commit is contained in:
23
cleaning.py
23
cleaning.py
@@ -5,12 +5,27 @@ import pandas as pd
|
|||||||
SCORE_COLS = ["Robert", "Robinson", "Suckling"]
|
SCORE_COLS = ["Robert", "Robinson", "Suckling"]
|
||||||
|
|
||||||
|
|
||||||
def display_info(df: DataFrame) -> None:
|
def display_info(df: DataFrame, name: str = "DataFrame") -> None:
|
||||||
df.describe()
|
"""
|
||||||
print(df.info())
|
Affiche un résumé du DataFrame
|
||||||
print("\nNombre de valeurs manquantes par colonne :")
|
-la taille
|
||||||
|
-types des colonnes
|
||||||
|
-valeurs manquantes
|
||||||
|
-statistiques numériques
|
||||||
|
"""
|
||||||
|
print(f"\n===== {name} =====")
|
||||||
|
|
||||||
|
print(f"Shape : {df.shape[0]} lignes × {df.shape[1]} colonnes")
|
||||||
|
|
||||||
|
print("\nTypes des colonnes :")
|
||||||
|
print(df.dtypes)
|
||||||
|
|
||||||
|
print("\nValeurs manquantes :")
|
||||||
print(df.isna().sum())
|
print(df.isna().sum())
|
||||||
|
|
||||||
|
print("\nStatistiques numériques :")
|
||||||
|
print(df.describe().round(2))
|
||||||
|
|
||||||
|
|
||||||
def drop_empty_appellation(df: DataFrame) -> DataFrame:
|
def drop_empty_appellation(df: DataFrame) -> DataFrame:
|
||||||
|
|
||||||
|
|||||||
26
main.py
26
main.py
@@ -29,41 +29,33 @@ def main() -> None:
|
|||||||
|
|
||||||
df = load_csv(argv[1])
|
df = load_csv(argv[1])
|
||||||
|
|
||||||
print("=== Avant nettoyage ===")
|
display_info(df, "Avant le nettoyage")
|
||||||
display_info(df)
|
|
||||||
|
|
||||||
df = drop_empty_appellation(df)
|
df = drop_empty_appellation(df)
|
||||||
save_csv(df, "donnee_clean.csv")
|
save_csv(df, "donnee_clean.csv")
|
||||||
|
display_info(df, "Après nettoyage d'appellations manquantes")
|
||||||
print("\n=== Après nettoyage d'appellations manquantes ===")
|
|
||||||
display_info(df)
|
|
||||||
|
|
||||||
#la moyenne des notes des vins pour chaque appellation
|
#la moyenne des notes des vins pour chaque appellation
|
||||||
robert_means = mean_robert(df)
|
robert_means = mean_robert(df)
|
||||||
save_csv(robert_means, "mean_robert_by_appellation.csv")
|
save_csv(robert_means, "mean_robert_by_appellation.csv")
|
||||||
print("\n=== moyenne Robert par appellation ===")
|
display_info(robert_means, "Moyennes Robert par appellation")
|
||||||
print(robert_means.head(10))
|
|
||||||
|
|
||||||
robinson_means = mean_robinson(df)
|
robinson_means = mean_robinson(df)
|
||||||
save_csv(robinson_means, "mean_robinson_by_appellation.csv")
|
save_csv(robinson_means, "mean_robinson_by_appellation.csv")
|
||||||
print("\n=== moyennes Robinson par appellation ===")
|
display_info(robinson_means, "Moyennes Robinson par appellation")
|
||||||
print(robinson_means.head(10))
|
|
||||||
|
|
||||||
suckling_means = mean_suckling(df)
|
suckling_means = mean_suckling(df)
|
||||||
save_csv(suckling_means, "mean_suckling_by_appellation.csv")
|
save_csv(suckling_means, "mean_suckling_by_appellation.csv")
|
||||||
print("\n=== moyennes Suckling par appellation ===")
|
display_info(suckling_means, "Moyennes Suckling par appellation")
|
||||||
print(suckling_means.head(10))
|
|
||||||
|
|
||||||
df_missing_scores = fill_missing_scores(df)
|
df_missing_scores = fill_missing_scores(df)
|
||||||
save_csv(df_missing_scores, "donnee_filled.csv")
|
save_csv(df_missing_scores, "donnee_filled.csv")
|
||||||
print("\n=== Après remplissage des notes manquantes ===")
|
display_info(df_missing_scores, "Après remplissage des notes manquantes par la moyenne de l'appellation")
|
||||||
display_info(df_missing_scores)
|
|
||||||
|
|
||||||
df_ready = encode_appellation(df_missing_scores)
|
df_ready = encode_appellation(df_missing_scores)
|
||||||
save_csv(df_ready, "donnee_ready.csv")
|
save_csv(df_ready, "donnee_ready.csv")
|
||||||
print("\n=== Après remplacer la colonne 'Appellation' par des colonnes indicatrices ===")
|
display_info(df_ready, "Après remplacer la colonne 'Appellation' par des colonnes indicatrices")
|
||||||
display_info(df_ready)
|
|
||||||
print(df_ready.filter(like="App_").any().head())
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
|
|||||||
64
test_cleaning.py
Normal file
64
test_cleaning.py
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
import pandas as pd
|
||||||
|
import pytest
|
||||||
|
from pandas import DataFrame
|
||||||
|
|
||||||
|
from cleaning import (
|
||||||
|
SCORE_COLS,
|
||||||
|
drop_empty_appellation,
|
||||||
|
mean_score,
|
||||||
|
fill_missing_scores,
|
||||||
|
encode_appellation,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def df_raw() -> DataFrame:
|
||||||
|
return pd.DataFrame({
|
||||||
|
"Appellation": ["Pauillac", "Pauillac ", "Margaux", None, "Pomerol", "Pomerol"],
|
||||||
|
"Robert": ["95", None, "bad", 90, None, None],
|
||||||
|
"Robinson": [None, "93", 18, None, None, None],
|
||||||
|
"Suckling": [96, None, None, None, 91, None],
|
||||||
|
"Prix": ["10.0", "11.0", "20.0", "30.0", "40.0", "50.0"],
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
def test_drop_empty_appellation(df_raw: DataFrame):
|
||||||
|
out = drop_empty_appellation(df_raw)
|
||||||
|
assert out["Appellation"].isna().sum() == 0
|
||||||
|
assert len(out) == 5
|
||||||
|
|
||||||
|
|
||||||
|
def test_mean_score_zero_when_no_scores(df_raw: DataFrame):
|
||||||
|
out = drop_empty_appellation(df_raw)
|
||||||
|
m = mean_score(out, "Robert")
|
||||||
|
assert list(m.columns) == ["Appellation", "mean_Robert"]
|
||||||
|
|
||||||
|
# Pomerol n'a aucune note Robert => moyenne doit être 0
|
||||||
|
pomerol_mean = m.loc[m["Appellation"].str.strip() == "Pomerol", "mean_Robert"].iloc[0]
|
||||||
|
assert pomerol_mean == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_fill_missing_scores(df_raw: DataFrame):
|
||||||
|
out = drop_empty_appellation(df_raw)
|
||||||
|
filled = fill_missing_scores(out)
|
||||||
|
|
||||||
|
# plus de NaN dans les colonnes de scores
|
||||||
|
for col in SCORE_COLS:
|
||||||
|
assert filled[col].isna().sum() == 0
|
||||||
|
|
||||||
|
assert filled.loc[1, "Robert"] == 95.0
|
||||||
|
|
||||||
|
# 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):
|
||||||
|
out = drop_empty_appellation(df_raw)
|
||||||
|
filled = fill_missing_scores(out)
|
||||||
|
encoded = encode_appellation(filled)
|
||||||
|
|
||||||
|
# la colonne texte disparaît
|
||||||
|
assert "Appellation" not in encoded.columns
|
||||||
|
assert "Pauillac" in encoded.columns
|
||||||
|
assert encoded.loc[0, "Pauillac"] == 1
|
||||||
Reference in New Issue
Block a user