From 33eda389df25f0554dcf27314b9edc31606ab31b Mon Sep 17 00:00:00 2001 From: Ailixier KUERBAN Date: Mon, 7 Sep 2026 23:00:15 +0200 Subject: [PATCH] Add files via upload --- LAB_Hyperparameter_Tuning.ipynb | 2190 +++++++++++++++++++++++++++++++ 1 file changed, 2190 insertions(+) create mode 100644 LAB_Hyperparameter_Tuning.ipynb diff --git a/LAB_Hyperparameter_Tuning.ipynb b/LAB_Hyperparameter_Tuning.ipynb new file mode 100644 index 0000000..9de96ec --- /dev/null +++ b/LAB_Hyperparameter_Tuning.ipynb @@ -0,0 +1,2190 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 19, + "id": "1c8994d8", + "metadata": {}, + "outputs": [], + "source": [ + "#Libraries\n", + "import pandas as pd\n", + "import numpy as np\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.preprocessing import StandardScaler\n", + "from sklearn.ensemble import RandomForestClassifier\n", + "from sklearn.feature_selection import SelectFromModel\n", + "from sklearn.ensemble import GradientBoostingClassifier\n", + "from sklearn.model_selection import GridSearchCV\n", + "from sklearn.metrics import accuracy_score, classification_report" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9e6d18ed", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.microsoft.datawrangler.viewer.v0+json": { + "columns": [ + { + "name": "index", + "rawType": "int64", + "type": "integer" + }, + { + "name": "PassengerId", + "rawType": "object", + "type": "string" + }, + { + "name": "HomePlanet", + "rawType": "object", + "type": "string" + }, + { + "name": "CryoSleep", + "rawType": "object", + "type": "unknown" + }, + { + "name": "Cabin", + "rawType": "object", + "type": "string" + }, + { + "name": "Destination", + "rawType": "object", + "type": "string" + }, + { + "name": "Age", + "rawType": "float64", + "type": "float" + }, + { + "name": "VIP", + "rawType": "object", + "type": "unknown" + }, + { + "name": "RoomService", + "rawType": "float64", + "type": "float" + }, + { + "name": "FoodCourt", + "rawType": "float64", + "type": "float" + }, + { + "name": "ShoppingMall", + "rawType": "float64", + "type": "float" + }, + { + "name": "Spa", + "rawType": "float64", + "type": "float" + }, + { + "name": "VRDeck", + "rawType": "float64", + "type": "float" + }, + { + "name": "Name", + "rawType": "object", + "type": "string" + }, + { + "name": "Transported", + "rawType": "bool", + "type": "boolean" + } + ], + "ref": "72c849aa-c1c2-46ef-9c45-b684a5d362bb", + "rows": [ + [ + "0", + "0001_01", + "Europa", + "False", + "B/0/P", + "TRAPPIST-1e", + "39.0", + "False", + "0.0", + "0.0", + "0.0", + "0.0", + "0.0", + "Maham Ofracculy", + "False" + ], + [ + "1", + "0002_01", + "Earth", + "False", + "F/0/S", + "TRAPPIST-1e", + "24.0", + "False", + "109.0", + "9.0", + "25.0", + "549.0", + "44.0", + "Juanna Vines", + "True" + ], + [ + "2", + "0003_01", + "Europa", + "False", + "A/0/S", + "TRAPPIST-1e", + "58.0", + "True", + "43.0", + "3576.0", + "0.0", + "6715.0", + "49.0", + "Altark Susent", + "False" + ], + [ + "3", + "0003_02", + "Europa", + "False", + "A/0/S", + "TRAPPIST-1e", + "33.0", + "False", + "0.0", + "1283.0", + "371.0", + "3329.0", + "193.0", + "Solam Susent", + "False" + ], + [ + "4", + "0004_01", + "Earth", + "False", + "F/1/S", + "TRAPPIST-1e", + "16.0", + "False", + "303.0", + "70.0", + "151.0", + "565.0", + "2.0", + "Willy Santantines", + "True" + ] + ], + "shape": { + "columns": 14, + "rows": 5 + } + }, + "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": 5, + "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": "code", + "execution_count": 29, + "id": "c9842b2c", + "metadata": {}, + "outputs": [], + "source": [ + "num_cols = ['Age', 'RoomService', 'FoodCourt', 'ShoppingMall', 'Spa', 'VRDeck']\n", + "spaceship[num_cols] = spaceship[num_cols].fillna(spaceship[num_cols].median())" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "5e4c4d13", + "metadata": {}, + "outputs": [], + "source": [ + "scaler = StandardScaler()\n", + "spaceship_scaled = spaceship.copy()\n", + "spaceship_scaled[num_cols] = scaler.fit_transform(spaceship[num_cols])" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "12fd2f30", + "metadata": {}, + "outputs": [], + "source": [ + "X_num = spaceship_scaled[num_cols]\n", + "y = spaceship['Transported'].astype(int)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "b794221e", + "metadata": {}, + "outputs": [], + "source": [ + "SEED = 1" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "e6675a7a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
RandomForestClassifier(random_state=1)
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": [ + "RandomForestClassifier(random_state=1)" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "selector_model = RandomForestClassifier(random_state=SEED)\n", + "selector_model.fit(X_num, y)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "3c846ba2", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "c:\\Users\\user\\anaconda3\\Lib\\site-packages\\sklearn\\utils\\validation.py:2742: UserWarning: X has feature names, but SelectFromModel was fitted without feature names\n", + " warnings.warn(\n" + ] + } + ], + "source": [ + "selector = SelectFromModel(selector_model, threshold='mean', prefit=True)\n", + "X = selector.transform(X_num)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "19581a94", + "metadata": {}, + "outputs": [], + "source": [ + "X_train, X_test, y_train, y_test = train_test_split(\n", + " X, y,\n", + " test_size=0.2,\n", + " random_state=SEED)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "3ceaec4d", + "metadata": {}, + "outputs": [], + "source": [ + "best_gb_model = GradientBoostingClassifier(random_state=SEED)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "62cc85ee", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Initial Model Accuracy: 0.7752\n" + ] + } + ], + "source": [ + "best_gb_model.fit(X_train, y_train)\n", + "y_pred_initial = best_gb_model.predict(X_test)\n", + "print(f\"Initial Model Accuracy: {accuracy_score(y_test, y_pred_initial):.4f}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "ad6ca136", + "metadata": {}, + "outputs": [], + "source": [ + "param_grid = {\n", + " 'n_estimators': [50, 100, 200],\n", + " 'learning_rate': [0.01, 0.1, 0.2],\n", + " 'max_depth': [3, 5]\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "b8272ffd", + "metadata": {}, + "outputs": [], + "source": [ + "gb = GradientBoostingClassifier(random_state=42)\n", + "grid_search = GridSearchCV(\n", + " estimator=gb,\n", + " param_grid=param_grid,\n", + " cv=3, scoring='accuracy',\n", + " n_jobs=-1)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "11556341", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
GridSearchCV(cv=3, estimator=GradientBoostingClassifier(random_state=42),\n",
+       "             n_jobs=-1,\n",
+       "             param_grid={'learning_rate': [0.01, 0.1, 0.2], 'max_depth': [3, 5],\n",
+       "                         'n_estimators': [50, 100, 200]},\n",
+       "             scoring='accuracy')
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": [ + "GridSearchCV(cv=3, estimator=GradientBoostingClassifier(random_state=42),\n", + " n_jobs=-1,\n", + " param_grid={'learning_rate': [0.01, 0.1, 0.2], 'max_depth': [3, 5],\n", + " 'n_estimators': [50, 100, 200]},\n", + " scoring='accuracy')" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "grid_search.fit(X_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "6f5f83a2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Best Parameters: {'learning_rate': 0.2, 'max_depth': 3, 'n_estimators': 50}\n" + ] + } + ], + "source": [ + "print(\"Best Parameters:\", grid_search.best_params_)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "9f69c44b", + "metadata": {}, + "outputs": [], + "source": [ + "tuned_model = grid_search.best_estimator_\n", + "y_pred_tuned = tuned_model.predict(X_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "517dd5ad", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Tuned Model Accuracy: 0.7740\n", + " precision recall f1-score support\n", + "\n", + " 0 0.78 0.74 0.76 843\n", + " 1 0.77 0.81 0.79 896\n", + "\n", + " accuracy 0.77 1739\n", + " macro avg 0.77 0.77 0.77 1739\n", + "weighted avg 0.77 0.77 0.77 1739\n", + "\n" + ] + } + ], + "source": [ + "y_pred_tuned = tuned_model.predict(X_test)\n", + "print(f\"Tuned Model Accuracy: {accuracy_score(y_test, y_pred_tuned):.4f}\")\n", + "print(classification_report(y_test, y_pred_tuned))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}