{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"colab":{"provenance":[],"gpuType":"T4"},"accelerator":"GPU","kaggle":{"accelerator":"none","dataSources":[{"sourceId":10737,"databundleVersionId":290346,"sourceType":"competition"},{"sourceId":2580,"sourceType":"modelInstanceVersion","modelInstanceId":1882,"modelId":244},{"sourceId":2938,"sourceType":"modelInstanceVersion","modelInstanceId":2180,"modelId":244}],"dockerImageVersionId":30919,"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:19:28.486670Z","iopub.execute_input":"2025-02-23T05:19:28.486947Z","iopub.status.idle":"2025-02-23T05:19:28.500618Z","shell.execute_reply.started":"2025-02-23T05:19:28.486918Z","shell.execute_reply":"2025-02-23T05:19:28.499809Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Import Libraries","metadata":{"id":"0z6i5M-iELve"}},{"cell_type":"code","source":"!pip install tensorflow_text --quiet\n!pip install tf-keras --quiet\n!pip install --upgrade tensorflow-hub --quiet\n!pip install focal_loss --quiet","metadata":{"id":"oJMd-2DhKsvI","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:19:30.667714Z","iopub.execute_input":"2025-02-23T05:19:30.668010Z","iopub.status.idle":"2025-02-23T05:19:45.849106Z","shell.execute_reply.started":"2025-02-23T05:19:30.667986Z","shell.execute_reply":"2025-02-23T05:19:45.848160Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nos.environ['TF_USE_LEGACY_KERAS'] = '1'","metadata":{"id":"nzhGasxtOlIq","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:19:45.850631Z","iopub.execute_input":"2025-02-23T05:19:45.850966Z","iopub.status.idle":"2025-02-23T05:19:45.855103Z","shell.execute_reply.started":"2025-02-23T05:19:45.850931Z","shell.execute_reply":"2025-02-23T05:19:45.854151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow_text as text\nimport tensorflow as tf\nimport tensorflow_hub as hub\nimport tf_keras as keras\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, confusion_matrix, f1_score\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom focal_loss import BinaryFocalLoss","metadata":{"id":"oxjvZewhEJtA","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:19:45.856339Z","iopub.execute_input":"2025-02-23T05:19:45.856575Z","iopub.status.idle":"2025-02-23T05:19:45.874021Z","shell.execute_reply.started":"2025-02-23T05:19:45.856555Z","shell.execute_reply":"2025-02-23T05:19:45.873322Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Process Data","metadata":{"id":"MOWnvPf5Yeds"}},{"cell_type":"code","source":"np.random.seed(42)\ntf.random.set_seed(42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T03:55:18.049861Z","iopub.execute_input":"2025-02-23T03:55:18.050374Z","iopub.status.idle":"2025-02-23T03:55:18.054185Z","shell.execute_reply.started":"2025-02-23T03:55:18.050349Z","shell.execute_reply":"2025-02-23T03:55:18.053353Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/quora-insincere-questions-classification/train.csv\")","metadata":{"id":"HlCYk_QbY_h_","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T04:44:42.210504Z","iopub.execute_input":"2025-02-23T04:44:42.210849Z","iopub.status.idle":"2025-02-23T04:44:44.732839Z","shell.execute_reply.started":"2025-02-23T04:44:42.210822Z","shell.execute_reply":"2025-02-23T04:44:44.731851Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.drop(\"qid\", axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T04:50:48.276560Z","iopub.execute_input":"2025-02-23T04:50:48.276864Z","iopub.status.idle":"2025-02-23T04:50:48.310675Z","shell.execute_reply.started":"2025-02-23T04:50:48.276842Z","shell.execute_reply":"2025-02-23T04:50:48.310030Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"id":"AHVsPmWmay2W","outputId":"6da1f579-0262-4a35-b146-2281d180de15","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T04:50:49.444328Z","iopub.execute_input":"2025-02-23T04:50:49.444653Z","iopub.status.idle":"2025-02-23T04:50:49.452691Z","shell.execute_reply.started":"2025-02-23T04:50:49.444625Z","shell.execute_reply":"2025-02-23T04:50:49.451609Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isna().sum()","metadata":{"id":"dgdypyLTawBO","outputId":"de22c90f-d5f0-4a43-c6be-4bf50ef83b84","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T03:55:22.306420Z","iopub.execute_input":"2025-02-23T03:55:22.306626Z","iopub.status.idle":"2025-02-23T03:55:22.401068Z","shell.execute_reply.started":"2025-02-23T03:55:22.306608Z","shell.execute_reply":"2025-02-23T03:55:22.400046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df[\"target\"].plot(kind='hist', title='Target Distribution', figsize=(4,4))","metadata":{"id":"bLuAVVsyaSH7","outputId":"15f52e31-01ff-4ace-83b9-43e99aa9fb39","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T03:55:22.402308Z","iopub.execute_input":"2025-02-23T03:55:22.402649Z","iopub.status.idle":"2025-02-23T03:55:22.809619Z","shell.execute_reply.started":"2025-02-23T03:55:22.402617Z","shell.execute_reply":"2025-02-23T03:55:22.808707Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Train Test Split","metadata":{"id":"UhuTHfy3an4a"}},{"cell_type":"code","source":"train_df, val_df = train_test_split(df, train_size=0.0075, test_size=0.00075, stratify=df.target.values)","metadata":{"id":"YHcjyMruaavg","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T04:00:30.760005Z","iopub.execute_input":"2025-02-23T04:00:30.760307Z","iopub.status.idle":"2025-02-23T04:00:30.966643Z","shell.execute_reply.started":"2025-02-23T04:00:30.760284Z","shell.execute_reply":"2025-02-23T04:00:30.965757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.shape","metadata":{"id":"L3RAyOiBbTmF","outputId":"7e6d5873-07d4-4f2b-9656-49704b0a57b7","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T04:00:33.689576Z","iopub.execute_input":"2025-02-23T04:00:33.690004Z","iopub.status.idle":"2025-02-23T04:00:33.696051Z","shell.execute_reply.started":"2025-02-23T04:00:33.689968Z","shell.execute_reply":"2025-02-23T04:00:33.695177Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_df.shape","metadata":{"id":"7Xo13Xh8btyp","outputId":"0cd85ed2-0b1b-4206-e5c9-8911f783389d","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T04:00:34.891538Z","iopub.execute_input":"2025-02-23T04:00:34.891856Z","iopub.status.idle":"2025-02-23T04:00:34.896785Z","shell.execute_reply.started":"2025-02-23T04:00:34.891827Z","shell.execute_reply":"2025-02-23T04:00:34.895886Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[\"target\"].plot(kind='hist', title='Target Distribution', figsize=(4,4))","metadata":{"id":"jYhcXDd2bviY","outputId":"b5bd8a39-7310-4e10-9683-ab469292a2b8","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T03:55:24.731402Z","iopub.execute_input":"2025-02-23T03:55:24.731707Z","iopub.status.idle":"2025-02-23T03:55:24.908668Z","shell.execute_reply.started":"2025-02-23T03:55:24.731683Z","shell.execute_reply":"2025-02-23T03:55:24.907825Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_df[\"target\"].plot(kind='hist', title='Target Distribution', figsize=(4,4))","metadata":{"id":"QFu1qmBLbyCA","outputId":"e66e98ea-ef7c-460c-b5a6-60ddd0e959c3","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T03:55:28.194301Z","iopub.execute_input":"2025-02-23T03:55:28.194619Z","iopub.status.idle":"2025-02-23T03:55:28.374472Z","shell.execute_reply.started":"2025-02-23T03:55:28.194594Z","shell.execute_reply":"2025-02-23T03:55:28.373579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = train_df[\"question_text\"].values\ny_train = train_df[\"target\"].values\n\nX_val = val_df[\"question_text\"].values\ny_val = val_df[\"target\"].values","metadata":{"id":"AIAAmDKUeQF9","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T03:55:30.650507Z","iopub.execute_input":"2025-02-23T03:55:30.650852Z","iopub.status.idle":"2025-02-23T03:55:30.656081Z","shell.execute_reply.started":"2025-02-23T03:55:30.650821Z","shell.execute_reply":"2025-02-23T03:55:30.655030Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(6, 4))\nplt.hist(y_train, bins=2)\nplt.xticks([0, 1])\nplt.xlabel(\"Class Label\")\nplt.ylabel(\"Frequency\")\nplt.title(\"Class Distribution in Train Data\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T03:55:32.016490Z","iopub.execute_input":"2025-02-23T03:55:32.016794Z","iopub.status.idle":"2025-02-23T03:55:32.167370Z","shell.execute_reply.started":"2025-02-23T03:55:32.016769Z","shell.execute_reply":"2025-02-23T03:55:32.166460Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(6, 4))\nplt.hist(y_val, bins=2)\nplt.xticks([0, 1])\nplt.xlabel(\"Class Label\")\nplt.ylabel(\"Frequency\")\nplt.title(\"Class Distribution in Test Data\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T03:55:34.665502Z","iopub.execute_input":"2025-02-23T03:55:34.665828Z","iopub.status.idle":"2025-02-23T03:55:34.786423Z","shell.execute_reply.started":"2025-02-23T03:55:34.665804Z","shell.execute_reply":"2025-02-23T03:55:34.785658Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Define Model Architecture","metadata":{"id":"QO18uaibLCRa"}},{"cell_type":"code","source":"preprocessor = hub.KerasLayer(\"https://kaggle.com/models/tensorflow/bert/frameworks/TensorFlow2/variations/en-uncased-preprocess/versions/3\")\nencoder = hub.KerasLayer(\"https://www.kaggle.com/models/tensorflow/bert/frameworks/TensorFlow2/variations/en-uncased-l-12-h-768-a-12/versions/4\", trainable=True)","metadata":{"id":"tOYe1wLUXU2_","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:19:48.222280Z","iopub.execute_input":"2025-02-23T05:19:48.222711Z","iopub.status.idle":"2025-02-23T05:20:03.516292Z","shell.execute_reply.started":"2025-02-23T05:19:48.222670Z","shell.execute_reply":"2025-02-23T05:20:03.515405Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"text_input = keras.layers.Input(shape=(), dtype=tf.string)\n\n# Preprocess the text with TF_HUB\nencoder_inputs = preprocessor(text_input)\n\n# Encode the processed text for BERT\noutputs = encoder(encoder_inputs)\n\n# Pool the output\npooled_output = outputs[\"pooled_output\"]","metadata":{"id":"3EvQmj89OqD-","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:20:03.517497Z","iopub.execute_input":"2025-02-23T05:20:03.517802Z","iopub.status.idle":"2025-02-23T05:20:04.059295Z","shell.execute_reply.started":"2025-02-23T05:20:03.517773Z","shell.execute_reply":"2025-02-23T05:20:04.058319Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x = keras.layers.Dropout(0.5, name=\"dropout-1\")(pooled_output)\nx = keras.layers.Dense(16, activation='relu', name=\"dense-16\")(x)\n\noutput = keras.layers.Dense(1, activation ='sigmoid', name=\"output\")(x)","metadata":{"id":"CVbyOq14U5Zz","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:20:04.061183Z","iopub.execute_input":"2025-02-23T05:20:04.061534Z","iopub.status.idle":"2025-02-23T05:20:04.124276Z","shell.execute_reply.started":"2025-02-23T05:20:04.061502Z","shell.execute_reply":"2025-02-23T05:20:04.123390Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = keras.Model(inputs = [text_input], outputs = [output])","metadata":{"id":"powCkKfsW2-x","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:20:04.125410Z","iopub.execute_input":"2025-02-23T05:20:04.125667Z","iopub.status.idle":"2025-02-23T05:20:04.134007Z","shell.execute_reply.started":"2025-02-23T05:20:04.125646Z","shell.execute_reply":"2025-02-23T05:20:04.133201Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"id":"8eyMjNaAcjWD","outputId":"4ae377c5-32a4-4188-d86e-6e02beebb5ab","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:20:04.134864Z","iopub.execute_input":"2025-02-23T05:20:04.135143Z","iopub.status.idle":"2025-02-23T05:20:04.192460Z","shell.execute_reply.started":"2025-02-23T05:20:04.135116Z","shell.execute_reply":"2025-02-23T05:20:04.191658Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model Training","metadata":{"id":"R_szY4YsYbLU"}},{"cell_type":"code","source":"model.compile(optimizer=keras.optimizers.Adam(learning_rate=2e-5), loss=BinaryFocalLoss(gamma=2), metrics=['accuracy'])","metadata":{"id":"ncwXshXopf7e","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:20:04.212577Z","iopub.execute_input":"2025-02-23T05:20:04.212886Z","iopub.status.idle":"2025-02-23T05:20:04.223955Z","shell.execute_reply.started":"2025-02-23T05:20:04.212857Z","shell.execute_reply":"2025-02-23T05:20:04.223064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"early_stopping = EarlyStopping(monitor=\"val_loss\", patience=3, verbose=1)\nreduce_lr = ReduceLROnPlateau(monitor=\"val_loss\", factor=0.5, patience=2, min_lr=1e-6, verbose=1)\ncheckpoint = ModelCheckpoint(filepath=\"/kaggle/working/Best-Quora-Questions-Classifier.h5\", monitor='val_loss', mode='min', save_best_only=True, save_weights_only=True, verbose=1)","metadata":{"id":"rGKtlR1QpgfH","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T04:00:41.454899Z","iopub.execute_input":"2025-02-23T04:00:41.455256Z","iopub.status.idle":"2025-02-23T04:00:41.460120Z","shell.execute_reply.started":"2025-02-23T04:00:41.455230Z","shell.execute_reply":"2025-02-23T04:00:41.459051Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tf.debugging.set_log_device_placement(True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T04:00:48.905252Z","iopub.execute_input":"2025-02-23T04:00:48.905551Z","iopub.status.idle":"2025-02-23T04:00:48.909311Z","shell.execute_reply.started":"2025-02-23T04:00:48.905527Z","shell.execute_reply":"2025-02-23T04:00:48.908470Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    X_train, y_train,\n    validation_data=(X_val, y_val),\n    epochs=5,\n    verbose=1,\n    callbacks=[early_stopping, reduce_lr, checkpoint]\n)","metadata":{"id":"X69xpujUXmch","outputId":"6ef7267d-0b27-4a09-d701-e02db5ccfa9e","trusted":true,"execution":{"iopub.status.busy":"2025-02-23T04:00:50.180566Z","iopub.execute_input":"2025-02-23T04:00:50.180885Z","iopub.status.idle":"2025-02-23T04:14:51.540257Z","shell.execute_reply.started":"2025-02-23T04:00:50.180857Z","shell.execute_reply":"2025-02-23T04:14:51.539537Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.load_weights(\"/kaggle/working/Best-Quora-Questions-Classifier.h5\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:20:30.974670Z","iopub.execute_input":"2025-02-23T05:20:30.974995Z","iopub.status.idle":"2025-02-23T05:20:31.023541Z","shell.execute_reply.started":"2025-02-23T05:20:30.974967Z","shell.execute_reply":"2025-02-23T05:20:31.022325Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Graphs","metadata":{}},{"cell_type":"code","source":"# Plot training & validation loss\nplt.figure(figsize=(12, 5))\n\n# Loss Graph\nplt.subplot(1, 2, 1)\nplt.plot(history.history['loss'], label='Loss')\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Loss\")\nplt.title(\"Training Loss\")\nplt.legend()\n\n# Accuracy Graph\nplt.subplot(1, 2, 2)\nplt.plot(history.history['accuracy'], label='Accuracy')\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Accuracy\")\nplt.title(\"Training Accuracy\")\nplt.legend()\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T04:15:23.437127Z","iopub.execute_input":"2025-02-23T04:15:23.437450Z","iopub.status.idle":"2025-02-23T04:15:23.788878Z","shell.execute_reply.started":"2025-02-23T04:15:23.437421Z","shell.execute_reply":"2025-02-23T04:15:23.788039Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot training & validation loss\nplt.figure(figsize=(12, 5))\n\n# Loss Graph\nplt.subplot(1, 2, 1)\nplt.plot(history.history['val_loss'], label='Loss')\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Loss\")\nplt.title(\"Validation Loss\")\nplt.legend()\n\n# Accuracy Graph\nplt.subplot(1, 2, 2)\nplt.plot(history.history['val_accuracy'], label='Accuracy')\nplt.xlabel(\"Epochs\")\nplt.ylabel(\"Accuracy\")\nplt.title(\"Validation Accuracy\")\nplt.legend()\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T04:15:29.721624Z","iopub.execute_input":"2025-02-23T04:15:29.722011Z","iopub.status.idle":"2025-02-23T04:15:30.108068Z","shell.execute_reply.started":"2025-02-23T04:15:29.721942Z","shell.execute_reply":"2025-02-23T04:15:30.107057Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model Testing","metadata":{}},{"cell_type":"markdown","source":"## Validation Accuracy and Loss","metadata":{}},{"cell_type":"code","source":"loss, accuracy = model.evaluate(X_val, y_val, batch_size=32)\nprint(f\"Accuracy: {accuracy}, Valudation Loss: {loss}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T04:18:54.280583Z","iopub.execute_input":"2025-02-23T04:18:54.280908Z","iopub.status.idle":"2025-02-23T04:19:00.387292Z","shell.execute_reply.started":"2025-02-23T04:18:54.280878Z","shell.execute_reply":"2025-02-23T04:19:00.386444Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Classification Report","metadata":{}},{"cell_type":"code","source":"best_f1 = best_threshold = 0\n\ny_pred = model.predict(X_val)\n\nfor t in np.arange(0.3, 0.8, 0.05):\n    y_pred_discrete = [1 if y > t else 0 for y in y_pred]\n    f1 = f1_score(y_val, y_pred_discrete)\n    \n    if f1 > best_f1:\n        best_f1 = f1\n        best_threshold = t\n\nprint(f\"Best Threshold: {best_threshold:.2f}, Best F1 Score: {best_f1:.2f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T04:19:06.578209Z","iopub.execute_input":"2025-02-23T04:19:06.578555Z","iopub.status.idle":"2025-02-23T04:19:13.414220Z","shell.execute_reply.started":"2025-02-23T04:19:06.578525Z","shell.execute_reply":"2025-02-23T04:19:13.413354Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = [1 if y > best_threshold else 0 for y in y_pred]\n\nprint(classification_report(y_val, y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T04:47:17.497727Z","iopub.execute_input":"2025-02-23T04:47:17.498060Z","iopub.status.idle":"2025-02-23T04:47:17.513033Z","shell.execute_reply.started":"2025-02-23T04:47:17.498035Z","shell.execute_reply":"2025-02-23T04:47:17.512328Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"X_test = pd.read_csv(\"/kaggle/input/quora-insincere-questions-classification/test.csv\")\nX_test = X_test[:10000]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:20:57.024961Z","iopub.execute_input":"2025-02-23T05:20:57.025283Z","iopub.status.idle":"2025-02-23T05:20:58.192908Z","shell.execute_reply.started":"2025-02-23T05:20:57.025240Z","shell.execute_reply":"2025-02-23T05:20:58.192012Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"questions = X_test['question_text'].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:21:01.171694Z","iopub.execute_input":"2025-02-23T05:21:01.172033Z","iopub.status.idle":"2025-02-23T05:21:01.179449Z","shell.execute_reply.started":"2025-02-23T05:21:01.172002Z","shell.execute_reply":"2025-02-23T05:21:01.178360Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(questions)\n\npredictions = [1 if y > best_threshold else 0 for y in predictions]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:12:19.748075Z","iopub.execute_input":"2025-02-23T05:12:19.748405Z","iopub.status.idle":"2025-02-23T05:12:19.767612Z","shell.execute_reply.started":"2025-02-23T05:12:19.748370Z","shell.execute_reply":"2025-02-23T05:12:19.766795Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_submit = pd.DataFrame({\n    'qid': X_test['qid'].values,\n    'prediction': predictions\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:12:23.486556Z","iopub.execute_input":"2025-02-23T05:12:23.486852Z","iopub.status.idle":"2025-02-23T05:12:23.493425Z","shell.execute_reply.started":"2025-02-23T05:12:23.486830Z","shell.execute_reply":"2025-02-23T05:12:23.492568Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_submit.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-23T05:12:51.218152Z","iopub.execute_input":"2025-02-23T05:12:51.218473Z","iopub.status.idle":"2025-02-23T05:12:53.613335Z","shell.execute_reply.started":"2025-02-23T05:12:51.218447Z","shell.execute_reply":"2025-02-23T05:12:53.612384Z"}},"outputs":[],"execution_count":null}]}