{ "cells": [ { "cell_type": "markdown", "id": "704c8595-1f9b-4511-85d1-398a489721c6", "metadata": {}, "source": [ "### Data Collection" ] }, { "cell_type": "code", "execution_count": 100, "id": "adac7be9-df90-4e81-a49a-28056ef18737", "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 101, "id": "ce25a764-8b1c-41c5-87db-4a68d8aad590", "metadata": {}, "outputs": [], "source": [ "df1=pd.read_csv(r\"C:\\Users\\sss\\Desktop\\datas_titles.csv\",usecols=[\"id\",\"title\"])" ] }, { "cell_type": "code", "execution_count": 102, "id": "356af908-968c-45d6-86e7-5fd70154e80a", "metadata": {}, "outputs": [], "source": [ "df2=pd.read_csv(r\"C:\\Users\\sss\\Desktop\\datas_tags.csv\",usecols=[\"id\",\"tags\"])" ] }, { "cell_type": "code", "execution_count": 103, "id": "196ecc83-1274-4769-ad5d-27997a5177bd", "metadata": {}, "outputs": [], "source": [ "df3=pd.read_csv(r\"C:\\Users\\sss\\Documents\\datas_urls_questions.csv\",usecols=[\"id\",\"questions_url\"])" ] }, { "cell_type": "code", "execution_count": 104, "id": "f0b50f03-3c5b-45d2-b7d4-bc16dd1931cb", "metadata": {}, "outputs": [], "source": [ "df_merged=pd.merge(df1,df2,on=\"id\")" ] }, { "cell_type": "code", "execution_count": 105, "id": "45b40742-759a-4a5f-9c45-748a79ac67e3", "metadata": {}, "outputs": [], "source": [ "final_merged=pd.merge(df_merged,df3,on=\"id\")" ] }, { "cell_type": "code", "execution_count": 106, "id": "0b7b6646-04a2-439b-84f1-1ac34fa7c66b", "metadata": {}, "outputs": [], "source": [ "final_merged.to_csv(r\"C:\\Users\\sss\\Documents\\final_dataset.csv\")" ] }, { "cell_type": "markdown", "id": "53974e55-90bf-4aee-a54e-784481332ba1", "metadata": {}, "source": [ "### Data Cleaning" ] }, { "cell_type": "code", "execution_count": 107, "id": "bf6765e0-bea0-4491-a8c7-b29c3c00f0e2", "metadata": {}, "outputs": [], "source": [ "df=final_merged" ] }, { "cell_type": "code", "execution_count": 447, "id": "ab026a7d-8628-4f05-8217-92033609e7e3", "metadata": {}, "outputs": [], "source": [ "import nltk\n", "import numpy as np\n", "from nltk.tokenize import sent_tokenize as s, word_tokenize as w\n", "from nltk.corpus import stopwords as stp\n", "import string as st\n", "import autocorrect\n", "from autocorrect import Speller as spp\n", "from nltk.stem import WordNetLemmatizer as wl\n", "from nltk.corpus import wordnet\n", "from nltk import pos_tag\n", "from sklearn.pipeline import Pipeline\n", "from sklearn.preprocessing import FunctionTransformer,MultiLabelBinarizer\n", "from sklearn.feature_extraction.text import TfidfVectorizer\n", "import ast\n", "from sklearn.multioutput import MultiOutputClassifier\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.ensemble import BaggingClassifier\n", "from sklearn.metrics import accuracy_score,hamming_loss\n", "import joblib" ] }, { "cell_type": "code", "execution_count": 109, "id": "b582c693-c536-4560-87c3-b1a89b7e842e", "metadata": {}, "outputs": [], "source": [ "df[\"tags\"]=df[\"tags\"].apply(ast.literal_eval)" ] }, { "cell_type": "code", "execution_count": 110, "id": "4860d2f8-fa39-4b19-b1e6-b4010665851a", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[nltk_data] Downloading package wordnet to\n", "[nltk_data] C:\\Users\\sss\\AppData\\Roaming\\nltk_data...\n", "[nltk_data] Package wordnet is already up-to-date!\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nltk.download(\"wordnet\")" ] }, { "cell_type": "code", "execution_count": 111, "id": "31960d58-4eba-4a4a-a22c-8537123161bc", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[nltk_data] Downloading package punkt to\n", "[nltk_data] C:\\Users\\sss\\AppData\\Roaming\\nltk_data...\n", "[nltk_data] Package punkt is already up-to-date!\n", "[nltk_data] Downloading package stopwords to\n", "[nltk_data] C:\\Users\\sss\\AppData\\Roaming\\nltk_data...\n", "[nltk_data] Package stopwords is already up-to-date!\n", "[nltk_data] Downloading package wordnet to\n", "[nltk_data] C:\\Users\\sss\\AppData\\Roaming\\nltk_data...\n", "[nltk_data] Package wordnet is already up-to-date!\n", "[nltk_data] Downloading package averaged_perceptron_tagger to\n", "[nltk_data] C:\\Users\\sss\\AppData\\Roaming\\nltk_data...\n", "[nltk_data] Package averaged_perceptron_tagger is already up-to-\n", "[nltk_data] date!\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nltk.download('punkt')\n", "nltk.download('stopwords')\n", "nltk.download(\"wordnet\")\n", "nltk.download('averaged_perceptron_tagger')" ] }, { "cell_type": "code", "execution_count": 112, "id": "df4a80b5-d935-4653-a363-48a8f2b1fd9e", "metadata": {}, "outputs": [], "source": [ "def sahi_karneka_function(x):\n", " nouns=[]\n", " li=[]\n", " lem=wl()\n", " l=s(x) \n", " for i in l:\n", " d=w(i.lower())\n", " for k in d:\n", " li.append(k)\n", " lw=len(li)\n", " j=0\n", " while j1:\n", " rep=count-1\n", " j=0\n", " while j#sk-container-id-9 {\n", " /* Definition of color scheme common for light and dark mode */\n", " --sklearn-color-text: #000;\n", " --sklearn-color-text-muted: #666;\n", " --sklearn-color-line: gray;\n", " /* Definition of color scheme for unfitted estimators */\n", " --sklearn-color-unfitted-level-0: #fff5e6;\n", " --sklearn-color-unfitted-level-1: #f6e4d2;\n", " --sklearn-color-unfitted-level-2: #ffe0b3;\n", " --sklearn-color-unfitted-level-3: chocolate;\n", " /* Definition of color scheme for fitted estimators */\n", " --sklearn-color-fitted-level-0: #f0f8ff;\n", " --sklearn-color-fitted-level-1: #d4ebff;\n", " --sklearn-color-fitted-level-2: #b3dbfd;\n", " --sklearn-color-fitted-level-3: cornflowerblue;\n", "\n", " /* Specific color for light theme */\n", " --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n", " --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n", " --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n", " --sklearn-color-icon: #696969;\n", "\n", " @media (prefers-color-scheme: dark) {\n", " /* Redefinition of color scheme for dark theme */\n", " --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n", " --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n", " --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n", " --sklearn-color-icon: #878787;\n", " }\n", "}\n", "\n", "#sk-container-id-9 {\n", " color: var(--sklearn-color-text);\n", "}\n", "\n", "#sk-container-id-9 pre {\n", " padding: 0;\n", "}\n", "\n", "#sk-container-id-9 input.sk-hidden--visually {\n", " border: 0;\n", " clip: rect(1px 1px 1px 1px);\n", " clip: rect(1px, 1px, 1px, 1px);\n", " height: 1px;\n", " margin: -1px;\n", " overflow: hidden;\n", " padding: 0;\n", " position: absolute;\n", " width: 1px;\n", "}\n", "\n", "#sk-container-id-9 div.sk-dashed-wrapped {\n", " border: 1px dashed var(--sklearn-color-line);\n", " margin: 0 0.4em 0.5em 0.4em;\n", " box-sizing: border-box;\n", " padding-bottom: 0.4em;\n", " background-color: var(--sklearn-color-background);\n", "}\n", "\n", "#sk-container-id-9 div.sk-container {\n", " /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n", " but bootstrap.min.css set `[hidden] { display: none !important; }`\n", " so we also need the `!important` here to be able to override the\n", " default hidden behavior on the sphinx rendered scikit-learn.org.\n", " See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n", " display: inline-block !important;\n", " position: relative;\n", "}\n", "\n", "#sk-container-id-9 div.sk-text-repr-fallback {\n", " display: none;\n", "}\n", "\n", "div.sk-parallel-item,\n", "div.sk-serial,\n", "div.sk-item {\n", " /* draw centered vertical line to link estimators */\n", " background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n", " background-size: 2px 100%;\n", " background-repeat: no-repeat;\n", " background-position: center center;\n", "}\n", "\n", "/* Parallel-specific style estimator block */\n", "\n", "#sk-container-id-9 div.sk-parallel-item::after {\n", " content: \"\";\n", " width: 100%;\n", " border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n", " flex-grow: 1;\n", "}\n", "\n", "#sk-container-id-9 div.sk-parallel {\n", " display: flex;\n", " align-items: stretch;\n", " justify-content: center;\n", " background-color: var(--sklearn-color-background);\n", " position: relative;\n", "}\n", "\n", "#sk-container-id-9 div.sk-parallel-item {\n", " display: flex;\n", " flex-direction: column;\n", "}\n", "\n", "#sk-container-id-9 div.sk-parallel-item:first-child::after {\n", " align-self: flex-end;\n", " width: 50%;\n", "}\n", "\n", "#sk-container-id-9 div.sk-parallel-item:last-child::after {\n", " align-self: flex-start;\n", " width: 50%;\n", "}\n", "\n", "#sk-container-id-9 div.sk-parallel-item:only-child::after {\n", " width: 0;\n", "}\n", "\n", "/* Serial-specific style estimator block */\n", "\n", "#sk-container-id-9 div.sk-serial {\n", " display: flex;\n", " flex-direction: column;\n", " align-items: center;\n", " background-color: var(--sklearn-color-background);\n", " padding-right: 1em;\n", " padding-left: 1em;\n", "}\n", "\n", "\n", "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n", "clickable and can be expanded/collapsed.\n", "- Pipeline and ColumnTransformer use this feature and define the default style\n", "- Estimators will overwrite some part of the style using the `sk-estimator` class\n", "*/\n", "\n", "/* Pipeline and ColumnTransformer style (default) */\n", "\n", "#sk-container-id-9 div.sk-toggleable {\n", " /* Default theme specific background. It is overwritten whether we have a\n", " specific estimator or a Pipeline/ColumnTransformer */\n", " background-color: var(--sklearn-color-background);\n", "}\n", "\n", "/* Toggleable label */\n", "#sk-container-id-9 label.sk-toggleable__label {\n", " cursor: pointer;\n", " display: flex;\n", " width: 100%;\n", " margin-bottom: 0;\n", " padding: 0.5em;\n", " box-sizing: border-box;\n", " text-align: center;\n", " align-items: start;\n", " justify-content: space-between;\n", " gap: 0.5em;\n", "}\n", "\n", "#sk-container-id-9 label.sk-toggleable__label .caption {\n", " font-size: 0.6rem;\n", " font-weight: lighter;\n", " color: var(--sklearn-color-text-muted);\n", "}\n", "\n", "#sk-container-id-9 label.sk-toggleable__label-arrow:before {\n", " /* Arrow on the left of the label */\n", " content: \"▸\";\n", " float: left;\n", " margin-right: 0.25em;\n", " color: var(--sklearn-color-icon);\n", "}\n", "\n", "#sk-container-id-9 label.sk-toggleable__label-arrow:hover:before {\n", " color: var(--sklearn-color-text);\n", "}\n", "\n", "/* Toggleable content - dropdown */\n", "\n", "#sk-container-id-9 div.sk-toggleable__content {\n", " display: none;\n", " text-align: left;\n", " /* unfitted */\n", " background-color: var(--sklearn-color-unfitted-level-0);\n", "}\n", "\n", "#sk-container-id-9 div.sk-toggleable__content.fitted {\n", " /* fitted */\n", " background-color: var(--sklearn-color-fitted-level-0);\n", "}\n", "\n", "#sk-container-id-9 div.sk-toggleable__content pre {\n", " margin: 0.2em;\n", " border-radius: 0.25em;\n", " color: var(--sklearn-color-text);\n", " /* unfitted */\n", " background-color: var(--sklearn-color-unfitted-level-0);\n", "}\n", "\n", "#sk-container-id-9 div.sk-toggleable__content.fitted pre {\n", " /* unfitted */\n", " background-color: var(--sklearn-color-fitted-level-0);\n", "}\n", "\n", "#sk-container-id-9 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n", " /* Expand drop-down */\n", " display: block;\n", " width: 100%;\n", " overflow: visible;\n", "}\n", "\n", "#sk-container-id-9 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n", " content: \"▾\";\n", "}\n", "\n", "/* Pipeline/ColumnTransformer-specific style */\n", "\n", "#sk-container-id-9 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n", " color: var(--sklearn-color-text);\n", " background-color: var(--sklearn-color-unfitted-level-2);\n", "}\n", "\n", "#sk-container-id-9 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n", " background-color: var(--sklearn-color-fitted-level-2);\n", "}\n", "\n", "/* Estimator-specific style */\n", "\n", "/* Colorize estimator box */\n", "#sk-container-id-9 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n", " /* unfitted */\n", " background-color: var(--sklearn-color-unfitted-level-2);\n", "}\n", "\n", "#sk-container-id-9 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n", " /* fitted */\n", " background-color: var(--sklearn-color-fitted-level-2);\n", "}\n", "\n", "#sk-container-id-9 div.sk-label label.sk-toggleable__label,\n", "#sk-container-id-9 div.sk-label label {\n", " /* The background is the default theme color */\n", " color: var(--sklearn-color-text-on-default-background);\n", "}\n", "\n", "/* On hover, darken the color of the background */\n", "#sk-container-id-9 div.sk-label:hover label.sk-toggleable__label {\n", " color: var(--sklearn-color-text);\n", " background-color: var(--sklearn-color-unfitted-level-2);\n", "}\n", "\n", "/* Label box, darken color on hover, fitted */\n", "#sk-container-id-9 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n", " color: var(--sklearn-color-text);\n", " background-color: var(--sklearn-color-fitted-level-2);\n", "}\n", "\n", "/* Estimator label */\n", "\n", "#sk-container-id-9 div.sk-label label {\n", " font-family: monospace;\n", " font-weight: bold;\n", " display: inline-block;\n", " line-height: 1.2em;\n", "}\n", "\n", "#sk-container-id-9 div.sk-label-container {\n", " text-align: center;\n", "}\n", "\n", "/* Estimator-specific */\n", "#sk-container-id-9 div.sk-estimator {\n", " font-family: monospace;\n", " border: 1px dotted var(--sklearn-color-border-box);\n", " border-radius: 0.25em;\n", " box-sizing: border-box;\n", " margin-bottom: 0.5em;\n", " /* unfitted */\n", " background-color: var(--sklearn-color-unfitted-level-0);\n", "}\n", "\n", "#sk-container-id-9 div.sk-estimator.fitted {\n", " /* fitted */\n", " background-color: var(--sklearn-color-fitted-level-0);\n", "}\n", "\n", "/* on hover */\n", "#sk-container-id-9 div.sk-estimator:hover {\n", " /* unfitted */\n", " background-color: var(--sklearn-color-unfitted-level-2);\n", "}\n", "\n", "#sk-container-id-9 div.sk-estimator.fitted:hover {\n", " /* fitted */\n", " background-color: var(--sklearn-color-fitted-level-2);\n", "}\n", "\n", "/* Specification for estimator info (e.g. \"i\" and \"?\") */\n", "\n", "/* Common style for \"i\" and \"?\" */\n", "\n", ".sk-estimator-doc-link,\n", "a:link.sk-estimator-doc-link,\n", "a:visited.sk-estimator-doc-link {\n", " float: right;\n", " font-size: smaller;\n", " line-height: 1em;\n", " font-family: monospace;\n", " background-color: var(--sklearn-color-background);\n", " border-radius: 1em;\n", " height: 1em;\n", " width: 1em;\n", " text-decoration: none !important;\n", " margin-left: 0.5em;\n", " text-align: center;\n", " /* unfitted */\n", " border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n", " color: var(--sklearn-color-unfitted-level-1);\n", "}\n", "\n", ".sk-estimator-doc-link.fitted,\n", "a:link.sk-estimator-doc-link.fitted,\n", "a:visited.sk-estimator-doc-link.fitted {\n", " /* fitted */\n", " border: var(--sklearn-color-fitted-level-1) 1pt solid;\n", " color: var(--sklearn-color-fitted-level-1);\n", "}\n", "\n", "/* On hover */\n", "div.sk-estimator:hover .sk-estimator-doc-link:hover,\n", ".sk-estimator-doc-link:hover,\n", "div.sk-label-container:hover .sk-estimator-doc-link:hover,\n", ".sk-estimator-doc-link:hover {\n", " /* unfitted */\n", " background-color: var(--sklearn-color-unfitted-level-3);\n", " color: var(--sklearn-color-background);\n", " text-decoration: none;\n", "}\n", "\n", "div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n", ".sk-estimator-doc-link.fitted:hover,\n", "div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n", ".sk-estimator-doc-link.fitted:hover {\n", " /* fitted */\n", " background-color: var(--sklearn-color-fitted-level-3);\n", " color: var(--sklearn-color-background);\n", " text-decoration: none;\n", "}\n", "\n", "/* Span, style for the box shown on hovering the info icon */\n", ".sk-estimator-doc-link span {\n", " display: none;\n", " z-index: 9999;\n", " position: relative;\n", " font-weight: normal;\n", " right: .2ex;\n", " padding: .5ex;\n", " margin: .5ex;\n", " width: min-content;\n", " min-width: 20ex;\n", " max-width: 50ex;\n", " color: var(--sklearn-color-text);\n", " box-shadow: 2pt 2pt 4pt #999;\n", " /* unfitted */\n", " background: var(--sklearn-color-unfitted-level-0);\n", " border: .5pt solid var(--sklearn-color-unfitted-level-3);\n", "}\n", "\n", ".sk-estimator-doc-link.fitted span {\n", " /* fitted */\n", " background: var(--sklearn-color-fitted-level-0);\n", " border: var(--sklearn-color-fitted-level-3);\n", "}\n", "\n", ".sk-estimator-doc-link:hover span {\n", " display: block;\n", "}\n", "\n", "/* \"?\"-specific style due to the `` HTML tag */\n", "\n", "#sk-container-id-9 a.estimator_doc_link {\n", " float: right;\n", " font-size: 1rem;\n", " line-height: 1em;\n", " font-family: monospace;\n", " background-color: var(--sklearn-color-background);\n", " border-radius: 1rem;\n", " height: 1rem;\n", " width: 1rem;\n", " text-decoration: none;\n", " /* unfitted */\n", " color: var(--sklearn-color-unfitted-level-1);\n", " border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n", "}\n", "\n", "#sk-container-id-9 a.estimator_doc_link.fitted {\n", " /* fitted */\n", " border: var(--sklearn-color-fitted-level-1) 1pt solid;\n", " color: var(--sklearn-color-fitted-level-1);\n", "}\n", "\n", "/* On hover */\n", "#sk-container-id-9 a.estimator_doc_link:hover {\n", " /* unfitted */\n", " background-color: var(--sklearn-color-unfitted-level-3);\n", " color: var(--sklearn-color-background);\n", " text-decoration: none;\n", "}\n", "\n", "#sk-container-id-9 a.estimator_doc_link.fitted:hover {\n", " /* fitted */\n", " background-color: var(--sklearn-color-fitted-level-3);\n", "}\n", "\n", ".estimator-table summary {\n", " padding: .5rem;\n", " font-family: monospace;\n", " cursor: pointer;\n", "}\n", "\n", ".estimator-table details[open] {\n", " padding-left: 0.1rem;\n", " padding-right: 0.1rem;\n", " padding-bottom: 0.3rem;\n", "}\n", "\n", ".estimator-table .parameters-table {\n", " margin-left: auto !important;\n", " margin-right: auto !important;\n", "}\n", "\n", ".estimator-table .parameters-table tr:nth-child(odd) {\n", " background-color: #fff;\n", "}\n", "\n", ".estimator-table .parameters-table tr:nth-child(even) {\n", " background-color: #f6f6f6;\n", "}\n", "\n", ".estimator-table .parameters-table tr:hover {\n", " background-color: #e0e0e0;\n", "}\n", "\n", ".estimator-table table td {\n", " border: 1px solid rgba(106, 105, 104, 0.232);\n", "}\n", "\n", ".user-set td {\n", " color:rgb(255, 94, 0);\n", " text-align: left;\n", "}\n", "\n", ".user-set td.value pre {\n", " color:rgb(255, 94, 0) !important;\n", " background-color: transparent !important;\n", "}\n", "\n", ".default td {\n", " color: black;\n", " text-align: left;\n", "}\n", "\n", ".user-set td i,\n", ".default td i {\n", " color: black;\n", "}\n", "\n", ".copy-paste-icon {\n", " background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0NDggNTEyIj48IS0tIUZvbnQgQXdlc29tZSBGcmVlIDYuNy4yIGJ5IEBmb250YXdlc29tZSAtIGh0dHBzOi8vZm9udGF3ZXNvbWUuY29tIExpY2Vuc2UgLSBodHRwczovL2ZvbnRhd2Vzb21lLmNvbS9saWNlbnNlL2ZyZWUgQ29weXJpZ2h0IDIwMjUgRm9udGljb25zLCBJbmMuLS0+PHBhdGggZD0iTTIwOCAwTDMzMi4xIDBjMTIuNyAwIDI0LjkgNS4xIDMzLjkgMTQuMWw2Ny45IDY3LjljOSA5IDE0LjEgMjEuMiAxNC4xIDMzLjlMNDQ4IDMzNmMwIDI2LjUtMjEuNSA0OC00OCA0OGwtMTkyIDBjLTI2LjUgMC00OC0yMS41LTQ4LTQ4bDAtMjg4YzAtMjYuNSAyMS41LTQ4IDQ4LTQ4ek00OCAxMjhsODAgMCAwIDY0LTY0IDAgMCAyNTYgMTkyIDAgMC0zMiA2NCAwIDAgNDhjMCAyNi41LTIxLjUgNDgtNDggNDhMNDggNTEyYy0yNi41IDAtNDgtMjEuNS00OC00OEwwIDE3NmMwLTI2LjUgMjEuNS00OCA0OC00OHoiLz48L3N2Zz4=);\n", " background-repeat: no-repeat;\n", " background-size: 14px 14px;\n", " background-position: 0;\n", " display: inline-block;\n", " width: 14px;\n", " height: 14px;\n", " cursor: pointer;\n", "}\n", "
MultiOutputClassifier(estimator=LogisticRegression(class_weight='balanced',\n",
       "                                                   max_iter=1000))
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" ], "text/plain": [ "MultiOutputClassifier(estimator=LogisticRegression(class_weight='balanced',\n", " max_iter=1000))" ] }, "execution_count": 375, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.fit(x_train_tfidf,y_train_encoded)" ] }, { "cell_type": "markdown", "id": "266f92ec-f07f-42f3-a4fb-4ed84f59f453", "metadata": {}, "source": [ "### default threshold of probability of label greater then 0.5 is predicted by logistic regression" ] }, { "cell_type": "code", "execution_count": 376, "id": "e22f91a1-8e4c-454f-b43e-c2b9067749ff", "metadata": {}, "outputs": [], "source": [ "y_pred_default_prob=model.predict(x_test_tfidf)" ] }, { "cell_type": "code", "execution_count": 377, "id": "4f51fa10-8b99-4473-9655-fb81b1c71b65", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('dataframe', 'pandas', 'python', 'r', 'spring-boot-2')]" ] }, "execution_count": 377, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ml.inverse_transform(y_pred_default_prob)" ] }, { "cell_type": "code", "execution_count": 442, "id": "6a92a35c-a678-41c3-bc67-9cbf4f56906f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('r',)]" ] }, "execution_count": 442, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ml.inverse_transform(y_encoded_test)" ] }, { "cell_type": "code", "execution_count": 378, "id": "fe4ec6d6-17b6-4f4a-aab3-8a3fa7d86136", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hamming Loss: 0.0009823182711198428\n", "Accuracy Score: 0.0\n" ] } ], "source": [ "print(\"Hamming Loss:\",hamming_loss(y_encoded_test,y_pred_default_prob))\n", "print(\"Accuracy Score:\",accuracy_score(y_encoded_test,y_pred_default_prob))" ] }, { "cell_type": "code", "execution_count": 379, "id": "c114e272-e3a1-43cd-b638-025fc0286b46", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "F1 Score (micro): 0.3333333333333333\n", "F1 Score (macro): 0.0002455795677799607\n" ] } ], "source": [ "print(\"F1 Score (micro):\", f1_score(y_encoded_test,y_pred_default_prob, average='micro',zero_division=0))\n", "print(\"F1 Score (macro):\", f1_score(y_encoded_test,y_pred_default_prob, average='macro',zero_division=0))" ] }, { "cell_type": "markdown", "id": "86956b7a-f9ce-402a-9d5f-d1e91d54eb81", "metadata": {}, "source": [ "### I want to predict the label whose probability is greater the 0.75 so i filtered by threshold after getting all probabilities of labels" ] }, { "cell_type": "code", "execution_count": 380, "id": "15ada427-5afa-4638-a938-ab35ab3a9d1b", "metadata": {}, "outputs": [], "source": [ "y_probs=model.predict_proba(x_test_tfidf)" ] }, { "cell_type": "code", "execution_count": 381, "id": "afe9180d-65e7-414a-bf39-32260c205724", "metadata": {}, "outputs": [], "source": [ "threshold=0.75\n", "probs_column1=np.array([i[0:,1] for i in y_probs]).T\n", "y_pred_customized_prob=(probs_column1>threshold).astype(int)" ] }, { "cell_type": "code", "execution_count": 441, "id": "14f950ff-2f62-4bfa-bd2a-c9e8ec8993b7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('r',)]" ] }, "execution_count": 441, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ml.inverse_transform(y_pred_customized_prob)" ] }, { "cell_type": "code", "execution_count": 444, "id": "5cd57d27-55db-41a9-a770-a9a5fd4fbf64", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('r',)]" ] }, "execution_count": 444, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ml.inverse_transform(y_encoded_test)" ] }, { "cell_type": "code", "execution_count": 382, "id": "c87755ac-4682-44fa-8bae-2acfb550d867", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hamming Loss: 0.0\n", "Accuracy Score: 1.0\n" ] } ], "source": [ "print(\"Hamming Loss:\",hamming_loss(y_encoded_test,y_pred_customized_prob))\n", "print(\"Accuracy Score:\",accuracy_score(y_encoded_test,y_pred_customized_prob))" ] }, { "cell_type": "code", "execution_count": 383, "id": "12a25f36-3cf7-457d-9c4e-124a2d9e6447", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "F1 Score (micro): 1.0\n", "F1 Score (macro): 0.0002455795677799607\n" ] } ], "source": [ "print(\"F1 Score (micro):\", f1_score(y_encoded_test,y_pred_customized_prob, average='micro',zero_division=0))\n", "print(\"F1 Score (macro):\", f1_score(y_encoded_test,y_pred_customized_prob, average='macro',zero_division=0))" ] }, { "cell_type": "markdown", "id": "9b125ddb-0691-418a-970f-73660b751a24", "metadata": {}, "source": [ "### ONLY WHEN USE IN STREAMLIT FOR QUESTION body as x_test and predicting tag" ] }, { "cell_type": "markdown", "id": "3d767df0-f1ff-4210-80c0-e310a4247e64", "metadata": {}, "source": [ "## Testing question body as x_test" ] }, { "cell_type": "code", "execution_count": 392, "id": "cf5fb6b7-02e5-4abb-9b74-9c63acada17b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 https://stackoverflow.com//questions/79689975/...\n", "Name: questions_url, dtype: object" ] }, "execution_count": 392, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[\"questions_url\"][df[\"id\"]==79689975][0:]" ] }, { "cell_type": "code", "execution_count": 415, "id": "4d4d8e85-f9bf-448b-a9f0-93d6eb8143b8", "metadata": {}, "outputs": [], "source": [ "y_testing=df[\"tags\"][df[\"id\"]==79689975]\n", "y_testing_encoded=m.transform(y_testing)" ] }, { "cell_type": "code", "execution_count": 435, "id": "cb91ca42-36f7-48bd-8da0-09764bccfded", "metadata": {}, "outputs": [], "source": [ "xtest_question_from_url='''I want to retrieve hierarchical aggregates of a dataframe, i. e. aggregating the data by an increasing number of grouping variables.'''\n", "question=[]\n", "final_xtest=sahi_karneka_function(xtest_question_from_url)\n", "question.append(final_xtest)\n", "tfidf_xtest_pipeline_question=tfidf_st.transform(question)" ] }, { "cell_type": "code", "execution_count": 436, "id": "bc6fff47-cd90-42cd-b366-e78ba78533c2", "metadata": {}, "outputs": [], "source": [ "y_pred=model.predict(tfidf_xtest_pipeline_question)" ] }, { "cell_type": "code", "execution_count": 438, "id": "c4b57c8e-e524-49b2-85e9-f1b16c2349ea", "metadata": {}, "outputs": [], "source": [ "y_probs=model.predict_proba(tfidf_xtest_pipeline_question)" ] }, { "cell_type": "code", "execution_count": 439, "id": "e2bccb82-69a6-4da3-8bd6-efa41c18e36b", "metadata": {}, "outputs": [], "source": [ "threshold=0.75\n", "probs_column1=np.array([i[0:,1] for i in y_probs]).T\n", "y_pred_customized_prob=(probs_column1>threshold).astype(int)" ] }, { "cell_type": "code", "execution_count": 440, "id": "e40814c4-2db1-47ff-8dea-4544f45bcae1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hamming Loss: 0.0\n", "Accuracy Score: 1.0\n" ] } ], "source": [ "print(\"Hamming Loss:\",hamming_loss(y_testing_encoded,y_pred_customized_prob))\n", "print(\"Accuracy Score:\",accuracy_score(y_testing_encoded,y_pred_customized_prob))" ] }, { "cell_type": "code", "execution_count": 446, "id": "10c08944-83e9-4f81-a7da-03e9bee517d6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('r',)]" ] }, "execution_count": 446, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ml.inverse_transform(y_pred_customized_prob)" ] }, { "cell_type": "code", "execution_count": 445, "id": "6802d5f4-e285-426f-87e2-c0341227e971", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[('r',)]" ] }, "execution_count": 445, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ml.inverse_transform(y_testing_encoded)" ] }, { "cell_type": "markdown", "id": "a69c48ab-a178-4231-8b3e-6cec68b6257a", "metadata": {}, "source": [ "### ------------------------Finish training code and streamlit testing---------------The end--------------------" ] }, { "cell_type": "code", "execution_count": 448, "id": "8acac052-cdb3-4b6c-9ce8-c6c6bf88c613", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['logistic_model.pkl']" ] }, "execution_count": 448, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joblib.dump(model,\"logistic_model.pkl\")" ] }, { "cell_type": "code", "execution_count": null, "id": "572472b0-5a36-42ce-b099-83d749ff108a", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.12.7" } }, "nbformat": 4, "nbformat_minor": 5 }