{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "7fca8158", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "print(\"hello\")" ] }, { "cell_type": "code", "execution_count": 18, "id": "b24cb367", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn.feature_extraction.text import TfidfVectorizer\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.naive_bayes import MultinomialNB\n", "from sklearn.pipeline import Pipeline\n", "from sklearn.model_selection import train_test_split\n", "import joblib\n", "from utils import mask_pii" ] }, { "cell_type": "code", "execution_count": null, "id": "f51ac2e6", "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv(\"dataset.csv\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "bc084a0b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
emailtype
0Subject: Unvorhergesehener Absturz der Datenan...Incident
1Subject: Customer Support Inquiry\\n\\nSeeking i...Request
2Subject: Data Analytics for Investment\\n\\nI am...Request
3Subject: Krankenhaus-Dienstleistung-Problem\\n\\...Incident
4Subject: Security\\n\\nDear Customer Support, I ...Request
.........
23995Subject: Problem mit der HP DeskJet 3755 WLAN-...Problem
23996Subject: Problemas com a impressora HP DeskJet...Incident
23997Subject: Problema urgente con el envío\\n\\nEsti...Incident
23998Subject: \\n\\nCher Service Client, nous rencont...Incident
23999Subject: Overcharge on yearly Microsoft Office...Problem
\n", "

24000 rows × 2 columns

\n", "
" ], "text/plain": [ " email type\n", "0 Subject: Unvorhergesehener Absturz der Datenan... Incident\n", "1 Subject: Customer Support Inquiry\\n\\nSeeking i... Request\n", "2 Subject: Data Analytics for Investment\\n\\nI am... Request\n", "3 Subject: Krankenhaus-Dienstleistung-Problem\\n\\... Incident\n", "4 Subject: Security\\n\\nDear Customer Support, I ... Request\n", "... ... ...\n", "23995 Subject: Problem mit der HP DeskJet 3755 WLAN-... Problem\n", "23996 Subject: Problemas com a impressora HP DeskJet... Incident\n", "23997 Subject: Problema urgente con el envío\\n\\nEsti... Incident\n", "23998 Subject: \\n\\nCher Service Client, nous rencont... Incident\n", "23999 Subject: Overcharge on yearly Microsoft Office... Problem\n", "\n", "[24000 rows x 2 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "code", "execution_count": 5, "id": "930212ec", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Subject: Unvorhergesehener Absturz der Datenanalyse-Plattform\\n\\nDie Datenanalyse-Plattform brach unerwartet ab, da die Speicheroberfläche zu gering war My name is Sophia Rossi.. Ich habe versucht, Laravel 8 und meinen MacBook Pro neu zu starten, aber das Problem behält sich bei. Ich benötige Ihre Unterstützung, um diesen Fehler zu beheben. You can reach me at janesmith@company.com.'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['email'][0]" ] }, { "cell_type": "code", "execution_count": 6, "id": "9a9a7167", "metadata": {}, "outputs": [], "source": [ "masked_texts = []\n", "for text in df['email']:\n", " masked, _ = mask_pii(text)\n", " masked_texts.append(masked)\n", "\n", "df['masked_body'] = masked_texts\n", "# This code take approx 4 to 7 min to run(that is mask every sensitive info in the dataset)" ] }, { "cell_type": "code", "execution_count": 9, "id": "7cf0fd9d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
typemasked_body
0IncidentSubject: Unvorhergesehener Absturz der Datenan...
1RequestSubject: Customer Support Inquiry\\n\\nSeeking i...
2RequestSubject: Data Analytics for Investment\\n\\nI am...
3IncidentSubject: Krankenhaus-Dienstleistung-Problem\\n\\...
4RequestSubject: Security\\n\\nDear Customer Support, I ...
.........
23995ProblemSubject: Problem mit der HP DeskJet 3755 WLAN-...
23996IncidentSubject: Problemas com a impressora HP DeskJet...
23997IncidentSubject: Problema urgente con el envío\\n\\nEsti...
23998IncidentSubject: \\n\\nCher Service Client, nous rencont...
23999ProblemSubject: [full_name] on yearly Microsoft Offic...
\n", "

24000 rows × 2 columns

\n", "
" ], "text/plain": [ " type masked_body\n", "0 Incident Subject: Unvorhergesehener Absturz der Datenan...\n", "1 Request Subject: Customer Support Inquiry\\n\\nSeeking i...\n", "2 Request Subject: Data Analytics for Investment\\n\\nI am...\n", "3 Incident Subject: Krankenhaus-Dienstleistung-Problem\\n\\...\n", "4 Request Subject: Security\\n\\nDear Customer Support, I ...\n", "... ... ...\n", "23995 Problem Subject: Problem mit der HP DeskJet 3755 WLAN-...\n", "23996 Incident Subject: Problemas com a impressora HP DeskJet...\n", "23997 Incident Subject: Problema urgente con el envío\\n\\nEsti...\n", "23998 Incident Subject: \\n\\nCher Service Client, nous rencont...\n", "23999 Problem Subject: [full_name] on yearly Microsoft Offic...\n", "\n", "[24000 rows x 2 columns]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.drop(columns=['email'])" ] }, { "cell_type": "code", "execution_count": 10, "id": "66e94d2a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Subject: Unvorhergesehener Absturz der Datenanalyse-Plattform\\n\\nDie Datenanalyse-Plattform brach unerwartet ab, da die Speicheroberfläche zu gering war My name is [full_name].. Ich habe versucht, Laravel 8 und meinen M[full_name]neu zu starten, aber das Problem behält sich bei. Ich benötige I[full_name] um diesen Fehler z[full_name]ben. You can reach me at [email].'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['masked_body'][0]" ] }, { "cell_type": "code", "execution_count": 12, "id": "02f957e9", "metadata": {}, "outputs": [], "source": [ "X_train, X_test, y_train, y_test = train_test_split(\n", " df['masked_body'], df['type'], test_size=0.2, random_state=42\n", ")" ] }, { "cell_type": "code", "execution_count": 19, "id": "1dc9d917", "metadata": {}, "outputs": [], "source": [ "# pipeline = Pipeline([\n", "# ('tfidf', TfidfVectorizer(max_features=5000, ngram_range=(1,2))),\n", "# ('clf', LogisticRegression(max_iter=1000)),\n", "# ])\n", "pipeline = Pipeline([\n", " ('tfidf', TfidfVectorizer(max_features=5000, ngram_range=(1,2))),\n", " ('clf', MultinomialNB()),\n", "])" ] }, { "cell_type": "code", "execution_count": 20, "id": "9c811f7a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Training accuracy: 0.7411979166666667\n", "Validation accuracy: 0.7235416666666666\n" ] } ], "source": [ "pipeline.fit(X_train, y_train)\n", "print(\"Training accuracy:\", pipeline.score(X_train, y_train))\n", "print(\"Validation accuracy:\", pipeline.score(X_test, y_test))" ] }, { "cell_type": "code", "execution_count": 21, "id": "af539dd5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['model.joblib']" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joblib.dump(pipeline, 'model.joblib')" ] } ], "metadata": { "kernelspec": { "display_name": "venv", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }