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
fraud_detection_analysis.ipynb
fraud_detection_analysis.ipynb
Raw Download
Find: Go to:
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Fraud Detection Dataset - Data Analysis\n",
        "\n",
        "<!--\n",
        "    Developer: Molla Samser\n",
        "    Designer & Tester: Rima Khatun\n",
        "    Website: https://rskworld.in\n",
        "    Email: help@rskworld.in, support@rskworld.in, info@rskworld.com\n",
        "    Phone: +91 93305 39277\n",
        "    Company: RSK World\n",
        "    Description: Comprehensive analysis of fraud detection dataset\n",
        "-->\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Developer: Molla Samser\n",
        "# Designer & Tester: Rima Khatun\n",
        "# Website: https://rskworld.in\n",
        "# Email: help@rskworld.in, support@rskworld.in, info@rskworld.com\n",
        "# Phone: +91 93305 39277\n",
        "# Company: RSK World\n",
        "\n",
        "import pandas as pd\n",
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.ensemble import RandomForestClassifier\n",
        "from sklearn.preprocessing import LabelEncoder\n",
        "from sklearn.metrics import classification_report, confusion_matrix, roc_auc_score\n",
        "from imblearn.over_sampling import SMOTE\n",
        "import warnings\n",
        "warnings.filterwarnings('ignore')\n",
        "\n",
        "# Set style\n",
        "plt.style.use('seaborn-v0_8-darkgrid')\n",
        "sns.set_palette('husl')\n",
        "%matplotlib inline\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 1. Load Data\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Load dataset\n",
        "df = pd.read_csv('fraud_detection_dataset.csv')\n",
        "print(f\"Dataset shape: {df.shape}\")\n",
        "df.head()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 2. Data Exploration\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Basic information\n",
        "print(\"Dataset Info:\")\n",
        "df.info()\n",
        "print(\"\\n\" + \"=\"*50)\n",
        "print(\"Dataset Statistics:\")\n",
        "df.describe()\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Check for missing values\n",
        "print(\"Missing Values:\")\n",
        "print(df.isnull().sum())\n",
        "print(f\"\\nTotal missing values: {df.isnull().sum().sum()}\")\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Fraud distribution\n",
        "fraud_counts = df['is_fraud'].value_counts()\n",
        "print(\"Fraud Distribution:\")\n",
        "print(fraud_counts)\n",
        "print(f\"\\nFraud percentage: {df['is_fraud'].mean()*100:.2f}%\")\n",
        "\n",
        "# Visualize\n",
        "plt.figure(figsize=(8, 6))\n",
        "fraud_counts.plot(kind='bar', color=['green', 'red'])\n",
        "plt.title('Fraud vs Normal Transactions')\n",
        "plt.xlabel('Is Fraud (0=Normal, 1=Fraud)')\n",
        "plt.ylabel('Count')\n",
        "plt.xticks(rotation=0)\n",
        "plt.grid(axis='y', alpha=0.3)\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3. Feature Analysis\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Transaction amount analysis\n",
        "plt.figure(figsize=(14, 5))\n",
        "\n",
        "plt.subplot(1, 2, 1)\n",
        "df[df['is_fraud'] == 0]['amount'].hist(bins=50, alpha=0.7, label='Normal', color='green')\n",
        "df[df['is_fraud'] == 1]['amount'].hist(bins=50, alpha=0.7, label='Fraud', color='red')\n",
        "plt.xlabel('Transaction Amount')\n",
        "plt.ylabel('Frequency')\n",
        "plt.title('Transaction Amount Distribution')\n",
        "plt.legend()\n",
        "plt.grid(alpha=0.3)\n",
        "\n",
        "plt.subplot(1, 2, 2)\n",
        "df.boxplot(column='amount', by='is_fraud', ax=plt.gca())\n",
        "plt.xlabel('Is Fraud')\n",
        "plt.ylabel('Transaction Amount')\n",
        "plt.title('Transaction Amount by Fraud Status')\n",
        "plt.suptitle('')\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Categorical features analysis\n",
        "fig, axes = plt.subplots(2, 2, figsize=(16, 12))\n",
        "\n",
        "# Merchant category\n",
        "merchant_fraud = pd.crosstab(df['merchant_category'], df['is_fraud'], normalize='index') * 100\n",
        "merchant_fraud.plot(kind='bar', ax=axes[0, 0], color=['green', 'red'])\n",
        "axes[0, 0].set_title('Fraud Rate by Merchant Category')\n",
        "axes[0, 0].set_xlabel('Merchant Category')\n",
        "axes[0, 0].set_ylabel('Percentage')\n",
        "axes[0, 0].legend(['Normal', 'Fraud'])\n",
        "axes[0, 0].tick_params(axis='x', rotation=45)\n",
        "\n",
        "# Device type\n",
        "device_fraud = pd.crosstab(df['device_type'], df['is_fraud'], normalize='index') * 100\n",
        "device_fraud.plot(kind='bar', ax=axes[0, 1], color=['green', 'red'])\n",
        "axes[0, 1].set_title('Fraud Rate by Device Type')\n",
        "axes[0, 1].set_xlabel('Device Type')\n",
        "axes[0, 1].set_ylabel('Percentage')\n",
        "axes[0, 1].legend(['Normal', 'Fraud'])\n",
        "axes[0, 1].tick_params(axis='x', rotation=45)\n",
        "\n",
        "# Foreign transaction\n",
        "foreign_fraud = pd.crosstab(df['is_foreign_transaction'], df['is_fraud'], normalize='index') * 100\n",
        "foreign_fraud.plot(kind='bar', ax=axes[1, 0], color=['green', 'red'])\n",
        "axes[1, 0].set_title('Fraud Rate by Foreign Transaction')\n",
        "axes[1, 0].set_xlabel('Is Foreign Transaction')\n",
        "axes[1, 0].set_ylabel('Percentage')\n",
        "axes[1, 0].legend(['Normal', 'Fraud'])\n",
        "axes[1, 0].set_xticklabels(['No', 'Yes'], rotation=0)\n",
        "\n",
        "# Hour of day\n",
        "hour_fraud = pd.crosstab(df['hour_of_day'], df['is_fraud'], normalize='index') * 100\n",
        "hour_fraud[1].plot(kind='line', ax=axes[1, 1], marker='o', color='red')\n",
        "axes[1, 1].set_title('Fraud Rate by Hour of Day')\n",
        "axes[1, 1].set_xlabel('Hour of Day')\n",
        "axes[1, 1].set_ylabel('Fraud Percentage')\n",
        "axes[1, 1].grid(True, alpha=0.3)\n",
        "\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4. Correlation Analysis\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Select numeric columns for correlation\n",
        "numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()\n",
        "corr_matrix = df[numeric_cols].corr()\n",
        "\n",
        "# Plot correlation matrix\n",
        "plt.figure(figsize=(12, 10))\n",
        "sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', center=0, \n",
        "            square=True, linewidths=1, cbar_kws={\"shrink\": .8}, fmt='.2f')\n",
        "plt.title('Correlation Matrix')\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5. Model Building\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Preprocess data\n",
        "df_model = df.copy()\n",
        "\n",
        "# Encode categorical variables\n",
        "label_encoders = {}\n",
        "categorical_cols = ['merchant_category', 'location', 'device_type', 'user_id']\n",
        "\n",
        "for col in categorical_cols:\n",
        "    le = LabelEncoder()\n",
        "    df_model[col] = le.fit_transform(df_model[col].astype(str))\n",
        "    label_encoders[col] = le\n",
        "\n",
        "# Prepare features and target\n",
        "X = df_model.drop(['transaction_id', 'is_fraud'], axis=1, errors='ignore')\n",
        "y = df_model['is_fraud']\n",
        "\n",
        "print(f\"Features shape: {X.shape}\")\n",
        "print(f\"Target distribution:\\n{y.value_counts()}\")\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Handle imbalanced data with SMOTE\n",
        "X_balanced, y_balanced = SMOTE(random_state=42).fit_resample(X, y)\n",
        "\n",
        "print(f\"After SMOTE - Features shape: {X_balanced.shape}\")\n",
        "print(f\"After SMOTE - Target distribution:\\n{pd.Series(y_balanced).value_counts()}\")\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Train-test split\n",
        "X_train, X_test, y_train, y_test = train_test_split(\n",
        "    X_balanced, y_balanced, test_size=0.2, random_state=42, stratify=y_balanced\n",
        ")\n",
        "\n",
        "print(f\"Training set size: {X_train.shape[0]}\")\n",
        "print(f\"Test set size: {X_test.shape[0]}\")\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Train Random Forest model\n",
        "model = RandomForestClassifier(\n",
        "    n_estimators=100,\n",
        "    max_depth=20,\n",
        "    min_samples_split=5,\n",
        "    min_samples_leaf=2,\n",
        "    random_state=42,\n",
        "    n_jobs=-1,\n",
        "    class_weight='balanced'\n",
        ")\n",
        "\n",
        "model.fit(X_train, y_train)\n",
        "\n",
        "# Predictions\n",
        "y_pred = model.predict(X_test)\n",
        "y_pred_proba = model.predict_proba(X_test)[:, 1]\n",
        "\n",
        "print(\"Model trained successfully!\")\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Model evaluation\n",
        "print(\"Classification Report:\")\n",
        "print(classification_report(y_test, y_pred))\n",
        "\n",
        "print(\"\\nConfusion Matrix:\")\n",
        "cm = confusion_matrix(y_test, y_pred)\n",
        "print(cm)\n",
        "\n",
        "# Visualize confusion matrix\n",
        "plt.figure(figsize=(8, 6))\n",
        "sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', \n",
        "            xticklabels=['Normal', 'Fraud'],\n",
        "            yticklabels=['Normal', 'Fraud'])\n",
        "plt.title('Confusion Matrix')\n",
        "plt.ylabel('True Label')\n",
        "plt.xlabel('Predicted Label')\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Feature importance\n",
        "feature_importance = pd.DataFrame({\n",
        "    'feature': X.columns,\n",
        "    'importance': model.feature_importances_\n",
        "}).sort_values('importance', ascending=False)\n",
        "\n",
        "print(\"Top 15 Most Important Features:\")\n",
        "print(feature_importance.head(15))\n",
        "\n",
        "# Visualize\n",
        "plt.figure(figsize=(10, 8))\n",
        "top_features = feature_importance.head(15)\n",
        "sns.barplot(data=top_features, y='feature', x='importance')\n",
        "plt.title('Top 15 Feature Importances')\n",
        "plt.xlabel('Importance')\n",
        "plt.tight_layout()\n",
        "plt.show()\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6. Conclusion\n",
        "\n",
        "This analysis demonstrates:\n",
        "- Data exploration and visualization\n",
        "- Handling of imbalanced datasets\n",
        "- Model training and evaluation\n",
        "- Feature importance analysis\n",
        "\n",
        "For more information, visit: https://rskworld.in\n"
      ]
    }
  ],
  "metadata": {
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}
398 lines•12.8 KB
json

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