Engineer New Features
Advanced · Write the code · from Predict Customer Churn: End-to-End Data Analysis (with sklearn)
From the lesson: Step 3: Feature Engineering
Raw data rarely gives the best predictions. Feature engineering is the art of creating new columns that capture patterns more clearly. For example, instead of raw tenure in months, we can create buckets like "new" (0–6 months), "growing" (7–24), and "loyal" (25+). This helps the model spot thresholds.
Read this part of the lessonStarting from the provided DataFrame df, create two new columns:
1. tenure_bucket - 'new' if tenure_months <= 6, 'growing' if 7–24, 'loyal' if 25+. Use pd.cut() or conditional logic.
2. charge_per_month - total_charges / tenure_months (handle division by zero by replacing inf/NaN with monthly_charges).
Print the value counts of tenure_bucket, then print the first 5 rows showing customer_id, tenure_bucket, and charge_per_month.
More Churn Prediction exercises
- Build the Customer DataFrameAdvanced
- Exploratory Data AnalysisAdvanced
- Encode and Scale FeaturesAdvanced
- Train a Logistic Regression ModelAdvanced
- Build a Confusion MatrixAdvanced
- Generate a Churn Risk ReportAdvanced
All Python exercises · 50 practice problems · Practical Projects quiz