{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "id": "aSxhoE7a0yU0" }, "outputs": [], "source": [ "\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import networkx as nx\n", "import seaborn as sns\n", "from mlxtend.frequent_patterns import apriori, association_rules\n", "from mlxtend.preprocessing import TransactionEncoder\n", "\n", "# Step 1: Load Healthcare Dataset\n", "# Simulating a healthcare dataset with patient diagnoses\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dn9q1e0wFJjD", "outputId": "87ee4f0a-d5fa-4503-bd1c-c379c3d27cf2" }, "outputs": [], "source": [ "# Load the data from CSV file\n", "data = pd.read_csv('healthcare_data.csv')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "x0h8r-OD1s9t", "outputId": "1dcf4acc-eda7-4edc-a3a4-8b8ec9786310" }, "outputs": [], "source": [ "# Step 2: Data Preprocessing\n", "\n", "# Convert the 'Diagnoses' column into a list of transactions\n", "import ast\n", "\n", "data['Diagnoses'] = data['Diagnoses'].apply(ast.literal_eval) # Convert string representation of list back to list\n", "transactions = data['Diagnoses'].tolist()\n", "\n", "# Apply TransactionEncoder to prepare the dataset for Apriori\n", "te = TransactionEncoder()\n", "transaction_data = te.fit_transform(transactions)\n", "transaction_df = pd.DataFrame(transaction_data, columns=te.columns_)\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 [Hypertension, Diabetes]\n", "1 [Asthma, Allergy]\n", "2 [Diabetes, Obesity]\n", "3 [Hypertension, Asthma]\n", "4 [Diabetes, Hypertension, Obesity]\n", "Name: Diagnoses, dtype: object" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data['Diagnoses'].head()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 262 }, "id": "2lHHH2DZFugN", "outputId": "893b08c5-8d86-4dc6-98de-2ce5026f60de" }, "outputs": [ { "data": { "text/html": [ "
| \n", " | Allergy | \n", "Asthma | \n", "Diabetes | \n", "Heart Disease | \n", "Hypertension | \n", "Obesity | \n", "
|---|---|---|---|---|---|---|
| 0 | \n", "False | \n", "False | \n", "True | \n", "False | \n", "True | \n", "False | \n", "
| 1 | \n", "True | \n", "True | \n", "False | \n", "False | \n", "False | \n", "False | \n", "
| 2 | \n", "False | \n", "False | \n", "True | \n", "False | \n", "False | \n", "True | \n", "
| 3 | \n", "False | \n", "True | \n", "False | \n", "False | \n", "True | \n", "False | \n", "
| 4 | \n", "False | \n", "False | \n", "True | \n", "False | \n", "True | \n", "True | \n", "