Spaces:
Runtime error
Runtime error
Create data_prep.py
Browse files- data_prep.py +65 -0
data_prep.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# data_prep.py
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
# example: recency from last_purchase_days_ago (if exists)
|
| 5 |
+
if 'last_purchase_days_ago' in df.columns:
|
| 6 |
+
df['recency'] = df['last_purchase_days_ago']
|
| 7 |
+
else:
|
| 8 |
+
df['recency'] = np.nan
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# example: tenure from signup_date
|
| 12 |
+
if 'signup_date' in df.columns:
|
| 13 |
+
df['signup_date'] = pd.to_datetime(df['signup_date'], errors='coerce')
|
| 14 |
+
df['tenure_days'] = (pd.Timestamp('today') - df['signup_date']).dt.days.fillna(df['signup_date'].median())
|
| 15 |
+
else:
|
| 16 |
+
df['tenure_days'] = np.nan
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# keep numeric features and encode categorical later
|
| 20 |
+
return df
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def prepare_features(df: pd.DataFrame, cat_cols=None, save_encoder_path=None):
|
| 26 |
+
df = df.copy()
|
| 27 |
+
if cat_cols is None:
|
| 28 |
+
cat_cols = [c for c in df.columns if df[c].dtype == 'object']
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
num_cols = [c for c in df.columns if pd.api.types.is_numeric_dtype(df[c])]
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# One-hot encode categories (simple)
|
| 35 |
+
if len(cat_cols) > 0:
|
| 36 |
+
encoder = OneHotEncoder(handle_unknown='ignore', sparse=False)
|
| 37 |
+
cat_mat = encoder.fit_transform(df[cat_cols].astype(str))
|
| 38 |
+
cat_df = pd.DataFrame(cat_mat, columns=encoder.get_feature_names_out(cat_cols), index=df.index)
|
| 39 |
+
features = pd.concat([df[num_cols], cat_df], axis=1)
|
| 40 |
+
if save_encoder_path:
|
| 41 |
+
joblib.dump(encoder, save_encoder_path)
|
| 42 |
+
else:
|
| 43 |
+
features = df[num_cols]
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
return features.fillna(0)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
if __name__ == '__main__':
|
| 52 |
+
import argparse
|
| 53 |
+
parser = argparse.ArgumentParser()
|
| 54 |
+
parser.add_argument('--input', default='data/customers_example.csv')
|
| 55 |
+
parser.add_argument('--out_features', default='data/features.parquet')
|
| 56 |
+
parser.add_argument('--save_encoder', default='data/ohe.joblib')
|
| 57 |
+
args = parser.parse_args()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
df = load_data(args.input)
|
| 61 |
+
df = basic_clean(df)
|
| 62 |
+
df = feature_engineer(df)
|
| 63 |
+
features = prepare_features(df, save_encoder_path=args.save_encoder)
|
| 64 |
+
features.to_parquet(args.out_features)
|
| 65 |
+
print('Saved features to', args.out_features)
|