{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "46bcfe02",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Step1: Load the python packages for data analysis and data visualization\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "05cb96f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#step2: load the dataset from your folder\n",
    "\n",
    "\n",
    "dataset=pd.read_csv('Cybercrime.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "58f6a718",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Card</th>\n",
       "      <th>Year</th>\n",
       "      <th>Month</th>\n",
       "      <th>Day</th>\n",
       "      <th>Amount</th>\n",
       "      <th>Transaction</th>\n",
       "      <th>MCC</th>\n",
       "      <th>Is Fraud?</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>2002</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>134.09</td>\n",
       "      <td>2</td>\n",
       "      <td>5300</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>2002</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>38.48</td>\n",
       "      <td>2</td>\n",
       "      <td>5411</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>2002</td>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>120.34</td>\n",
       "      <td>2</td>\n",
       "      <td>5411</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>2002</td>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>128.95</td>\n",
       "      <td>2</td>\n",
       "      <td>5651</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>2002</td>\n",
       "      <td>9</td>\n",
       "      <td>3</td>\n",
       "      <td>104.71</td>\n",
       "      <td>2</td>\n",
       "      <td>5912</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19958</th>\n",
       "      <td>4</td>\n",
       "      <td>2009</td>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "      <td>9.71</td>\n",
       "      <td>2</td>\n",
       "      <td>5411</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19959</th>\n",
       "      <td>4</td>\n",
       "      <td>2009</td>\n",
       "      <td>2</td>\n",
       "      <td>22</td>\n",
       "      <td>27.94</td>\n",
       "      <td>2</td>\n",
       "      <td>5211</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19960</th>\n",
       "      <td>4</td>\n",
       "      <td>2009</td>\n",
       "      <td>2</td>\n",
       "      <td>24</td>\n",
       "      <td>13.97</td>\n",
       "      <td>2</td>\n",
       "      <td>5912</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19961</th>\n",
       "      <td>4</td>\n",
       "      <td>2009</td>\n",
       "      <td>3</td>\n",
       "      <td>29</td>\n",
       "      <td>14.27</td>\n",
       "      <td>2</td>\n",
       "      <td>5411</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19962</th>\n",
       "      <td>4</td>\n",
       "      <td>2009</td>\n",
       "      <td>3</td>\n",
       "      <td>31</td>\n",
       "      <td>13.55</td>\n",
       "      <td>2</td>\n",
       "      <td>5812</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>19963 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       Card  Year  Month  Day  Amount  Transaction   MCC Is Fraud?\n",
       "0         0  2002      9    1  134.09            2  5300        No\n",
       "1         0  2002      9    1   38.48            2  5411        No\n",
       "2         0  2002      9    2  120.34            2  5411        No\n",
       "3         0  2002      9    2  128.95            2  5651        No\n",
       "4         0  2002      9    3  104.71            2  5912        No\n",
       "...     ...   ...    ...  ...     ...          ...   ...       ...\n",
       "19958     4  2009      2   12    9.71            2  5411        No\n",
       "19959     4  2009      2   22   27.94            2  5211        No\n",
       "19960     4  2009      2   24   13.97            2  5912        No\n",
       "19961     4  2009      3   29   14.27            2  5411        No\n",
       "19962     4  2009      3   31   13.55            2  5812        No\n",
       "\n",
       "[19963 rows x 8 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c1fb4c57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Card</th>\n",
       "      <th>Year</th>\n",
       "      <th>Month</th>\n",
       "      <th>Day</th>\n",
       "      <th>Amount</th>\n",
       "      <th>Transaction</th>\n",
       "      <th>MCC</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>19963.000000</td>\n",
       "      <td>19963.000000</td>\n",
       "      <td>19963.000000</td>\n",
       "      <td>19963.000000</td>\n",
       "      <td>19963.000000</td>\n",
       "      <td>19963.000000</td>\n",
       "      <td>19963.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.910735</td>\n",
       "      <td>2011.011922</td>\n",
       "      <td>6.568101</td>\n",
       "      <td>15.743876</td>\n",
       "      <td>81.299989</td>\n",
       "      <td>1.652808</td>\n",
       "      <td>5617.940239</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>1.237763</td>\n",
       "      <td>5.048146</td>\n",
       "      <td>3.477497</td>\n",
       "      <td>8.801378</td>\n",
       "      <td>94.159093</td>\n",
       "      <td>0.712738</td>\n",
       "      <td>707.982901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>2002.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-499.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1711.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>2007.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>36.630000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>5311.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>2011.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>16.000000</td>\n",
       "      <td>69.450000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>5499.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>3.000000</td>\n",
       "      <td>2015.000000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>23.000000</td>\n",
       "      <td>125.680000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>5912.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>4.000000</td>\n",
       "      <td>2020.000000</td>\n",
       "      <td>12.000000</td>\n",
       "      <td>31.000000</td>\n",
       "      <td>1409.400000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>9402.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               Card          Year         Month           Day        Amount  \\\n",
       "count  19963.000000  19963.000000  19963.000000  19963.000000  19963.000000   \n",
       "mean       1.910735   2011.011922      6.568101     15.743876     81.299989   \n",
       "std        1.237763      5.048146      3.477497      8.801378     94.159093   \n",
       "min        0.000000   2002.000000      1.000000      1.000000   -499.000000   \n",
       "25%        0.000000   2007.000000      4.000000      8.000000     36.630000   \n",
       "50%        2.000000   2011.000000      7.000000     16.000000     69.450000   \n",
       "75%        3.000000   2015.000000     10.000000     23.000000    125.680000   \n",
       "max        4.000000   2020.000000     12.000000     31.000000   1409.400000   \n",
       "\n",
       "        Transaction           MCC  \n",
       "count  19963.000000  19963.000000  \n",
       "mean       1.652808   5617.940239  \n",
       "std        0.712738    707.982901  \n",
       "min        0.000000   1711.000000  \n",
       "25%        2.000000   5311.000000  \n",
       "50%        2.000000   5499.000000  \n",
       "75%        2.000000   5912.000000  \n",
       "max        2.000000   9402.000000  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#step4: ststistical analysis using describe()\n",
    "\n",
    "#a) mean of the dataset values\n",
    "\n",
    "#b) standard deviation of the dataset values\n",
    "\n",
    "#c) Minimum and maximum values and the data in 25%,50% and 75%\n",
    "\n",
    "dataset.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5cd6ef0a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Card           0\n",
       "Year           0\n",
       "Month          0\n",
       "Day            0\n",
       "Amount         0\n",
       "Transaction    0\n",
       "MCC            0\n",
       "Is Fraud?      0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#step4: Checking missing values.\n",
    "\n",
    "dataset.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1c4951c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Online Transaction=1 have no Merchant state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "75d8e0d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#step7: Divide the data into dependent and independent variables\n",
    "\n",
    "#a)x=independent variable\n",
    "\n",
    "#b)y=dependent variabel\n",
    "\n",
    "X = dataset.iloc[:, :-1].values\n",
    "y = dataset.iloc[:, -1].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0b33b4b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#step9: Divide the data into training and testing using train test split\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ac3dae74",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "sc = StandardScaler()\n",
    "X_train = sc.fit_transform(X_train)\n",
    "X_test = sc.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6dd5ed06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RandomForestClassifier(criterion='entropy', n_estimators=10, random_state=0)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#step10: RandomForest\n",
    "\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "classifier = RandomForestClassifier(n_estimators = 10, random_state = 0, criterion='entropy')\n",
    "classifier.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4d45c4a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['No' 'No']\n",
      " ['No' 'No']\n",
      " ['No' 'No']\n",
      " ...\n",
      " ['No' 'No']\n",
      " ['No' 'No']\n",
      " ['No' 'No']]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#step11: predict the test dataset value\n",
    "y_pred = classifier.predict(X_test)\n",
    "print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a767e930",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3987    0]\n",
      " [   4    2]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.9989982469321312"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "#step12: create confusion matrix and Accuracy\n",
    "\n",
    "from sklearn.metrics import confusion_matrix, accuracy_score\n",
    "cm = confusion_matrix(y_test, y_pred)\n",
    "print(cm)\n",
    "accuracy_score(y_test, y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "78268fc7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['No']\n"
     ]
    }
   ],
   "source": [
    "print(classifier.predict(sc.transform([[0,2002,9,1,135,2,5300]])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e32197d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "pickle.dump(classifier,open(\"cybercrime.sav\", \"wb\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
