varsha2002 commited on
Commit
ee64ce5
·
1 Parent(s): 72e2e9f

Upload 2 files

Browse files
TextClassification_Patient_Symptoms_and_Diseases (1).ipynb ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "markdown",
19
+ "source": [
20
+ "AIMERS"
21
+ ],
22
+ "metadata": {
23
+ "id": "D1-ngpe5C5_X"
24
+ }
25
+ },
26
+ {
27
+ "cell_type": "code",
28
+ "execution_count": null,
29
+ "metadata": {
30
+ "id": "FU57l9-06L5O"
31
+ },
32
+ "outputs": [],
33
+ "source": [
34
+ "!pip install transformers\n",
35
+ "import pandas as pd\n",
36
+ "import re\n",
37
+ "import spacy\n",
38
+ "from sklearn.model_selection import train_test_split\n",
39
+ "from sklearn.feature_extraction.text import TfidfVectorizer\n",
40
+ "from sklearn.pipeline import Pipeline\n",
41
+ "from sklearn.metrics import accuracy_score, classification_report\n",
42
+ "from sklearn.linear_model import LogisticRegression\n",
43
+ "#from transformers import TfidfVectorizerForTransformers\n",
44
+ "\n",
45
+ "\n",
46
+ "# Load the data\n",
47
+ "data = pd.read_csv('symptomssingle.csv')\n",
48
+ "\n",
49
+ "# Check for any missing values and remove them\n",
50
+ "data = data.dropna()\n",
51
+ "\n",
52
+ "# Define a function to separate symptoms and diseases from the text\n",
53
+ "def separate_symptoms_and_diseases(text):\n",
54
+ " symptoms = re.findall(r'{\"symptoms\":\"(.*?)\"}', text)\n",
55
+ " disease = re.sub(r'(?:{\"symptoms\":\".*?\"},?)+', '', text).strip()\n",
56
+ " disease = disease.replace('],', '').strip() # Remove '],' from the disease name\n",
57
+ " return symptoms, disease\n",
58
+ "\n",
59
+ "# Apply the function to the data\n",
60
+ "data['symptoms_and_diseases'] = data['data'].apply(separate_symptoms_and_diseases)\n",
61
+ "data[['symptoms', 'disease']] = pd.DataFrame(data['symptoms_and_diseases'].tolist(), index=data.index)\n",
62
+ "data = data.drop(columns=['data', 'symptoms_and_diseases'])\n",
63
+ "\n",
64
+ "# Load the spaCy model\n",
65
+ "nlp = spacy.load('en_core_web_sm')\n",
66
+ "\n",
67
+ "# Preprocessing function\n",
68
+ "def preprocess(symptoms):\n",
69
+ " processed_symptoms = []\n",
70
+ " for symptom in symptoms:\n",
71
+ " doc = nlp(symptom)\n",
72
+ " processed_symptom = ' '.join(token.lemma_.lower() for token in doc if not token.is_stop and token.is_alpha)\n",
73
+ " processed_symptoms.append(processed_symptom)\n",
74
+ " return ' '.join(processed_symptoms)\n",
75
+ "\n",
76
+ "# Preprocess the symptoms column\n",
77
+ "data['symptoms_preprocessed'] = data['symptoms'].apply(preprocess)\n",
78
+ "\n",
79
+ "\n",
80
+ "# Split the data into train and test sets\n",
81
+ "X_train, X_test, y_train, y_test = train_test_split(data['symptoms_preprocessed'], data['disease'], test_size=0.2, random_state=42)\n",
82
+ "\n",
83
+ "\n",
84
+ "# Create a pipeline for text classification\n",
85
+ "pipeline = Pipeline([\n",
86
+ " ('tfidf', TfidfVectorizer(ngram_range=(1, 2))),\n",
87
+ " ('classifier', LogisticRegression(solver='liblinear', C=10))\n",
88
+ "])\n",
89
+ "# Train the model\n",
90
+ "pipeline.fit(X_train, y_train)\n",
91
+ "\n",
92
+ "# Make predictions\n",
93
+ "y_pred = pipeline.predict(X_test)\n",
94
+ "\n",
95
+ "# Evaluate the model\n",
96
+ "print(\"Accuracy: \", accuracy_score(y_test, y_pred))\n",
97
+ "print(\"Classification Report:\\n\", classification_report(y_test, y_pred))\n"
98
+ ]
99
+ },
100
+ {
101
+ "cell_type": "code",
102
+ "source": [],
103
+ "metadata": {
104
+ "id": "_ERQV-cI1ENp"
105
+ },
106
+ "execution_count": null,
107
+ "outputs": []
108
+ },
109
+ {
110
+ "cell_type": "code",
111
+ "source": [
112
+ "!pip install joblib\n",
113
+ "\n",
114
+ "import joblib\n",
115
+ "\n",
116
+ "# Save the TfidfVectorizer\n",
117
+ "joblib.dump(pipeline.named_steps['tfidf'], 'tfidf_vectorizer.joblib')\n",
118
+ "\n",
119
+ "# Save the Logistic Regression model\n",
120
+ "joblib.dump(pipeline.named_steps['classifier'], 'logistic_regression_classifier.joblib')\n",
121
+ "\n"
122
+ ],
123
+ "metadata": {
124
+ "colab": {
125
+ "base_uri": "https://localhost:8080/"
126
+ },
127
+ "id": "emwnJJVwAupA",
128
+ "outputId": "9fa7f22f-2909-4431-d57e-5528a7e81663"
129
+ },
130
+ "execution_count": null,
131
+ "outputs": [
132
+ {
133
+ "output_type": "stream",
134
+ "name": "stdout",
135
+ "text": [
136
+ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
137
+ "Requirement already satisfied: joblib in /usr/local/lib/python3.9/dist-packages (1.1.1)\n"
138
+ ]
139
+ },
140
+ {
141
+ "output_type": "execute_result",
142
+ "data": {
143
+ "text/plain": [
144
+ "['logistic_regression_classifier.joblib']"
145
+ ]
146
+ },
147
+ "metadata": {},
148
+ "execution_count": 8
149
+ }
150
+ ]
151
+ },
152
+ {
153
+ "cell_type": "code",
154
+ "source": [
155
+ "import joblib\n",
156
+ "\n",
157
+ "\n",
158
+ "# Convert the TfidfVectorizer to a Hugging Face compatible format\n",
159
+ "tfidf_transformer = TfidfVectorizerForTransformers(pipeline.named_steps['tfidf'])\n",
160
+ "\n",
161
+ "# Save the TfidfVectorizer\n",
162
+ "tfidf_transformer.save_pretrained('tfidf_transformer')\n",
163
+ "\n",
164
+ "\n",
165
+ "# Load the saved model\n",
166
+ "loaded_pipeline = joblib.load('DiseasePredictionBasedonSymptoms.joblib')\n",
167
+ "\n",
168
+ "# Make predictions using the loaded model (example)\n",
169
+ "sample_symptom = \"Skin Rash\"\n",
170
+ "processed_symptom = preprocess([sample_symptom])\n",
171
+ "prediction = loaded_pipeline.predict([processed_symptom])\n",
172
+ "\n",
173
+ "print(\"Predicted disease:\", prediction[0])\n"
174
+ ],
175
+ "metadata": {
176
+ "colab": {
177
+ "base_uri": "https://localhost:8080/"
178
+ },
179
+ "id": "Tu4fmj1bBYNw",
180
+ "outputId": "a1a33056-3a0d-49ad-8cb8-b356fba6dd73"
181
+ },
182
+ "execution_count": null,
183
+ "outputs": [
184
+ {
185
+ "output_type": "stream",
186
+ "name": "stdout",
187
+ "text": [
188
+ "Predicted disease: Contact dermatitis\n"
189
+ ]
190
+ }
191
+ ]
192
+ },
193
+ {
194
+ "cell_type": "code",
195
+ "source": [],
196
+ "metadata": {
197
+ "id": "CY5qrRCkBGuJ"
198
+ },
199
+ "execution_count": null,
200
+ "outputs": []
201
+ }
202
+ ]
203
+ }
symptomssingle.csv ADDED
The diff for this file is too large to render. See raw diff