help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
fraud-detection
RSK World
fraud-detection
Fraud Detection Dataset - Financial Fraud ML + Anti-Fraud AI + Fraud Detection Deep Learning
fraud-detection
  • __pycache__
  • .gitignore866 B
  • ADVANCED_FEATURES.md10.6 KB
  • ERROR_CHECK_REPORT.md1.8 KB
  • GITHUB_RELEASE_GUIDE.md4.6 KB
  • LICENSE1.6 KB
  • README.md8.2 KB
  • RELEASE_NOTES.md2.8 KB
  • advanced_feature_engineering.py9.5 KB
  • feature_engineering.py9.2 KB
  • fraud_detection_analysis.ipynb12.8 KB
  • fraud_detection_dataset.csv2.3 MB
  • generate_data.py9.8 KB
  • hyperparameter_tuning.py9.8 KB
  • index.html12.3 KB
  • model_evaluation_advanced.py9.9 KB
  • predict_pipeline.py8.1 KB
  • requirements.txt462 B
  • shap_explainability.py7.4 KB
  • test_imports.py2.6 KB
  • train_model.py12.4 KB
  • verify_dataset.py1 KB
train_model.pyfeature_engineering.py
train_model.py
Raw Download
Find: Go to:
"""
Fraud Detection Model Training Script

Developer: Molla Samser
Designer & Tester: Rima Khatun
Website: https://rskworld.in
Email: help@rskworld.in, support@rskworld.in, info@rskworld.com
Phone: +91 93305 39277
Company: RSK World
Description: Trains machine learning models for fraud detection
"""

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, cross_val_score, StratifiedKFold
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score, roc_curve, precision_recall_curve, average_precision_score
from imblearn.over_sampling import SMOTE
import matplotlib.pyplot as plt
import seaborn as sns
import joblib
import warnings
warnings.filterwarnings('ignore')

# Advanced models
try:
    import xgboost as xgb
    XGBOOST_AVAILABLE = True
except ImportError:
    XGBOOST_AVAILABLE = False
    print("XGBoost not available. Install with: pip install xgboost")

try:
    import lightgbm as lgb
    LIGHTGBM_AVAILABLE = True
except ImportError:
    LIGHTGBM_AVAILABLE = False
    print("LightGBM not available. Install with: pip install lightgbm")

try:
    import shap
    SHAP_AVAILABLE = True
except ImportError:
    SHAP_AVAILABLE = False
    print("SHAP not available. Install with: pip install shap")

# Set style for plots
try:
    plt.style.use('seaborn-v0_8-darkgrid')
except OSError:
    try:
        plt.style.use('seaborn-darkgrid')
    except OSError:
        plt.style.use('default')
sns.set_palette("husl")

def load_data(file_path='fraud_detection_dataset.csv'):
    """
    Load the fraud detection dataset
    
    Parameters:
    -----------
    file_path : str
        Path to the CSV file
    
    Returns:
    --------
    pd.DataFrame
        Loaded dataset
    """
    print("Loading dataset...")
    df = pd.read_csv(file_path)
    print(f"Dataset loaded: {len(df)} records")
    return df

def preprocess_data(df, use_advanced_features=True):
    """
    Preprocess the data for machine learning with advanced feature engineering
    
    Parameters:
    -----------
    df : pd.DataFrame
        Raw dataset
    use_advanced_features : bool
        Whether to use advanced feature engineering
    
    Returns:
    --------
    tuple
        Features (X) and target (y)
    """
    print("\nPreprocessing data...")
    
    # Import feature engineering module
    if use_advanced_features:
        try:
            from advanced_feature_engineering import engineer_advanced_features
            print("Using advanced feature engineering...")
            df_processed = engineer_advanced_features(df)
        except ImportError:
            print("Advanced feature engineering module not found, using basic preprocessing...")
            df_processed = df.copy()
    else:
        df_processed = df.copy()
    
    # Convert timestamp to datetime if it's string
    if 'timestamp' in df_processed.columns:
        df_processed['timestamp'] = pd.to_datetime(df_processed['timestamp'])
        if 'day_of_week' not in df_processed.columns:
            df_processed['day_of_week'] = df_processed['timestamp'].dt.dayofweek
        if 'month' not in df_processed.columns:
            df_processed['month'] = df_processed['timestamp'].dt.month
        df_processed = df_processed.drop('timestamp', axis=1)
    
    # Encode categorical variables
    label_encoders = {}
    categorical_cols = ['merchant_category', 'location', 'device_type', 'user_id']
    
    for col in categorical_cols:
        if col in df_processed.columns:
            le = LabelEncoder()
            df_processed[col] = le.fit_transform(df_processed[col].astype(str))
            label_encoders[col] = le
    
    # Drop transaction_id (not useful for prediction)
    if 'transaction_id' in df_processed.columns:
        df_processed = df_processed.drop('transaction_id', axis=1)
    
    # Separate features and target
    X = df_processed.drop('is_fraud', axis=1)
    y = df_processed['is_fraud']
    
    print(f"Features shape: {X.shape}")
    print(f"Target distribution:\n{y.value_counts()}")
    
    return X, y, label_encoders

