{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":9120,"databundleVersionId":860599,"sourceType":"competition"},{"sourceId":50160,"databundleVersionId":7921029,"sourceType":"competition"},{"sourceId":3244803,"sourceType":"datasetVersion","datasetId":1966610},{"sourceId":12699,"sourceType":"datasetVersion","datasetId":9109},{"sourceId":16695845,"sourceType":"kernelVersion"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:08.828882Z","iopub.execute_input":"2024-04-27T07:52:08.829172Z","iopub.status.idle":"2024-04-27T07:52:09.919464Z","shell.execute_reply.started":"2024-04-27T07:52:08.829147Z","shell.execute_reply":"2024-04-27T07:52:09.918515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data =pd.read_csv('/kaggle/input/creditcardfraud/creditcard.csv')","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:18.744971Z","iopub.execute_input":"2024-04-27T07:52:18.745444Z","iopub.status.idle":"2024-04-27T07:52:22.031629Z","shell.execute_reply.started":"2024-04-27T07:52:18.745416Z","shell.execute_reply":"2024-04-27T07:52:22.030732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:23.244954Z","iopub.execute_input":"2024-04-27T07:52:23.245290Z","iopub.status.idle":"2024-04-27T07:52:23.285143Z","shell.execute_reply.started":"2024-04-27T07:52:23.245267Z","shell.execute_reply":"2024-04-27T07:52:23.284287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.options.display.max_columns = None","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:24.221548Z","iopub.execute_input":"2024-04-27T07:52:24.222111Z","iopub.status.idle":"2024-04-27T07:52:24.226103Z","shell.execute_reply.started":"2024-04-27T07:52:24.222080Z","shell.execute_reply":"2024-04-27T07:52:24.225180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:25.284701Z","iopub.execute_input":"2024-04-27T07:52:25.285048Z","iopub.status.idle":"2024-04-27T07:52:25.313558Z","shell.execute_reply.started":"2024-04-27T07:52:25.285021Z","shell.execute_reply":"2024-04-27T07:52:25.312565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.tail()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:26.159074Z","iopub.execute_input":"2024-04-27T07:52:26.159454Z","iopub.status.idle":"2024-04-27T07:52:26.189807Z","shell.execute_reply.started":"2024-04-27T07:52:26.159426Z","shell.execute_reply":"2024-04-27T07:52:26.189071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:26.959506Z","iopub.execute_input":"2024-04-27T07:52:26.959826Z","iopub.status.idle":"2024-04-27T07:52:26.966284Z","shell.execute_reply.started":"2024-04-27T07:52:26.959784Z","shell.execute_reply":"2024-04-27T07:52:26.965320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number of columns: {}\".format(data.shape[1]))\nprint(\"Number of rows: {}\".format(data.shape[0]))","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:27.699935Z","iopub.execute_input":"2024-04-27T07:52:27.700568Z","iopub.status.idle":"2024-04-27T07:52:27.705581Z","shell.execute_reply.started":"2024-04-27T07:52:27.700537Z","shell.execute_reply":"2024-04-27T07:52:27.704642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:28.445780Z","iopub.execute_input":"2024-04-27T07:52:28.446110Z","iopub.status.idle":"2024-04-27T07:52:28.486601Z","shell.execute_reply.started":"2024-04-27T07:52:28.446084Z","shell.execute_reply":"2024-04-27T07:52:28.485681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:29.144545Z","iopub.execute_input":"2024-04-27T07:52:29.145184Z","iopub.status.idle":"2024-04-27T07:52:29.165666Z","shell.execute_reply.started":"2024-04-27T07:52:29.145154Z","shell.execute_reply":"2024-04-27T07:52:29.164868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:29.844427Z","iopub.execute_input":"2024-04-27T07:52:29.845263Z","iopub.status.idle":"2024-04-27T07:52:30.290722Z","shell.execute_reply.started":"2024-04-27T07:52:29.845233Z","shell.execute_reply":"2024-04-27T07:52:30.289731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc = StandardScaler()\ndata['Amount'] = sc.fit_transform(pd.DataFrame(data['Amount']))","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:31.745909Z","iopub.execute_input":"2024-04-27T07:52:31.746564Z","iopub.status.idle":"2024-04-27T07:52:31.758204Z","shell.execute_reply.started":"2024-04-27T07:52:31.746531Z","shell.execute_reply":"2024-04-27T07:52:31.756991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:32.644964Z","iopub.execute_input":"2024-04-27T07:52:32.645886Z","iopub.status.idle":"2024-04-27T07:52:32.677328Z","shell.execute_reply.started":"2024-04-27T07:52:32.645847Z","shell.execute_reply":"2024-04-27T07:52:32.676399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.drop(['Time'], axis =1)","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:33.409800Z","iopub.execute_input":"2024-04-27T07:52:33.410132Z","iopub.status.idle":"2024-04-27T07:52:33.439212Z","shell.execute_reply.started":"2024-04-27T07:52:33.410106Z","shell.execute_reply":"2024-04-27T07:52:33.438143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:34.220802Z","iopub.execute_input":"2024-04-27T07:52:34.221710Z","iopub.status.idle":"2024-04-27T07:52:34.254262Z","shell.execute_reply.started":"2024-04-27T07:52:34.221678Z","shell.execute_reply":"2024-04-27T07:52:34.253285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.duplicated().any()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:34.871116Z","iopub.execute_input":"2024-04-27T07:52:34.871458Z","iopub.status.idle":"2024-04-27T07:52:35.457433Z","shell.execute_reply.started":"2024-04-27T07:52:34.871431Z","shell.execute_reply":"2024-04-27T07:52:35.456426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data.drop_duplicates()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:35.574331Z","iopub.execute_input":"2024-04-27T07:52:35.574630Z","iopub.status.idle":"2024-04-27T07:52:36.199875Z","shell.execute_reply.started":"2024-04-27T07:52:35.574605Z","shell.execute_reply":"2024-04-27T07:52:36.199055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:36.306595Z","iopub.execute_input":"2024-04-27T07:52:36.306918Z","iopub.status.idle":"2024-04-27T07:52:36.312918Z","shell.execute_reply.started":"2024-04-27T07:52:36.306886Z","shell.execute_reply":"2024-04-27T07:52:36.312018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['Class'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:37.318326Z","iopub.execute_input":"2024-04-27T07:52:37.319047Z","iopub.status.idle":"2024-04-27T07:52:37.330456Z","shell.execute_reply.started":"2024-04-27T07:52:37.318996Z","shell.execute_reply":"2024-04-27T07:52:37.329567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nplt.style.use('ggplot')","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:38.168667Z","iopub.execute_input":"2024-04-27T07:52:38.168982Z","iopub.status.idle":"2024-04-27T07:52:38.404172Z","shell.execute_reply.started":"2024-04-27T07:52:38.168957Z","shell.execute_reply":"2024-04-27T07:52:38.403390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data['Class'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:38.950840Z","iopub.execute_input":"2024-04-27T07:52:38.951710Z","iopub.status.idle":"2024-04-27T07:52:39.178977Z","shell.execute_reply.started":"2024-04-27T07:52:38.951676Z","shell.execute_reply":"2024-04-27T07:52:39.177902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data.drop('Class', axis = 1)\ny=data['Class']","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:39.611933Z","iopub.execute_input":"2024-04-27T07:52:39.612284Z","iopub.status.idle":"2024-04-27T07:52:39.638501Z","shell.execute_reply.started":"2024-04-27T07:52:39.612258Z","shell.execute_reply":"2024-04-27T07:52:39.637406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:40.489125Z","iopub.execute_input":"2024-04-27T07:52:40.489476Z","iopub.status.idle":"2024-04-27T07:52:40.596498Z","shell.execute_reply.started":"2024-04-27T07:52:40.489443Z","shell.execute_reply":"2024-04-27T07:52:40.595575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:41.244665Z","iopub.execute_input":"2024-04-27T07:52:41.245329Z","iopub.status.idle":"2024-04-27T07:52:41.329621Z","shell.execute_reply.started":"2024-04-27T07:52:41.245294Z","shell.execute_reply":"2024-04-27T07:52:41.328579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:41.977604Z","iopub.execute_input":"2024-04-27T07:52:41.978333Z","iopub.status.idle":"2024-04-27T07:52:41.982446Z","shell.execute_reply.started":"2024-04-27T07:52:41.978299Z","shell.execute_reply":"2024-04-27T07:52:41.981324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, classification_report\nfrom sklearn.linear_model import LogisticRegression, SGDClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier, BaggingClassifier, ExtraTreesClassifier, VotingClassifier\nfrom sklearn.svm import SVC\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.naive_bayes import GaussianNB\n","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:42.730222Z","iopub.execute_input":"2024-04-27T07:52:42.730582Z","iopub.status.idle":"2024-04-27T07:52:42.984139Z","shell.execute_reply.started":"2024-04-27T07:52:42.730556Z","shell.execute_reply":"2024-04-27T07:52:42.983033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, classification_report\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.svm import SVC\nfrom sklearn.ensemble import AdaBoostClassifier\nfrom sklearn.ensemble import GradientBoostingClassifier\nfrom sklearn.ensemble import BaggingClassifier\nfrom sklearn.ensemble import ExtraTreesClassifier\nfrom sklearn.linear_model import SGDClassifier\nfrom sklearn.ensemble import VotingClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.linear_model import LogisticRegression\n\nclassifier = {\n    \"Logistic Regression\": LogisticRegression(),\n    \"Decision Tree Classifier\": DecisionTreeClassifier(),\n    \"Random Forest Classifier\": RandomForestClassifier(),\n    \"Support Vector Classifier\": SVC(),\n    \"K-Nearest Neighbors Classifier\": KNeighborsClassifier(),\n    \"Gaussian Naive Bayes\": GaussianNB(),\n    \"AdaBoost Classifier\": AdaBoostClassifier(),\n    \"Gradient Boosting Classifier\": GradientBoostingClassifier(),\n    \"Bagging Classifier\": BaggingClassifier(),\n    \"Extra Trees Classifier\": ExtraTreesClassifier(),\n    \"Stochastic Gradient Descent Classifier\": SGDClassifier(),\n    \"Voting Classifier\": VotingClassifier(estimators=[\n        ('lr', LogisticRegression()),\n        ('dt', DecisionTreeClassifier()),\n        ('rf', RandomForestClassifier()),\n        ('svc', SVC()),\n        ('knn', KNeighborsClassifier())\n    ], voting='hard')\n}\n\nfor name, clf in classifier.items():\n    print(f\"\\n=========={name}===========\")\n    clf.fit(X_train, y_train)\n    y_pred = clf.predict(X_test)\n    \n    # Evaluation Metrics\n    accuracy = accuracy_score(y_test, y_pred)\n    precision = precision_score(y_test, y_pred)\n    recall = recall_score(y_test, y_pred)\n    f1 = f1_score(y_test, y_pred)\n    print(f\"\\n Accuracy: {accuracy}\")\n    print(f\" Precision: {precision}\")\n    print(f\" Recall: {recall}\")\n    print(f\" F1 Score: {f1}\")\n    \n    # Confusion Matrix\n    print(\"\\n Confusion Matrix:\")\n    print(confusion_matrix(y_test, y_pred))\n    \n    # Classification Report\n    print(\"\\n Classification Report:\")\n    print(classification_report(y_test, y_pred))\n","metadata":{"execution":{"iopub.status.busy":"2024-04-27T07:52:43.909147Z","iopub.execute_input":"2024-04-27T07:52:43.909809Z","iopub.status.idle":"2024-04-27T08:14:28.940648Z","shell.execute_reply.started":"2024-04-27T07:52:43.909771Z","shell.execute_reply":"2024-04-27T08:14:28.939647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Undersampling","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normal = data[data['Class']==0]\nfraud = data[data['Class']==1]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normal.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fraud.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normal_sample = normal.sample(n=473)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normal_sample.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_data = pd.concat([normal_sample,fraud], ignore_index=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_data.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_data['Class'].value_counts()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = new_data.drop('Class', axis = 1)\ny= new_data['Class']","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier = {\n    \"Logistic Regression\": LogisticRegression(),\n    \"Decision Tree Classifier\": DecisionTreeClassifier()\n}\n\nfor name, clf in classifier.items():\n    print(f\"\\n=========={name}===========\")\n    clf.fit(X_train, y_train)\n    y_pred = clf.predict(X_test)\n    print(f\"\\n Accuaracy: {accuracy_score(y_test, y_pred)}\")\n    print(f\"\\n Precision: {precision_score(y_test, y_pred)}\")\n    print(f\"\\n Recall: {recall_score(y_test, y_pred)}\")\n    print(f\"\\n F1 Score: {f1_score(y_test, y_pred)}\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# OVERSAMPLING","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data.drop('Class', axis = 1)\ny= data['Class']","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from imblearn.over_sampling import SMOTE","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_res, y_res = SMOTE().fit_resample(X,y)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_res.value_counts()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X_res, y_res, test_size = 0.2, random_state = 42)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier = {\n    \"Logistic Regression\": LogisticRegression(),\n    \"Decision Tree Classifier\": DecisionTreeClassifier()\n}\n\nfor name, clf in classifier.items():\n    print(f\"\\n=========={name}===========\")\n    clf.fit(X_train, y_train)\n    y_pred = clf.predict(X_test)\n    print(f\"\\n Accuaracy: {accuracy_score(y_test, y_pred)}\")\n    print(f\"\\n Precision: {precision_score(y_test, y_pred)}\")\n    print(f\"\\n Recall: {recall_score(y_test, y_pred)}\")\n    print(f\"\\n F1 Score: {f1_score(y_test, y_pred)}\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtc = DecisionTreeClassifier()\ndtc.fit(X_res, y_res)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import joblib","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"joblib.dump(dtc, \"credit_card_model.pkl\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = joblib.load(\"credit_card_model.pkl\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict([[-1.3598071336738,-0.0727811733098497,2.53634673796914,1.37815522427443,-0.338320769942518,0.462387777762292,0.239598554061257,0.0986979012610507,0.363786969611213,0.0907941719789316,-0.551599533260813,-0.617800855762348,-0.991389847235408,-0.311169353699879,1.46817697209427,-0.470400525259478,0.207971241929242,0.0257905801985591,0.403992960255733,0.251412098239705,-0.018306777944153,0.277837575558899,-0.110473910188767,0.0669280749146731,0.128539358273528,-0.189114843888824,0.133558376740387,-0.0210530534538215,149.62]])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred[0]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if pred[0] == 0:\n    print(\"Normal Transcation\")\nelse:\n    print(\"Fraud Transcation\")","metadata":{},"execution_count":null,"outputs":[]}]}