{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":88400,"databundleVersionId":10121331,"sourceType":"competition"}],"dockerImageVersionId":31259,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:39:02.237761Z","iopub.execute_input":"2026-01-25T18:39:02.238055Z","iopub.status.idle":"2026-01-25T18:39:02.243893Z","shell.execute_reply.started":"2026-01-25T18:39:02.238034Z","shell.execute_reply":"2026-01-25T18:39:02.243282Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ntrain = pd.read_csv(\n    \"/kaggle/input/aiffel-ds-3-credit-card-fraud-detection/train.csv\"\n)\n\ntest = pd.read_csv(\n    \"/kaggle/input/aiffel-ds-3-credit-card-fraud-detection/test.csv\"\n)\n\nsample_sub = pd.read_csv(\n    \"/kaggle/input/aiffel-ds-3-credit-card-fraud-detection/sample_submission.csv\"\n)\n\ntrain.head(), test.head(), sample_sub.head()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:39:02.250793Z","iopub.execute_input":"2026-01-25T18:39:02.251085Z","iopub.status.idle":"2026-01-25T18:39:03.531393Z","shell.execute_reply.started":"2026-01-25T18:39:02.251058Z","shell.execute_reply":"2026-01-25T18:39:03.530711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:39:03.532483Z","iopub.execute_input":"2026-01-25T18:39:03.532689Z","iopub.status.idle":"2026-01-25T18:39:03.545483Z","shell.execute_reply.started":"2026-01-25T18:39:03.532671Z","shell.execute_reply":"2026-01-25T18:39:03.544852Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:39:03.546424Z","iopub.execute_input":"2026-01-25T18:39:03.546645Z","iopub.status.idle":"2026-01-25T18:39:03.744835Z","shell.execute_reply.started":"2026-01-25T18:39:03.546622Z","shell.execute_reply":"2026-01-25T18:39:03.744156Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:39:03.746417Z","iopub.execute_input":"2026-01-25T18:39:03.746652Z","iopub.status.idle":"2026-01-25T18:39:03.762663Z","shell.execute_reply.started":"2026-01-25T18:39:03.746630Z","shell.execute_reply":"2026-01-25T18:39:03.761994Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:39:03.763597Z","iopub.execute_input":"2026-01-25T18:39:03.763832Z","iopub.status.idle":"2026-01-25T18:39:03.786580Z","shell.execute_reply.started":"2026-01-25T18:39:03.763814Z","shell.execute_reply":"2026-01-25T18:39:03.785989Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"Class\"].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:39:03.787282Z","iopub.execute_input":"2026-01-25T18:39:03.787469Z","iopub.status.idle":"2026-01-25T18:39:03.794450Z","shell.execute_reply.started":"2026-01-25T18:39:03.787444Z","shell.execute_reply":"2026-01-25T18:39:03.793912Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train[\"Amount\"].min()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:39:03.795458Z","iopub.execute_input":"2026-01-25T18:39:03.795695Z","iopub.status.idle":"2026-01-25T18:39:03.807686Z","shell.execute_reply.started":"2026-01-25T18:39:03.795677Z","shell.execute_reply":"2026-01-25T18:39:03.807243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x= train.drop(['Class', 'id'], axis=1)\ny = train['Class']\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:39:03.808421Z","iopub.execute_input":"2026-01-25T18:39:03.808638Z","iopub.status.idle":"2026-01-25T18:39:03.829393Z","shell.execute_reply.started":"2026-01-25T18:39:03.808616Z","shell.execute_reply":"2026-01-25T18:39:03.828650Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x[\"Amount\"].plot()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:39:03.830228Z","iopub.execute_input":"2026-01-25T18:39:03.830433Z","iopub.status.idle":"2026-01-25T18:39:03.999787Z","shell.execute_reply.started":"2026-01-25T18:39:03.830415Z","shell.execute_reply":"2026-01-25T18:39:03.999229Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x[\"Time\"].plot()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:39:04.001552Z","iopub.execute_input":"2026-01-25T18:39:04.001744Z","iopub.status.idle":"2026-01-25T18:39:04.130975Z","shell.execute_reply.started":"2026-01-25T18:39:04.001727Z","shell.execute_reply":"2026-01-25T18:39:04.130133Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train, x_val, y_train, y_val = train_test_split(\n    x, y,\n    test_size=0.2,\n    stratify=y,\n    random_state=42\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:39:04.131965Z","iopub.execute_input":"2026-01-25T18:39:04.132200Z","iopub.status.idle":"2026-01-25T18:39:04.204828Z","shell.execute_reply.started":"2026-01-25T18:39:04.132181Z","shell.execute_reply":"2026-01-25T18:39:04.204226Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"neg, pos = y_train.value_counts()\nscale_pos_weight = neg / pos\nscale_pos_weight\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:39:04.205633Z","iopub.execute_input":"2026-01-25T18:39:04.205807Z","iopub.status.idle":"2026-01-25T18:39:04.211274Z","shell.execute_reply.started":"2026-01-25T18:39:04.205791Z","shell.execute_reply":"2026-01-25T18:39:04.210393Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from lightgbm import LGBMClassifier\nlgbm = LGBMClassifier(\n    n_estimators=650,\n    learning_rate=0.03,\n    num_leaves=55,\n    max_depth=8,\n    min_child_samples=20,\n    subsample=0.8,\n    colsample_bytree=0.8,\n    scale_pos_weight=scale_pos_weight,\n    objective='binary',\n    random_state=46,\n    n_jobs=-1\n)\n\n\nlgbm.fit(x_train, y_train)\n\n\ny_prob = lgbm.predict_proba(x_val)[:,1]\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:57:00.933257Z","iopub.execute_input":"2026-01-25T18:57:00.933531Z","iopub.status.idle":"2026-01-25T18:57:07.912767Z","shell.execute_reply.started":"2026-01-25T18:57:00.933509Z","shell.execute_reply":"2026-01-25T18:57:07.912315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import classification_report, roc_auc_score\n\nlgbm.fit(\n    x_train, y_train,\n    eval_set=[(x_val, y_val)],\n    eval_metric='auc'\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:57:07.914544Z","iopub.execute_input":"2026-01-25T18:57:07.914966Z","iopub.status.idle":"2026-01-25T18:57:15.695607Z","shell.execute_reply.started":"2026-01-25T18:57:07.914935Z","shell.execute_reply":"2026-01-25T18:57:15.695000Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lgbm.fit(x_train, y_train)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:57:15.696382Z","iopub.execute_input":"2026-01-25T18:57:15.696567Z","iopub.status.idle":"2026-01-25T18:57:21.766505Z","shell.execute_reply.started":"2026-01-25T18:57:15.696543Z","shell.execute_reply":"2026-01-25T18:57:21.765815Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_prob = lgbm.predict_proba(x_val)[:, 1]\n\n# Threshold 0.5 أو أي قيمة منخفضة لتحسين Recall\nthreshold = 0.15\ny_pred = (y_prob > threshold).astype(int)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:57:21.767956Z","iopub.execute_input":"2026-01-25T18:57:21.768154Z","iopub.status.idle":"2026-01-25T18:57:22.355153Z","shell.execute_reply.started":"2026-01-25T18:57:21.768135Z","shell.execute_reply":"2026-01-25T18:57:22.354649Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import classification_report, roc_auc_score\n\nprint(\"ROC AUC:\", roc_auc_score(y_val, y_prob))\nprint(classification_report(y_val, y_pred))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:57:22.355902Z","iopub.execute_input":"2026-01-25T18:57:22.356314Z","iopub.status.idle":"2026-01-25T18:57:22.377847Z","shell.execute_reply.started":"2026-01-25T18:57:22.356293Z","shell.execute_reply":"2026-01-25T18:57:22.377046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_x = test.drop('id', axis=1)\ntest_prob = lgbm.predict_proba(test_x)[:, 1]\ntest_pred = (test_prob > threshold).astype(int)\n\nsubmission = sample_sub.copy()\nsubmission['Class'] = test_pred\nsubmission.to_csv(\"lgbm_submission.csv\", index=False)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T18:57:22.379048Z","iopub.execute_input":"2026-01-25T18:57:22.379800Z","iopub.status.idle":"2026-01-25T18:57:24.291670Z","shell.execute_reply.started":"2026-01-25T18:57:22.379772Z","shell.execute_reply":"2026-01-25T18:57:24.290866Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import xgboost as xgb\nxgb_model = xgb.XGBClassifier(\n    n_estimators=600,\n    learning_rate=0.03,\n    max_depth=9,\n    min_child_weight=1,\n    subsample=0.8,\n    colsample_bytree=0.8,\n    scale_pos_weight=scale_pos_weight,\n    objective='binary:logistic',\n    eval_metric='auc',\n    use_label_encoder=False,\n    random_state=42,\n    n_jobs=-1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T19:07:32.004433Z","iopub.execute_input":"2026-01-25T19:07:32.004718Z","iopub.status.idle":"2026-01-25T19:07:32.009588Z","shell.execute_reply.started":"2026-01-25T19:07:32.004694Z","shell.execute_reply":"2026-01-25T19:07:32.008834Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb_model.fit(\n    x_train, y_train,\n    eval_set=[(x_val, y_val)]\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T19:07:32.804995Z","iopub.execute_input":"2026-01-25T19:07:32.805254Z","iopub.status.idle":"2026-01-25T19:07:42.041973Z","shell.execute_reply.started":"2026-01-25T19:07:32.805232Z","shell.execute_reply":"2026-01-25T19:07:42.041474Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_prob = xgb_model.predict_proba(x_val)[:, 1]\n\nthreshold = 0.2\ny_pred = (y_prob > threshold).astype(int)\n\nprint(\"ROC AUC:\", roc_auc_score(y_val, y_prob))\nprint(classification_report(y_val, y_pred))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-25T19:08:00.893632Z","iopub.execute_input":"2026-01-25T19:08:00.893902Z","iopub.status.idle":"2026-01-25T19:08:01.079859Z","shell.execute_reply.started":"2026-01-25T19:08:00.893878Z","shell.execute_reply":"2026-01-25T19:08:01.079279Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}