def handle_imbalance(X, y):
    """
    Handle class imbalance using SMOTE
    
    Parameters:
    -----------
    X : pd.DataFrame
        Features
    y : pd.Series
        Target variable
    
    Returns:
    --------
    tuple
        Balanced X and y
    """
    print("\nHandling class imbalance with SMOTE...")
    smote = SMOTE(random_state=42)
    X_balanced, y_balanced = smote.fit_resample(X, y)
    print(f"After SMOTE - Features shape: {X_balanced.shape}")
    print(f"After SMOTE - Target distribution:\n{pd.Series(y_balanced).value_counts()}")
    return X_balanced, y_balanced

def train_advanced_models(X, y, use_smote=True):
    """
    Train multiple advanced models for fraud detection
    
    Parameters:
    -----------
    X : pd.DataFrame
        Features
    y : pd.Series
        Target variable
    use_smote : bool
        Whether to use SMOTE for balancing
    
    Returns:
    --------
    dict
        Dictionary of trained models and their results
    """
    print("\nTraining advanced models...")
    
    # Handle imbalance if requested
    if use_smote:
        X_train, y_train = handle_imbalance(X, y)
        X_train, X_test, y_train, y_test = train_test_split(
            X_train, y_train, test_size=0.2, random_state=42, stratify=y_train
        )
    else:
        X_train, X_test, y_train, y_test = train_test_split(
            X, y, test_size=0.2, random_state=42, stratify=y
        )
    
    models = {}
    results = {}
    
    # 1. Random Forest
    print("\n" + "="*50)
    print("Training Random Forest...")
    print("="*50)
    rf_model = RandomForestClassifier(
        n_estimators=200,
        max_depth=25,
        min_samples_split=5,
        min_samples_leaf=2,
        random_state=42,
        n_jobs=-1,
        class_weight='balanced'
    )
    rf_model.fit(X_train, y_train)
    models['RandomForest'] = rf_model
    
    # 2. XGBoost
    if XGBOOST_AVAILABLE:
        print("\n" + "="*50)
        print("Training XGBoost...")
        print("="*50)
        xgb_model = xgb.XGBClassifier(
            n_estimators=200,
            max_depth=6,
            learning_rate=0.1,
            subsample=0.8,
            colsample_bytree=0.8,
            random_state=42,
            eval_metric='logloss',
            scale_pos_weight=(y_train == 0).sum() / (y_train == 1).sum()
        )
        xgb_model.fit(X_train, y_train)
        models['XGBoost'] = xgb_model
    
    # 3. LightGBM
    if LIGHTGBM_AVAILABLE:
        print("\n" + "="*50)
        print("Training LightGBM...")
        print("="*50)
        lgb_model = lgb.LGBMClassifier(
            n_estimators=200,
            max_depth=7,
            learning_rate=0.1,
            subsample=0.8,
            colsample_bytree=0.8,
            random_state=42,
            class_weight='balanced',
            verbose=-1
        )
        lgb_model.fit(X_train, y_train)
        models['LightGBM'] = lgb_model
    
    # Evaluate all models
    print("\n" + "="*70)
    print("MODEL COMPARISON")
    print("="*70)
    
    for model_name, model in models.items():
        print(f"\n{model_name} Evaluation:")
        print("-" * 50)
        
        y_pred = model.predict(X_test)
        y_pred_proba = model.predict_proba(X_test)[:, 1]
        
        # Metrics
        roc_auc = roc_auc_score(y_test, y_pred_proba)
        avg_precision = average_precision_score(y_test, y_pred_proba)
        
        print(f"ROC AUC Score: {roc_auc:.4f}")
        print(f"Average Precision: {avg_precision:.4f}")
        print("\nClassification Report:")
        print(classification_report(y_test, y_pred))
        
        results[model_name] = {
            'model': model,
            'y_pred': y_pred,
            'y_pred_proba': y_pred_proba,
            'roc_auc': roc_auc,
            'avg_precision': avg_precision
        }
    
    # Find best model
    best_model_name = max(results.keys(), key=lambda x: results[x]['roc_auc'])
    print(f"\n{'='*70}")
    print(f"Best Model: {best_model_name} (ROC AUC: {results[best_model_name]['roc_auc']:.4f})")
    print(f"{'='*70}")
    
    # Plot comparison
    plot_model_comparison(results, y_test, X.columns)
    
    return models, results, X_test, y_test, best_model_name

