{"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":"gpu","dataSources":[{"sourceType":"competition","sourceId":50160,"databundleVersionId":7921029}],"dockerImageVersionId":31329,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\n\npath = '/kaggle/input/competitions/home-credit-credit-risk-model-stability/csv_files'\n\nprint(os.listdir(path))","metadata":{"_uuid":"051d70d956493feee0c6d64651c6a088724dca2a","_execution_state":"idle","trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:55:57.931021Z","iopub.execute_input":"2026-04-21T19:55:57.931325Z","iopub.status.idle":"2026-04-21T19:55:57.942832Z","shell.execute_reply.started":"2026-04-21T19:55:57.931299Z","shell.execute_reply":"2026-04-21T19:55:57.941756Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:55:57.944560Z","iopub.execute_input":"2026-04-21T19:55:57.944912Z","iopub.status.idle":"2026-04-21T19:55:58.198795Z","shell.execute_reply.started":"2026-04-21T19:55:57.944872Z","shell.execute_reply":"2026-04-21T19:55:58.197796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ntrain = pd.read_csv('/kaggle/input/competitions/home-credit-credit-risk-model-stability/csv_files/train/train_base.csv')\n\ntrain.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:55:58.200107Z","iopub.execute_input":"2026-04-21T19:55:58.200344Z","iopub.status.idle":"2026-04-21T19:55:59.029058Z","shell.execute_reply.started":"2026-04-21T19:55:58.200319Z","shell.execute_reply":"2026-04-21T19:55:59.028404Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape\ntrain.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:55:59.030817Z","iopub.execute_input":"2026-04-21T19:55:59.031061Z","iopub.status.idle":"2026-04-21T19:55:59.037018Z","shell.execute_reply.started":"2026-04-21T19:55:59.031038Z","shell.execute_reply":"2026-04-21T19:55:59.036357Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['target'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:55:59.038223Z","iopub.execute_input":"2026-04-21T19:55:59.039157Z","iopub.status.idle":"2026-04-21T19:55:59.063830Z","shell.execute_reply.started":"2026-04-21T19:55:59.039128Z","shell.execute_reply":"2026-04-21T19:55:59.063138Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.isnull().sum().sort_values(ascending=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:55:59.064931Z","iopub.execute_input":"2026-04-21T19:55:59.065262Z","iopub.status.idle":"2026-04-21T19:55:59.159397Z","shell.execute_reply.started":"2026-04-21T19:55:59.065236Z","shell.execute_reply":"2026-04-21T19:55:59.158462Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape\ntrain.info()\ntrain.describe()\ntrain['target'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:55:59.160829Z","iopub.execute_input":"2026-04-21T19:55:59.161685Z","iopub.status.idle":"2026-04-21T19:55:59.389011Z","shell.execute_reply.started":"2026-04-21T19:55:59.161640Z","shell.execute_reply":"2026-04-21T19:55:59.388350Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# حذف الأعمدة اللي فيها missing كتير\ntrain = train.loc[:, train.isnull().mean() < 0.5]\n\n# تعويض القيم\nfor col in train.columns:\n    if train[col].dtype == 'object':\n        train[col] = train[col].fillna(train[col].mode()[0])\n    else:\n        train[col] = train[col].fillna(train[col].mean())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:55:59.389938Z","iopub.execute_input":"2026-04-21T19:55:59.390271Z","iopub.status.idle":"2026-04-21T19:55:59.762388Z","shell.execute_reply.started":"2026-04-21T19:55:59.390235Z","shell.execute_reply":"2026-04-21T19:55:59.761392Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# مثال feature جديد\nif 'AMT_INCOME_TOTAL' in train.columns and 'AMT_CREDIT' in train.columns:\n    train['income_credit_ratio'] = train['AMT_INCOME_TOTAL'] / (train['AMT_CREDIT'] + 1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:55:59.763373Z","iopub.execute_input":"2026-04-21T19:55:59.763713Z","iopub.status.idle":"2026-04-21T19:55:59.767991Z","shell.execute_reply.started":"2026-04-21T19:55:59.763674Z","shell.execute_reply":"2026-04-21T19:55:59.767255Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.get_dummies(train, drop_first=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:55:59.770109Z","iopub.execute_input":"2026-04-21T19:55:59.770447Z","iopub.status.idle":"2026-04-21T19:56:00.667288Z","shell.execute_reply.started":"2026-04-21T19:55:59.770424Z","shell.execute_reply":"2026-04-21T19:56:00.666510Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX = train.drop('target', axis=1)\ny = train['target']\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:56:00.668330Z","iopub.execute_input":"2026-04-21T19:56:00.668711Z","iopub.status.idle":"2026-04-21T19:56:04.164913Z","shell.execute_reply.started":"2026-04-21T19:56:00.668665Z","shell.execute_reply":"2026-04-21T19:56:04.163794Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\nscaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:56:04.166228Z","iopub.execute_input":"2026-04-21T19:56:04.166886Z","iopub.status.idle":"2026-04-21T19:56:41.948189Z","shell.execute_reply.started":"2026-04-21T19:56:04.166844Z","shell.execute_reply":"2026-04-21T19:56:41.947216Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Input\nfrom tensorflow.keras.regularizers import l2\n\nmodel = Sequential()\n\nmodel.add(Input(shape=(X_train.shape[1],)))\n\nmodel.add(Dense(128, activation='relu', kernel_regularizer=l2(0.001)))\nmodel.add(Dropout(0.3))\n\nmodel.add(Dense(64, activation='relu', kernel_regularizer=l2(0.001)))\nmodel.add(Dropout(0.3))\n\nmodel.add(Dense(1, activation='sigmoid'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:56:41.949246Z","iopub.execute_input":"2026-04-21T19:56:41.949856Z","iopub.status.idle":"2026-04-21T19:56:44.434189Z","shell.execute_reply.started":"2026-04-21T19:56:41.949829Z","shell.execute_reply":"2026-04-21T19:56:44.433553Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:56:44.435120Z","iopub.execute_input":"2026-04-21T19:56:44.435431Z","iopub.status.idle":"2026-04-21T19:56:44.449327Z","shell.execute_reply.started":"2026-04-21T19:56:44.435407Z","shell.execute_reply":"2026-04-21T19:56:44.448454Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='sgd', loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:56:44.450218Z","iopub.execute_input":"2026-04-21T19:56:44.450563Z","iopub.status.idle":"2026-04-21T19:56:44.459776Z","shell.execute_reply.started":"2026-04-21T19:56:44.450531Z","shell.execute_reply":"2026-04-21T19:56:44.459045Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:56:44.460757Z","iopub.execute_input":"2026-04-21T19:56:44.461079Z","iopub.status.idle":"2026-04-21T19:56:44.475015Z","shell.execute_reply.started":"2026-04-21T19:56:44.461046Z","shell.execute_reply":"2026-04-21T19:56:44.474213Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\n\nearly_stop = EarlyStopping(monitor='val_loss', patience=3)\n\nhistory = model.fit(\n    X_train, y_train,\n    epochs=20,\n    batch_size=32,\n    validation_split=0.2,\n    callbacks=[early_stop]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T19:56:44.476072Z","iopub.execute_input":"2026-04-21T19:56:44.476329Z","iopub.status.idle":"2026-04-21T20:04:57.084181Z","shell.execute_reply.started":"2026-04-21T19:56:44.476274Z","shell.execute_reply":"2026-04-21T20:04:57.083296Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loss, acc = model.evaluate(X_test, y_test)\nprint(\"Accuracy:\", acc)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-04-21T20:04:57.085234Z","iopub.execute_input":"2026-04-21T20:04:57.085531Z","iopub.status.idle":"2026-04-21T20:05:22.451708Z","shell.execute_reply.started":"2026-04-21T20:04:57.085467Z","shell.execute_reply":"2026-04-21T20:05:22.450592Z"}},"outputs":[],"execution_count":null}]}