Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recommendation.ipynb notebook #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
326 changes: 326 additions & 0 deletions recommendation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,326 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Recommendation System for Apna_Vaidya\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1. Rule-Based System"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"user_profile = None\n",
"user_scenario = None"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def rule_based_recommendation(user_profile, user_scenario):\n",
" if user_scenario == 'Patient with Symptoms':\n",
" return [\"Talk to a Doctor\", \"Verify with a Doctor\", \"Symptom Checker (AI-based)\"]\n",
" elif user_scenario == 'Chronic Condition Management':\n",
" return [\"Get a Prescription\", \"Order Medication\", \"Talk to a Doctor\", \"Talk to a Pharmacist\", \"Health Coach Consultation\"]\n",
" elif user_scenario == 'New Medication Inquiry':\n",
" return [\"Talk to a Doctor\", \"Talk to a Pharmacist\", \"Medication Information (AI-based)\"]\n",
" elif user_scenario == 'Diagnostic Needs':\n",
" return [\"Connect with a Diagnostic Center\", \"Verify with a Doctor\", \"Home Sample Collection\"]\n",
" elif user_scenario == 'Prescription Refill':\n",
" return [\"Order Medication\", \"Get a Prescription\", \"Talk to a Pharmacist\", \"Auto-Refill Subscription\"]\n",
" elif user_scenario == 'Preventive Care':\n",
" return [\"Schedule a Check-up\", \"Health Coach Consultation\", \"Vaccination Booking\"]\n",
" elif user_scenario == 'Post-Treatment Follow-up':\n",
" return [\"Talk to a Doctor\", \"Schedule a Follow-up\", \"Physical Therapy Consultation\"]\n",
" else:\n",
" return [\"Talk to a Doctor\"]\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Talk to a Doctor', 'Verify with a Doctor', 'Symptom Checker (AI-based)']\n"
]
}
],
"source": [
"# Example usage\n",
"user_profile = 'Chronic Condition Management'\n",
"user_scenario = 'Patient with Symptoms'\n",
"recommendation = rule_based_recommendation(user_profile, user_scenario)\n",
"print(recommendation)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2. Collaborative Filtering"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from sklearn.metrics.pairwise import cosine_similarity"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Example user-item interaction matrix (rows: users, columns: options)\n",
"interaction_matrix = np.array([\n",
" [1, 1, 0, 0, 0, 0, 0, 0, 0], # User 1\n",
" [0, 1, 1, 0, 0, 0, 0, 0, 0], # User 2\n",
" [0, 0, 1, 1, 0, 0, 0, 0, 0], # User 3\n",
" [0, 0, 0, 1, 1, 1, 0, 0, 0], # User 4\n",
" [0, 0, 0, 0, 1, 1, 1, 0, 0], # User 5\n",
" [0, 0, 0, 0, 0, 1, 1, 1, 0], # User 6\n",
"])\n",
"\n",
"# Calculate similarity between users\n",
"user_similarity = cosine_similarity(interaction_matrix)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Recommend based on the most similar user's interactions\n",
"def collaborative_filtering_recommendation(user_index, interaction_matrix, user_similarity):\n",
" similar_user_index = np.argmax(user_similarity[user_index])\n",
" recommendations = np.where(interaction_matrix[similar_user_index] == 1)[0]\n",
" return recommendations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Example usage\n",
"user_index = 0 # User 1\n",
"recommendations = collaborative_filtering_recommendation(user_index, interaction_matrix, user_similarity)\n",
"print(recommendations) # Output the indices of the recommended options"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3. Content-Based Filtering"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
"\n",
"# Example option descriptions\n",
"options = [\n",
" \"Talk to a Doctor for immediate consultation\",\n",
" \"Verify with a Doctor for second opinion\",\n",
" \"Use Symptom Checker to self-diagnose\",\n",
" \"Get a Prescription for chronic condition\",\n",
" \"Order Medication online\",\n",
" \"Talk to a Pharmacist for medication advice\",\n",
" \"Health Coach Consultation for chronic condition management\",\n",
" \"Medication Information for new prescriptions\",\n",
" \"Medication Interaction Checker\",\n",
" \"Connect with a Diagnostic Center for tests\",\n",
" \"Home Sample Collection for diagnostics\",\n",
" \"Book Lab Tests Online\",\n",
" \"Schedule a Check-up for preventive care\",\n",
" \"Health Screening Packages for preventive care\",\n",
" \"Vaccination Booking for preventive care\",\n",
" \"Schedule a Follow-up for post-treatment\",\n",
" \"Physical Therapy Consultation for recovery\",\n",
" \"Remote Monitoring Services for follow-up\",\n",
" \"Talk to a Therapist for mental health support\",\n",
" \"Join a Support Group for mental health\",\n",
" \"Mental Health Self-assessment (AI-based)\",\n",
" \"Meditation and Mindfulness Resources\"\n",
"]\n",
"\n",
"# User profile description\n",
"user_profile_description = \"Patient with chronic condition needing regular medication and follow-ups\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Vectorize the descriptions\n",
"vectorizer = TfidfVectorizer()\n",
"option_vectors = vectorizer.fit_transform(options)\n",
"user_vector = vectorizer.transform([user_profile_description])\n",
"\n",
"similarity = cosine_similarity(user_vector, option_vectors)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Recommend based on the highest similarity scores\n",
"def content_based_recommendation(similarity, options):\n",
" recommendations = np.argsort(similarity[0])[::-1]\n",
" return [options[i] for i in recommendations[:5]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Example usage\n",
"recommendations = content_based_recommendation(similarity, options)\n",
"print(recommendations)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 4. Machine Learning Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# using a decision tree classifier\n",
"from sklearn.tree import DecisionTreeClassifier\n",
"from sklearn.model_selection import train_test_split"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Example dataset (features: user profile data, labels: recommended options)\n",
"# This is a simplified example; in practice, you would have a more complex dataset\n",
"X = np.array([\n",
" [25, 1, 0, 1], # User 1: age, chronic condition, acute symptoms, preventive care\n",
" [45, 1, 1, 0], # User 2\n",
" [60, 0, 0, 1], # User 3\n",
" [35, 1, 0, 1], # User 4\n",
" [50, 0, 1, 0], # User 5\n",
"])\n",
"y = np.array([\n",
" [0, 1, 1, 0], # Recommended options for User 1\n",
" [1, 1, 0, 1], # Recommended options for User 2\n",
" [0, 0, 1, 1], # Recommended options for User 3\n",
" [1, 1, 0, 0], # Recommended options for User 4\n",
" [0, 0, 1, 1], # Recommended options for User 5\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Split the data into training and testing sets\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Train the decision tree classifier\n",
"clf = DecisionTreeClassifier()\n",
"clf.fit(X_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Predict recommendations for a new user\n",
"new_user = np.array([[40, 1, 1, 0]]) # New user profile\n",
"predictions = clf.predict(new_user)\n",
"print(predictions)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}