def plot_model_comparison(results, y_test, feature_names):
    """Plot comparison of different models"""
    
    # ROC Curves Comparison
    plt.figure(figsize=(12, 5))
    
    plt.subplot(1, 2, 1)
    for model_name, result in results.items():
        fpr, tpr, _ = roc_curve(y_test, result['y_pred_proba'])
        plt.plot(fpr, tpr, label=f"{model_name} (AUC = {result['roc_auc']:.3f})")
    plt.plot([0, 1], [0, 1], 'k--', label='Random Classifier')
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('ROC Curves Comparison')
    plt.legend()
    plt.grid(True, alpha=0.3)
    
    # Precision-Recall Curves
    plt.subplot(1, 2, 2)
    for model_name, result in results.items():
        precision, recall, _ = precision_recall_curve(y_test, result['y_pred_proba'])
        plt.plot(recall, precision, label=f"{model_name} (AP = {result['avg_precision']:.3f})")
    plt.xlabel('Recall')
    plt.ylabel('Precision')
    plt.title('Precision-Recall Curves Comparison')
    plt.legend()
    plt.grid(True, alpha=0.3)
    
    plt.tight_layout()
    plt.savefig('model_comparison.png', dpi=300, bbox_inches='tight')
    print("\nModel comparison plots saved to model_comparison.png")
    
    # Feature importance for best model
    best_model_name = max(results.keys(), key=lambda x: results[x]['roc_auc'])
    best_model = results[best_model_name]['model']
    
    if hasattr(best_model, 'feature_importances_'):
        feature_importance = pd.DataFrame({
            'feature': feature_names,
            'importance': best_model.feature_importances_
        }).sort_values('importance', ascending=False)
        
        plt.figure(figsize=(10, 8))
        top_features = feature_importance.head(20)
        sns.barplot(data=top_features, y='feature', x='importance')
        plt.title(f'Top 20 Feature Importances - {best_model_name}')
        plt.xlabel('Importance')
        plt.tight_layout()
        plt.savefig('feature_importance_advanced.png', dpi=300, bbox_inches='tight')
        print(f"Feature importance plot saved to feature_importance_advanced.png")
        
        print(f"\nTop 15 Most Important Features ({best_model_name}):")
        print(feature_importance.head(15))

def train_model(X, y, use_smote=True):
    """
    Train Random Forest model for fraud detection (backward compatibility)
    
    Parameters:
    -----------
    X : pd.DataFrame
        Features
    y : pd.Series
        Target variable
    use_smote : bool
        Whether to use SMOTE for balancing
    
    Returns:
    --------
    sklearn.estimator
        Trained model
    """
    models, results, X_test, y_test, best_model_name = train_advanced_models(X, y, use_smote)
    return results[best_model_name]['model'], X_test, y_test

def save_model(model, filename='fraud_detection_model.pkl'):
    """
    Save the trained model
    
    Parameters:
    -----------
    model : sklearn.estimator
        Trained model
    filename : str
        Output filename
    """
    joblib.dump(model, filename)
    print(f"\nModel saved to {filename}")

def main():
    """Main function"""
    print("="*50)
    print("FRAUD DETECTION MODEL TRAINING")
    print("Developer: Molla Samser")
    print("Website: https://rskworld.in")
    print("="*50)
    
    # Load data
    df = load_data()
    
    # Preprocess with advanced features
    X, y, label_encoders = preprocess_data(df, use_advanced_features=True)
    
    # Train advanced models
    models, results, X_test, y_test, best_model_name = train_advanced_models(X, y, use_smote=True)
    model = results[best_model_name]['model']
    
    # Save model
    save_model(model)
    
    print("\n" + "="*50)
    print("Training completed successfully!")
    print("="*50)

if __name__ == "__main__":
    main()

405 lines•12.4 KB
python
feature_engineering.py
Raw Download
Find: Go to:
"""
Advanced Feature Engineering for Fraud Detection

Developer: Molla Samser
Designer & Tester: Rima Khatun
Website: https://rskworld.in
Email: help@rskworld.in, support@rskworld.in, info@rskworld.com
Phone: +91 93305 39277
Company: RSK World
Description: Advanced feature engineering functions for fraud detection
"""

import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler, RobustScaler
import warnings
warnings.filterwarnings('ignore')

