Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /kraftaa_transformdash /ml /train_telco_churn.py
| """ | |
| Train Telco Customer Churn Model | |
| Uses real public dataset with proper train/test split for realistic metrics | |
| """ | |
| import sys | |
| from pathlib import Path | |
| sys.path.append(str(Path(__file__).parent.parent)) | |
| import pandas as pd | |
| import numpy as np | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.preprocessing import StandardScaler, LabelEncoder | |
| from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score | |
| from ml.registry.model_registry import model_registry | |
| import requests | |
| import os | |
| def download_telco_dataset(): | |
| """Download Telco Customer Churn dataset""" | |
| print("Downloading Telco Customer Churn dataset...") | |
| # Direct link to Telco Customer Churn CSV | |
| url = "https://raw.githubusercontent.com/IBM/telco-customer-churn-on-icp4d/master/data/Telco-Customer-Churn.csv" | |
| data_dir = Path(__file__).parent / "data" | |
| data_dir.mkdir(exist_ok=True) | |
| csv_path = data_dir / "telco_churn.csv" | |
| if csv_path.exists(): | |
| print(f"Dataset already exists at {csv_path}") | |
| return pd.read_csv(csv_path) | |
| try: | |
| response = requests.get(url) | |
| response.raise_for_status() | |
| with open(csv_path, 'wb') as f: | |
| f.write(response.content) | |
| print(f"Downloaded dataset to {csv_path}") | |
| return pd.read_csv(csv_path) | |
| except Exception as e: | |
| print(f"Error downloading dataset: {e}") | |
| print("Creating synthetic realistic dataset instead...") | |
| return create_synthetic_realistic_data() | |
| def create_synthetic_realistic_data(): | |
| """Create synthetic but realistic churn data if download fails""" | |
| np.random.seed(42) | |
| n_samples = 1000 | |
| data = { | |
| 'tenure': np.random.randint(1, 72, n_samples), | |
| 'MonthlyCharges': np.random.uniform(20, 120, n_samples), | |
| 'TotalCharges': np.random.uniform(20, 8000, n_samples), | |
| 'Contract': np.random.choice(['Month-to-month', 'One year', 'Two year'], n_samples, p=[0.5, 0.3, 0.2]), | |
| 'InternetService': np.random.choice(['DSL', 'Fiber optic', 'No'], n_samples, p=[0.4, 0.4, 0.2]), | |
| 'OnlineSecurity': np.random.choice(['Yes', 'No', 'No internet service'], n_samples, p=[0.3, 0.5, 0.2]), | |
| 'TechSupport': np.random.choice(['Yes', 'No', 'No internet service'], n_samples, p=[0.3, 0.5, 0.2]), | |
| 'PaymentMethod': np.random.choice(['Electronic check', 'Mailed check', 'Bank transfer', 'Credit card'], n_samples), | |
| } | |
| df = pd.DataFrame(data) | |
| # Create realistic churn based on features | |
| churn_prob = ( | |
| (df['Contract'] == 'Month-to-month').astype(float) * 0.3 + | |
| (df['tenure'] < 12).astype(float) * 0.2 + | |
| (df['MonthlyCharges'] > 80).astype(float) * 0.15 + | |
| (df['OnlineSecurity'] == 'No').astype(float) * 0.1 + | |
| np.random.uniform(0, 0.25, n_samples) | |
| ) | |
| df['Churn'] = (churn_prob > 0.5).astype(int) | |
| return df | |
| def preprocess_telco_data(df): | |
| """Preprocess Telco dataset""" | |
| print("\nPreprocessing data...") | |
| # Make a copy | |
| df = df.copy() | |
| # Handle TotalCharges if it's a string | |
| if df['TotalCharges'].dtype == 'object': | |
| df['TotalCharges'] = pd.to_numeric(df['TotalCharges'], errors='coerce') | |
| df['TotalCharges'].fillna(df['TotalCharges'].median(), inplace=True) | |
| # Convert Churn to binary (if it's Yes/No) | |
| if df['Churn'].dtype == 'object': | |
| df['Churn'] = (df['Churn'] == 'Yes').astype(int) | |
| # Select features | |
| feature_cols = [ | |
| 'tenure', 'MonthlyCharges', 'TotalCharges', | |
| 'Contract', 'InternetService', 'OnlineSecurity', | |
| 'TechSupport', 'PaymentMethod' | |
| ] | |
| # Keep only available columns | |
| feature_cols = [col for col in feature_cols if col in df.columns] | |
| X = df[feature_cols].copy() | |
| y = df['Churn'] | |
| # Encode categorical variables | |
| categorical_cols = X.select_dtypes(include=['object']).columns | |
| label_encoders = {} | |
| for col in categorical_cols: | |
| le = LabelEncoder() | |
| X[col] = le.fit_transform(X[col].astype(str)) | |
| label_encoders[col] = le | |
| print(f"Features: {list(X.columns)}") | |
| print(f"Total samples: {len(X)}") | |
| print(f"Churn rate: {y.mean():.2%}") | |
| return X, y, feature_cols | |
| def train_telco_churn_model(): | |
| """Train customer churn model on real data""" | |
| print("\n" + "="*60) | |
| print("TRAINING TELCO CUSTOMER CHURN MODEL") | |
| print("="*60 + "\n") | |
| # Load dataset | |
| df = download_telco_dataset() | |
| # Preprocess | |
| X, y, feature_cols = preprocess_telco_data(df) | |
| # Split data - THIS IS KEY FOR REALISTIC METRICS | |
| print("\nSplitting data (80% train, 20% test)...") | |
| X_train, X_test, y_train, y_test = train_test_split( | |
| X, y, test_size=0.2, random_state=42, stratify=y | |
| ) | |
| print(f"Training set: {len(X_train)} samples") | |
| print(f"Test set: {len(X_test)} samples") | |
| # Scale features | |
| print("\nScaling features...") | |
| scaler = StandardScaler() | |
| X_train_scaled = scaler.fit_transform(X_train) | |
| X_test_scaled = scaler.transform(X_test) | |
| # Train model | |
| print("\nTraining Random Forest model...") | |
| model = RandomForestClassifier( | |
| n_estimators=100, | |
| max_depth=10, | |
| min_samples_split=5, | |
| random_state=42, | |
| n_jobs=-1 | |
| ) | |
| model.fit(X_train_scaled, y_train) | |
| # Evaluate on TEST SET (not training set!) | |
| print("\nEvaluating on test set...") | |
| y_pred = model.predict(X_test_scaled) | |
| y_pred_proba = model.predict_proba(X_test_scaled)[:, 1] | |
| metrics = { | |
| 'accuracy': accuracy_score(y_test, y_pred), | |
| 'precision': precision_score(y_test, y_pred), | |
| 'recall': recall_score(y_test, y_pred), | |
| 'f1_score': f1_score(y_test, y_pred), | |
| 'roc_auc': roc_auc_score(y_test, y_pred_proba) | |
| } | |
| print("\n" + "="*60) | |
| print("TEST SET METRICS (Realistic!):") | |
| print("="*60) | |
| for metric, value in metrics.items(): | |
| print(f" {metric:12s}: {value:.4f}") | |
| # Feature importance | |
| feature_importance = pd.DataFrame({ | |
| 'feature': feature_cols, | |
| 'importance': model.feature_importances_ | |
| }).sort_values('importance', ascending=False) | |
| print("\nTop 5 Most Important Features:") | |
| for idx, row in feature_importance.head(5).iterrows(): | |
| print(f" {row['feature']:20s}: {row['importance']:.4f}") | |
| # Prepare training configuration | |
| training_config = { | |
| "dataset": "IBM Telco Customer Churn", | |
| "dataset_size": len(df), | |
| "train_samples": len(X_train), | |
| "test_samples": len(X_test), | |
| "test_split_ratio": 0.2, | |
| "churn_rate": float(y.mean()), | |
| "train_churn_rate": float(y_train.mean()), | |
| "test_churn_rate": float(y_test.mean()), | |
| "scaling": "StandardScaler", | |
| "random_seed": 42, | |
| "stratified_split": True | |
| } | |
| # Register model | |
| print("\nRegistering model in model registry...") | |
| model_id = model_registry.register_model( | |
| model=model, | |
| model_name="telco_customer_churn", | |
| model_type="classification", | |
| metrics=metrics, | |
| feature_columns=feature_cols, | |
| target_column="Churn", | |
| description="Predicts telecom customer churn using Random Forest. Trained on real Telco dataset with proper train/test split.", | |
| tags=["telecom", "churn", "classification", "random-forest", "real-data"], | |
| hyperparameters=model.get_params(), | |
| training_config=training_config | |
| ) | |
| print(f"\n✅ Model registered: {model_id}") | |
| print("\nYou can now view this model in the ML Models UI!") | |
| return model, metrics, feature_cols | |
| if __name__ == "__main__": | |
| try: | |
| model, metrics, features = train_telco_churn_model() | |
| except Exception as e: | |
| print(f"\n❌ Training failed: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| sys.exit(1) | |
Xet Storage Details
- Size:
- 7.88 kB
- Xet hash:
- 93878f18e358b6682ac2b3064efa3dca2f4af744396a087a4c1cc86e3c72df42
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.