diff --git a/lab-hyper-tuning.ipynb b/lab-hyper-tuning.ipynb deleted file mode 100644 index 847d487..0000000 --- a/lab-hyper-tuning.ipynb +++ /dev/null @@ -1,343 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# LAB | Hyperparameter Tuning" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Load the data**\n", - "\n", - "Finally step in order to maximize the performance on your Spaceship Titanic model.\n", - "\n", - "The data can be found here:\n", - "\n", - "https://raw.githubusercontent.com/data-bootcamp-v4/data/main/spaceship_titanic.csv\n", - "\n", - "Metadata\n", - "\n", - "https://github.com/data-bootcamp-v4/data/blob/main/spaceship_titanic.md" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "So far we've been training and evaluating models with default values for hyperparameters.\n", - "\n", - "Today we will perform the same feature engineering as before, and then compare the best working models you got so far, but now fine tuning it's hyperparameters." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "#Libraries\n", - "import pandas as pd\n", - "import numpy as np\n", - "from sklearn.model_selection import train_test_split" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "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", - " \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", - " \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", - "
PassengerIdHomePlanetCryoSleepCabinDestinationAgeVIPRoomServiceFoodCourtShoppingMallSpaVRDeckNameTransported
00001_01EuropaFalseB/0/PTRAPPIST-1e39.0False0.00.00.00.00.0Maham OfracculyFalse
10002_01EarthFalseF/0/STRAPPIST-1e24.0False109.09.025.0549.044.0Juanna VinesTrue
20003_01EuropaFalseA/0/STRAPPIST-1e58.0True43.03576.00.06715.049.0Altark SusentFalse
30003_02EuropaFalseA/0/STRAPPIST-1e33.0False0.01283.0371.03329.0193.0Solam SusentFalse
40004_01EarthFalseF/1/STRAPPIST-1e16.0False303.070.0151.0565.02.0Willy SantantinesTrue
\n", - "
" - ], - "text/plain": [ - " PassengerId HomePlanet CryoSleep Cabin Destination Age VIP \\\n", - "0 0001_01 Europa False B/0/P TRAPPIST-1e 39.0 False \n", - "1 0002_01 Earth False F/0/S TRAPPIST-1e 24.0 False \n", - "2 0003_01 Europa False A/0/S TRAPPIST-1e 58.0 True \n", - "3 0003_02 Europa False A/0/S TRAPPIST-1e 33.0 False \n", - "4 0004_01 Earth False F/1/S TRAPPIST-1e 16.0 False \n", - "\n", - " RoomService FoodCourt ShoppingMall Spa VRDeck Name \\\n", - "0 0.0 0.0 0.0 0.0 0.0 Maham Ofracculy \n", - "1 109.0 9.0 25.0 549.0 44.0 Juanna Vines \n", - "2 43.0 3576.0 0.0 6715.0 49.0 Altark Susent \n", - "3 0.0 1283.0 371.0 3329.0 193.0 Solam Susent \n", - "4 303.0 70.0 151.0 565.0 2.0 Willy Santantines \n", - "\n", - " Transported \n", - "0 False \n", - "1 True \n", - "2 False \n", - "3 False \n", - "4 True " - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "spaceship = pd.read_csv(\"https://raw.githubusercontent.com/data-bootcamp-v4/data/main/spaceship_titanic.csv\")\n", - "spaceship.head()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now perform the same as before:\n", - "- Feature Scaling\n", - "- Feature Selection\n" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "#your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- Now let's use the best model we got so far in order to see how it can improve when we fine tune it's hyperparameters." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "#your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- Evaluate your model" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "#your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Grid/Random Search**" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For this lab we will use Grid Search." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- Define hyperparameters to fine tune." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "#your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- Run Grid Search" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- Evaluate your model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "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.10.9" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/lab-hyper_tuning.ipynb b/lab-hyper_tuning.ipynb new file mode 100644 index 0000000..86d4c44 --- /dev/null +++ b/lab-hyper_tuning.ipynb @@ -0,0 +1,1104 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "5Dq4EULCJ0Rr" + }, + "source": [ + "# LAB | Hyperparameter Tuning" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "boDSy21iJ0R4" + }, + "source": [ + "**Load the data**\n", + "\n", + "Finally step in order to maximize the performance on your Spaceship Titanic model.\n", + "\n", + "The data can be found here:\n", + "\n", + "https://raw.githubusercontent.com/data-bootcamp-v4/data/main/spaceship_titanic.csv\n", + "\n", + "Metadata\n", + "\n", + "https://github.com/data-bootcamp-v4/data/blob/main/spaceship_titanic.md" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6fu8ECNTJ0R6" + }, + "source": [ + "So far we've been training and evaluating models with default values for hyperparameters.\n", + "\n", + "Today we will perform the same feature engineering as before, and then compare the best working models you got so far, but now fine tuning it's hyperparameters." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "id": "SlHmS7qMaE-_" + }, + "outputs": [], + "source": [ + "#Libraries\n", + "\n", + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "import matplotlib.pyplot as plt\n", + "import seaborn as sns\n", + "\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.tree import DecisionTreeRegressor\n", + "from sklearn.ensemble import BaggingRegressor, RandomForestRegressor,AdaBoostRegressor, GradientBoostingRegressor\n", + "\n", + "from sklearn.preprocessing import MinMaxScaler, StandardScaler\n", + "from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error, root_mean_squared_error" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 313 + }, + "id": "uKj6PVg6J0R_", + "outputId": "6577a783-6153-406d-a681-8801cd23e7bb" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + " PassengerId HomePlanet CryoSleep Cabin Destination Age VIP \\\n", + "0 0001_01 Europa False B/0/P TRAPPIST-1e 39.0 False \n", + "1 0002_01 Earth False F/0/S TRAPPIST-1e 24.0 False \n", + "2 0003_01 Europa False A/0/S TRAPPIST-1e 58.0 True \n", + "3 0003_02 Europa False A/0/S TRAPPIST-1e 33.0 False \n", + "4 0004_01 Earth False F/1/S TRAPPIST-1e 16.0 False \n", + "\n", + " RoomService FoodCourt ShoppingMall Spa VRDeck Name \\\n", + "0 0.0 0.0 0.0 0.0 0.0 Maham Ofracculy \n", + "1 109.0 9.0 25.0 549.0 44.0 Juanna Vines \n", + "2 43.0 3576.0 0.0 6715.0 49.0 Altark Susent \n", + "3 0.0 1283.0 371.0 3329.0 193.0 Solam Susent \n", + "4 303.0 70.0 151.0 565.0 2.0 Willy Santantines \n", + "\n", + " Transported \n", + "0 False \n", + "1 True \n", + "2 False \n", + "3 False \n", + "4 True " + ], + "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", + " \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", + " \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", + "
PassengerIdHomePlanetCryoSleepCabinDestinationAgeVIPRoomServiceFoodCourtShoppingMallSpaVRDeckNameTransported
00001_01EuropaFalseB/0/PTRAPPIST-1e39.0False0.00.00.00.00.0Maham OfracculyFalse
10002_01EarthFalseF/0/STRAPPIST-1e24.0False109.09.025.0549.044.0Juanna VinesTrue
20003_01EuropaFalseA/0/STRAPPIST-1e58.0True43.03576.00.06715.049.0Altark SusentFalse
30003_02EuropaFalseA/0/STRAPPIST-1e33.0False0.01283.0371.03329.0193.0Solam SusentFalse
40004_01EarthFalseF/1/STRAPPIST-1e16.0False303.070.0151.0565.02.0Willy SantantinesTrue
\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe", + "variable_name": "spaceship", + "summary": "{\n \"name\": \"spaceship\",\n \"rows\": 8693,\n \"fields\": [\n {\n \"column\": \"PassengerId\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 8693,\n \"samples\": [\n \"0337_02\",\n \"2891_01\",\n \"8998_01\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"HomePlanet\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"Europa\",\n \"Earth\",\n \"Mars\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"CryoSleep\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n true,\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Cabin\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 6560,\n \"samples\": [\n \"A/29/P\",\n \"G/1452/P\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Destination\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"TRAPPIST-1e\",\n \"PSO J318.5-22\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Age\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 14.489021423908726,\n \"min\": 0.0,\n \"max\": 79.0,\n \"num_unique_values\": 80,\n \"samples\": [\n 30.0,\n 39.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"VIP\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 2,\n \"samples\": [\n true,\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"RoomService\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 666.7176629280303,\n \"min\": 0.0,\n \"max\": 14327.0,\n \"num_unique_values\": 1273,\n \"samples\": [\n 2103.0,\n 1720.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"FoodCourt\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1611.4892403551144,\n \"min\": 0.0,\n \"max\": 29813.0,\n \"num_unique_values\": 1507,\n \"samples\": [\n 61.0,\n 783.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"ShoppingMall\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 604.6964584708251,\n \"min\": 0.0,\n \"max\": 23492.0,\n \"num_unique_values\": 1115,\n \"samples\": [\n 619.0,\n 356.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Spa\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1136.705534834416,\n \"min\": 0.0,\n \"max\": 22408.0,\n \"num_unique_values\": 1327,\n \"samples\": [\n 190.0,\n 622.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"VRDeck\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1145.7171888057144,\n \"min\": 0.0,\n \"max\": 24133.0,\n \"num_unique_values\": 1306,\n \"samples\": [\n 977.0,\n 624.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Name\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 8473,\n \"samples\": [\n \"Nancey Bookerson\",\n \"Nelly Benney\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Transported\",\n \"properties\": {\n \"dtype\": \"boolean\",\n \"num_unique_values\": 2,\n \"samples\": [\n true,\n false\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" + } + }, + "metadata": {}, + "execution_count": 22 + } + ], + "source": [ + "spaceship = pd.read_csv(\"https://raw.githubusercontent.com/data-bootcamp-v4/data/main/spaceship_titanic.csv\")\n", + "spaceship.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AyCh2r5OJ0SB" + }, + "source": [ + "Now perform the same as before:\n", + "- Feature Scaling\n", + "- Feature Selection\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "a182e2e9" + }, + "source": [ + "from sklearn.preprocessing import StandardScaler, OneHotEncoder\n", + "from sklearn.impute import SimpleImputer\n", + "from sklearn.compose import ColumnTransformer\n", + "from sklearn.pipeline import Pipeline\n", + "\n", + "# Make a copy to avoid modifying the original DataFrame directly\n", + "df = spaceship.copy()\n", + "\n", + "# Separate target variable\n", + "X = df.drop('Transported', axis=1)\n", + "y = df['Transported']" + ], + "execution_count": 23, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "cdb93f41" + }, + "source": [ + "# Feature Engineering for 'Cabin' column\n", + "# Split 'Cabin' into 'Cabin_deck', 'Cabin_num', 'Cabin_side'\n", + "X[['Cabin_deck', 'Cabin_num', 'Cabin_side']] = X['Cabin'].str.split('/', expand=True)\n", + "X = X.drop('Cabin', axis=1)\n", + "\n", + "# Convert 'Cabin_num' to numeric, coerce errors to NaN\n", + "X['Cabin_num'] = pd.to_numeric(X['Cabin_num'], errors='coerce')" + ], + "execution_count": 24, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 273 + }, + "id": "bc0ef1b2", + "outputId": "05bc457b-8805-4334-8b0c-6a264ad030a0" + }, + "source": [ + "numerical_features = X.select_dtypes(include=np.number).columns.tolist()\n", + "categorical_features = X.select_dtypes(include='object').columns.tolist()\n", + "\n", + "# Remove 'PassengerId' and 'Name' from features if they are present\n", + "if 'PassengerId' in numerical_features:\n", + " numerical_features.remove('PassengerId')\n", + "if 'PassengerId' in categorical_features:\n", + " categorical_features.remove('PassengerId')\n", + "if 'Name' in categorical_features:\n", + " categorical_features.remove('Name')\n", + "\n", + "# Define preprocessing steps\n", + "numerical_transformer = Pipeline(steps=[\n", + " ('imputer', SimpleImputer(strategy='mean')),\n", + " ('scaler', StandardScaler())\n", + "])\n", + "\n", + "categorical_transformer = Pipeline(steps=[\n", + " ('imputer', SimpleImputer(strategy='most_frequent')),\n", + " ('onehot', OneHotEncoder(handle_unknown='ignore'))\n", + "])\n", + "\n", + "# Create a column transformer to apply different transformations to different columns\n", + "preprocessor = ColumnTransformer(\n", + " transformers=[\n", + " ('num', numerical_transformer, numerical_features),\n", + " ('cat', categorical_transformer, categorical_features)\n", + " ],\n", + " remainder='passthrough' # Keep other columns (like PassengerId for submission if needed)\n", + ")\n", + "\n", + "# Apply preprocessing\n", + "X_preprocessed = preprocessor.fit_transform(X)\n", + "\n", + "# Get feature names after one-hot encoding for categorical features and including passthrough columns\n", + "all_feature_names = preprocessor.get_feature_names_out()\n", + "\n", + "# Convert the preprocessed data back to a DataFrame\n", + "X_processed_df = pd.DataFrame(X_preprocessed, columns=all_feature_names)\n", + "\n", + "print(\"Shape of preprocessed data:\", X_processed_df.shape)\n", + "display(X_processed_df.head())" + ], + "execution_count": 25, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Shape of preprocessed data: (8693, 29)\n" + ] + }, + { + "output_type": "display_data", + "data": { + "text/plain": [ + " num__Age num__RoomService num__FoodCourt num__ShoppingMall num__Spa \\\n", + "0 0.709437 -0.34059 -0.287314 -0.290817 -0.276663 \n", + "1 -0.336717 -0.175364 -0.281669 -0.248968 0.211505 \n", + "2 2.034566 -0.275409 1.955616 -0.290817 5.694289 \n", + "3 0.290975 -0.34059 0.517406 0.330225 2.683471 \n", + "4 -0.894666 0.118709 -0.243409 -0.038048 0.225732 \n", + "\n", + " num__VRDeck num__Cabin_num cat__HomePlanet_Earth cat__HomePlanet_Europa \\\n", + "0 -0.269023 -1.186627 0.0 1.0 \n", + "1 -0.230194 -1.186627 1.0 0.0 \n", + "2 -0.225782 -1.186627 0.0 1.0 \n", + "3 -0.098708 -1.186627 0.0 1.0 \n", + "4 -0.267258 -1.184651 1.0 0.0 \n", + "\n", + " cat__HomePlanet_Mars ... cat__Cabin_deck_C cat__Cabin_deck_D \\\n", + "0 0.0 ... 0.0 0.0 \n", + "1 0.0 ... 0.0 0.0 \n", + "2 0.0 ... 0.0 0.0 \n", + "3 0.0 ... 0.0 0.0 \n", + "4 0.0 ... 0.0 0.0 \n", + "\n", + " cat__Cabin_deck_E cat__Cabin_deck_F cat__Cabin_deck_G cat__Cabin_deck_T \\\n", + "0 0.0 0.0 0.0 0.0 \n", + "1 0.0 1.0 0.0 0.0 \n", + "2 0.0 0.0 0.0 0.0 \n", + "3 0.0 0.0 0.0 0.0 \n", + "4 0.0 1.0 0.0 0.0 \n", + "\n", + " cat__Cabin_side_P cat__Cabin_side_S remainder__PassengerId \\\n", + "0 1.0 0.0 0001_01 \n", + "1 0.0 1.0 0002_01 \n", + "2 0.0 1.0 0003_01 \n", + "3 0.0 1.0 0003_02 \n", + "4 0.0 1.0 0004_01 \n", + "\n", + " remainder__Name \n", + "0 Maham Ofracculy \n", + "1 Juanna Vines \n", + "2 Altark Susent \n", + "3 Solam Susent \n", + "4 Willy Santantines \n", + "\n", + "[5 rows x 29 columns]" + ], + "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", + " \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", + " \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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + "
num__Agenum__RoomServicenum__FoodCourtnum__ShoppingMallnum__Spanum__VRDecknum__Cabin_numcat__HomePlanet_Earthcat__HomePlanet_Europacat__HomePlanet_Mars...cat__Cabin_deck_Ccat__Cabin_deck_Dcat__Cabin_deck_Ecat__Cabin_deck_Fcat__Cabin_deck_Gcat__Cabin_deck_Tcat__Cabin_side_Pcat__Cabin_side_Sremainder__PassengerIdremainder__Name
00.709437-0.34059-0.287314-0.290817-0.276663-0.269023-1.1866270.01.00.0...0.00.00.00.00.00.01.00.00001_01Maham Ofracculy
1-0.336717-0.175364-0.281669-0.2489680.211505-0.230194-1.1866271.00.00.0...0.00.00.01.00.00.00.01.00002_01Juanna Vines
22.034566-0.2754091.955616-0.2908175.694289-0.225782-1.1866270.01.00.0...0.00.00.00.00.00.00.01.00003_01Altark Susent
30.290975-0.340590.5174060.3302252.683471-0.098708-1.1866270.01.00.0...0.00.00.00.00.00.00.01.00003_02Solam Susent
4-0.8946660.118709-0.243409-0.0380480.225732-0.267258-1.1846511.00.00.0...0.00.00.01.00.00.00.01.00004_01Willy Santantines
\n", + "

5 rows × 29 columns

\n", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + "
\n" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "dataframe" + } + }, + "metadata": {} + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7CAOdSI5aE_C" + }, + "source": [ + "**Perform Train Test Split**" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "id": "q9xAYgf2aE_C", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "9492beab-e399-404e-d5a5-8ec311dcf0f5" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "X_train shape: (6954, 29)\n", + "X_test shape: (1739, 29)\n", + "y_train shape: (6954,)\n", + "y_test shape: (1739,)\n" + ] + } + ], + "source": [ + "X_train, X_test, y_train, y_test = train_test_split(X_processed_df, y, test_size=0.2, random_state=42)\n", + "\n", + "print(f\"X_train shape: {X_train.shape}\")\n", + "print(f\"X_test shape: {X_test.shape}\")\n", + "print(f\"y_train shape: {y_train.shape}\")\n", + "print(f\"y_test shape: {y_test.shape}\")" + ] + }, + { + "cell_type": "code", + "source": [ + "# Drop non-numerical 'remainder' columns from X_train before fitting\n", + "X_train_cleaned = X_train.drop(columns=['remainder__PassengerId', 'remainder__Name'], errors='ignore')\n", + "\n", + "# Drop non-numerical 'remainder' columns from X_test before making predictions\n", + "X_test_cleaned = X_test.drop(columns=['remainder__PassengerId', 'remainder__Name'], errors='ignore')" + ], + "metadata": { + "id": "dwSWldsPMS4N" + }, + "execution_count": 27, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DcAjoYncJ0SD" + }, + "source": [ + "- Now let's use the best model we got so far in order to see how it can improve when we fine tune it's hyperparameters." + ] + }, + { + "cell_type": "markdown", + "source": [ + "The past lab after the evaluation of several models we found that:\n", + "\n", + "---** Gradient Boosting Regressor** ---\n", + "\n", + "R2 Score (Gradient Boosting): 0.4682\n", + "\n", + "Mean Absolute Error (MAE) (Gradient Boosting): 0.2812\n", + "\n", + "Mean Squared Error (MSE) (Gradient Boosting): 0.1329\n", + "\n", + "Root Mean Squared Error (RMSE) (Gradient Boosting): 0.3646\n", + "\n", + "\n", + "Got the best R2 Score so we will improve this model" + ], + "metadata": { + "id": "UEMtPxIETQBw" + } + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "c345417d", + "outputId": "cc8fd496-4300-4490-9a5a-43311e79e0c0" + }, + "source": [ + "print('\\n--- Gradient Boosting Regressor ---')\n", + "\n", + "# Initialize and train the Gradient Boosting Regressor\n", + "gradient_boosting_reg = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42)\n", + "gradient_boosting_reg.fit(X_train_cleaned, y_train)\n", + "\n", + "# Make predictions on the cleaned test set\n", + "y_pred_gb = gradient_boosting_reg.predict(X_test_cleaned)\n", + "\n" + ], + "execution_count": 28, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\n", + "--- Gradient Boosting Regressor ---\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6sLo_i-yJ0SF" + }, + "source": [ + "- Evaluate your model" + ] + }, + { + "cell_type": "code", + "source": [ + "# Evaluate the Gradient Boosting model\n", + "r2_gb = r2_score(y_test, y_pred_gb)\n", + "mae_gb = mean_absolute_error(y_test, y_pred_gb)\n", + "mse_gb = mean_squared_error(y_test, y_pred_gb)\n", + "rmse_gb = root_mean_squared_error(y_test, y_pred_gb)\n", + "\n", + "print(f\"R2 Score (Gradient Boosting): {r2_gb:.4f}\")\n", + "print(f\"Mean Absolute Error (MAE) (Gradient Boosting): {mae_gb:.4f}\")\n", + "print(f\"Mean Squared Error (MSE) (Gradient Boosting): {mse_gb:.4f}\")\n", + "print(f\"Root Mean Squared Error (RMSE) (Gradient Boosting): {rmse_gb:.4f}\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "IitLGWeSS6jW", + "outputId": "10a081f9-8f07-4888-e2ad-ad733d1ddfa9" + }, + "execution_count": 29, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "R2 Score (Gradient Boosting): 0.4682\n", + "Mean Absolute Error (MAE) (Gradient Boosting): 0.2812\n", + "Mean Squared Error (MSE) (Gradient Boosting): 0.1329\n", + "Root Mean Squared Error (RMSE) (Gradient Boosting): 0.3646\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MPLTV_DxJ0SG" + }, + "source": [ + "**Grid Search**" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "B0jVC1TvJ0SG" + }, + "source": [ + "For this lab we will use Grid Search." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BiJM6bfKJ0SH" + }, + "source": [ + "- Define hyperparameters to fine tune." + ] + }, + { + "cell_type": "code", + "source": [ + "print('\\n--- Ajusting Hyperparameters for Gradient Boosting ---')\n", + "\n", + "# Defining the parameters to look for\n", + "param_grid = {\n", + " 'n_estimators': [50, 100, 200], # Número de árboles\n", + " 'learning_rate': [0.01, 0.1, 0.2], # Tasa de aprendizaje\n", + " 'max_depth': [3, 4, 5] # Profundidad máxima de cada árbol\n", + "}" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "jQId01e2Wj-v", + "outputId": "9b7d5f6d-1e8c-468b-9c43-4d8795279431" + }, + "execution_count": 32, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\n", + "--- Ajusting Hyperparameters for Gradient Boosting ---\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "He43pe8dJ0SH" + }, + "source": [ + "- Run Grid Search" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "2d234858", + "outputId": "9f4b8fed-2721-44f1-a7dd-63f27f0f2858" + }, + "source": [ + "from sklearn.model_selection import GridSearchCV\n", + "\n", + "\n", + "# Initializing Model Gradient Boosting\n", + "gradient_boosting_reg = GradientBoostingRegressor(random_state=42)\n", + "\n", + "# Configuring GridSearchCV\n", + "# cv=5 indicates 5-fold cross-validation\n", + "# scoring='r2' to optimize the meter for R2\n", + "grid_search = GridSearchCV(estimator=gradient_boosting_reg, param_grid=param_grid,\n", + " cv=5, scoring='r2', n_jobs=-1, verbose=1)\n", + "\n", + "# Ejecuting the search of the fit in the clean data train\n", + "grid_search.fit(X_train_cleaned, y_train)\n", + "\n", + "print(f\"Mejores hiperparámetros encontrados: {grid_search.best_params_}\")\n", + "print(f\"Mejor puntuación R2 (en validación cruzada): {grid_search.best_score_:.4f}\")\n", + "\n", + "# Getting the best model\n", + "best_gb_model = grid_search.best_estimator_\n", + "\n", + "# Predctions of the best model in test data\n", + "y_pred_gb_tuned = best_gb_model.predict(X_test_cleaned)\n" + ], + "execution_count": 33, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Fitting 5 folds for each of 27 candidates, totalling 135 fits\n", + "Mejores hiperparámetros encontrados: {'learning_rate': 0.1, 'max_depth': 4, 'n_estimators': 100}\n", + "Mejor puntuación R2 (en validación cruzada): 0.4814\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OU_hXu0EJ0SI" + }, + "source": [ + "- Evaluate your model" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "7gQHI-haJ0SH", + "outputId": "a5b95486-d4f8-4e86-d08d-c9258beb72a0" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\n", + "--- Evaluación del Mejor Modelo Gradient Boosting ---\n", + "R2 Score (Gradient Boosting Ajustado): 0.4803\n", + "Mean Absolute Error (MAE) (Gradient Boosting Ajustado): 0.2712\n", + "Mean Squared Error (MSE) (Gradient Boosting Ajustado): 0.1299\n", + "Root Mean Squared Error (RMSE) (Gradient Boosting Ajustado): 0.3604\n" + ] + } + ], + "source": [ + "# Evaluating the best model\n", + "r2_gb_tuned = r2_score(y_test, y_pred_gb_tuned)\n", + "mae_gb_tuned = mean_absolute_error(y_test, y_pred_gb_tuned)\n", + "mse_gb_tuned = mean_squared_error(y_test, y_pred_gb_tuned)\n", + "rmse_gb_tuned = root_mean_squared_error(y_test, y_pred_gb_tuned)\n", + "\n", + "print(f\"\\n--- Evaluación del Mejor Modelo Gradient Boosting ---\")\n", + "print(f\"R2 Score (Gradient Boosting Ajustado): {r2_gb_tuned:.4f}\")\n", + "print(f\"Mean Absolute Error (MAE) (Gradient Boosting Ajustado): {mae_gb_tuned:.4f}\")\n", + "print(f\"Mean Squared Error (MSE) (Gradient Boosting Ajustado): {mse_gb_tuned:.4f}\")\n", + "print(f\"Root Mean Squared Error (RMSE) (Gradient Boosting Ajustado): {rmse_gb_tuned:.4f}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "id": "TKPW3XHSJ0SI" + }, + "outputs": [], + "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.10.9" + }, + "colab": { + "provenance": [] + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file