|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "### Recommendation System for Apna_Vaidya\n" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "#### 1. Rule-Based System" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": null, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "user_profile = None\n", |
| 24 | + "user_scenario = None" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": 1, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "def rule_based_recommendation(user_profile, user_scenario):\n", |
| 34 | + " if user_scenario == 'Patient with Symptoms':\n", |
| 35 | + " return [\"Talk to a Doctor\", \"Verify with a Doctor\", \"Symptom Checker (AI-based)\"]\n", |
| 36 | + " elif user_scenario == 'Chronic Condition Management':\n", |
| 37 | + " return [\"Get a Prescription\", \"Order Medication\", \"Talk to a Doctor\", \"Talk to a Pharmacist\", \"Health Coach Consultation\"]\n", |
| 38 | + " elif user_scenario == 'New Medication Inquiry':\n", |
| 39 | + " return [\"Talk to a Doctor\", \"Talk to a Pharmacist\", \"Medication Information (AI-based)\"]\n", |
| 40 | + " elif user_scenario == 'Diagnostic Needs':\n", |
| 41 | + " return [\"Connect with a Diagnostic Center\", \"Verify with a Doctor\", \"Home Sample Collection\"]\n", |
| 42 | + " elif user_scenario == 'Prescription Refill':\n", |
| 43 | + " return [\"Order Medication\", \"Get a Prescription\", \"Talk to a Pharmacist\", \"Auto-Refill Subscription\"]\n", |
| 44 | + " elif user_scenario == 'Preventive Care':\n", |
| 45 | + " return [\"Schedule a Check-up\", \"Health Coach Consultation\", \"Vaccination Booking\"]\n", |
| 46 | + " elif user_scenario == 'Post-Treatment Follow-up':\n", |
| 47 | + " return [\"Talk to a Doctor\", \"Schedule a Follow-up\", \"Physical Therapy Consultation\"]\n", |
| 48 | + " else:\n", |
| 49 | + " return [\"Talk to a Doctor\"]\n" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": 5, |
| 55 | + "metadata": {}, |
| 56 | + "outputs": [ |
| 57 | + { |
| 58 | + "name": "stdout", |
| 59 | + "output_type": "stream", |
| 60 | + "text": [ |
| 61 | + "['Talk to a Doctor', 'Verify with a Doctor', 'Symptom Checker (AI-based)']\n" |
| 62 | + ] |
| 63 | + } |
| 64 | + ], |
| 65 | + "source": [ |
| 66 | + "# Example usage\n", |
| 67 | + "user_profile = 'Chronic Condition Management'\n", |
| 68 | + "user_scenario = 'Patient with Symptoms'\n", |
| 69 | + "recommendation = rule_based_recommendation(user_profile, user_scenario)\n", |
| 70 | + "print(recommendation)" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "markdown", |
| 75 | + "metadata": {}, |
| 76 | + "source": [ |
| 77 | + "#### 2. Collaborative Filtering" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": 2, |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [], |
| 85 | + "source": [ |
| 86 | + "import numpy as np\n", |
| 87 | + "from sklearn.metrics.pairwise import cosine_similarity" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": null, |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [], |
| 95 | + "source": [ |
| 96 | + "# Example user-item interaction matrix (rows: users, columns: options)\n", |
| 97 | + "interaction_matrix = np.array([\n", |
| 98 | + " [1, 1, 0, 0, 0, 0, 0, 0, 0], # User 1\n", |
| 99 | + " [0, 1, 1, 0, 0, 0, 0, 0, 0], # User 2\n", |
| 100 | + " [0, 0, 1, 1, 0, 0, 0, 0, 0], # User 3\n", |
| 101 | + " [0, 0, 0, 1, 1, 1, 0, 0, 0], # User 4\n", |
| 102 | + " [0, 0, 0, 0, 1, 1, 1, 0, 0], # User 5\n", |
| 103 | + " [0, 0, 0, 0, 0, 1, 1, 1, 0], # User 6\n", |
| 104 | + "])\n", |
| 105 | + "\n", |
| 106 | + "# Calculate similarity between users\n", |
| 107 | + "user_similarity = cosine_similarity(interaction_matrix)" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": null, |
| 113 | + "metadata": {}, |
| 114 | + "outputs": [], |
| 115 | + "source": [ |
| 116 | + "# Recommend based on the most similar user's interactions\n", |
| 117 | + "def collaborative_filtering_recommendation(user_index, interaction_matrix, user_similarity):\n", |
| 118 | + " similar_user_index = np.argmax(user_similarity[user_index])\n", |
| 119 | + " recommendations = np.where(interaction_matrix[similar_user_index] == 1)[0]\n", |
| 120 | + " return recommendations" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "code", |
| 125 | + "execution_count": null, |
| 126 | + "metadata": {}, |
| 127 | + "outputs": [], |
| 128 | + "source": [ |
| 129 | + "# Example usage\n", |
| 130 | + "user_index = 0 # User 1\n", |
| 131 | + "recommendations = collaborative_filtering_recommendation(user_index, interaction_matrix, user_similarity)\n", |
| 132 | + "print(recommendations) # Output the indices of the recommended options" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "metadata": {}, |
| 138 | + "source": [ |
| 139 | + "#### 3. Content-Based Filtering" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": 3, |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [], |
| 147 | + "source": [ |
| 148 | + "from sklearn.feature_extraction.text import TfidfVectorizer\n", |
| 149 | + "\n", |
| 150 | + "# Example option descriptions\n", |
| 151 | + "options = [\n", |
| 152 | + " \"Talk to a Doctor for immediate consultation\",\n", |
| 153 | + " \"Verify with a Doctor for second opinion\",\n", |
| 154 | + " \"Use Symptom Checker to self-diagnose\",\n", |
| 155 | + " \"Get a Prescription for chronic condition\",\n", |
| 156 | + " \"Order Medication online\",\n", |
| 157 | + " \"Talk to a Pharmacist for medication advice\",\n", |
| 158 | + " \"Health Coach Consultation for chronic condition management\",\n", |
| 159 | + " \"Medication Information for new prescriptions\",\n", |
| 160 | + " \"Medication Interaction Checker\",\n", |
| 161 | + " \"Connect with a Diagnostic Center for tests\",\n", |
| 162 | + " \"Home Sample Collection for diagnostics\",\n", |
| 163 | + " \"Book Lab Tests Online\",\n", |
| 164 | + " \"Schedule a Check-up for preventive care\",\n", |
| 165 | + " \"Health Screening Packages for preventive care\",\n", |
| 166 | + " \"Vaccination Booking for preventive care\",\n", |
| 167 | + " \"Schedule a Follow-up for post-treatment\",\n", |
| 168 | + " \"Physical Therapy Consultation for recovery\",\n", |
| 169 | + " \"Remote Monitoring Services for follow-up\",\n", |
| 170 | + " \"Talk to a Therapist for mental health support\",\n", |
| 171 | + " \"Join a Support Group for mental health\",\n", |
| 172 | + " \"Mental Health Self-assessment (AI-based)\",\n", |
| 173 | + " \"Meditation and Mindfulness Resources\"\n", |
| 174 | + "]\n", |
| 175 | + "\n", |
| 176 | + "# User profile description\n", |
| 177 | + "user_profile_description = \"Patient with chronic condition needing regular medication and follow-ups\"\n" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "code", |
| 182 | + "execution_count": null, |
| 183 | + "metadata": {}, |
| 184 | + "outputs": [], |
| 185 | + "source": [ |
| 186 | + "# Vectorize the descriptions\n", |
| 187 | + "vectorizer = TfidfVectorizer()\n", |
| 188 | + "option_vectors = vectorizer.fit_transform(options)\n", |
| 189 | + "user_vector = vectorizer.transform([user_profile_description])\n", |
| 190 | + "\n", |
| 191 | + "similarity = cosine_similarity(user_vector, option_vectors)" |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "code", |
| 196 | + "execution_count": null, |
| 197 | + "metadata": {}, |
| 198 | + "outputs": [], |
| 199 | + "source": [ |
| 200 | + "# Recommend based on the highest similarity scores\n", |
| 201 | + "def content_based_recommendation(similarity, options):\n", |
| 202 | + " recommendations = np.argsort(similarity[0])[::-1]\n", |
| 203 | + " return [options[i] for i in recommendations[:5]]" |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "code", |
| 208 | + "execution_count": null, |
| 209 | + "metadata": {}, |
| 210 | + "outputs": [], |
| 211 | + "source": [ |
| 212 | + "# Example usage\n", |
| 213 | + "recommendations = content_based_recommendation(similarity, options)\n", |
| 214 | + "print(recommendations)" |
| 215 | + ] |
| 216 | + }, |
| 217 | + { |
| 218 | + "cell_type": "markdown", |
| 219 | + "metadata": {}, |
| 220 | + "source": [ |
| 221 | + "#### 4. Machine Learning Model" |
| 222 | + ] |
| 223 | + }, |
| 224 | + { |
| 225 | + "cell_type": "code", |
| 226 | + "execution_count": null, |
| 227 | + "metadata": {}, |
| 228 | + "outputs": [], |
| 229 | + "source": [ |
| 230 | + "# using a decision tree classifier\n", |
| 231 | + "from sklearn.tree import DecisionTreeClassifier\n", |
| 232 | + "from sklearn.model_selection import train_test_split" |
| 233 | + ] |
| 234 | + }, |
| 235 | + { |
| 236 | + "cell_type": "code", |
| 237 | + "execution_count": null, |
| 238 | + "metadata": {}, |
| 239 | + "outputs": [], |
| 240 | + "source": [ |
| 241 | + "# Example dataset (features: user profile data, labels: recommended options)\n", |
| 242 | + "# This is a simplified example; in practice, you would have a more complex dataset\n", |
| 243 | + "X = np.array([\n", |
| 244 | + " [25, 1, 0, 1], # User 1: age, chronic condition, acute symptoms, preventive care\n", |
| 245 | + " [45, 1, 1, 0], # User 2\n", |
| 246 | + " [60, 0, 0, 1], # User 3\n", |
| 247 | + " [35, 1, 0, 1], # User 4\n", |
| 248 | + " [50, 0, 1, 0], # User 5\n", |
| 249 | + "])\n", |
| 250 | + "y = np.array([\n", |
| 251 | + " [0, 1, 1, 0], # Recommended options for User 1\n", |
| 252 | + " [1, 1, 0, 1], # Recommended options for User 2\n", |
| 253 | + " [0, 0, 1, 1], # Recommended options for User 3\n", |
| 254 | + " [1, 1, 0, 0], # Recommended options for User 4\n", |
| 255 | + " [0, 0, 1, 1], # Recommended options for User 5\n", |
| 256 | + "])" |
| 257 | + ] |
| 258 | + }, |
| 259 | + { |
| 260 | + "cell_type": "code", |
| 261 | + "execution_count": null, |
| 262 | + "metadata": {}, |
| 263 | + "outputs": [], |
| 264 | + "source": [ |
| 265 | + "# Split the data into training and testing sets\n", |
| 266 | + "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)" |
| 267 | + ] |
| 268 | + }, |
| 269 | + { |
| 270 | + "cell_type": "code", |
| 271 | + "execution_count": null, |
| 272 | + "metadata": {}, |
| 273 | + "outputs": [], |
| 274 | + "source": [ |
| 275 | + "# Train the decision tree classifier\n", |
| 276 | + "clf = DecisionTreeClassifier()\n", |
| 277 | + "clf.fit(X_train, y_train)" |
| 278 | + ] |
| 279 | + }, |
| 280 | + { |
| 281 | + "cell_type": "code", |
| 282 | + "execution_count": null, |
| 283 | + "metadata": {}, |
| 284 | + "outputs": [], |
| 285 | + "source": [ |
| 286 | + "# Predict recommendations for a new user\n", |
| 287 | + "new_user = np.array([[40, 1, 1, 0]]) # New user profile\n", |
| 288 | + "predictions = clf.predict(new_user)\n", |
| 289 | + "print(predictions)" |
| 290 | + ] |
| 291 | + }, |
| 292 | + { |
| 293 | + "cell_type": "code", |
| 294 | + "execution_count": null, |
| 295 | + "metadata": {}, |
| 296 | + "outputs": [], |
| 297 | + "source": [] |
| 298 | + }, |
| 299 | + { |
| 300 | + "cell_type": "markdown", |
| 301 | + "metadata": {}, |
| 302 | + "source": [] |
| 303 | + } |
| 304 | + ], |
| 305 | + "metadata": { |
| 306 | + "kernelspec": { |
| 307 | + "display_name": "Python 3", |
| 308 | + "language": "python", |
| 309 | + "name": "python3" |
| 310 | + }, |
| 311 | + "language_info": { |
| 312 | + "codemirror_mode": { |
| 313 | + "name": "ipython", |
| 314 | + "version": 3 |
| 315 | + }, |
| 316 | + "file_extension": ".py", |
| 317 | + "mimetype": "text/x-python", |
| 318 | + "name": "python", |
| 319 | + "nbconvert_exporter": "python", |
| 320 | + "pygments_lexer": "ipython3", |
| 321 | + "version": "3.11.9" |
| 322 | + } |
| 323 | + }, |
| 324 | + "nbformat": 4, |
| 325 | + "nbformat_minor": 2 |
| 326 | +} |
0 commit comments