Encode and Scale Features
Advanced · Write the code · from Predict Customer Churn: End-to-End Data Analysis (with sklearn)
From the lesson: Step 4: Prepare Features for the Model
Machine learning models work with numbers, not strings. We need to convert categorical columns like "contract" into numerical form. One-hot encoding creates a separate binary column for each category. We also need to separate our features (X) from our target (y).
Read this part of the lessonStarting from the provided DataFrame df:
1. One-hot encode the contract column using pd.get_dummies() with drop_first=True.
2. Create feature matrix X with columns: tenure_months, monthly_charges, and the encoded contract columns. Create target y from the churned column.
3. Scale X using StandardScaler from sklearn.
4. Print the shape of X and the column names.
More Churn Prediction exercises
- Build the Customer DataFrameAdvanced
- Exploratory Data AnalysisAdvanced
- Engineer New FeaturesAdvanced
- Train a Logistic Regression ModelAdvanced
- Build a Confusion MatrixAdvanced
- Generate a Churn Risk ReportAdvanced
All Python exercises · 50 practice problems · Practical Projects quiz