def engineer_advanced_features(df):
    """
    Create advanced features from raw transaction data
    
    Parameters:
    -----------
    df : pd.DataFrame
        Raw transaction dataframe with timestamp, user_id, amount, etc.
    
    Returns:
    --------
    pd.DataFrame
        DataFrame with additional engineered features
    """
    df = df.copy()
    
    # Ensure timestamp is datetime
    if 'timestamp' in df.columns:
        df['timestamp'] = pd.to_datetime(df['timestamp'])
        df = df.sort_values(['user_id', 'timestamp']).reset_index(drop=True)
    
    print("Engineering advanced features...")
    print(f"Starting with {df.shape[1]} features")
    
    # Time-based features
    if 'timestamp' in df.columns:
        df['day_of_week'] = df['timestamp'].dt.dayofweek
        df['month'] = df['timestamp'].dt.month
        df['day_of_month'] = df['timestamp'].dt.day
        df['is_month_end'] = (df['timestamp'].dt.day > 25).astype(int)
        df['is_month_start'] = (df['timestamp'].dt.day <= 5).astype(int)
        df['quarter'] = df['timestamp'].dt.quarter
    
    # Transaction velocity features
    if 'user_id' in df.columns and 'timestamp' in df.columns:
        df['time_since_last_transaction'] = df.groupby('user_id')['timestamp'].diff().dt.total_seconds() / 3600
        df['time_since_last_transaction'] = df['time_since_last_transaction'].fillna(24)
        
        # Transaction frequency
        df['transactions_per_hour'] = 1 / (df['time_since_last_transaction'] + 0.1)
        df['transactions_per_day'] = df['transactions_per_hour'] * 24
    
    # Rolling window features
    if 'user_id' in df.columns and 'amount' in df.columns:
        # Amount statistics over rolling windows
        for window in [3, 7, 14, 30]:
            df[f'amount_rolling_mean_{window}d'] = df.groupby('user_id')['amount'].transform(
                lambda x: x.rolling(window=window, min_periods=1).mean()
            )
            df[f'amount_rolling_std_{window}d'] = df.groupby('user_id')['amount'].transform(
                lambda x: x.rolling(window=window, min_periods=1).std()
            )
            df[f'amount_rolling_max_{window}d'] = df.groupby('user_id')['amount'].transform(
                lambda x: x.rolling(window=window, min_periods=1).max()
            )
    
    # Z-scores and percentiles
    if 'user_id' in df.columns and 'amount' in df.columns:
        df['amount_zscore'] = df.groupby('user_id')['amount'].transform(
            lambda x: (x - x.mean()) / x.std() if x.std() > 0 else 0
        )
        df['amount_zscore'] = df['amount_zscore'].fillna(0)
        
        df['amount_percentile'] = df.groupby('user_id')['amount'].transform(lambda x: x.rank(pct=True))
        df['amount_percentile'] = df['amount_percentile'].fillna(0.5)
        
        # Deviation from average
        if 'avg_transaction_amount' in df.columns:
            df['amount_deviation_from_avg'] = (df['amount'] - df['avg_transaction_amount']) / (df['avg_transaction_amount'] + 1)
    
    # Change indicators
    change_cols = ['location', 'device_type', 'merchant_category']
    for col in change_cols:
        if col in df.columns and 'user_id' in df.columns:
            df[f'{col}_changed'] = (df.groupby('user_id')[col].shift() != df[col]).astype(int)
            df[f'{col}_changed'] = df[f'{col}_changed'].fillna(0)
            
            # Count of unique values in recent transactions
            df[f'{col}_unique_count_7d'] = df.groupby('user_id')[col].transform(
                lambda x: x.rolling(window=7, min_periods=1).nunique()
            )
    
    # Risk score calculation
    risk_factors = []
    if 'is_foreign_transaction' in df.columns:
        risk_factors.append(df['is_foreign_transaction'] * 0.15)
    if 'transaction_count_24h' in df.columns:
        risk_factors.append((df['transaction_count_24h'] > 5).astype(int) * 0.15)
    if 'amount' in df.columns and 'user_id' in df.columns:
        amount_threshold = df.groupby('user_id')['amount'].transform('quantile', 0.95)
        risk_factors.append((df['amount'] > amount_threshold).astype(int) * 0.2)
    if 'account_age_days' in df.columns:
        risk_factors.append((df['account_age_days'] < 30).astype(int) * 0.15)
    if 'hour_of_day' in df.columns:
        risk_factors.append((df['hour_of_day'].isin([0, 1, 2, 3, 4, 5, 22, 23])).astype(int) * 0.1)
    if 'time_since_last_transaction' in df.columns:
        risk_factors.append((df['time_since_last_transaction'] < 1).astype(int) * 0.1)
    if 'location_changed' in df.columns:
        risk_factors.append(df['location_changed'] * 0.15)
    
    if risk_factors:
        df['risk_score'] = pd.concat(risk_factors, axis=1).sum(axis=1)
        df['risk_score'] = df['risk_score'].clip(0, 1)
    
    # Interaction features
    if 'amount' in df.columns and 'transaction_count_24h' in df.columns:
        df['amount_x_transaction_count'] = df['amount'] * df['transaction_count_24h']
    if 'amount' in df.columns and 'is_foreign_transaction' in df.columns:
        df['amount_x_is_foreign'] = df['amount'] * df['is_foreign_transaction']
    if 'transaction_count_24h' in df.columns and 'is_foreign_transaction' in df.columns:
        df['transaction_count_x_is_foreign'] = df['transaction_count_24h'] * df['is_foreign_transaction']
    
    # Time pattern features
    if 'hour_of_day' in df.columns:
        df['is_rush_hour'] = df['hour_of_day'].isin([7, 8, 9, 17, 18, 19]).astype(int)
        df['is_off_hours'] = df['hour_of_day'].isin([0, 1, 2, 3, 4, 5, 22, 23]).astype(int)
        df['is_business_hours'] = df['hour_of_day'].isin(range(9, 18)).astype(int)
    
    # User behavior patterns
    if 'amount' in df.columns and 'avg_transaction_amount' in df.columns:
        df['avg_amount_ratio'] = df['amount'] / (df['avg_transaction_amount'] + 1)
    if 'transaction_count_24h' in df.columns and 'time_since_last_transaction' in df.columns:
        df['transaction_velocity'] = df['transaction_count_24h'] / (df['time_since_last_transaction'] + 0.1)
    
    # Account age features
    if 'account_age_days' in df.columns:
        df['account_age_months'] = df['account_age_days'] / 30
        df['is_new_account'] = (df['account_age_days'] < 30).astype(int)
        df['is_mature_account'] = (df['account_age_days'] > 365).astype(int)
    
    # Fill NaN values
    numeric_cols = df.select_dtypes(include=[np.number]).columns
    df[numeric_cols] = df[numeric_cols].fillna(0)
    
    # Fill object columns with 'unknown'
    object_cols = df.select_dtypes(include=['object']).columns
    for col in object_cols:
        if col not in ['transaction_id', 'user_id', 'timestamp']:
            df[col] = df[col].fillna('unknown')
    
    print(f"Finished with {df.shape[1]} features")
    print(f"Added {df.shape[1] - len([c for c in df.columns if c not in df.columns])} new features")
    
    return df

