{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "8cf922fd",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import warnings \n",
"warnings.filterwarnings('ignore')"
]
},
{
"cell_type": "markdown",
"id": "6c53d9c9",
"metadata": {},
"source": [
"# 2021 methods"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "043ac410",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Id_Number | \n",
" Patient Question | \n",
" Distorted part | \n",
" Dominant Distortion | \n",
" Secondary Distortion (Optional) | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 4500 | \n",
" Hello, I have a beautiful,smart,outgoing and a... | \n",
" The voice are always fimilar (someone she know... | \n",
" Personalization | \n",
" NaN | \n",
"
\n",
" \n",
" | 1 | \n",
" 4501 | \n",
" Since I was about 16 years old I’ve had these ... | \n",
" I feel trapped inside my disgusting self and l... | \n",
" Labeling | \n",
" Emotional Reasoning | \n",
"
\n",
" \n",
" | 2 | \n",
" 4502 | \n",
" So I’ve been dating on and off this guy for a... | \n",
" NaN | \n",
" No Distortion | \n",
" NaN | \n",
"
\n",
" \n",
" | 3 | \n",
" 4503 | \n",
" My parents got divorced in 2004. My mother has... | \n",
" NaN | \n",
" No Distortion | \n",
" NaN | \n",
"
\n",
" \n",
" | 4 | \n",
" 4504 | \n",
" I don’t really know how to explain the situati... | \n",
" I refused to go because I didn’t know if it wa... | \n",
" Fortune-telling | \n",
" Emotional Reasoning | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Id_Number Patient Question \\\n",
"0 4500 Hello, I have a beautiful,smart,outgoing and a... \n",
"1 4501 Since I was about 16 years old I’ve had these ... \n",
"2 4502 So I’ve been dating on and off this guy for a... \n",
"3 4503 My parents got divorced in 2004. My mother has... \n",
"4 4504 I don’t really know how to explain the situati... \n",
"\n",
" Distorted part Dominant Distortion \\\n",
"0 The voice are always fimilar (someone she know... Personalization \n",
"1 I feel trapped inside my disgusting self and l... Labeling \n",
"2 NaN No Distortion \n",
"3 NaN No Distortion \n",
"4 I refused to go because I didn’t know if it wa... Fortune-telling \n",
"\n",
" Secondary Distortion (Optional) \n",
"0 NaN \n",
"1 Emotional Reasoning \n",
"2 NaN \n",
"3 NaN \n",
"4 Emotional Reasoning "
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_2021 = pd.read_csv('data/distortion.csv')\n",
"df_2021.head()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7dac60fb",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Id_Number | \n",
" Patient Question | \n",
" Distorted part | \n",
" Dominant Distortion | \n",
" Secondary Distortion (Optional) | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 4500 | \n",
" Hello, I have a beautiful,smart,outgoing and a... | \n",
" The voice are always fimilar (someone she know... | \n",
" Personalization | \n",
" NaN | \n",
"
\n",
" \n",
" | 1 | \n",
" 4501 | \n",
" Since I was about 16 years old I’ve had these ... | \n",
" I feel trapped inside my disgusting self and l... | \n",
" Labeling | \n",
" Emotional Reasoning | \n",
"
\n",
" \n",
" | 4 | \n",
" 4504 | \n",
" I don’t really know how to explain the situati... | \n",
" I refused to go because I didn’t know if it wa... | \n",
" Fortune-telling | \n",
" Emotional Reasoning | \n",
"
\n",
" \n",
" | 9 | \n",
" 4510 | \n",
" I have been with my fiancé for two years now.... | \n",
" I felt like the response was totally irrationa... | \n",
" Magnification | \n",
" NaN | \n",
"
\n",
" \n",
" | 10 | \n",
" 4511 | \n",
" My husband and I have been married for over a ... | \n",
" I thought that he displayed traits of honor, l... | \n",
" Labeling | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Id_Number Patient Question \\\n",
"0 4500 Hello, I have a beautiful,smart,outgoing and a... \n",
"1 4501 Since I was about 16 years old I’ve had these ... \n",
"4 4504 I don’t really know how to explain the situati... \n",
"9 4510 I have been with my fiancé for two years now.... \n",
"10 4511 My husband and I have been married for over a ... \n",
"\n",
" Distorted part Dominant Distortion \\\n",
"0 The voice are always fimilar (someone she know... Personalization \n",
"1 I feel trapped inside my disgusting self and l... Labeling \n",
"4 I refused to go because I didn’t know if it wa... Fortune-telling \n",
"9 I felt like the response was totally irrationa... Magnification \n",
"10 I thought that he displayed traits of honor, l... Labeling \n",
"\n",
" Secondary Distortion (Optional) \n",
"0 NaN \n",
"1 Emotional Reasoning \n",
"4 Emotional Reasoning \n",
"9 NaN \n",
"10 NaN "
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_2021 = df_2021[df_2021['Dominant Distortion'] != 'No Distortion']\n",
"df_2021.head()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c75ed93d",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[nltk_data] Downloading package punkt to\n",
"[nltk_data] C:\\Users\\Admin\\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\\Admin\\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\\Admin\\AppData\\Roaming\\nltk_data...\n",
"[nltk_data] Package wordnet is already up-to-date!\n"
]
}
],
"source": [
"import re\n",
"import string\n",
"import nltk\n",
"from nltk.tokenize import word_tokenize\n",
"from nltk.corpus import stopwords\n",
"from nltk.stem import PorterStemmer, WordNetLemmatizer\n",
"\n",
"nltk.download(\"punkt\")\n",
"nltk.download(\"stopwords\")\n",
"nltk.download(\"wordnet\")\n",
"\n",
"stemmer = PorterStemmer()\n",
"lemmatizer = WordNetLemmatizer()\n",
"stop_words = set(stopwords.words(\"english\"))\n",
"\n",
"def preprocess_text(text, use_stemming=False, use_lemmatization=True, correct_spelling=False):\n",
" text = text.lower()\n",
" text = text.translate(str.maketrans(\"\", \"\", string.punctuation))\n",
" return text"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c1943d1a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Id_Number | \n",
" Patient Question | \n",
" Distorted part | \n",
" Dominant Distortion | \n",
" Secondary Distortion (Optional) | \n",
" cleaned | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 4500 | \n",
" Hello, I have a beautiful,smart,outgoing and a... | \n",
" The voice are always fimilar (someone she know... | \n",
" Personalization | \n",
" NaN | \n",
" hello i have a beautifulsmartoutgoing and amaz... | \n",
"
\n",
" \n",
" | 1 | \n",
" 4501 | \n",
" Since I was about 16 years old I’ve had these ... | \n",
" I feel trapped inside my disgusting self and l... | \n",
" Labeling | \n",
" Emotional Reasoning | \n",
" since i was about 16 years old i’ve had these ... | \n",
"
\n",
" \n",
" | 4 | \n",
" 4504 | \n",
" I don’t really know how to explain the situati... | \n",
" I refused to go because I didn’t know if it wa... | \n",
" Fortune-telling | \n",
" Emotional Reasoning | \n",
" i don’t really know how to explain the situati... | \n",
"
\n",
" \n",
" | 9 | \n",
" 4510 | \n",
" I have been with my fiancé for two years now.... | \n",
" I felt like the response was totally irrationa... | \n",
" Magnification | \n",
" NaN | \n",
" i have been with my fiancé for two years now ... | \n",
"
\n",
" \n",
" | 10 | \n",
" 4511 | \n",
" My husband and I have been married for over a ... | \n",
" I thought that he displayed traits of honor, l... | \n",
" Labeling | \n",
" NaN | \n",
" my husband and i have been married for over a ... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Id_Number Patient Question \\\n",
"0 4500 Hello, I have a beautiful,smart,outgoing and a... \n",
"1 4501 Since I was about 16 years old I’ve had these ... \n",
"4 4504 I don’t really know how to explain the situati... \n",
"9 4510 I have been with my fiancé for two years now.... \n",
"10 4511 My husband and I have been married for over a ... \n",
"\n",
" Distorted part Dominant Distortion \\\n",
"0 The voice are always fimilar (someone she know... Personalization \n",
"1 I feel trapped inside my disgusting self and l... Labeling \n",
"4 I refused to go because I didn’t know if it wa... Fortune-telling \n",
"9 I felt like the response was totally irrationa... Magnification \n",
"10 I thought that he displayed traits of honor, l... Labeling \n",
"\n",
" Secondary Distortion (Optional) \\\n",
"0 NaN \n",
"1 Emotional Reasoning \n",
"4 Emotional Reasoning \n",
"9 NaN \n",
"10 NaN \n",
"\n",
" cleaned \n",
"0 hello i have a beautifulsmartoutgoing and amaz... \n",
"1 since i was about 16 years old i’ve had these ... \n",
"4 i don’t really know how to explain the situati... \n",
"9 i have been with my fiancé for two years now ... \n",
"10 my husband and i have been married for over a ... "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_2021['cleaned'] = df_2021['Patient Question'].map(lambda text: preprocess_text(text))\n",
"df_2021.head()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0c116e42",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(df_2021['cleaned'].values,\n",
" df_2021['Dominant Distortion'].values,\n",
" test_size=0.2, random_state=42)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c7bf8c80",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"\n",
"tfidf_vectorizer = TfidfVectorizer()\n",
"X_train_tfidf = tfidf_vectorizer.fit_transform(X_train)\n",
"X_test_tfidf = tfidf_vectorizer.transform(X_test)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "59a4d305",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.svm import LinearSVC\n",
"from sklearn.tree import DecisionTreeClassifier\n",
"from sklearn.neighbors import KNeighborsClassifier\n",
"from sklearn.neural_network import MLPClassifier\n",
"from sklearn.svm import SVC\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "5fdc025f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"LinearSVC()
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": [
"LinearSVC()"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model = LinearSVC()\n",
"model.fit(X_train_tfidf,y_train)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "6bfc79e4",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score, classification_report\n",
"def calculate_results(y_true, y_pred, y_pred_proba=None):\n",
" \"\"\"\n",
" Tính toán các chỉ số đánh giá, bao gồm accuracy, precision, recall, f1 và roc-auc.\n",
" Nếu `y_pred_proba` được cung cấp, tính thêm ROC-AUC cho từng nhãn trong bài toán đa nhãn.\n",
" \"\"\"\n",
" results = {\n",
" \"accuracy\": accuracy_score(y_true, y_pred) * 100,\n",
" \"precision\": precision_score(y_true, y_pred, average='weighted'),\n",
" \"recall\": recall_score(y_true, y_pred, average='weighted'),\n",
" \"f1\": f1_score(y_true, y_pred, average='weighted')\n",
" }\n",
" \n",
" return results"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "f213c25e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'accuracy': 24.6875, 'precision': 0.23199561875314, 'recall': 0.246875, 'f1': 0.23512261720447172}\n",
" precision recall f1-score support\n",
"\n",
"All-or-nothing thinking 0.00 0.00 0.00 15\n",
" Emotional Reasoning 0.27 0.20 0.23 30\n",
" Fortune-telling 0.31 0.33 0.32 24\n",
" Labeling 0.14 0.10 0.12 40\n",
" Magnification 0.18 0.16 0.17 37\n",
" Mental filter 0.15 0.23 0.18 13\n",
" Mind Reading 0.39 0.52 0.45 54\n",
" Overgeneralization 0.25 0.32 0.28 50\n",
" Personalization 0.25 0.18 0.21 39\n",
" Should statements 0.06 0.06 0.06 18\n",
"\n",
" accuracy 0.25 320\n",
" macro avg 0.20 0.21 0.20 320\n",
" weighted avg 0.23 0.25 0.24 320\n",
"\n"
]
}
],
"source": [
"y_pred = model.predict(X_test_tfidf)\n",
"print(calculate_results(y_test, y_pred))\n",
"print(classification_report(y_test, y_pred))"
]
},
{
"cell_type": "markdown",
"id": "7664dae2",
"metadata": {},
"source": [
"# Our methods"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "13ccfe1c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Id_Number | \n",
" Patient Question | \n",
" Distorted part | \n",
" Dominant Distortion | \n",
" Secondary Distortion (Optional) | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 4500 | \n",
" Hello, I have a beautiful,smart,outgoing and a... | \n",
" The voice are always fimilar (someone she know... | \n",
" Personalization | \n",
" NaN | \n",
"
\n",
" \n",
" | 1 | \n",
" 4501 | \n",
" Since I was about 16 years old I’ve had these ... | \n",
" I feel trapped inside my disgusting self and l... | \n",
" Labeling | \n",
" Emotional Reasoning | \n",
"
\n",
" \n",
" | 2 | \n",
" 4502 | \n",
" So I’ve been dating on and off this guy for a... | \n",
" NaN | \n",
" No Distortion | \n",
" NaN | \n",
"
\n",
" \n",
" | 3 | \n",
" 4503 | \n",
" My parents got divorced in 2004. My mother has... | \n",
" NaN | \n",
" No Distortion | \n",
" NaN | \n",
"
\n",
" \n",
" | 4 | \n",
" 4504 | \n",
" I don’t really know how to explain the situati... | \n",
" I refused to go because I didn’t know if it wa... | \n",
" Fortune-telling | \n",
" Emotional Reasoning | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Id_Number Patient Question \\\n",
"0 4500 Hello, I have a beautiful,smart,outgoing and a... \n",
"1 4501 Since I was about 16 years old I’ve had these ... \n",
"2 4502 So I’ve been dating on and off this guy for a... \n",
"3 4503 My parents got divorced in 2004. My mother has... \n",
"4 4504 I don’t really know how to explain the situati... \n",
"\n",
" Distorted part Dominant Distortion \\\n",
"0 The voice are always fimilar (someone she know... Personalization \n",
"1 I feel trapped inside my disgusting self and l... Labeling \n",
"2 NaN No Distortion \n",
"3 NaN No Distortion \n",
"4 I refused to go because I didn’t know if it wa... Fortune-telling \n",
"\n",
" Secondary Distortion (Optional) \n",
"0 NaN \n",
"1 Emotional Reasoning \n",
"2 NaN \n",
"3 NaN \n",
"4 Emotional Reasoning "
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_our = pd.read_csv('data/distortion.csv')\n",
"df_our.head()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "6c37e38a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Id_Number | \n",
" Patient Question | \n",
" Distorted part | \n",
" Dominant Distortion | \n",
" Secondary Distortion (Optional) | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 4500 | \n",
" Hello, I have a beautiful,smart,outgoing and a... | \n",
" The voice are always fimilar (someone she know... | \n",
" Personalization | \n",
" NaN | \n",
"
\n",
" \n",
" | 1 | \n",
" 4501 | \n",
" Since I was about 16 years old I’ve had these ... | \n",
" I feel trapped inside my disgusting self and l... | \n",
" Labeling | \n",
" Emotional Reasoning | \n",
"
\n",
" \n",
" | 4 | \n",
" 4504 | \n",
" I don’t really know how to explain the situati... | \n",
" I refused to go because I didn’t know if it wa... | \n",
" Fortune-telling | \n",
" Emotional Reasoning | \n",
"
\n",
" \n",
" | 9 | \n",
" 4510 | \n",
" I have been with my fiancé for two years now.... | \n",
" I felt like the response was totally irrationa... | \n",
" Magnification | \n",
" NaN | \n",
"
\n",
" \n",
" | 10 | \n",
" 4511 | \n",
" My husband and I have been married for over a ... | \n",
" I thought that he displayed traits of honor, l... | \n",
" Labeling | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Id_Number Patient Question \\\n",
"0 4500 Hello, I have a beautiful,smart,outgoing and a... \n",
"1 4501 Since I was about 16 years old I’ve had these ... \n",
"4 4504 I don’t really know how to explain the situati... \n",
"9 4510 I have been with my fiancé for two years now.... \n",
"10 4511 My husband and I have been married for over a ... \n",
"\n",
" Distorted part Dominant Distortion \\\n",
"0 The voice are always fimilar (someone she know... Personalization \n",
"1 I feel trapped inside my disgusting self and l... Labeling \n",
"4 I refused to go because I didn’t know if it wa... Fortune-telling \n",
"9 I felt like the response was totally irrationa... Magnification \n",
"10 I thought that he displayed traits of honor, l... Labeling \n",
"\n",
" Secondary Distortion (Optional) \n",
"0 NaN \n",
"1 Emotional Reasoning \n",
"4 Emotional Reasoning \n",
"9 NaN \n",
"10 NaN "
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_our = df_our[df_our['Dominant Distortion'] != 'No Distortion']\n",
"df_our.head()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "1503580c",
"metadata": {},
"outputs": [],
"source": [
"# df_our = df_our.dropna()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "597bb4ca",
"metadata": {},
"outputs": [],
"source": [
"df_our['cleaned'] = df_our['Distorted part'].map(lambda text: preprocess_text(text))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "626adab6",
"metadata": {},
"outputs": [],
"source": [
"df_our['cleaned_full'] = df_our['Patient Question'].map(lambda text: preprocess_text(text))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "b573c357",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"\n",
"train_full, test_full, _, _ = train_test_split(df_our['cleaned_full'].values,\n",
" df_our['Dominant Distortion'].values,\n",
" test_size=0.2, random_state=42)\n",
"\n",
"X_train_our, X_test_our, y_train_our, y_test_our = train_test_split(df_our['cleaned'].values,\n",
" df_our['Dominant Distortion'].values,\n",
" test_size=0.2, random_state=42)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "2a3b3c97",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"\n",
"tfidf_vectorizer_our = TfidfVectorizer()\n",
"X_train_tfidf_our = tfidf_vectorizer_our.fit_transform(X_train_our)\n",
"X_test_tfidf_our = tfidf_vectorizer_our.transform(X_test_our)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "894cf240",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"LinearSVC()
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": [
"LinearSVC()"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model = LinearSVC()\n",
"model.fit(X_train_tfidf_our,y_train_our)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "c52c869e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'accuracy': 34.6875, 'precision': 0.36036002269633405, 'recall': 0.346875, 'f1': 0.34796169864321225}\n",
" precision recall f1-score support\n",
"\n",
"All-or-nothing thinking 0.15 0.13 0.14 15\n",
" Emotional Reasoning 0.35 0.20 0.26 30\n",
" Fortune-telling 0.54 0.58 0.56 24\n",
" Labeling 0.44 0.35 0.39 40\n",
" Magnification 0.30 0.24 0.27 37\n",
" Mental filter 0.06 0.15 0.09 13\n",
" Mind Reading 0.48 0.57 0.52 54\n",
" Overgeneralization 0.29 0.34 0.31 50\n",
" Personalization 0.33 0.28 0.31 39\n",
" Should statements 0.38 0.28 0.32 18\n",
"\n",
" accuracy 0.35 320\n",
" macro avg 0.33 0.31 0.32 320\n",
" weighted avg 0.36 0.35 0.35 320\n",
"\n"
]
}
],
"source": [
"y_pred_our = model.predict(X_test_tfidf_our)\n",
"print(calculate_results(y_test_our, y_pred_our))\n",
"print(classification_report(y_test_our, y_pred_our))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "2f56b5fd",
"metadata": {},
"outputs": [],
"source": [
"results_our = pd.DataFrame({\n",
" \"text\": X_test_our,\n",
" \"full\": test_full,\n",
" \"gold_label\": y_test_our,\n",
" \"predicted_label\": y_pred_our\n",
"})\n",
"# df.to_csv(\"predictions.csv\", index=False, encoding='utf-8-sig')\n",
"\n",
"# print(\"Đã lưu file predictions.csv\")\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "79f9a8e6",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" text | \n",
" full | \n",
" gold_label | \n",
" predicted_label | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" i had an extreme hate towards myself sabotagin... | \n",
" from lebanon it has been a long period that i’... | \n",
" Mental filter | \n",
" Overgeneralization | \n",
"
\n",
" \n",
" | 1 | \n",
" my husband was acting like he was having an af... | \n",
" last year my husband said he knew this girl wh... | \n",
" Emotional Reasoning | \n",
" Labeling | \n",
"
\n",
" \n",
" | 2 | \n",
" i feel as if moving on to higher education wil... | \n",
" i find myself increasingly disillusioned with ... | \n",
" Fortune-telling | \n",
" Fortune-telling | \n",
"
\n",
" \n",
" | 3 | \n",
" i’m really struggling with understanding how i... | \n",
" my husband left almost 3 years ago and lives w... | \n",
" Mind Reading | \n",
" Mind Reading | \n",
"
\n",
" \n",
" | 4 | \n",
" i’m 17 now and should be off to university nex... | \n",
" i’m 17 now and should be off to university nex... | \n",
" Should statements | \n",
" Magnification | \n",
"
\n",
" \n",
" | ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" | 315 | \n",
" another thing is that my mother loves lecturin... | \n",
" my mother and father treat me quite differentl... | \n",
" Mind Reading | \n",
" Mind Reading | \n",
"
\n",
" \n",
" | 316 | \n",
" part of me thinks this is a routine and just d... | \n",
" my boyfriend of 2 years is a wonderful person ... | \n",
" Personalization | \n",
" Mind Reading | \n",
"
\n",
" \n",
" | 317 | \n",
" all my life i’ve tried to be different people ... | \n",
" from a 14 year old girl in the us all my life ... | \n",
" Personalization | \n",
" Overgeneralization | \n",
"
\n",
" \n",
" | 318 | \n",
" she knows she has memory problems and is not i... | \n",
" my wife has been seeing the same psychiatrist ... | \n",
" Mind Reading | \n",
" Mind Reading | \n",
"
\n",
" \n",
" | 319 | \n",
" i just feel like i’ve never been happy | \n",
" is lifetime depression a thing is there some o... | \n",
" Overgeneralization | \n",
" Emotional Reasoning | \n",
"
\n",
" \n",
"
\n",
"
320 rows × 4 columns
\n",
"
"
],
"text/plain": [
" text \\\n",
"0 i had an extreme hate towards myself sabotagin... \n",
"1 my husband was acting like he was having an af... \n",
"2 i feel as if moving on to higher education wil... \n",
"3 i’m really struggling with understanding how i... \n",
"4 i’m 17 now and should be off to university nex... \n",
".. ... \n",
"315 another thing is that my mother loves lecturin... \n",
"316 part of me thinks this is a routine and just d... \n",
"317 all my life i’ve tried to be different people ... \n",
"318 she knows she has memory problems and is not i... \n",
"319 i just feel like i’ve never been happy \n",
"\n",
" full gold_label \\\n",
"0 from lebanon it has been a long period that i’... Mental filter \n",
"1 last year my husband said he knew this girl wh... Emotional Reasoning \n",
"2 i find myself increasingly disillusioned with ... Fortune-telling \n",
"3 my husband left almost 3 years ago and lives w... Mind Reading \n",
"4 i’m 17 now and should be off to university nex... Should statements \n",
".. ... ... \n",
"315 my mother and father treat me quite differentl... Mind Reading \n",
"316 my boyfriend of 2 years is a wonderful person ... Personalization \n",
"317 from a 14 year old girl in the us all my life ... Personalization \n",
"318 my wife has been seeing the same psychiatrist ... Mind Reading \n",
"319 is lifetime depression a thing is there some o... Overgeneralization \n",
"\n",
" predicted_label \n",
"0 Overgeneralization \n",
"1 Labeling \n",
"2 Fortune-telling \n",
"3 Mind Reading \n",
"4 Magnification \n",
".. ... \n",
"315 Mind Reading \n",
"316 Mind Reading \n",
"317 Overgeneralization \n",
"318 Mind Reading \n",
"319 Emotional Reasoning \n",
"\n",
"[320 rows x 4 columns]"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results_our"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "666e3ea7",
"metadata": {},
"outputs": [],
"source": [
"results = pd.DataFrame({\n",
" \"text\": X_test,\n",
" \"gold_label\": y_test,\n",
" \"predicted_label\": y_pred\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "70529b00",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" text | \n",
" gold_label | \n",
" predicted_label | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" from lebanon it has been a long period that i’... | \n",
" Mental filter | \n",
" Overgeneralization | \n",
"
\n",
" \n",
" | 1 | \n",
" last year my husband said he knew this girl wh... | \n",
" Emotional Reasoning | \n",
" Overgeneralization | \n",
"
\n",
" \n",
" | 2 | \n",
" i find myself increasingly disillusioned with ... | \n",
" Fortune-telling | \n",
" Fortune-telling | \n",
"
\n",
" \n",
" | 3 | \n",
" my husband left almost 3 years ago and lives w... | \n",
" Mind Reading | \n",
" Mind Reading | \n",
"
\n",
" \n",
" | 4 | \n",
" i’m 17 now and should be off to university nex... | \n",
" Should statements | \n",
" Mental filter | \n",
"
\n",
" \n",
" | ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" | 315 | \n",
" my mother and father treat me quite differentl... | \n",
" Mind Reading | \n",
" Mind Reading | \n",
"
\n",
" \n",
" | 316 | \n",
" my boyfriend of 2 years is a wonderful person ... | \n",
" Personalization | \n",
" Mind Reading | \n",
"
\n",
" \n",
" | 317 | \n",
" from a 14 year old girl in the us all my life ... | \n",
" Personalization | \n",
" Magnification | \n",
"
\n",
" \n",
" | 318 | \n",
" my wife has been seeing the same psychiatrist ... | \n",
" Mind Reading | \n",
" Mind Reading | \n",
"
\n",
" \n",
" | 319 | \n",
" is lifetime depression a thing is there some o... | \n",
" Overgeneralization | \n",
" Should statements | \n",
"
\n",
" \n",
"
\n",
"
320 rows × 3 columns
\n",
"
"
],
"text/plain": [
" text gold_label \\\n",
"0 from lebanon it has been a long period that i’... Mental filter \n",
"1 last year my husband said he knew this girl wh... Emotional Reasoning \n",
"2 i find myself increasingly disillusioned with ... Fortune-telling \n",
"3 my husband left almost 3 years ago and lives w... Mind Reading \n",
"4 i’m 17 now and should be off to university nex... Should statements \n",
".. ... ... \n",
"315 my mother and father treat me quite differentl... Mind Reading \n",
"316 my boyfriend of 2 years is a wonderful person ... Personalization \n",
"317 from a 14 year old girl in the us all my life ... Personalization \n",
"318 my wife has been seeing the same psychiatrist ... Mind Reading \n",
"319 is lifetime depression a thing is there some o... Overgeneralization \n",
"\n",
" predicted_label \n",
"0 Overgeneralization \n",
"1 Overgeneralization \n",
"2 Fortune-telling \n",
"3 Mind Reading \n",
"4 Mental filter \n",
".. ... \n",
"315 Mind Reading \n",
"316 Mind Reading \n",
"317 Magnification \n",
"318 Mind Reading \n",
"319 Should statements \n",
"\n",
"[320 rows x 3 columns]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "ce61b9cd",
"metadata": {},
"outputs": [],
"source": [
"common_texts_df1 = results[results['text'].isin(results_our['full'])]\n",
"\n",
"common_texts_df2 = results_our[results_our['full'].isin(results['text'])]\n"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "2e2fbb23",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"since may i haven’t been sleeping great i had a roommate before i came home and as soon as i slept in my own room i’ve been freaking out at night i see the light flickering by my window someone’s there i hear something under my desk someone’s there every little thing is making me terrified and unless i take benedryl i physically can’t sleep because i’m so afraid since i haven’t been sleeping great i’ve also started having minor hallucinations occasionally mostly auditory the microwave running and dishes breaking but i keep feeling like there’s bugs on me whenever i’m really paranoid how do i cope with thus fear\n",
"Magnification\n",
"Overgeneralization\n"
]
}
],
"source": [
"i=14\n",
"print(common_texts_df1['text'][i])\n",
"print(common_texts_df1['gold_label'][i])\n",
"print(common_texts_df1['predicted_label'][i])"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "0dded9a0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"since may i haven’t been sleeping great i had a roommate before i came home and as soon as i slept in my own room i’ve been freaking out at night i see the light flickering by my window someone’s there i hear something under my desk someone’s there every little thing is making me terrified and unless i take benedryl i physically can’t sleep because i’m so afraid since i haven’t been sleeping great i’ve also started having minor hallucinations occasionally mostly auditory the microwave running and dishes breaking but i keep feeling like there’s bugs on me whenever i’m really paranoid how do i cope with thus fear\n",
"i see the light flickering by my window someone’s there i hear something under my desk someone’s there every little thing is making me terrified and unless i take benedryl i physically can’t sleep because i’m so afraid\n",
"Magnification\n",
"Personalization\n"
]
}
],
"source": [
"print(common_texts_df2['full'][i])\n",
"print(common_texts_df2['text'][i])\n",
"print(common_texts_df2['gold_label'][i])\n",
"print(common_texts_df2['predicted_label'][i])"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "453506b2",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Id_Number | \n",
" Patient Question | \n",
" Distorted part | \n",
" Dominant Distortion | \n",
" Secondary Distortion (Optional) | \n",
" cleaned | \n",
" cleaned_full | \n",
"
\n",
" \n",
" \n",
" \n",
" | 2468 | \n",
" 2500 | \n",
" I can’t get close to anyone, I can’t feel, I c... | \n",
" I’m not shy at all, I’m a very confident perso... | \n",
" Labeling | \n",
" NaN | \n",
" i’m not shy at all i’m a very confident person... | \n",
" i can’t get close to anyone i can’t feel i can... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Id_Number Patient Question \\\n",
"2468 2500 I can’t get close to anyone, I can’t feel, I c... \n",
"\n",
" Distorted part Dominant Distortion \\\n",
"2468 I’m not shy at all, I’m a very confident perso... Labeling \n",
"\n",
" Secondary Distortion (Optional) \\\n",
"2468 NaN \n",
"\n",
" cleaned \\\n",
"2468 i’m not shy at all i’m a very confident person... \n",
"\n",
" cleaned_full \n",
"2468 i can’t get close to anyone i can’t feel i can... "
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"example = df_our[df_our['Id_Number'] == 2500]\n",
"example"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "3cb9c242",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I can’t get close to anyone, I can’t feel, I can’t make friends or i just don’t want to, i feel the need to be alone. I’m not shy at all, I’m a very confident person I can talk to anyone in fact I don’t even hesitate to talk with strangers at all. and yes I’m a good person at heart i’m not selfish yet I can’t get close to anyone sometimes I feel like there’s an invisible wall around me that I can’t let anyone close. mostly people like me they are inspired by me they want to get close but each time I just push them away even sometimes when I really like them, but I don’t feel. What’s wrong with me?\n"
]
}
],
"source": [
"print(example['Patient Question'][2468])"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "879feb79",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I’m not shy at all, I’m a very confident person I can talk to anyone in fact I don’t even hesitate to talk with strangers at all.\n"
]
}
],
"source": [
"print(example['Distorted part'][2468])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "962aaac3",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}