Commit b82572c0 authored by Almouhannad's avatar Almouhannad

Add classifier

parent f1c1b376
test.ipynb test.ipynb
clf-test.ipynb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ***Setup***"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import nltk\n",
"from nltk.stem import PorterStemmer\n",
"from nltk.tokenize import word_tokenize\n",
"from nltk.corpus import stopwords\n",
"\n",
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"from sklearn.naive_bayes import MultinomialNB\n",
"from sklearn.metrics import accuracy_score, classification_report\n",
"\n",
"from tabulate import tabulate\n",
"\n",
"import pandas as pd\n",
"TRAIN_SET_PATH = 'data/Preprocessed_Symptom2Disease_Train.csv'\n",
"TEST_SET_PATH = 'data/Preprocessed_Symptom2Disease_Test.csv'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Now, we have to download `Punkt Tokenizer Model`, try running following cell, if it didn't work successfully then try to download model manually from following links: [Manual installation](https://www.nltk.org/data.html), and [Model link](https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/packages/tokenizers/punkt.zip).\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[nltk_data] Downloading package punkt to\n",
"[nltk_data] C:\\Users\\TOSHIBA\\AppData\\Roaming\\nltk_data...\n",
"[nltk_data] Package punkt is already up-to-date!\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nltk.download('punkt')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### You must see an output similar to the following output:\n",
"> `[nltk_data] Downloading package punkt to` \n",
"> `[nltk_data] C:\\Users\\TOSHIBA\\AppData\\Roaming\\nltk_data...` \n",
"> `[nltk_data] Package punkt is already up-to-date!` \n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[nltk_data] Downloading package stopwords to\n",
"[nltk_data] C:\\Users\\TOSHIBA\\AppData\\Roaming\\nltk_data...\n",
"[nltk_data] Package stopwords is already up-to-date!\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nltk.download('stopwords')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***Some helper functions***"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"stemmer = PorterStemmer()\n",
"\n",
"def stem_text(text):\n",
" tokens = word_tokenize(text) # Tokenize the text\n",
" stemmed_tokens = [stemmer.stem(token) for token in tokens] # Apply stemming\n",
" return ' '.join(stemmed_tokens)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"stop_words = set(stopwords.words('english'))\n",
"\n",
"def remove_stopwords(text):\n",
" tokens = word_tokenize(text) # Tokenize the text\n",
" filtered_tokens = [word for word in tokens if word not in stop_words] # Remove stop words\n",
" return ' '.join(filtered_tokens)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def print_clf_report_as_table(report):\n",
" data = []\n",
" for key, value in report.items():\n",
" if key != 'accuracy' and key != 'macro avg' and key != 'weighted avg':\n",
" data.append([key, value['precision'], value['recall'], value['f1-score'], value['support']])\n",
"\n",
" data.append(['accuracy', '', '', report['accuracy'], ''])\n",
"\n",
" data.append(['macro avg', report['macro avg']['precision'], report['macro avg']['recall'], report['macro avg']['f1-score'], ''])\n",
"\n",
" data.append(['weighted avg', report['weighted avg']['precision'], report['weighted avg']['recall'], report['weighted avg']['f1-score'], ''])\n",
"\n",
" print(tabulate(data, headers=['Class', 'Precision', 'Recall', 'F1-score', 'Support'], tablefmt='psql'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ***0- Read train and test sets***"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"train_set = pd.read_csv(TRAIN_SET_PATH)\n",
"X_train = train_set['text']\n",
"y_train = train_set['label']"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"test_set = pd.read_csv(TEST_SET_PATH)\n",
"X_test = test_set['text']\n",
"y_test = test_set['label']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ***1- Apply stemming to all symptoms(texts)***\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"X_train = X_train.apply(stem_text)\n",
"X_test = X_test.apply(stem_text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ***2- Remove stopping words***"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"X_train = X_train.apply(remove_stopwords)\n",
"X_test = X_test.apply(remove_stopwords)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ***3-Feature Vectorization using `TF-IDF`***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### `TF-IDF` stands for `Term Frequency-Inverse Document Frequency` which measure how important the word is in a document\n",
"- ##### ***Why?*** Text needs to be transformed into numerical form to be used in machine learning models."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# Initialize TF-IDF Vectorizer\n",
"vectorizer = TfidfVectorizer()\n",
"\n",
"# Fit the vectorizer on the training data and transform the training and test data\n",
"X_train = vectorizer.fit_transform(X_train)\n",
"X_test = vectorizer.transform(X_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ***4- Train a Classifier (Naive Bayes)***"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>#sk-container-id-1 {color: black;background-color: white;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>MultinomialNB()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">MultinomialNB</label><div class=\"sk-toggleable__content\"><pre>MultinomialNB()</pre></div></div></div></div></div>"
],
"text/plain": [
"MultinomialNB()"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"classifier = MultinomialNB()\n",
"classifier.fit(X_train, y_train)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ***5- Evaluate the Model***"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Accuracy: 0.961038961038961\n"
]
}
],
"source": [
"y_pred = classifier.predict(X_test)\n",
"accuracy = accuracy_score(y_test, y_pred)\n",
"print(f'Accuracy: {accuracy}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Classification report for each class (each class is a disease)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"+---------------------------------+--------------------+--------------------+------------+-----------+\n",
"| Class | Precision | Recall | F1-score | Support |\n",
"|---------------------------------+--------------------+--------------------+------------+-----------|\n",
"| acne | 1.0 | 1.0 | 1 | 9 |\n",
"| allergy | 0.875 | 1.0 | 0.933333 | 7 |\n",
"| arthritis | 1.0 | 1.0 | 1 | 7 |\n",
"| bronchial asthma | 1.0 | 1.0 | 1 | 8 |\n",
"| cervical spondylosis | 1.0 | 1.0 | 1 | 9 |\n",
"| chicken pox | 1.0 | 0.875 | 0.933333 | 8 |\n",
"| common cold | 0.8888888888888888 | 1.0 | 0.941176 | 8 |\n",
"| dengue | 0.8888888888888888 | 1.0 | 0.941176 | 8 |\n",
"| diabetes | 1.0 | 0.8 | 0.888889 | 10 |\n",
"| dimorphic hemorrhoids | 1.0 | 1.0 | 1 | 7 |\n",
"| drug reaction | 0.8571428571428571 | 0.8571428571428571 | 0.857143 | 7 |\n",
"| fungal infection | 0.9090909090909091 | 1.0 | 0.952381 | 10 |\n",
"| gastroesophageal reflux disease | 0.8461538461538461 | 1.0 | 0.916667 | 11 |\n",
"| hypertension | 1.0 | 1.0 | 1 | 7 |\n",
"| impetigo | 1.0 | 1.0 | 1 | 11 |\n",
"| jaundice | 1.0 | 1.0 | 1 | 9 |\n",
"| malaria | 1.0 | 1.0 | 1 | 15 |\n",
"| migraine | 1.0 | 0.9090909090909091 | 0.952381 | 11 |\n",
"| peptic ulcer disease | 1.0 | 0.8333333333333334 | 0.909091 | 12 |\n",
"| pneumonia | 0.9166666666666666 | 1.0 | 0.956522 | 11 |\n",
"| psoriasis | 1.0 | 0.9285714285714286 | 0.962963 | 14 |\n",
"| typhoid | 1.0 | 0.9230769230769231 | 0.96 | 13 |\n",
"| urinary tract infection | 0.9230769230769231 | 1.0 | 0.96 | 12 |\n",
"| varicose veins | 1.0 | 1.0 | 1 | 7 |\n",
"| accuracy | | | 0.961039 | |\n",
"| macro avg | 0.9627045408295407 | 0.9635923104673104 | 0.961044 | |\n",
"| weighted avg | 0.9649613854159308 | 0.961038961038961 | 0.960794 | |\n",
"+---------------------------------+--------------------+--------------------+------------+-----------+\n"
]
}
],
"source": [
"report = classification_report(y_test, y_pred, output_dict=True)\n",
"print_clf_report_as_table(report)"
]
}
],
"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.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment