|
| 1 | + |
| 2 | +#? STAGE 2: DATA PREPROCESSING |
| 3 | + |
| 4 | +#* Importing dependencies |
| 5 | +import pandas as pd |
| 6 | +import numpy as np |
| 7 | +from sklearn.impute import SimpleImputer |
| 8 | +from sklearn.preprocessing import StandardScaler, OneHotEncoder |
| 9 | +from sklearn.feature_selection import VarianceThreshold, mutual_info_classif |
| 10 | +from sklearn.decomposition import PCA |
| 11 | +from sklearn.model_selection import train_test_split |
| 12 | +import seaborn as sns |
| 13 | +import matplotlib.pyplot as plt |
| 14 | +import os |
| 15 | +from src.components.data_ingestion import datasets |
| 16 | + |
| 17 | + |
| 18 | +#* Define Preprocessing Function |
| 19 | +def preprocess_data(train_df, test_df): |
| 20 | + # Store target variable |
| 21 | + train_target = train_df['smoking'] |
| 22 | + test_target = test_df['smoking'] |
| 23 | + |
| 24 | + # Remove target from features |
| 25 | + train_features = train_df.drop('smoking', axis=1) |
| 26 | + test_features = test_df.drop('smoking', axis=1) |
| 27 | + |
| 28 | + # Get numeric columns excluding target |
| 29 | + num_cols = train_features.select_dtypes(include=['int64', 'float64']).columns.tolist() |
| 30 | + |
| 31 | + # Handle missing values for numeric columns |
| 32 | + imputer = SimpleImputer(strategy='mean') |
| 33 | + train_features[num_cols] = imputer.fit_transform(train_features[num_cols]) |
| 34 | + test_features[num_cols] = imputer.transform(test_features[num_cols]) |
| 35 | + |
| 36 | + # Handle categorical values |
| 37 | + cat_cols = train_features.select_dtypes(include=['object']).columns |
| 38 | + encoder = OneHotEncoder(handle_unknown='ignore', sparse_output=False) |
| 39 | + |
| 40 | + # Encode categorical columns |
| 41 | + if len(cat_cols) > 0: |
| 42 | + train_encoded = pd.DataFrame( |
| 43 | + encoder.fit_transform(train_features[cat_cols]), |
| 44 | + index=train_features.index, |
| 45 | + columns=encoder.get_feature_names_out(cat_cols) |
| 46 | + ) |
| 47 | + test_encoded = pd.DataFrame( |
| 48 | + encoder.transform(test_features[cat_cols]), |
| 49 | + index=test_features.index, |
| 50 | + columns=encoder.get_feature_names_out(cat_cols) |
| 51 | + ) |
| 52 | + |
| 53 | + # Drop original categorical columns and reset index |
| 54 | + train_features = train_features.drop(cat_cols, axis=1) |
| 55 | + test_features = test_features.drop(cat_cols, axis=1) |
| 56 | + |
| 57 | + # Concatenate encoded features |
| 58 | + train_features = pd.concat([train_features, train_encoded], axis=1) |
| 59 | + test_features = pd.concat([test_features, test_encoded], axis=1) |
| 60 | + |
| 61 | + # Feature Scaling - only scale numeric columns |
| 62 | + scaler = StandardScaler() |
| 63 | + train_features[num_cols] = scaler.fit_transform(train_features[num_cols]) |
| 64 | + test_features[num_cols] = scaler.transform(test_features[num_cols]) |
| 65 | + |
| 66 | + # Split features and target |
| 67 | + X = train_features |
| 68 | + y = train_target |
| 69 | + |
| 70 | + # Split training data into train and validation sets |
| 71 | + x_train, x_val, y_train, y_val = train_test_split( |
| 72 | + X, y, |
| 73 | + test_size=0.2, |
| 74 | + random_state=42 |
| 75 | + ) |
| 76 | + |
| 77 | + # Store selected features |
| 78 | + selected_features = x_train.columns.tolist() |
| 79 | + |
| 80 | + # Return all 5 expected values |
| 81 | + return x_train, x_val, y_train, y_val, selected_features |
| 82 | + |
| 83 | + |
| 84 | +def remove_low_variance_features(train_df, test_df, threshold=0.01): |
| 85 | + train_target = train_df['smoking'] if 'smoking' in train_df.columns else None |
| 86 | + train_features = train_df.drop('smoking', axis=1) if 'smoking' in train_df.columns else train_df |
| 87 | + |
| 88 | + test_target = test_df['smoking'] if 'smoking' in test_df.columns else None |
| 89 | + test_features = test_df.drop('smoking', axis=1) if 'smoking' in test_df.columns else test_df |
| 90 | + |
| 91 | + selector = VarianceThreshold(threshold) |
| 92 | + train_features_var = selector.fit_transform(train_features) |
| 93 | + test_features_var = selector.transform(test_features) |
| 94 | + |
| 95 | + selected_columns = train_features.columns[selector.get_support()] |
| 96 | + |
| 97 | + train_selected = pd.DataFrame(train_features_var, columns=selected_columns, index=train_df.index) |
| 98 | + test_selected = pd.DataFrame(test_features_var, columns=selected_columns, index=test_df.index) |
| 99 | + |
| 100 | + if train_target is not None: |
| 101 | + train_selected['smoking'] = train_target |
| 102 | + if test_target is not None: |
| 103 | + test_selected['smoking'] = test_target |
| 104 | + |
| 105 | + return train_selected, test_selected |
| 106 | + |
| 107 | + |
| 108 | +def remove_highly_correlated_features(train_df, test_df, threshold=0.9): |
| 109 | + correlation_matrix = train_df.corr() |
| 110 | + upper_triangle = correlation_matrix.where(np.triu(np.ones(correlation_matrix.shape), k=1).astype(bool)) |
| 111 | + drop_cols = [column for column in upper_triangle.columns if any(upper_triangle[column] > threshold)] |
| 112 | + return train_df.drop(columns=drop_cols), test_df.drop(columns=drop_cols) |
| 113 | + |
| 114 | + |
| 115 | +def select_features_by_mutual_info(train_df, test_df, target_column, num_features=15): |
| 116 | + X = train_df.drop(columns=[target_column]) |
| 117 | + y = train_df[target_column] |
| 118 | + |
| 119 | + mutual_info = mutual_info_classif(X, y, discrete_features='auto') |
| 120 | + feature_scores = pd.Series(mutual_info, index=X.columns) |
| 121 | + selected_features = feature_scores.nlargest(num_features).index.to_list() |
| 122 | + |
| 123 | + if target_column in test_df.columns: |
| 124 | + return train_df[selected_features + [target_column]], test_df[selected_features + [target_column]] |
| 125 | + else: |
| 126 | + return train_df[selected_features + [target_column]], test_df[selected_features] |
| 127 | + |
| 128 | + |
| 129 | +def apply_pca(train_df, test_df, n_components=10): |
| 130 | + pca = PCA(n_components=n_components) |
| 131 | + train_pca = pca.fit_transform(train_df) |
| 132 | + test_pca = pca.transform(test_df) |
| 133 | + return pd.DataFrame(train_pca), pd.DataFrame(test_pca) |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + #* Load both Train and Test Datasets |
| 138 | + train_ml = pd.DataFrame(datasets["ml-olympiad-smoking"]["train"]) |
| 139 | + test_ml = pd.DataFrame(datasets["ml-olympiad-smoking"]["test"]) |
| 140 | + train_archive = pd.DataFrame(datasets["archive"]["train"]) |
| 141 | + test_archive = pd.DataFrame(datasets["archive"]["test"]) |
| 142 | + |
| 143 | + print("DISPLAY BASIC INFORMATION") |
| 144 | + print("ML Olympiad Train Data Shape:", train_ml.shape) |
| 145 | + print("ML Olympiad Test Data Shape:", test_ml.shape) |
| 146 | + print(train_ml.head()) |
| 147 | + print("Archive Train Data Shape:", train_archive.shape) |
| 148 | + print("Archive Test Data Shape:", test_archive.shape) |
| 149 | + print(test_archive.head()) |
| 150 | + |
| 151 | + #* Apply Preprocessing to all datasets |
| 152 | + x_train_ml, x_val_ml, y_train_ml, y_val_ml, selected_features_ml = preprocess_data(train_ml, test_ml) |
| 153 | + x_train_archive, x_val_archive, y_train_archive, y_val_archive, selected_features_archive = preprocess_data(train_archive, test_archive) |
| 154 | + |
| 155 | + preprocessed_data_paths = { |
| 156 | + "ml-olympiad-smoking": { |
| 157 | + "train": "Y:/SmokingML V2/data/processed/ml_olympiad_train.csv", |
| 158 | + "test": "Y:/SmokingML V2/data/processed/ml_olympiad_test.csv" |
| 159 | + }, |
| 160 | + "archive": { |
| 161 | + "train": "Y:/SmokingML V2/data/processed/archive_train.csv", |
| 162 | + "test": "Y:/SmokingML V2/data/processed/archive_test.csv" |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + for dataset_name, paths in preprocessed_data_paths.items(): |
| 167 | + for key, path in paths.items(): |
| 168 | + os.makedirs(os.path.dirname(path), exist_ok=True) |
| 169 | + |
| 170 | + pd.concat([x_train_ml, y_train_ml], axis=1).to_csv(preprocessed_data_paths["ml-olympiad-smoking"]["train"], index=False) |
| 171 | + pd.concat([x_val_ml, y_val_ml], axis=1).to_csv(preprocessed_data_paths["ml-olympiad-smoking"]["test"], index=False) |
| 172 | + pd.concat([x_train_archive, y_train_archive], axis=1).to_csv(preprocessed_data_paths["archive"]["train"], index=False) |
| 173 | + pd.concat([x_val_archive, y_val_archive], axis=1).to_csv(preprocessed_data_paths["archive"]["test"], index=False) |
| 174 | + |
| 175 | + print("Preprocessed data has been saved successfully!") |
| 176 | + |
| 177 | + #* Variance Thresholding |
| 178 | + preprocessed_train_ml, preprocessed_test_ml = remove_low_variance_features(pd.concat([x_train_ml, y_train_ml], axis=1), pd.concat([x_val_ml, y_val_ml], axis=1)) |
| 179 | + preprocessed_train_archive, preprocessed_test_archive = remove_low_variance_features(pd.concat([x_train_archive, y_train_archive], axis=1), pd.concat([x_val_archive, y_val_archive], axis=1)) |
| 180 | + |
| 181 | + #* Feature Selection |
| 182 | + preprocessed_train_ml, preprocessed_test_ml = select_features_by_mutual_info(preprocessed_train_ml, preprocessed_test_ml, target_column='smoking') |
| 183 | + preprocessed_train_archive, preprocessed_test_archive = select_features_by_mutual_info(preprocessed_train_archive, preprocessed_test_archive, target_column='smoking') |
| 184 | + |
| 185 | + #* ✅ Optional assertion checks |
| 186 | + assert 'smoking' in preprocessed_train_ml.columns, "Target column 'smoking' missing in training set!" |
| 187 | + assert 'smoking' in preprocessed_test_ml.columns, "Target column 'smoking' missing in test set!" |
| 188 | + assert 'smoking' in preprocessed_train_archive.columns, "Target column 'smoking' missing in archive training set!" |
| 189 | + assert 'smoking' in preprocessed_test_archive.columns, "Target column 'smoking' missing in archive test set!" |
| 190 | + |
| 191 | + #* ✅ Debug: Show absolute save paths |
| 192 | + print("\n✅ Saving preprocessed files to:") |
| 193 | + print("ML Train Path :", os.path.abspath("Y:/SmokingML V2/data/processed/train_ml.csv")) |
| 194 | + print("ML Test Path :", os.path.abspath("Y:/SmokingML V2/data/processed/test_ml.csv")) |
| 195 | + print("Archive Train Path :", os.path.abspath("Y:/SmokingML V2/data/processed/train_archive.csv")) |
| 196 | + print("Archive Test Path :", os.path.abspath("Y:/SmokingML V2/data/processed/test_archive.csv")) |
| 197 | + |
| 198 | + #* Save final preprocessed files |
| 199 | + preprocessed_train_ml.to_csv("Y:/SmokingML V2/data/processed/train_ml.csv", index=False) |
| 200 | + preprocessed_test_ml.to_csv("Y:/SmokingML V2/data/processed/test_ml.csv", index=False) |
| 201 | + preprocessed_train_archive.to_csv("Y:/SmokingML V2/data/processed/train_archive.csv", index=False) |
| 202 | + preprocessed_test_archive.to_csv("Y:/SmokingML V2/data/processed/test_archive.csv", index=False) |
| 203 | + |
| 204 | + print("Feature Engineering and Selection completed Successfully!") |
| 205 | + |
| 206 | + |
| 207 | + import json |
| 208 | + |
| 209 | + #* Save selected features to JSON for both datasets |
| 210 | + selected_features_dir = "Y:/SmokingML V2/artifacts/models" |
| 211 | + os.makedirs(selected_features_dir, exist_ok=True) |
| 212 | + |
| 213 | + # Remove 'smoking' from selected columns before saving (optional based on use-case) |
| 214 | + selected_columns_olympiad = [col for col in preprocessed_train_ml.columns if col != 'smoking'] |
| 215 | + selected_columns_archive = [col for col in preprocessed_train_archive.columns if col != 'smoking'] |
| 216 | + |
| 217 | + # Save to JSON |
| 218 | + with open(os.path.join(selected_features_dir, "feature_columns_olympiad.json"), "w") as f: |
| 219 | + json.dump(selected_columns_olympiad, f, indent=4) |
| 220 | + |
| 221 | + with open(os.path.join(selected_features_dir, "feature_columns_archive.json"), "w") as f: |
| 222 | + json.dump(selected_columns_archive, f, indent=4) |
| 223 | + |
| 224 | + print("✅ Feature columns JSON files saved successfully!") |
0 commit comments