Commit 1ec5d3ad authored by Weththasinghe A.S's avatar Weththasinghe A.S

Merge branch 'IT20154530-PP1' into 'master'

Complete PP1

See merge request !7
parents 8ef9efe1 a60c7856
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"source": [
"import pandas as pd\n",
"from sklearn.preprocessing import StandardScaler, OneHotEncoder\n",
"from sklearn.compose import ColumnTransformer\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.pipeline import Pipeline\n",
"\n",
"# Load your data\n",
"df = pd.read_csv(\"/content/c4Data.csv\")\n"
],
"metadata": {
"id": "ESL2GKosDtJQ"
},
"execution_count": 16,
"outputs": []
},
{
"cell_type": "code",
"source": [
"df.head()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 302
},
"id": "0tdLcppxDv0C",
"outputId": "c9b5b050-1f92-4bda-df8b-fc661779d165"
},
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Age Gender BMI Current Protein Intake (g) \\\n",
"0 73 M 19.946739 65 \n",
"1 74 F 18.849295 50 \n",
"2 54 F 30.723429 45 \n",
"3 59 M 19.123082 53 \n",
"4 76 F 23.106224 59 \n",
"\n",
" Current Sodium Intake (mg) Current Potassium Intake (mg) \\\n",
"0 1663 3529 \n",
"1 1920 3668 \n",
"2 2013 3213 \n",
"3 2377 3470 \n",
"4 2280 3616 \n",
"\n",
" Current Phosphorus Intake (mg) Other Conditions GFR Proteinuria \\\n",
"0 936 None 38 2 \n",
"1 1028 Hypertension 26 2 \n",
"2 1268 Diabetes 30 1 \n",
"3 900 Heart Disease 29 1 \n",
"4 953 Diabetes 46 2 \n",
"\n",
" Preferred Food Level \n",
"0 Low Potassium 1 \n",
"1 Balanced 2 \n",
"2 Low Sodium 3 \n",
"3 Low Phosphorus 1 \n",
"4 Low Phosphorus 3 "
],
"text/html": [
"\n",
" <div id=\"df-e59467c3-c0a1-40d6-ad07-a712ddbf8e81\">\n",
" <div class=\"colab-df-container\">\n",
" <div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Age</th>\n",
" <th>Gender</th>\n",
" <th>BMI</th>\n",
" <th>Current Protein Intake (g)</th>\n",
" <th>Current Sodium Intake (mg)</th>\n",
" <th>Current Potassium Intake (mg)</th>\n",
" <th>Current Phosphorus Intake (mg)</th>\n",
" <th>Other Conditions</th>\n",
" <th>GFR</th>\n",
" <th>Proteinuria</th>\n",
" <th>Preferred Food</th>\n",
" <th>Level</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>73</td>\n",
" <td>M</td>\n",
" <td>19.946739</td>\n",
" <td>65</td>\n",
" <td>1663</td>\n",
" <td>3529</td>\n",
" <td>936</td>\n",
" <td>None</td>\n",
" <td>38</td>\n",
" <td>2</td>\n",
" <td>Low Potassium</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>74</td>\n",
" <td>F</td>\n",
" <td>18.849295</td>\n",
" <td>50</td>\n",
" <td>1920</td>\n",
" <td>3668</td>\n",
" <td>1028</td>\n",
" <td>Hypertension</td>\n",
" <td>26</td>\n",
" <td>2</td>\n",
" <td>Balanced</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>54</td>\n",
" <td>F</td>\n",
" <td>30.723429</td>\n",
" <td>45</td>\n",
" <td>2013</td>\n",
" <td>3213</td>\n",
" <td>1268</td>\n",
" <td>Diabetes</td>\n",
" <td>30</td>\n",
" <td>1</td>\n",
" <td>Low Sodium</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>59</td>\n",
" <td>M</td>\n",
" <td>19.123082</td>\n",
" <td>53</td>\n",
" <td>2377</td>\n",
" <td>3470</td>\n",
" <td>900</td>\n",
" <td>Heart Disease</td>\n",
" <td>29</td>\n",
" <td>1</td>\n",
" <td>Low Phosphorus</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>76</td>\n",
" <td>F</td>\n",
" <td>23.106224</td>\n",
" <td>59</td>\n",
" <td>2280</td>\n",
" <td>3616</td>\n",
" <td>953</td>\n",
" <td>Diabetes</td>\n",
" <td>46</td>\n",
" <td>2</td>\n",
" <td>Low Phosphorus</td>\n",
" <td>3</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>\n",
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-e59467c3-c0a1-40d6-ad07-a712ddbf8e81')\"\n",
" title=\"Convert this dataframe to an interactive table.\"\n",
" style=\"display:none;\">\n",
" \n",
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
" width=\"24px\">\n",
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
" </svg>\n",
" </button>\n",
" \n",
" <style>\n",
" .colab-df-container {\n",
" display:flex;\n",
" flex-wrap:wrap;\n",
" gap: 12px;\n",
" }\n",
"\n",
" .colab-df-convert {\n",
" background-color: #E8F0FE;\n",
" border: none;\n",
" border-radius: 50%;\n",
" cursor: pointer;\n",
" display: none;\n",
" fill: #1967D2;\n",
" height: 32px;\n",
" padding: 0 0 0 0;\n",
" width: 32px;\n",
" }\n",
"\n",
" .colab-df-convert:hover {\n",
" background-color: #E2EBFA;\n",
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
" fill: #174EA6;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert {\n",
" background-color: #3B4455;\n",
" fill: #D2E3FC;\n",
" }\n",
"\n",
" [theme=dark] .colab-df-convert:hover {\n",
" background-color: #434B5C;\n",
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
" fill: #FFFFFF;\n",
" }\n",
" </style>\n",
"\n",
" <script>\n",
" const buttonEl =\n",
" document.querySelector('#df-e59467c3-c0a1-40d6-ad07-a712ddbf8e81 button.colab-df-convert');\n",
" buttonEl.style.display =\n",
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
"\n",
" async function convertToInteractive(key) {\n",
" const element = document.querySelector('#df-e59467c3-c0a1-40d6-ad07-a712ddbf8e81');\n",
" const dataTable =\n",
" await google.colab.kernel.invokeFunction('convertToInteractive',\n",
" [key], {});\n",
" if (!dataTable) return;\n",
"\n",
" const docLinkHtml = 'Like what you see? Visit the ' +\n",
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
" + ' to learn more about interactive tables.';\n",
" element.innerHTML = '';\n",
" dataTable['output_type'] = 'display_data';\n",
" await google.colab.output.renderOutput(dataTable, element);\n",
" const docLink = document.createElement('div');\n",
" docLink.innerHTML = docLinkHtml;\n",
" element.appendChild(docLink);\n",
" }\n",
" </script>\n",
" </div>\n",
" </div>\n",
" "
]
},
"metadata": {},
"execution_count": 17
}
]
},
{
"cell_type": "code",
"source": [
"# Separate features and target variable\n",
"X = df.drop('Level', axis=1)\n",
"y = df['Level']\n",
"\n",
"# Preprocessing\n",
"numeric_features = ['Age', 'BMI', 'Current Protein Intake (g)', 'Current Sodium Intake (mg)', \n",
" 'Current Potassium Intake (mg)', 'Current Phosphorus Intake (mg)', 'GFR']\n",
"categorical_features = ['Gender', 'Other Conditions', 'Proteinuria', 'Preferred Food']\n",
"\n",
"preprocessor = ColumnTransformer(\n",
" transformers=[\n",
" ('num', StandardScaler(), numeric_features),\n",
" ('cat', OneHotEncoder(), categorical_features)])\n",
"\n",
"# Model\n",
"model = LogisticRegression()\n",
"\n",
"# Combine preprocessing and model in a pipeline\n",
"clf = Pipeline(steps=[('preprocessor', preprocessor),\n",
" ('classifier', model)])\n",
"\n",
"# Split the data into train and test sets\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
"\n",
"# Train the model\n",
"clf.fit(X_train, y_train)\n",
"\n",
"# Use the model to make predictions\n",
"predictions = clf.predict(X_test)\n",
"\n",
"print(predictions)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ioAXbch_AbQb",
"outputId": "08983ca0-9d63-4055-b4fa-539728e9b7d3"
},
"execution_count": 18,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[3 1 2 2 1 3 1 3 1 3 1 2 2 3 1 1 2 2 1 2]\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"def recommend_diet(patient_data):\n",
" # Convert the data to a dataframe and preprocess it\n",
" patient_df = pd.DataFrame([patient_data])\n",
" \n",
" # Predict the kidney disease stage\n",
" level = clf.predict(patient_df)[0] \n",
" print(level)\n",
"\n",
" if level == 1:\n",
" return \"Maintain a balanced diet. No specific restrictions but ensure a healthy intake of protein, sodium, potassium and phosphorus.\"\n",
" elif level == 2:\n",
" return \"Start to monitor and limit the intake of protein, sodium, potassium and phosphorus to prevent further kidney damage.\"\n",
" elif level == 3:\n",
" return \"Follow a diet low in protein, sodium, potassium and phosphorus. Consult a dietitian for a personalized diet plan.\"\n",
"\n",
"# Test the function with patient data\n",
"patient_data = {\n",
" 'Age': 60,\n",
" 'Gender': 'M',\n",
" 'BMI': 25,\n",
" 'Current Protein Intake (g)': 60,\n",
" 'Current Sodium Intake (mg)': 2000,\n",
" 'Current Potassium Intake (mg)': 3500,\n",
" 'Current Phosphorus Intake (mg)': 1200,\n",
" 'Other Conditions': 'Diabetes',\n",
" 'GFR': 50,\n",
" 'Proteinuria': 1,\n",
" 'Preferred Food': 'Low Sodium',\n",
"}\n",
"\n",
"suggesting = recommend_diet(patient_data)\n",
"print(suggesting)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "M7yHk1VC8E71",
"outputId": "1df457d6-506d-40fb-dd3c-6ebaab56b5f3"
},
"execution_count": 19,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"3\n",
"Follow a diet low in protein, sodium, potassium and phosphorus. Consult a dietitian for a personalized diet plan.\n"
]
}
]
}
]
}
\ No newline at end of file
Age,Gender,BMI,Current Protein Intake (g),Current Sodium Intake (mg),Current Potassium Intake (mg),Current Phosphorus Intake (mg),Other Conditions,GFR,Proteinuria,Preferred Food,Level
73,M,19.946738594006803,65,1663,3529,936,None,38,2,Low Potassium,1
74,F,18.849295084845252,50,1920,3668,1028,Hypertension,26,2,Balanced,2
54,F,30.723428990623162,45,2013,3213,1268,Diabetes,30,1,Low Sodium,3
59,M,19.123082110619897,53,2377,3470,900,Heart Disease,29,1,Low Phosphorus,1
76,F,23.106224221129764,59,2280,3616,953,Diabetes,46,2,Low Phosphorus,3
75,F,32.86961360838057,61,1673,3133,1175,None,17,1,Low Potassium,3
78,M,28.56216528111437,60,1945,3071,1273,None,29,1,Balanced,3
69,F,34.66537391016402,53,1548,3613,1221,Heart Disease,41,2,Low Sodium,2
71,M,19.759002144240537,53,2130,3342,1189,Heart Disease,36,1,Low Potassium,2
46,M,27.851140342169625,54,1789,3116,961,None,60,1,Low Sodium,2
72,F,30.299198608513276,56,1680,3118,1242,Hypertension,49,2,Low Phosphorus,2
40,F,34.19055213437372,61,2091,3501,1093,Diabetes,54,1,High Protein,1
48,M,20.309892977440224,45,2305,3373,1123,None,42,1,Low Potassium,2
72,M,31.719150447080622,63,2058,3414,1189,None,26,1,Low Sodium,1
63,M,29.167901023658857,46,1710,3039,1272,Heart Disease,57,2,High Protein,1
76,F,27.449046942457123,65,2008,3006,1295,Hypertension,55,2,High Protein,3
40,M,30.97086786856528,65,2001,3110,1024,Hypertension,29,2,Low Potassium,2
51,M,27.51079800240438,59,2272,3104,1299,Heart Disease,49,2,High Protein,1
56,M,34.43588604858363,62,1714,3130,1298,Hypertension,30,1,Low Phosphorus,2
52,F,18.780563577603726,60,1897,3632,1222,Diabetes,29,1,Low Sodium,2
50,M,25.34533093182862,54,1961,3165,934,Heart Disease,57,2,Low Phosphorus,1
47,F,24.534799505347088,63,1753,3053,1246,Hypertension,21,2,Low Sodium,1
45,F,34.54118549812746,49,2398,3570,1150,Heart Disease,54,2,Low Sodium,2
67,F,33.487058174659246,51,1927,3419,1129,Hypertension,58,2,High Protein,2
62,M,23.323522405583482,51,1636,3435,1146,Heart Disease,49,2,Balanced,3
73,M,20.965549180198177,64,1752,3520,1206,Hypertension,35,2,Low Potassium,1
69,F,20.09665354423427,52,2125,3004,1006,Hypertension,59,1,Low Sodium,3
62,M,19.23067261469212,63,1930,3140,1174,None,56,2,Balanced,3
44,M,30.167864064028926,51,2034,3076,1030,Hypertension,51,2,Balanced,2
61,F,34.142184127676316,53,1720,3664,1047,None,15,2,Low Sodium,3
64,F,32.012063038019996,64,2138,3632,957,Diabetes,15,1,High Protein,2
48,F,32.70237200276655,62,2121,3187,1032,Hypertension,35,1,Low Potassium,3
45,F,21.72790071904713,61,1754,3670,1156,Diabetes,53,2,Low Potassium,2
76,M,26.477956441707075,54,1508,3332,1247,Heart Disease,58,2,High Protein,2
67,M,28.337412161586634,56,2361,3064,1249,None,19,2,High Protein,2
50,M,28.952915348166528,47,1729,3430,1260,None,28,1,Balanced,3
75,F,29.76458354667856,46,2294,3557,952,Diabetes,45,1,High Protein,2
55,F,20.249280857147433,55,2250,3465,924,Hypertension,50,1,Low Phosphorus,1
55,M,26.85393585039305,65,1508,3560,1123,Diabetes,19,2,Low Phosphorus,1
60,M,23.71783240951293,56,1669,3101,968,None,57,2,High Protein,2
72,F,19.267606818106664,61,2328,3046,968,Diabetes,57,1,High Protein,3
55,M,19.378900737806788,49,1899,3475,922,Hypertension,31,2,Balanced,2
65,F,25.539040630511924,55,1959,3404,1240,Hypertension,41,2,High Protein,1
51,F,33.97185081513596,47,1757,3645,907,Heart Disease,51,1,High Protein,3
44,M,23.59954333480335,55,1990,3245,1146,Heart Disease,48,2,Low Potassium,1
47,F,33.141708270700676,65,2180,3625,970,Diabetes,16,1,Low Sodium,1
79,M,28.440485960154433,55,2267,3526,1109,None,31,2,Low Sodium,1
65,F,19.218088157164363,62,2157,3405,1121,Hypertension,44,2,Low Phosphorus,1
47,M,20.889231185284178,46,2220,3537,945,None,38,1,Low Sodium,3
46,F,33.79131615647411,62,1951,3074,1211,None,27,2,Balanced,2
47,F,23.728281775155672,57,2275,3500,1045,None,35,1,High Protein,2
63,M,22.711230013098966,51,1780,3678,1209,None,41,2,Low Phosphorus,1
62,F,23.069726457737858,60,1562,3323,1026,Heart Disease,43,2,Low Phosphorus,1
62,F,19.56215909696882,59,1722,3508,965,None,30,2,Low Phosphorus,3
44,M,19.262161892310417,61,1666,3677,1242,Heart Disease,23,2,Balanced,3
69,F,33.565028567812085,51,2039,3446,916,Heart Disease,48,2,Balanced,1
68,M,29.067258536558732,64,2277,3398,990,Heart Disease,34,2,High Protein,3
56,M,20.093910609647807,60,2336,3409,1262,Hypertension,54,1,Low Phosphorus,1
58,M,31.039148862906792,48,2369,3522,988,Hypertension,27,1,High Protein,1
49,M,28.56868630699509,52,1650,3687,1141,None,56,2,High Protein,1
51,M,25.81897229211454,57,1962,3693,1220,Heart Disease,50,1,Low Sodium,2
51,F,32.099316439033466,60,1671,3001,957,Diabetes,22,2,Low Phosphorus,3
55,F,27.642174156945075,60,2028,3004,1071,None,33,1,Balanced,2
51,M,34.618710704465585,53,1531,3447,1172,Hypertension,33,1,Balanced,2
62,F,34.74951553399714,48,1982,3497,1089,Diabetes,57,1,Balanced,3
65,F,26.487645392036654,61,2084,3461,1093,Heart Disease,25,1,Low Phosphorus,3
75,M,33.507331899204246,51,1816,3288,1238,Hypertension,15,1,Low Sodium,1
61,F,34.26258230275746,62,2049,3059,952,Heart Disease,16,2,Low Sodium,3
49,F,21.517459080661496,49,2274,3304,1271,None,48,1,Low Sodium,1
50,M,24.51043358428764,65,2230,3308,1253,Heart Disease,29,2,Low Potassium,1
68,F,25.37891994519571,52,2278,3266,1135,Diabetes,20,1,Balanced,1
48,F,33.43451403269776,61,1794,3349,932,Heart Disease,17,1,High Protein,2
49,M,24.083538966036972,48,1774,3015,953,Hypertension,19,2,Low Sodium,2
41,M,21.64837901605087,50,1682,3133,915,Diabetes,40,1,High Protein,3
71,F,29.864586820882764,65,2203,3227,1244,None,56,1,Low Sodium,3
80,M,20.115662318102103,64,1584,3276,1173,Hypertension,48,1,Low Potassium,3
50,F,30.51296987600044,55,1957,3250,1119,Diabetes,15,2,Low Sodium,1
50,F,25.760111105875822,52,2097,3221,936,Diabetes,36,1,Low Potassium,2
53,M,31.93473117285959,65,1815,3463,954,Hypertension,53,2,Balanced,3
72,F,30.356885094933524,52,2056,3274,1265,Hypertension,38,1,Balanced,1
67,F,19.5808247547552,60,2081,3349,1241,Heart Disease,47,2,Balanced,2
61,M,23.32876429838346,58,2042,3004,938,Diabetes,59,1,Low Phosphorus,3
43,F,22.94998773094898,49,1636,3321,964,None,49,1,Low Sodium,3
79,F,31.414732384113776,59,2346,3054,989,Hypertension,40,1,Low Phosphorus,2
42,F,30.319641498778196,50,2244,3145,901,None,57,1,Balanced,1
66,M,20.14260825964677,58,1527,3050,1114,Diabetes,34,2,High Protein,2
53,F,20.79479305116346,57,1543,3096,1084,Heart Disease,57,1,Low Phosphorus,3
73,M,30.348057927507533,55,1969,3593,913,Hypertension,35,1,Low Sodium,1
47,F,29.052469759127746,56,2189,3010,1161,None,22,2,Low Potassium,1
70,M,19.803148291345618,57,1714,3586,962,Hypertension,39,2,Balanced,2
46,M,27.838260711096098,46,2026,3439,1129,Hypertension,15,2,Low Sodium,3
41,F,31.715011543157758,56,2334,3558,1080,None,27,2,Balanced,1
59,M,25.612389672826044,51,2387,3610,1276,Hypertension,41,1,Low Phosphorus,3
47,M,30.664290348600712,53,1565,3332,968,None,17,2,Low Sodium,1
68,M,20.4700103000671,53,2115,3254,1149,Heart Disease,31,1,Low Sodium,2
74,F,21.857767752479983,50,2208,3162,994,Heart Disease,54,1,High Protein,3
78,M,23.407578872788292,54,1655,3041,1287,Diabetes,56,2,Low Sodium,3
44,F,31.311648187454786,57,2276,3399,961,Hypertension,45,1,Low Phosphorus,2
72,F,25.539987775722587,54,1927,3110,905,Diabetes,37,1,Low Potassium,2
58,F,27.107305005680168,50,2331,3257,948,Heart Disease,23,2,Low Sodium,1
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