From 587825947b0fc2a5befa59fbf64895b1bb0def19 Mon Sep 17 00:00:00 2001 From: Ray Date: Sat, 2 May 2026 20:36:49 +0000 Subject: [PATCH] Add progress --- HospitalCancerClassification.ipynb | 1409 +++++++++++++++++++++++++++- 1 file changed, 1392 insertions(+), 17 deletions(-) diff --git a/HospitalCancerClassification.ipynb b/HospitalCancerClassification.ipynb index b32dbeb..cc15719 100644 --- a/HospitalCancerClassification.ipynb +++ b/HospitalCancerClassification.ipynb @@ -50,7 +50,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "3a05014a-200c-4bc6-9667-568898547852", "metadata": {}, "outputs": [], @@ -71,7 +71,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "id": "10d93e7f-9ba8-45a4-831d-7ba48046fea7", "metadata": {}, "outputs": [ @@ -81,7 +81,7 @@ "np.int64(10)" ] }, - "execution_count": 3, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -95,7 +95,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "id": "3434f6bb-7cdb-4434-a948-1dc7d9e7d260", "metadata": {}, "outputs": [ @@ -105,7 +105,7 @@ "np.int64(0)" ] }, - "execution_count": 4, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -127,7 +127,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "id": "a8887198-0dbd-471d-bd1e-836d34def83a", "metadata": {}, "outputs": [ @@ -195,7 +195,7 @@ "4 60 55-64" ] }, - "execution_count": 5, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -224,7 +224,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "id": "f9beff06-ce08-479d-b63d-56ca8aaf6338", "metadata": {}, "outputs": [ @@ -310,7 +310,7 @@ "max 1.735732e+00 1.772241e+00" ] }, - "execution_count": 6, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -342,7 +342,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "id": "8bc14115-e855-4233-9058-eff0ce5c9386", "metadata": {}, "outputs": [ @@ -542,7 +542,7 @@ "4 60 no none -1.446316 0.649743 212.8 yes 5 moderate False False False False True False False False True False False False False False True" ] }, - "execution_count": 7, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -568,7 +568,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "id": "a5174be9-c40b-457e-afd5-1ec97c70ed0a", "metadata": { "scrolled": true @@ -580,7 +580,7 @@ "((693, 23), (148, 23), (149, 23))" ] }, - "execution_count": 8, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -621,7 +621,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "id": "c94e2161-e95d-4a84-864d-bc1388bd4d48", "metadata": {}, "outputs": [ @@ -631,13 +631,12 @@ "(693, 21)" ] }, - "execution_count": 9, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "\n", "# Drop two unnecessary features/columns that aren't relevant to cancer risk evaluation and wouldn't affect if someone would be likely to have cancer.\n", "X_train = X_train.drop(['travelled_overseas', 'number_of_children'], axis=1)\n", "X_val = X_val.drop(['travelled_overseas', 'number_of_children'], axis=1)\n", @@ -646,6 +645,1382 @@ "# Print the results to allow the user to determine the changes have worked\n", "X_train.shape\n" ] + }, + { + "cell_type": "markdown", + "id": "49174e92-2e15-461a-844f-ffa90c0d7c99", + "metadata": {}, + "source": [ + "# Part 3: Train and evaluate ML model - Task 1: Train ML model 1" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "b95cb28a-5312-4f55-8485-61c4e95e782e", + "metadata": {}, + "outputs": [], + "source": [ + "# Select an appropriate initial model size by setting the number of layers and neurons.\n", + "\n", + "\n", + "from sklearn.neural_network import MLPClassifier\n", + "\n", + "mlp_model_1 = MLPClassifier(\n", + " hidden_layer_sizes=(64, 32),\n", + " activation='relu',\n", + " solver='adam',\n", + " max_iter=300,\n", + " random_state=42\n", + ")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "9a44bcdd-8605-4e21-9a7a-1a3296f46909", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "age int64\n", + "family_history str\n", + "alcohol_consumption str\n", + "bmi float64\n", + "blood_pressure float64\n", + "cholesterol_level float64\n", + "age_group_15-24 bool\n", + "age_group_25-34 bool\n", + "age_group_35-44 bool\n", + "age_group_45-54 bool\n", + "age_group_55-64 bool\n", + "age_group_65-74 bool\n", + "age_group_75-84 bool\n", + "age_group_85+ bool\n", + "smoking_history_former bool\n", + "smoking_history_never bool\n", + "diet_good bool\n", + "diet_poor bool\n", + "exercise_frequency_low bool\n", + "exercise_frequency_moderate bool\n", + "exercise_frequency_none bool\n", + "dtype: object" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# BUG FIXING: List any non numerical rows\n", + "X_train.dtypes" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "f16e2e40-e065-44ca-b944-e9a2921d89db", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/tmp/ipykernel_82/146862157.py:2: Pandas4Warning: For backward compatibility, 'str' dtypes are included by select_dtypes when 'object' dtype is specified. This behavior is deprecated and will be removed in a future version. Explicitly pass 'str' to `include` to select them, or to `exclude` to remove them and silence this warning.\n", + "See https://pandas.pydata.org/docs/user_guide/migration-3-strings.html#string-migration-select-dtypes for details on how to write code that works with pandas 2 and 3.\n", + " X_train.select_dtypes(include=['object']).head()\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
family_historyalcohol_consumption
494yesnone
379yeshigh
220yesnone
732yeshigh
869nolow
\n", + "
" + ], + "text/plain": [ + " family_history alcohol_consumption\n", + "494 yes none\n", + "379 yes high\n", + "220 yes none\n", + "732 yes high\n", + "869 no low" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# BUG FIXING: Check if the text value exists\n", + "X_train.select_dtypes(include=['object']).head()" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "f62a9503-664a-43d9-9afa-0029695acce8", + "metadata": {}, + "outputs": [], + "source": [ + "# BUG FIXING - Rayyan Hodges\n", + "\n", + "# Force all non-numeric columns to be one-hot encoded\n", + "X_train = pd.get_dummies(X_train)\n", + "X_val = pd.get_dummies(X_val)\n", + "X_test = pd.get_dummies(X_test)\n", + "\n", + "# Align columns so they match exactly\n", + "X_train, X_val = X_train.align(X_val, join=\"left\", axis=1, fill_value=0)\n", + "X_train, X_test = X_train.align(X_test, join=\"left\", axis=1, fill_value=0)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "cfcddd52-1a78-4c07-8f5d-036682a8cad6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
MLPClassifier(hidden_layer_sizes=(64, 32), max_iter=300, random_state=42)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ], + "text/plain": [ + "MLPClassifier(hidden_layer_sizes=(64, 32), max_iter=300, random_state=42)" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Train model 1 on generated data\n", + "mlp_model_1.fit(X_train, y_train)" + ] + }, + { + "cell_type": "markdown", + "id": "600e90f1-fc12-4300-ab60-76b4656bf5d8", + "metadata": {}, + "source": [ + "# Run the validation data through the ML model." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "4d08a5ac-93cf-4da5-a266-7052ea31ac6a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array(['low', 'high', 'high', 'moderate', 'low', 'high', 'high', 'high',\n", + " 'low', 'low'], dtype='