def scale_features(X, scaler_type='standard'):
    """
    Scale features for better model performance
    
    Parameters:
    -----------
    X : pd.DataFrame or np.array
        Features to scale
    scaler_type : str
        Type of scaler ('standard' or 'robust')
    
    Returns:
    --------
    tuple
        Scaled features and scaler object
    """
    if scaler_type == 'standard':
        scaler = StandardScaler()
    elif scaler_type == 'robust':
        scaler = RobustScaler()
    else:
        raise ValueError("scaler_type must be 'standard' or 'robust'")
    
    if isinstance(X, pd.DataFrame):
        X_scaled = pd.DataFrame(
            scaler.fit_transform(X),
            columns=X.columns,
            index=X.index
        )
    else:
        X_scaled = scaler.fit_transform(X)
    
    return X_scaled, scaler

if __name__ == "__main__":
    # Example usage
    print("Advanced Feature Engineering Module")
    print("Developer: Molla Samser")
    print("Website: https://rskworld.in")
    print("-" * 50)
    
    # Load sample data
    try:
        df = pd.read_csv('fraud_detection_dataset.csv')
        print(f"Loaded dataset with {df.shape[0]} records and {df.shape[1]} features")
        
        # Engineer features
        df_engineered = engineer_advanced_features(df)
        print(f"\nEngineered dataset has {df_engineered.shape[1]} features")
        print(f"\nNew features added:")
        new_features = [c for c in df_engineered.columns if c not in df.columns]
        print(f"Total new features: {len(new_features)}")
        for feat in new_features[:20]:  # Show first 20
            print(f"  - {feat}")
        if len(new_features) > 20:
            print(f"  ... and {len(new_features) - 20} more")
    except FileNotFoundError:
        print("Please generate the dataset first using generate_data.py")

223 lines•9.2 KB
python
🚀 Support RSK World

Subscribe to our YouTube channel for latest tutorials & updates!



Click subscribe & support our work ❤️

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer