{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":19018,"databundleVersionId":2703900,"sourceType":"competition"}],"dockerImageVersionId":30761,"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\nimport shap\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import confusion_matrix,classification_report\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-30T16:06:00.021243Z","iopub.execute_input":"2024-08-30T16:06:00.021781Z","iopub.status.idle":"2024-08-30T16:06:00.044314Z","shell.execute_reply.started":"2024-08-30T16:06:00.021733Z","shell.execute_reply":"2024-08-30T16:06:00.043015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install interpret","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:04:56.971955Z","iopub.execute_input":"2024-08-30T15:04:56.972580Z","iopub.status.idle":"2024-08-30T15:05:27.194012Z","shell.execute_reply.started":"2024-08-30T15:04:56.972537Z","shell.execute_reply":"2024-08-30T15:05:27.192488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/jigsaw-multilingual-toxic-comment-classification/jigsaw-unintended-bias-train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:28:20.291595Z","iopub.execute_input":"2024-08-30T15:28:20.293440Z","iopub.status.idle":"2024-08-30T15:28:42.184128Z","shell.execute_reply.started":"2024-08-30T15:28:20.293360Z","shell.execute_reply":"2024-08-30T15:28:42.182957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = df.toxic >= 0.50\nsum(mask),(sum(mask)/len(mask))*100","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:40:33.857530Z","iopub.execute_input":"2024-08-30T15:40:33.858022Z","iopub.status.idle":"2024-08-30T15:40:34.368693Z","shell.execute_reply.started":"2024-08-30T15:40:33.857979Z","shell.execute_reply":"2024-08-30T15:40:34.367507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"toxic = df.loc[mask,[\"comment_text\",\"toxic\"]].sample(10000,random_state=42)\nclean = df.loc[~mask,[\"comment_text\",\"toxic\"]].sample(10000,random_state=42)\ndata = pd.concat([toxic,clean])","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:43:23.254432Z","iopub.execute_input":"2024-08-30T15:43:23.254934Z","iopub.status.idle":"2024-08-30T15:43:23.662433Z","shell.execute_reply.started":"2024-08-30T15:43:23.254890Z","shell.execute_reply":"2024-08-30T15:43:23.661138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask = data.toxic >= 0.50\ndata.loc[mask,\"toxic\"] = 1\ndata.loc[~mask,\"toxic\"] = 0\nmask = data.toxic == 1\nsum(mask),(sum(mask)/len(mask))*100","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:46:46.528407Z","iopub.execute_input":"2024-08-30T15:46:46.528915Z","iopub.status.idle":"2024-08-30T15:46:46.546274Z","shell.execute_reply.started":"2024-08-30T15:46:46.528870Z","shell.execute_reply":"2024-08-30T15:46:46.545137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data['comment_text']\ny = data['toxic']","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:46:51.023721Z","iopub.execute_input":"2024-08-30T15:46:51.024621Z","iopub.status.idle":"2024-08-30T15:46:51.030201Z","shell.execute_reply.started":"2024-08-30T15:46:51.024571Z","shell.execute_reply":"2024-08-30T15:46:51.028821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head(1)","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:58:57.799776Z","iopub.execute_input":"2024-08-30T15:58:57.800224Z","iopub.status.idle":"2024-08-30T15:58:57.810429Z","shell.execute_reply.started":"2024-08-30T15:58:57.800187Z","shell.execute_reply":"2024-08-30T15:58:57.808988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer = TfidfVectorizer(stop_words='english')\nX_vectorized = vectorizer.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:46:52.434779Z","iopub.execute_input":"2024-08-30T15:46:52.435227Z","iopub.status.idle":"2024-08-30T15:46:53.784456Z","shell.execute_reply.started":"2024-08-30T15:46:52.435187Z","shell.execute_reply":"2024-08-30T15:46:53.783436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inv_str = vectorizer.inverse_transform(X_vectorized)","metadata":{"execution":{"iopub.status.busy":"2024-08-30T16:10:56.216850Z","iopub.execute_input":"2024-08-30T16:10:56.217344Z","iopub.status.idle":"2024-08-30T16:11:03.089000Z","shell.execute_reply.started":"2024-08-30T16:10:56.217302Z","shell.execute_reply":"2024-08-30T16:11:03.087894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2024-08-30T16:13:01.716824Z","iopub.execute_input":"2024-08-30T16:13:01.717281Z","iopub.status.idle":"2024-08-30T16:13:01.727039Z","shell.execute_reply.started":"2024-08-30T16:13:01.717239Z","shell.execute_reply":"2024-08-30T16:13:01.725802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inv_str","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(vectorizer.vocabulary_)","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:49:30.046408Z","iopub.execute_input":"2024-08-30T15:49:30.046912Z","iopub.status.idle":"2024-08-30T15:49:30.057360Z","shell.execute_reply.started":"2024-08-30T15:49:30.046864Z","shell.execute_reply":"2024-08-30T15:49:30.056191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X_vectorized, y, test_size=0.2, random_state=42)\n_, X_test_str, _, y_test_str = train_test_split(X, y, test_size=0.2, random_state=42)\nmodel = LogisticRegression()\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-30T16:00:58.485002Z","iopub.execute_input":"2024-08-30T16:00:58.485536Z","iopub.status.idle":"2024-08-30T16:01:00.536797Z","shell.execute_reply.started":"2024-08-30T16:00:58.485488Z","shell.execute_reply":"2024-08-30T16:01:00.535654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"explainer = shap.LinearExplainer(model, X_train, feature_perturbation=\"interventional\")\nshap_values = explainer.shap_values(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:47:05.635796Z","iopub.execute_input":"2024-08-30T15:47:05.636247Z","iopub.status.idle":"2024-08-30T15:47:07.911196Z","shell.execute_reply.started":"2024-08-30T15:47:05.636205Z","shell.execute_reply":"2024-08-30T15:47:07.910142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shap_values.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:48:30.701876Z","iopub.execute_input":"2024-08-30T15:48:30.702344Z","iopub.status.idle":"2024-08-30T15:48:30.711178Z","shell.execute_reply.started":"2024-08-30T15:48:30.702304Z","shell.execute_reply":"2024-08-30T15:48:30.709869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shap_values[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:50:16.474799Z","iopub.execute_input":"2024-08-30T15:50:16.475239Z","iopub.status.idle":"2024-08-30T15:50:16.483178Z","shell.execute_reply.started":"2024-08-30T15:50:16.475199Z","shell.execute_reply":"2024-08-30T15:50:16.481748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shap.initjs()\nshap.force_plot(explainer.expected_value, shap_values[1], feature_names=vectorizer.get_feature_names_out())\n","metadata":{"execution":{"iopub.status.busy":"2024-08-30T16:15:45.041049Z","iopub.execute_input":"2024-08-30T16:15:45.041595Z","iopub.status.idle":"2024-08-30T16:15:45.208132Z","shell.execute_reply.started":"2024-08-30T16:15:45.041550Z","shell.execute_reply":"2024-08-30T16:15:45.206855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shap.decision_plot(explainer.expected_value, shap_values[1], feature_names=vectorizer.get_feature_names_out())","metadata":{"execution":{"iopub.status.busy":"2024-08-30T16:09:27.489116Z","iopub.execute_input":"2024-08-30T16:09:27.489993Z","iopub.status.idle":"2024-08-30T16:09:28.094256Z","shell.execute_reply.started":"2024-08-30T16:09:27.489936Z","shell.execute_reply":"2024-08-30T16:09:28.093160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_str.head(2).values,y_test_str.head(2).values","metadata":{"execution":{"iopub.status.busy":"2024-08-30T16:04:12.467707Z","iopub.execute_input":"2024-08-30T16:04:12.468184Z","iopub.status.idle":"2024-08-30T16:04:12.477015Z","shell.execute_reply.started":"2024-08-30T16:04:12.468139Z","shell.execute_reply":"2024-08-30T16:04:12.475810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict(X_test[1,:])","metadata":{"execution":{"iopub.status.busy":"2024-08-30T16:04:32.538098Z","iopub.execute_input":"2024-08-30T16:04:32.538567Z","iopub.status.idle":"2024-08-30T16:04:32.547495Z","shell.execute_reply.started":"2024-08-30T16:04:32.538522Z","shell.execute_reply":"2024-08-30T16:04:32.546178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(X_test)\nprint(confusion_matrix(y_test,pred))\nprint(classification_report(y_test,pred))","metadata":{"execution":{"iopub.status.busy":"2024-08-30T16:06:39.448336Z","iopub.execute_input":"2024-08-30T16:06:39.449445Z","iopub.status.idle":"2024-08-30T16:06:39.481196Z","shell.execute_reply.started":"2024-08-30T16:06:39.449352Z","shell.execute_reply":"2024-08-30T16:06:39.480134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shap.summary_plot(shap_values, X_test, feature_names=vectorizer.get_feature_names_out())","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:51:01.589392Z","iopub.execute_input":"2024-08-30T15:51:01.590892Z","iopub.status.idle":"2024-08-30T15:51:03.660104Z","shell.execute_reply.started":"2024-08-30T15:51:01.590835Z","shell.execute_reply":"2024-08-30T15:51:03.658856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(vectorizer.get_feature_names_out())","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:51:21.049018Z","iopub.execute_input":"2024-08-30T15:51:21.050092Z","iopub.status.idle":"2024-08-30T15:51:21.092743Z","shell.execute_reply.started":"2024-08-30T15:51:21.050045Z","shell.execute_reply":"2024-08-30T15:51:21.091266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer.vocabulary_[\"stupid\"]","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:55:40.845448Z","iopub.execute_input":"2024-08-30T15:55:40.846515Z","iopub.status.idle":"2024-08-30T15:55:40.853867Z","shell.execute_reply.started":"2024-08-30T15:55:40.846464Z","shell.execute_reply":"2024-08-30T15:55:40.852682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer.get_feature_names_out()[31402]","metadata":{"execution":{"iopub.status.busy":"2024-08-30T15:55:54.504703Z","iopub.execute_input":"2024-08-30T15:55:54.505130Z","iopub.status.idle":"2024-08-30T15:55:54.552628Z","shell.execute_reply.started":"2024-08-30T15:55:54.505092Z","shell.execute_reply":"2024-08-30T15:55:54.551465Z"},"trusted":true},"execution_count":null,"outputs":[]}]}