hichamiamiri/Machine-Learning
0
1import pandas as pd2from sklearn.impute import SimpleImputer3import numpy as np4 5DATA_SET_URL = "./data/original_set.csv"6 7EXPORT=True8EXPORT_FILE="./data/cleaned_set.csv"9 10#* Load the dataset from CSV file11df = pd.read_csv(DATA_SET_URL)12 13#* Drop unnecessary columns that won't be used in analysis14df.drop(labels=["Titre","Localisation"], axis=1, inplace=True)15 16 17#* Rename Columns18df = df.rename(columns={19 'Prix': 'price',20 'Année-Modèle': 'model_year',21 'Boite de vitesses': 'transmission',22 'Type de carburant': 'fuel_type',23 'Kilométrage': 'mileage',24 'Marque': 'brand',25 'Modèle': 'model',26 'Nombre de portes': 'number_of_doors',27 'Origine': 'origin',28 'Première main': 'first_owner',29 'Puissance fiscale': 'tax_horsepower',30 'État': 'condition',31 'ABS': 'abs',32 'Airbags': 'airbags',33 'CD/MP3/Bluetooth': 'multimedia',34 'Caméra de recul': 'backup_camera',35 'Climatisation': 'air_conditioning',36 'ESP': 'esp',37 'Jantes aluminium': 'aluminum_wheels',38 'Limiteur de vitesse': 'speed_limiter',39 'Ordinateur de bord': 'onboard_computer',40 'Radar de recul': 'parking_sensors',41 'Régulateur de vitesse': 'cruise_control',42 'Sièges cuir': 'leather_seats',43 'Système de navigation/GPS': 'navigation_gps',44 'Toit ouvrant': 'sunroof',45 'Verrouillage centralisé à distance': 'remote_central_locking',46 'Vitres électriques': 'power_windows'47})48 49#* Remove rows where 'brand' is missing, as it's essential50df.dropna(subset=["brand"], inplace=True)51 52#* Replace zero values in 'number_of_doors' with NaN, to impute them later53df["number_of_doors"].replace(0, np.nan)54 55#* Impute missing 'number_of_doors' using the mean strategy56impute_stratigy = SimpleImputer(strategy="mean")57df["number_of_doors"] = impute_stratigy.fit_transform(df["number_of_doors"].values.reshape(-1, 1))58 59#* Convert the imputed values from float to integer60df["number_of_doors"] = df["number_of_doors"].astype(int)61 62#* handle origin63df = df[df['origin'].isin(['WW au Maroc', 'Dédouanée', 'Importée neuve','Pas encore dédouanée'])]64 65 66#* Normalize 'first_owner' column: keep only "Oui" and "Non", replace others with "Non"67df["first_owner"] = df["first_owner"].where(df["first_owner"].isin(["Oui","Non"]), "Non")68df["first_owner"] = df["first_owner"].replace("Oui",True).replace("Non",False)69 70#* Drop rows with missing 'tax_horsepower' values71df = df.dropna(subset=["tax_horsepower"])72 73#* Drop rows with missing 'condition' values, as condition is important74df = df.dropna(subset=["condition"])75 76#* Clean 'price' column:77# 1. Remove 'DH' and special characters, strip whitespace78df["price"] = df["price"].astype(str).str.replace("DH", "").str.replace("\u202f", "").str.strip()79 80# 2. Convert cleaned values to numeric, invalid strings become NaN81df["price"] = pd.to_numeric(df["price"], errors='coerce')82 83# 4. Convert price values to integers84df["price"] = df["price"].astype(int)85 86#* Handle outliers in 'price' using IQR method87Q1 = df['price'].quantile(0.25)88Q3 = df['price'].quantile(0.75)89IQR = Q3 - Q190lower_bound = Q1 - 1.5 * IQR91upper_bound = Q3 + 1.5 * IQR92outliers = df[(df['price'] < lower_bound) | (df['price'] > upper_bound)]93 94#* Clip 'price' to within IQR bounds and store in a new column95df['price'] = df['price'].clip(lower_bound, upper_bound)96 97 98#* Convert mileage ranges like "120 000 - 129 999" to the mean value99def mileage_to_mean(x):100 try:101 parts = x.split('-')102 return (int(parts[0].replace(' ', '')) + int(parts[1].replace(' ', ''))) / 2103 except:104 return np.nan105 106df['mileage'] = df['mileage'].apply(mileage_to_mean)107 108#* Fill missing mileage values with the column mean109df['mileage'] = df['mileage'].fillna(df['mileage'].mean())110 111#* Convert 'model_year' to numeric and fill missing values with median112df['model_year'] = pd.to_numeric(df['model_year'], errors='coerce')113df['model_year'] = df['model_year'].fillna(df['model_year'].median())114 115#* Extract numeric value from 'tax_horsepower' (e.g., "6 CV" -> 6.0)116df['tax_horsepower'] = df['tax_horsepower'].str.extract(r"(\d+)").astype(float)117df['tax_horsepower'] = df['tax_horsepower'].fillna(df['tax_horsepower'].median())118 119 120#* Calculate car age from model year121df['car_age'] = 2025 - df['model_year']122 123#* Translate french values to english124df["transmission"] = df["transmission"].replace("Automatique","automatic").replace("Manuelle","manual")125df["fuel_type"] = df["fuel_type"].replace("Essence","Gasoline").replace("Electrique","Electric").replace("Hybride","Hybrid")126df["origin"] = df["origin"].replace("WW au Maroc","Morocco WW").replace("Dédouanée","Customs cleared").replace("Importée neuve","Newly imported").replace("Pas encore dédouanée","Not yet customs cleared")127 128#* export cleaned up dataset129if EXPORT:130 df.to_csv(EXPORT_FILE,index=False)