Search is not available for this dataset
text
stringlengths
2.22k
90.5M
id
stringlengths
23
24
file_path
stringclasses
59 values
{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "9d3205eb", "metadata": { "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19", "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5", "execution": { "iopub.execute_input": "2022-11-11T13:49:50.512038Z", "iopub.status.busy": "2022-11-11T13:49:50.511211Z", "iopub.status.idle": "2022-11-11T13:49:50.527831Z", "shell.execute_reply": "2022-11-11T13:49:50.527005Z" }, "papermill": { "duration": 0.040684, "end_time": "2022-11-11T13:49:50.530130", "exception": false, "start_time": "2022-11-11T13:49:50.489446", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/kaggle/input/question-pairs-dataset/questions.csv\n" ] } ], "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", "\n", "import numpy as np # linear algebra\n", "import 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", "\n", "import os\n", "for 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" ] }, { "cell_type": "code", "execution_count": 2, "id": "781d993c", "metadata": { "execution": { "iopub.execute_input": "2022-11-11T13:49:50.560813Z", "iopub.status.busy": "2022-11-11T13:49:50.559792Z", "iopub.status.idle": "2022-11-11T13:49:52.348638Z", "shell.execute_reply": "2022-11-11T13:49:52.347463Z" }, "papermill": { "duration": 1.806483, "end_time": "2022-11-11T13:49:52.350924", "exception": false, "start_time": "2022-11-11T13:49:50.544441", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import torch\n", "\n", "torch.cuda.is_available()" ] }, { "cell_type": "code", "execution_count": 3, "id": "a87a7bec", "metadata": { "execution": { "iopub.execute_input": "2022-11-11T13:49:52.382025Z", "iopub.status.busy": "2022-11-11T13:49:52.381172Z", "iopub.status.idle": "2022-11-11T13:49:52.388211Z", "shell.execute_reply": "2022-11-11T13:49:52.387354Z" }, "papermill": { "duration": 0.024547, "end_time": "2022-11-11T13:49:52.390204", "exception": false, "start_time": "2022-11-11T13:49:52.365657", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.cuda.device_count()" ] }, { "cell_type": "code", "execution_count": 4, "id": "1cb11c07", "metadata": { "execution": { "iopub.execute_input": "2022-11-11T13:49:52.420771Z", "iopub.status.busy": "2022-11-11T13:49:52.420183Z", "iopub.status.idle": "2022-11-11T13:49:52.533807Z", "shell.execute_reply": "2022-11-11T13:49:52.532193Z" }, "papermill": { "duration": 0.131068, "end_time": "2022-11-11T13:49:52.535576", "exception": true, "start_time": "2022-11-11T13:49:52.404508", "status": "failed" }, "tags": [] }, "outputs": [ { "ename": "AssertionError", "evalue": "Torch not compiled with CUDA enabled", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_19/361484350.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcuda\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcurrent_device\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/opt/conda/lib/python3.7/site-packages/torch/cuda/__init__.py\u001b[0m in \u001b[0;36mcurrent_device\u001b[0;34m()\u001b[0m\n\u001b[1;32m 479\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcurrent_device\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 480\u001b[0m \u001b[0;34mr\"\"\"Returns the index of a currently selected device.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 481\u001b[0;31m \u001b[0m_lazy_init\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 482\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_C\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_cuda_getDevice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 483\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/opt/conda/lib/python3.7/site-packages/torch/cuda/__init__.py\u001b[0m in \u001b[0;36m_lazy_init\u001b[0;34m()\u001b[0m\n\u001b[1;32m 208\u001b[0m \"multiprocessing, you must use the 'spawn' start method\")\n\u001b[1;32m 209\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_C\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'_cuda_getDeviceCount'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 210\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mAssertionError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Torch not compiled with CUDA enabled\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 211\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m_cudart\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 212\u001b[0m raise AssertionError(\n", "\u001b[0;31mAssertionError\u001b[0m: Torch not compiled with CUDA enabled" ] } ], "source": [ "torch.cuda.current_device()" ] }, { "cell_type": "code", "execution_count": null, "id": "d7a72c05", "metadata": { "execution": { "iopub.execute_input": "2022-11-09T19:26:13.802969Z", "iopub.status.busy": "2022-11-09T19:26:13.802344Z", "iopub.status.idle": "2022-11-09T19:26:13.822329Z", "shell.execute_reply": "2022-11-09T19:26:13.820634Z", "shell.execute_reply.started": "2022-11-09T19:26:13.802935Z" }, "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "torch.cuda.get_device_name(0)" ] }, { "cell_type": "code", "execution_count": null, "id": "5fe47a8a", "metadata": { "execution": { "iopub.execute_input": "2022-11-09T19:26:14.898879Z", "iopub.status.busy": "2022-11-09T19:26:14.898492Z", "iopub.status.idle": "2022-11-09T19:26:28.604925Z", "shell.execute_reply": "2022-11-09T19:26:28.603487Z", "shell.execute_reply.started": "2022-11-09T19:26:14.898847Z" }, "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "!pip install sentence_transformers" ] }, { "cell_type": "code", "execution_count": null, "id": "795a7291", "metadata": { "execution": { "iopub.execute_input": "2022-11-09T19:26:28.608535Z", "iopub.status.busy": "2022-11-09T19:26:28.608061Z", "iopub.status.idle": "2022-11-09T19:26:38.923255Z", "shell.execute_reply": "2022-11-09T19:26:38.922007Z", "shell.execute_reply.started": "2022-11-09T19:26:28.608491Z" }, "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "from sentence_transformers import SentenceTransformer\n", "\n", "model = SentenceTransformer('all-MiniLM-L6-v2')\n" ] }, { "cell_type": "code", "execution_count": null, "id": "4f1bf04a", "metadata": { "execution": { "iopub.execute_input": "2022-11-09T19:30:09.224142Z", "iopub.status.busy": "2022-11-09T19:30:09.222940Z", "iopub.status.idle": "2022-11-09T19:30:11.053487Z", "shell.execute_reply": "2022-11-09T19:30:11.052507Z", "shell.execute_reply.started": "2022-11-09T19:30:09.224092Z" }, "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_q = pd.read_csv(\"/kaggle/input/question-pairs-dataset/questions.csv\")\n", "df_q.head(5)" ] }, { "cell_type": "code", "execution_count": null, "id": "948cc64f", "metadata": { "execution": { "iopub.execute_input": "2022-11-09T19:30:26.248230Z", "iopub.status.busy": "2022-11-09T19:30:26.247824Z", "iopub.status.idle": "2022-11-09T19:30:26.316651Z", "shell.execute_reply": "2022-11-09T19:30:26.315119Z", "shell.execute_reply.started": "2022-11-09T19:30:26.248201Z" }, "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_q.isnull().any(axis=1).sum()" ] }, { "cell_type": "code", "execution_count": null, "id": "db457f33", "metadata": { "execution": { "iopub.execute_input": "2022-11-09T19:30:36.673911Z", "iopub.status.busy": "2022-11-09T19:30:36.673482Z", "iopub.status.idle": "2022-11-09T19:30:36.798913Z", "shell.execute_reply": "2022-11-09T19:30:36.797517Z", "shell.execute_reply.started": "2022-11-09T19:30:36.673875Z" }, "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "print(df_q.shape)\n", "df_q.dropna(subset=['question1', 'question2'], inplace=True)\n", "print(df_q.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "3bccc42f", "metadata": { "execution": { "iopub.execute_input": "2022-11-09T19:31:24.541791Z", "iopub.status.busy": "2022-11-09T19:31:24.541354Z", "iopub.status.idle": "2022-11-09T19:31:24.546975Z", "shell.execute_reply": "2022-11-09T19:31:24.545833Z", "shell.execute_reply.started": "2022-11-09T19:31:24.541752Z" }, "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "def create_embeddings(inputs):\n", " print(\"encoding with model\")\n", " sentence_embeddings = model.encode(inputs, show_progress_bar=True)\n", " return sentence_embeddings\n" ] }, { "cell_type": "code", "execution_count": null, "id": "c4d7d4ef", "metadata": { "execution": { "iopub.execute_input": "2022-11-09T19:31:26.529537Z", "iopub.status.busy": "2022-11-09T19:31:26.528812Z", "iopub.status.idle": "2022-11-09T19:31:26.535629Z", "shell.execute_reply": "2022-11-09T19:31:26.534748Z", "shell.execute_reply.started": "2022-11-09T19:31:26.529488Z" }, "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "from scipy import spatial\n", "\n", "def calc_similarity(embeddings1, embeddings2):\n", " cosine_distance = spatial.distance.cosine(embeddings1, embeddings2)\n", " cosine_similarity = 1 - cosine_distance\n", " return cosine_similarity\n" ] }, { "cell_type": "code", "execution_count": null, "id": "aff594c1", "metadata": { "execution": { "iopub.execute_input": "2022-11-09T19:31:37.972353Z", "iopub.status.busy": "2022-11-09T19:31:37.971625Z", "iopub.status.idle": "2022-11-09T19:31:37.978618Z", "shell.execute_reply": "2022-11-09T19:31:37.977527Z", "shell.execute_reply.started": "2022-11-09T19:31:37.972297Z" }, "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "from tqdm import tqdm\n", "tqdm.pandas()" ] }, { "cell_type": "code", "execution_count": null, "id": "c71b2d18", "metadata": { "execution": { "iopub.execute_input": "2022-11-09T19:32:00.972219Z", "iopub.status.busy": "2022-11-09T19:32:00.971309Z", "iopub.status.idle": "2022-11-09T20:03:45.700725Z", "shell.execute_reply": "2022-11-09T20:03:45.699700Z", "shell.execute_reply.started": "2022-11-09T19:32:00.972169Z" }, "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "questions_1 = list(df_q[\"question1\"].values)\n", "embeddings_1 = create_embeddings(questions_1)" ] }, { "cell_type": "code", "execution_count": null, "id": "52dcc871", "metadata": { "execution": { "iopub.execute_input": "2022-11-09T20:03:45.706661Z", "iopub.status.busy": "2022-11-09T20:03:45.704100Z" }, "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "questions_2 = list(df_q[\"question2\"].values)\n", "embeddings_2 = create_embeddings(questions_2)" ] }, { "cell_type": "code", "execution_count": null, "id": "8aa9bcec", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "32*12636" ] }, { "cell_type": "code", "execution_count": null, "id": "b3d9e28e", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "similarities = []\n", "for row in tqdm(zip(embeddings_1, embeddings_2), total=len(embeddings_1)):\n", " similarities.append(calc_similarity(row[0], row[1]))" ] }, { "cell_type": "code", "execution_count": null, "id": "34617149", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_compare = df_q.copy()" ] }, { "cell_type": "code", "execution_count": null, "id": "2146800f", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_compare[\"similarity\"] = similarities" ] }, { "cell_type": "code", "execution_count": null, "id": "42a9a6d4", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "\n", "df_compare[\"abs_similarity\"] = np.abs(similarities)" ] }, { "cell_type": "code", "execution_count": null, "id": "343fd58a", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_compare.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "4b1918e4", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "min(df_compare[\"similarity\"])" ] }, { "cell_type": "code", "execution_count": null, "id": "ea69e784", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "max(df_compare[\"similarity\"])" ] }, { "cell_type": "code", "execution_count": null, "id": "efc666cf", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "min(df_compare[\"abs_similarity\"])" ] }, { "cell_type": "code", "execution_count": null, "id": "a3cfa29a", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "def print_n_top(n, df):\n", " indices = []\n", " for x in range(n):\n", " q1 = df.iloc[x][\"question1\"]\n", " q2 = df.iloc[x][\"question2\"]\n", " sim = df.iloc[x][\"similarity\"]\n", " abs_sim = df.iloc[x][\"abs_similarity\"]\n", " idx = df.index[x]\n", " indices.append(idx)\n", " print(f\"{q1} vs\\n{q2}\\n - {sim}:{abs_sim} index={idx}\\n\")\n", " print(indices)\n", " return indices" ] }, { "cell_type": "code", "execution_count": null, "id": "e3b5b993", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "#thresold=1 -> no filtering\n", "def filter_and_print_top(df, threshold=1, threshold_up=False, col=\"similarity\", ascending=False, n=5):\n", " if threshold_up:\n", " df_filtered = df[df[col] > threshold]\n", " else:\n", " df_filtered = df[df[col] < threshold]\n", "\n", " dropped_size = df.shape[0] - df_filtered.shape[0]\n", " print(f\"dropped after filtering: {dropped_size}\")\n", " df_filtered = df_filtered.sort_values(by=col, ascending=ascending)\n", " indices = print_n_top(n, df_filtered)\n", " return indices" ] }, { "cell_type": "code", "execution_count": null, "id": "720a2598", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "n = 20\n", "most_similar_indices = filter_and_print_top(df_compare, ascending=False, n=n)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "bd8e167d", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "most_dissimilar_indices = filter_and_print_top(df_compare, ascending=True, n=n)" ] }, { "cell_type": "code", "execution_count": null, "id": "25ad79ce", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "filter_and_print_top(df_compare, col=\"abs_similarity\", ascending=True, n=n)" ] }, { "cell_type": "code", "execution_count": null, "id": "a4d8b2dc", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "filter_and_print_top(df_compare, threshold=0.99, n=n)" ] }, { "cell_type": "code", "execution_count": null, "id": "1b3d0421", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "most_similar_indices_90 = filter_and_print_top(df_compare, threshold=0.90, n=n)" ] }, { "cell_type": "code", "execution_count": null, "id": "87040227", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_compare[\"prediction\"] = df_compare[\"similarity\"] > 0.9\n", "df_compare[\"prediction\"] = df_compare[\"prediction\"].astype(int)" ] }, { "cell_type": "code", "execution_count": null, "id": "8b39d245", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_compare.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "73f1cb3e", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "sum(df_compare[\"is_duplicate\"] == df_compare[\"prediction\"])" ] }, { "cell_type": "code", "execution_count": null, "id": "7bbc8d34", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_compare.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "5cb44e7e", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "301950/404348" ] }, { "cell_type": "code", "execution_count": null, "id": "59e74061", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "def calculate_accuracy(df, threshold):\n", " df[\"prediction\"] = df[\"similarity\"] > threshold\n", " df[\"prediction\"] = df[\"prediction\"].astype(int)\n", " correct = sum(df_compare[\"is_duplicate\"] == df_compare[\"prediction\"])\n", " total = df.shape[0]\n", " accuracy = correct / total\n", " return correct, accuracy" ] }, { "cell_type": "code", "execution_count": null, "id": "92a31ddc", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "thresholds = []\n", "accuracies = []\n", "corrects = []\n", "\n", "for threshold in range(1, 100, 1):\n", " threshold = threshold / 100.0\n", " correct, accuracy = calculate_accuracy(df_compare, threshold)\n", " thresholds.append(threshold)\n", " accuracies.append(accuracy)\n", " corrects.append(correct)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "18f7a495", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_accuracy = pd.DataFrame()\n", "df_accuracy[\"threshold\"] = thresholds\n", "df_accuracy[\"accuracy\"] = accuracies\n", "df_accuracy[\"correct\"] = corrects" ] }, { "cell_type": "code", "execution_count": null, "id": "0d65eefd", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_accuracy[\"accuracy\"].plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "43b201f0", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "np.max(df_accuracy[\"accuracy\"])" ] }, { "cell_type": "code", "execution_count": null, "id": "778ba817", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "np.argmax(df_accuracy[\"accuracy\"])" ] }, { "cell_type": "code", "execution_count": null, "id": "78f85dc4", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "from sklearn.decomposition import PCA\n", "\n", "pca = PCA(n_components=2)\n", "pca_components = pca.fit_transform(embeddings_1)\n", "print(pca.explained_variance_ratio_)\n", "df_pca = pd.DataFrame(data = pca_components, columns = ['pca1', 'pca2'])\n" ] }, { "cell_type": "code", "execution_count": null, "id": "56ae7101", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "from matplotlib import pyplot as plt\n", "\n", "plt.scatter(df_pca[\"pca1\"],df_pca[\"pca2\"])\n", "#plt.scatter(data2[0],data2[1])\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "365cd887", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "pca = PCA(n_components=3)\n", "pca_components = pca.fit_transform(embeddings_1)\n", "print(pca.explained_variance_ratio_)\n", "df_pca = pd.DataFrame(data = pca_components, columns = ['pca1', 'pca2', 'pca3'])\n" ] }, { "cell_type": "code", "execution_count": null, "id": "6f79fedb", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "pca = PCA(n_components=50)\n", "pca_components = pca.fit_transform(embeddings_1)\n", "print(pca.explained_variance_ratio_)\n", "#df_pca = pd.DataFrame(data = pca_components, columns = ['pca1', 'pca2', 'pca3'])\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9b45ac2a", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "%%time\n", "import numpy as np\n", "from sklearn.manifold import TSNE\n", "#from cuml.manifold import TSNE\n", "\n", "tsne = TSNE(n_components=2, learning_rate='auto', init='random', perplexity=3, verbose=1)\n", "tsne_embedded = tsne.fit_transform(pca_components)\n", "tsne_embedded.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "83895d41", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "%%time\n", "from umap import UMAP\n", "import plotly.express as px\n", "\n", "print(\"creating 2d UMAP component\")\n", "umap_2d = UMAP(n_components=2, init='random', random_state=0)\n", "print(\"creating 3d UMAP component\")\n", "umap_3d = UMAP(n_components=3, init='random', random_state=0)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "014ee28f", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "%%time\n", "print(\"fitting 2d UMAP\")\n", "proj_2d = umap_2d.fit_transform(embeddings_1)\n", "\n", "print(\"fitting 3d UMAP\")\n", "proj_3d = umap_3d.fit_transform(embeddings_1)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "2ce3e233", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "%%time\n", "print(\"creating 2d scatter\")\n", "fig_2d = px.scatter(\n", " proj_2d, x=0, y=1,\n", " #color=[per dot color in array], labels={'color': 'species'}\n", ")\n", "print(\"creating 3d scatter\")\n", "fig_3d = px.scatter_3d(\n", " proj_3d, x=0, y=1, z=2,\n", " #color=[per dot color in array], labels={'color': 'species'}\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "7a7ff5b8", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "%%time\n", "print(\"updating traces\")\n", "fig_3d.update_traces(marker_size=5)\n", "\n", "print(\"showing figures\")\n", "#fig_2d.show()\n", "#fig_3d.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "9fb61ef3", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_sorted = df_compare.sort_values(by=\"similarity\")\n", "df_sorted.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "6a67cfdd", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_sorted.tail()" ] }, { "cell_type": "code", "execution_count": null, "id": "1eb01c4f", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "#filter_indices_top=[241636, 3099, 359487, 102181, 186992]\n", "filter_indices_top = most_similar_indices\n", "top_embeddings = embeddings_1[filter_indices_top]\n" ] }, { "cell_type": "code", "execution_count": null, "id": "427579ed", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "#filter_indices_bottom=[74220, 54367, 314641, 218913, 282911]\n", "filter_indices_bottom=most_dissimilar_indices\n", "bottom_embeddings = embeddings_1[filter_indices_bottom]\n" ] }, { "cell_type": "code", "execution_count": null, "id": "0346bb3e", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "filter_indices = filter_indices_top + filter_indices_bottom" ] }, { "cell_type": "code", "execution_count": null, "id": "20df5b6a", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "top_bottom_embeddings = np.concatenate([top_embeddings, bottom_embeddings])\n", "top_bottom_embeddings.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "197e9ad9", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "colors = [\"1\"]*len(top_embeddings) + [\"2\"]*len(bottom_embeddings)\n", "colors" ] }, { "cell_type": "code", "execution_count": null, "id": "f3ce241b", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "%%time\n", "print(\"fitting 2d UMAP\")\n", "#proj_2d = umap_2d.fit_transform(top_bottom_embeddings)\n", "proj_2d = umap_2d.fit_transform(embeddings_1)\n", "\n", "print(\"fitting 3d UMAP\")\n", "#proj_3d = umap_3d.fit_transform(top_bottom_embeddings)\n", "proj_3d = umap_3d.fit_transform(embeddings_1)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "6d4425bc", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "proj_2d_slice = proj_2d[filter_indices]\n", "proj_3d_slice = proj_3d[filter_indices]\n" ] }, { "cell_type": "code", "execution_count": null, "id": "85e94ddc", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "%%time\n", "print(\"creating 2d scatter\")\n", "fig_2d = px.scatter(\n", " proj_2d_slice, x=0, y=1,\n", " color=colors, labels={'color': 'species'}\n", ")\n", "print(\"creating 3d scatter\")\n", "fig_3d = px.scatter_3d(\n", " proj_3d_slice, x=0, y=1, z=2,\n", " color=colors, labels={'color': 'species'}\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b8ebe19d", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "%%time\n", "print(\"updating traces\")\n", "fig_3d.update_traces(marker_size=5)\n", "\n", "print(\"showing figures\")\n", "fig_2d.show()\n", "fig_3d.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "0f7faf0c", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "from sklearn.decomposition import PCA\n", "\n", "pca = PCA(n_components=2)\n", "pca_components = pca.fit_transform(embeddings_1)\n", "print(pca.explained_variance_ratio_)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a3149e57", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_pca_top = pd.DataFrame(data = pca_components[filter_indices_top], columns = ['pca1', 'pca2'])\n", "df_pca_top[\"type\"] = 1\n", "df_pca_bottom = pd.DataFrame(data = pca_components[filter_indices_bottom], columns = ['pca1', 'pca2'])\n", "df_pca_bottom[\"type\"] = 2\n" ] }, { "cell_type": "code", "execution_count": null, "id": "79b87613", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "from matplotlib import pyplot as plt\n", "\n", "plt.scatter(df_pca_top[\"pca1\"],df_pca_top[\"pca2\"])\n", "plt.scatter(df_pca_bottom[\"pca1\"],df_pca_bottom[\"pca2\"])\n", "#plt.scatter(data2[0],data2[1])\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "d23b9e6f", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "pca = PCA(n_components=3)\n", "#pca = pca.fit(embeddings_1)\n", "pca_components = pca.fit_transform(embeddings_1)\n", "print(pca.explained_variance_ratio_)\n", "df_pca = pd.DataFrame(data = pca_components, columns = ['pca1', 'pca2', 'pca3'])\n" ] }, { "cell_type": "code", "execution_count": null, "id": "2f81dac1", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_pca_top = pd.DataFrame(data = df_pca.iloc[filter_indices_top], columns = ['pca1', 'pca2', 'pca3'])\n", "df_pca_bottom = pd.DataFrame(data = df_pca.iloc[filter_indices_bottom], columns = ['pca1', 'pca2', 'pca3'])\n" ] }, { "cell_type": "code", "execution_count": null, "id": "8bd9ed3e", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "from matplotlib import pyplot as plt\n", "\n", "plt.scatter(df_pca_top[\"pca1\"],df_pca_top[\"pca2\"])\n", "plt.scatter(df_pca_bottom[\"pca1\"],df_pca_bottom[\"pca2\"])\n", "#plt.scatter(data2[0],data2[1])\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "66715ff3", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_all = pd.concat([df_pca_top, df_pca_bottom], axis=0)\n", "df_all.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "21b28549", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "import plotly.express as px\n", "\n", "#fig = px.scatter_3d(df_pca, x='pca1', y='pca2', z='pca3', color=colors, labels={'color': 'species'})\n", "fig = px.scatter_3d(df_all, x='pca1', y='pca2', z='pca3', color=colors)\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "535651b8", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "%%time\n", "#taking slot 0 is pointless as the question is exact same in both embeddings (the first question)\n", "#thus index 1 = question 2\n", "cosine_scores = util.cos_sim(embeddings_1[1], embeddings_2)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e58581a7", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "arr = np.array(cosine_scores)\n", "arr[0]" ] }, { "cell_type": "code", "execution_count": null, "id": "a158a832", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "best_match_idx = np.argmax(arr[0])\n", "best_match_idx" ] }, { "cell_type": "code", "execution_count": null, "id": "4d9f85e8", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "#need to +1 the index since 330777 starting from 1: is actual 330778 as 0 was skilled by 1:\n", "arr[0][best_match_idx]" ] }, { "cell_type": "code", "execution_count": null, "id": "a2e9bbf0", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_q.iloc[best_match_idx]" ] }, { "cell_type": "code", "execution_count": null, "id": "3dbb6776", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "calc_similarity(embeddings_1[0], embeddings_2[0])" ] }, { "cell_type": "code", "execution_count": null, "id": "d377f730", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "util.cos_sim(embeddings_1[0], embeddings_2[0])\n" ] }, { "cell_type": "code", "execution_count": null, "id": "65b0e681", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "%%time\n", "reference = embeddings_1[1]\n", "bext_idx = 0\n", "highest_sim = -100\n", "for idx, embed_2 in tqdm(enumerate(embeddings_2)):\n", " sim = calc_similarity(reference, embed_2)\n", " if sim > highest_sim:\n", " highest_sim = sim\n", " best_idx = idx" ] }, { "cell_type": "code", "execution_count": null, "id": "31cdefbe", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "best_idx" ] }, { "cell_type": "code", "execution_count": null, "id": "d34d2d18", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [ "df_q.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "f985742f", "metadata": { "papermill": { "duration": null, "end_time": null, "exception": null, "start_time": null, "status": "pending" }, "tags": [] }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.12" }, "papermill": { "default_parameters": {}, "duration": 11.129218, "end_time": "2022-11-11T13:49:53.270814", "environment_variables": {}, "exception": true, "input_path": "__notebook__.ipynb", "output_path": "__notebook__.ipynb", "parameters": {}, "start_time": "2022-11-11T13:49:42.141596", "version": "2.3.4" } }, "nbformat": 4, "nbformat_minor": 5 }
0110/704/110704507.ipynb
s3://data-agents/kaggle-outputs/sharded/026_00110.jsonl.gz
"{\n \"cells\": [\n {\n \"cell_type\": \"markdown\",\n \"id\": \"bb1c8756\",\n \"metadata\": (...TRUNCATED)
0110/704/110704873.ipynb
s3://data-agents/kaggle-outputs/sharded/026_00110.jsonl.gz
"{\"metadata\":{\"kernelspec\":{\"language\":\"python\",\"display_name\":\"Python 3\",\"name\":\"pyt(...TRUNCATED)
0110/704/110704935.ipynb
s3://data-agents/kaggle-outputs/sharded/026_00110.jsonl.gz
"{\"metadata\":{\"kernelspec\":{\"name\":\"python3\",\"display_name\":\"Python 3\",\"language\":\"py(...TRUNCATED)
0110/704/110704987.ipynb
s3://data-agents/kaggle-outputs/sharded/026_00110.jsonl.gz
"{\n \"cells\": [\n {\n \"cell_type\": \"markdown\",\n \"id\": \"eab3825e\",\n \"metadata\": (...TRUNCATED)
0110/705/110705276.ipynb
s3://data-agents/kaggle-outputs/sharded/026_00110.jsonl.gz
"{\n \"cells\": [\n {\n \"cell_type\": \"code\",\n \"execution_count\": 1,\n \"id\": \"460708(...TRUNCATED)
0110/705/110705353.ipynb
s3://data-agents/kaggle-outputs/sharded/026_00110.jsonl.gz
"{\"metadata\":{\"kernelspec\":{\"language\":\"python\",\"display_name\":\"Python 3\",\"name\":\"pyt(...TRUNCATED)
0110/705/110705574.ipynb
s3://data-agents/kaggle-outputs/sharded/026_00110.jsonl.gz
"{\"metadata\":{\"kernelspec\":{\"language\":\"python\",\"display_name\":\"Python 3\",\"name\":\"pyt(...TRUNCATED)
0110/705/110705674.ipynb
s3://data-agents/kaggle-outputs/sharded/026_00110.jsonl.gz
"{\n \"cells\": [\n {\n \"cell_type\": \"markdown\",\n \"id\": \"9fc0addc\",\n \"metadata\": (...TRUNCATED)
0110/705/110705792.ipynb
s3://data-agents/kaggle-outputs/sharded/026_00110.jsonl.gz
"{\n \"cells\": [\n {\n \"cell_type\": \"code\",\n \"execution_count\": 1,\n \"id\": \"d8a0bc(...TRUNCATED)
0110/705/110705803.ipynb
s3://data-agents/kaggle-outputs/sharded/026_00110.jsonl.gz
README.md exists but content is empty.
Downloads last month
5