diff --git a/.ipynb_checkpoints/lab-hyper-tuning-checkpoint.ipynb b/.ipynb_checkpoints/lab-hyper-tuning-checkpoint.ipynb
new file mode 100644
index 0000000..127f2d8
--- /dev/null
+++ b/.ipynb_checkpoints/lab-hyper-tuning-checkpoint.ipynb
@@ -0,0 +1,2162 @@
+{
+ "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",
+ "import time\n",
+ "\n",
+ "import scipy.stats as st\n",
+ "\n",
+ "from sklearn.model_selection import train_test_split\n",
+ "from sklearn.preprocessing import OneHotEncoder\n",
+ "\n",
+ "\n",
+ "from sklearn.model_selection import train_test_split\n",
+ "from sklearn.tree import DecisionTreeClassifier\n",
+ "from sklearn.ensemble import BaggingClassifier, RandomForestClassifier,AdaBoostClassifier, GradientBoostingClassifier\n",
+ "\n",
+ "from sklearn.preprocessing import MinMaxScaler, StandardScaler\n",
+ "\n",
+ "from sklearn.metrics import accuracy_score, recall_score, f1_score, precision_score\n",
+ "\n",
+ "from sklearn.model_selection import GridSearchCV, RandomizedSearchCV"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " PassengerId \n",
+ " HomePlanet \n",
+ " CryoSleep \n",
+ " Cabin \n",
+ " Destination \n",
+ " Age \n",
+ " VIP \n",
+ " RoomService \n",
+ " FoodCourt \n",
+ " ShoppingMall \n",
+ " Spa \n",
+ " VRDeck \n",
+ " Name \n",
+ " Transported \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " 0 \n",
+ " 0001_01 \n",
+ " Europa \n",
+ " False \n",
+ " B/0/P \n",
+ " TRAPPIST-1e \n",
+ " 39.0 \n",
+ " False \n",
+ " 0.0 \n",
+ " 0.0 \n",
+ " 0.0 \n",
+ " 0.0 \n",
+ " 0.0 \n",
+ " Maham Ofracculy \n",
+ " False \n",
+ " \n",
+ " \n",
+ " 1 \n",
+ " 0002_01 \n",
+ " Earth \n",
+ " False \n",
+ " F/0/S \n",
+ " TRAPPIST-1e \n",
+ " 24.0 \n",
+ " False \n",
+ " 109.0 \n",
+ " 9.0 \n",
+ " 25.0 \n",
+ " 549.0 \n",
+ " 44.0 \n",
+ " Juanna Vines \n",
+ " True \n",
+ " \n",
+ " \n",
+ " 2 \n",
+ " 0003_01 \n",
+ " Europa \n",
+ " False \n",
+ " A/0/S \n",
+ " TRAPPIST-1e \n",
+ " 58.0 \n",
+ " True \n",
+ " 43.0 \n",
+ " 3576.0 \n",
+ " 0.0 \n",
+ " 6715.0 \n",
+ " 49.0 \n",
+ " Altark Susent \n",
+ " False \n",
+ " \n",
+ " \n",
+ " 3 \n",
+ " 0003_02 \n",
+ " Europa \n",
+ " False \n",
+ " A/0/S \n",
+ " TRAPPIST-1e \n",
+ " 33.0 \n",
+ " False \n",
+ " 0.0 \n",
+ " 1283.0 \n",
+ " 371.0 \n",
+ " 3329.0 \n",
+ " 193.0 \n",
+ " Solam Susent \n",
+ " False \n",
+ " \n",
+ " \n",
+ " 4 \n",
+ " 0004_01 \n",
+ " Earth \n",
+ " False \n",
+ " F/1/S \n",
+ " TRAPPIST-1e \n",
+ " 16.0 \n",
+ " False \n",
+ " 303.0 \n",
+ " 70.0 \n",
+ " 151.0 \n",
+ " 565.0 \n",
+ " 2.0 \n",
+ " Willy Santantines \n",
+ " True \n",
+ " \n",
+ " \n",
+ "
\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": 3,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "PassengerId 0.000000\n",
+ "HomePlanet 2.312205\n",
+ "CryoSleep 2.496261\n",
+ "Cabin 2.289198\n",
+ "Destination 2.093639\n",
+ "Age 2.059128\n",
+ "VIP 2.335212\n",
+ "RoomService 2.082135\n",
+ "FoodCourt 2.105142\n",
+ "ShoppingMall 2.392730\n",
+ "Spa 2.105142\n",
+ "VRDeck 2.162660\n",
+ "Name 2.300702\n",
+ "Transported 0.000000\n",
+ "dtype: float64"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "#your code here\n",
+ "missing_pct = spaceship.isna().mean() * 100\n",
+ "missing_pct"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "spaceship = spaceship.dropna(subset=[\"HomePlanet\", \"CryoSleep\", \"Cabin\", \"Destination\", \"Age\", \"VIP\", \"RoomService\", \"FoodCourt\", \"ShoppingMall\", \"Spa\", \"VRDeck\", \"Name\"])\n",
+ "spaceship = spaceship.drop(['PassengerId', 'Name'], axis=1)\n",
+ "spaceship[\"Transported\"] = np.where(spaceship[\"Transported\"] == True, 1, 0)\n",
+ "spaceship[\"Cabin\"] = spaceship[\"Cabin\"].str[0]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "features = spaceship.drop(columns=[\"Transported\"])\n",
+ "target = spaceship[\"Transported\"]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "X_train, X_test, y_train, y_test = train_test_split(features, target, test_size = 0.20, random_state=0)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "/var/folders/1q/2t5621cd5kz7blvfr_nw9dl80000gn/T/ipykernel_2877/1876233162.py:1: 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",
+ " categ_cols = X_train.select_dtypes(include=['object'])\n"
+ ]
+ }
+ ],
+ "source": [
+ "categ_cols = X_train.select_dtypes(include=['object'])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "ohe = OneHotEncoder(sparse_output=False, drop='first') \n",
+ "ohe.fit(X_train[categ_cols.columns])\n",
+ "X_train_trans_np = ohe.transform(X_train[categ_cols.columns])\n",
+ "X_test_trans_np = ohe.transform(X_test[categ_cols.columns])\n",
+ "X_train_trans_df = pd.DataFrame(X_train_trans_np, columns=ohe.get_feature_names_out(), index=X_train.index)\n",
+ "X_test_trans_np = ohe.transform(X_test[categ_cols.columns])\n",
+ "X_test_trans_df = pd.DataFrame(X_test_trans_np, columns=ohe.get_feature_names_out(), index=X_test.index)\n",
+ "X_train_num = X_train.select_dtypes(include='number')\n",
+ "X_train_num = X_train_num[X_train_num.columns]\n",
+ "X_test_num = X_test.select_dtypes('number')\n",
+ "X_test_num = X_test_num[X_test_num.columns]\n",
+ "X_train_full = pd.concat([X_train_trans_df, X_train_num], axis=1)\n",
+ "X_test_full = pd.concat([X_test_trans_df, X_test_num], axis=1)\n",
+ "std_scaler = StandardScaler()\n",
+ "std_scaler.fit(X_train_full)\n",
+ "X_train_full_np = std_scaler.transform(X_train_full)\n",
+ "X_test_full_np = std_scaler.transform(X_test_full)\n",
+ "X_train_full_np_df = pd.DataFrame(X_train_full_np, columns=X_train_full.columns, index=X_train_full.index)\n",
+ "X_test_full_np_df = pd.DataFrame(X_test_full_np, columns=X_test_full.columns, index=X_test_full.index)"
+ ]
+ },
+ {
+ "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": 9,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#your code here\n",
+ "gb_reg = GradientBoostingClassifier(max_depth=20,\n",
+ " n_estimators=100)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "- Evaluate your model"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "GradientBoostingClassifier(max_depth=20) 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. GradientBoostingClassifier
\n",
+ "
\n",
+ "
\n",
+ " Parameters \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " max_depth\n",
+ " \n",
+ " max_depth: int or None, default=3 Maximum depth of the individual regression estimators. The maximum depth limits the number of nodes in the tree. Tune this parameter for best performance; the best value depends on the interaction of the input variables. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. If int, values must be in the range `[1, inf)`. \n",
+ " \n",
+ " \n",
+ " 20 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " loss\n",
+ " \n",
+ " loss: {'log_loss', 'exponential'}, default='log_loss' The loss function to be optimized. 'log_loss' refers to binomial and multinomial deviance, the same as used in logistic regression. It is a good choice for classification with probabilistic outputs. For loss 'exponential', gradient boosting recovers the AdaBoost algorithm. \n",
+ " \n",
+ " \n",
+ " 'log_loss' \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " learning_rate\n",
+ " \n",
+ " learning_rate: float, default=0.1 Learning rate shrinks the contribution of each tree by `learning_rate`. There is a trade-off between learning_rate and n_estimators. Values must be in the range `[0.0, inf)`. For an example of the effects of this parameter and its interaction with ``subsample``, see :ref:`sphx_glr_auto_examples_ensemble_plot_gradient_boosting_regularization.py`. \n",
+ " \n",
+ " \n",
+ " 0.1 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " n_estimators\n",
+ " \n",
+ " n_estimators: int, default=100 The number of boosting stages to perform. Gradient boosting is fairly robust to over-fitting so a large number usually results in better performance. Values must be in the range `[1, inf)`. \n",
+ " \n",
+ " \n",
+ " 100 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " subsample\n",
+ " \n",
+ " subsample: float, default=1.0 The fraction of samples to be used for fitting the individual base learners. If smaller than 1.0 this results in Stochastic Gradient Boosting. `subsample` interacts with the parameter `n_estimators`. Choosing `subsample < 1.0` leads to a reduction of variance and an increase in bias. Values must be in the range `(0.0, 1.0]`. \n",
+ " \n",
+ " \n",
+ " 1.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " criterion\n",
+ " \n",
+ " criterion: {'friedman_mse', 'squared_error'}, default='friedman_mse' This parameter has no effect. .. versionadded:: 0.18 .. deprecated:: 1.9 `criterion` is deprecated and will be removed in 1.11. \n",
+ " \n",
+ " \n",
+ " 'deprecated' \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " min_samples_split\n",
+ " \n",
+ " min_samples_split: int or float, default=2 The minimum number of samples required to split an internal node: - If int, values must be in the range `[2, inf)`. - If float, values must be in the range `(0.0, 1.0]` and `min_samples_split` will be `ceil(min_samples_split * n_samples)`. .. versionchanged:: 0.18 Added float values for fractions. \n",
+ " \n",
+ " \n",
+ " 2 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " min_samples_leaf\n",
+ " \n",
+ " min_samples_leaf: int or float, default=1 The minimum number of samples required to be at a leaf node. A split point at any depth will only be considered if it leaves at least ``min_samples_leaf`` training samples in each of the left and right branches. This may have the effect of smoothing the model, especially in regression. - If int, values must be in the range `[1, inf)`. - If float, values must be in the range `(0.0, 1.0)` and `min_samples_leaf` will be `ceil(min_samples_leaf * n_samples)`. .. versionchanged:: 0.18 Added float values for fractions. \n",
+ " \n",
+ " \n",
+ " 1 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " min_weight_fraction_leaf\n",
+ " \n",
+ " min_weight_fraction_leaf: float, default=0.0 The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided. Values must be in the range `[0.0, 0.5]`. \n",
+ " \n",
+ " \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " min_impurity_decrease\n",
+ " \n",
+ " min_impurity_decrease: float, default=0.0 A node will be split if this split induces a decrease of the impurity greater than or equal to this value. Values must be in the range `[0.0, inf)`. The weighted impurity decrease equation is the following:: N_t / N * (impurity - N_t_R / N_t * right_impurity - N_t_L / N_t * left_impurity) where ``N`` is the total number of samples, ``N_t`` is the number of samples at the current node, ``N_t_L`` is the number of samples in the left child, and ``N_t_R`` is the number of samples in the right child. ``N``, ``N_t``, ``N_t_R`` and ``N_t_L`` all refer to the weighted sum, if ``sample_weight`` is passed. .. versionadded:: 0.19 \n",
+ " \n",
+ " \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " init\n",
+ " \n",
+ " init: estimator or 'zero', default=None An estimator object that is used to compute the initial predictions. ``init`` has to provide :term:`fit` and :term:`predict_proba`. If 'zero', the initial raw predictions are set to zero. By default, a ``DummyEstimator`` predicting the classes priors is used. \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " random_state\n",
+ " \n",
+ " random_state: int, RandomState instance or None, default=None Controls the random seed given to each Tree estimator at each boosting iteration. In addition, it controls the random permutation of the features at each split (see Notes for more details). It also controls the random splitting of the training data to obtain a validation set if `n_iter_no_change` is not None. Pass an int for reproducible output across multiple function calls. See :term:`Glossary <random_state>`. \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " max_features\n",
+ " \n",
+ " max_features: {'sqrt', 'log2'}, int or float, default=None The number of features to consider when looking for the best split: - If int, values must be in the range `[1, inf)`. - If float, values must be in the range `(0.0, 1.0]` and the features considered at each split will be `max(1, int(max_features * n_features_in_))`. - If 'sqrt', then `max_features=sqrt(n_features)`. - If 'log2', then `max_features=log2(n_features)`. - If None, then `max_features=n_features`. Choosing `max_features < n_features` leads to a reduction of variance and an increase in bias. Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than ``max_features`` features. \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " verbose\n",
+ " \n",
+ " verbose: int, default=0 Enable verbose output. If 1 then it prints progress and performance once in a while (the more trees the lower the frequency). If greater than 1 then it prints progress and performance for every tree. Values must be in the range `[0, inf)`. \n",
+ " \n",
+ " \n",
+ " 0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " max_leaf_nodes\n",
+ " \n",
+ " max_leaf_nodes: int, default=None Grow trees with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. Values must be in the range `[2, inf)`. If `None`, then unlimited number of leaf nodes. \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " warm_start\n",
+ " \n",
+ " warm_start: bool, default=False When set to ``True``, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just erase the previous solution. See :term:`the Glossary <warm_start>`. \n",
+ " \n",
+ " \n",
+ " False \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " validation_fraction\n",
+ " \n",
+ " validation_fraction: float, default=0.1 The proportion of training data to set aside as validation set for early stopping. Values must be in the range `(0.0, 1.0)`. Only used if ``n_iter_no_change`` is set to an integer. .. versionadded:: 0.20 \n",
+ " \n",
+ " \n",
+ " 0.1 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " n_iter_no_change\n",
+ " \n",
+ " n_iter_no_change: int, default=None ``n_iter_no_change`` is used to decide if early stopping will be used to terminate training when validation score is not improving. By default it is set to None to disable early stopping. If set to a number, it will set aside ``validation_fraction`` size of the training data as validation and terminate training when validation score is not improving in all of the previous ``n_iter_no_change`` numbers of iterations. The split is stratified. Values must be in the range `[1, inf)`. See :ref:`sphx_glr_auto_examples_ensemble_plot_gradient_boosting_early_stopping.py`. .. versionadded:: 0.20 \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " tol\n",
+ " \n",
+ " tol: float, default=1e-4 Tolerance for the early stopping. When the loss is not improving by at least tol for ``n_iter_no_change`` iterations (if set to a number), the training stops. Values must be in the range `[0.0, inf)`. .. versionadded:: 0.20 \n",
+ " \n",
+ " \n",
+ " 0.0001 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " ccp_alpha\n",
+ " \n",
+ " ccp_alpha: non-negative float, default=0.0 Complexity parameter used for Minimal Cost-Complexity Pruning. The subtree with the largest cost complexity that is smaller than ``ccp_alpha`` will be chosen. By default, no pruning is performed. Values must be in the range `[0.0, inf)`. See :ref:`minimal_cost_complexity_pruning` for details. See :ref:`sphx_glr_auto_examples_tree_plot_cost_complexity_pruning.py` for an example of such pruning. .. versionadded:: 0.22 \n",
+ " \n",
+ " \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ " \n",
+ "
\n",
+ " \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ " Fitted attributes \n",
+ " \n",
+ " \n",
+ " \n",
+ " Name \n",
+ " Type \n",
+ " Value \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " classes_\n",
+ " \n",
+ " classes_: ndarray of shape (n_classes,) The classes labels. \n",
+ " \n",
+ " \n",
+ " ndarray[int64](2,) \n",
+ " [0,1] \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " estimators_\n",
+ " \n",
+ " estimators_: ndarray of DecisionTreeRegressor of shape (n_estimators, ``n_trees_per_iteration_``) The collection of fitted sub-estimators. ``n_trees_per_iteration_`` is 1 for binary classification, otherwise ``n_classes``. \n",
+ " \n",
+ " \n",
+ " ndarray[object](100, 1) \n",
+ " [[DecisionTreeRegressor(max_depth=20,\n",
+ " random_state=RandomState(MT19937) at 0x10B895C40)],\n",
+ " [DecisionTreeRegressor(max_depth=20,\n",
+ " random_state=RandomState(MT19937) at 0x10B895C40)],\n",
+ " [DecisionTreeRegressor(max_depth=20,\n",
+ " random_state=RandomState(MT19937) at 0x10B895C40)],\n",
+ " ...,\n",
+ " [DecisionTreeRegressor(max_depth=20,\n",
+ " random_state=RandomState(MT19937) at 0x10B895C40)],\n",
+ " [DecisionTreeRegressor(max_depth=20,\n",
+ " random_state=RandomState(MT19937) at 0x10B895C40)],\n",
+ " [DecisionTreeRegressor(max_depth=20,\n",
+ " random_state=RandomState(MT19937) at 0x10B895C40)]] \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " feature_importances_\n",
+ " \n",
+ " feature_importances_: ndarray of shape (n_features,) The impurity-based feature importances. The higher, the more important the feature. The importance of a feature is computed as the (normalized) total reduction of the MSE brought by that feature. It is also known as the Gini importance. Warning: impurity-based feature importances can be misleading for high cardinality features (many unique values). See :func:`sklearn.inspection.permutation_importance` as an alternative. \n",
+ " \n",
+ " \n",
+ " ndarray[float64](19,) \n",
+ " [0.01,0.01,0.25,...,0.08,0.1 ,0.12] \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " feature_names_in_\n",
+ " \n",
+ " feature_names_in_: ndarray of shape (`n_features_in_`,) Names of features seen during :term:`fit`. Defined only when `X` has feature names that are all strings. .. versionadded:: 1.0 \n",
+ " \n",
+ " \n",
+ " ndarray[object](19,) \n",
+ " ['HomePlanet_Europa','HomePlanet_Mars','CryoSleep_True',...,'ShoppingMall',\n",
+ " 'Spa','VRDeck'] \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " init_\n",
+ " \n",
+ " init_: estimator The estimator that provides the initial predictions. Set via the ``init`` argument. \n",
+ " \n",
+ " \n",
+ " DummyClassifier \n",
+ " DummyClassifier() \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " max_features_\n",
+ " \n",
+ " max_features_: int The inferred value of max_features. \n",
+ " \n",
+ " \n",
+ " int \n",
+ " 19 \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " n_classes_\n",
+ " \n",
+ " n_classes_: int The number of classes. \n",
+ " \n",
+ " \n",
+ " int \n",
+ " 2 \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " n_estimators_\n",
+ " \n",
+ " n_estimators_: int The number of estimators as selected by early stopping (if ``n_iter_no_change`` is specified). Otherwise it is set to ``n_estimators``. .. versionadded:: 0.20 \n",
+ " \n",
+ " \n",
+ " int \n",
+ " 100 \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " n_features_in_\n",
+ " \n",
+ " n_features_in_: int Number of features seen during :term:`fit`. .. versionadded:: 0.24 \n",
+ " \n",
+ " \n",
+ " int \n",
+ " 19 \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " n_trees_per_iteration_\n",
+ " \n",
+ " n_trees_per_iteration_: int The number of trees that are built at each iteration. For binary classifiers, this is always 1. .. versionadded:: 1.4.0 \n",
+ " \n",
+ " \n",
+ " int \n",
+ " 1 \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " train_score_\n",
+ " \n",
+ " train_score_: ndarray of shape (n_estimators,) The i-th score ``train_score_[i]`` is the loss of the model at iteration ``i`` on the in-bag sample. If ``subsample == 1`` this is the loss on the training data. \n",
+ " \n",
+ " \n",
+ " ndarray[float64](100,) \n",
+ " [1.24,1.11,1.01,...,0.22,0.22,0.22] \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ "GradientBoostingClassifier(max_depth=20)"
+ ]
+ },
+ "execution_count": 10,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "#your code here\n",
+ "gb_reg.fit(X_train_full, y_train)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "accuracy 78.37\n",
+ "recall 0.80\n",
+ "F1-score 0.79\n",
+ "Precision_score 0.77\n"
+ ]
+ }
+ ],
+ "source": [
+ "y_pred_test_gb = gb_reg.predict(X_test_full)\n",
+ "\n",
+ "print(f\"accuracy {accuracy_score(y_test, y_pred_test_gb)*100: .2f}\")\n",
+ "print(f\"recall {recall_score(y_test, y_pred_test_gb): .2f}\")\n",
+ "print(f\"F1-score {f1_score(y_test, y_pred_test_gb): .2f}\")\n",
+ "print(f\"Precision_score {precision_score(y_test, y_pred_test_gb): .2f}\")"
+ ]
+ },
+ {
+ "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": 12,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#your code here\n",
+ "parameter_grid = {\"max_depth\": [3, 10],\n",
+ " \"n_estimators\": [10, 200]}\n",
+ "\n",
+ "gb_classif = GradientBoostingClassifier(random_state=123)\n",
+ "\n",
+ "confidence_level = 0.95\n",
+ "folds = 10"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "- Run Grid Search"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Fitting 10 folds for each of 4 candidates, totalling 40 fits\n",
+ "[CV 1/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 1/10; 1/4] END max_depth=3, n_estimators=10;, score=0.737 total time= 0.0s\n",
+ "[CV 2/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 2/10; 1/4] END max_depth=3, n_estimators=10;, score=0.777 total time= 0.0s\n",
+ "[CV 3/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 3/10; 1/4] END max_depth=3, n_estimators=10;, score=0.798 total time= 0.0s\n",
+ "[CV 4/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 4/10; 1/4] END max_depth=3, n_estimators=10;, score=0.771 total time= 0.0s\n",
+ "[CV 5/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 5/10; 1/4] END max_depth=3, n_estimators=10;, score=0.758 total time= 0.0s\n",
+ "[CV 6/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 6/10; 1/4] END max_depth=3, n_estimators=10;, score=0.775 total time= 0.0s\n",
+ "[CV 7/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 7/10; 1/4] END max_depth=3, n_estimators=10;, score=0.758 total time= 0.0s\n",
+ "[CV 8/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 8/10; 1/4] END max_depth=3, n_estimators=10;, score=0.771 total time= 0.0s\n",
+ "[CV 9/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 9/10; 1/4] END max_depth=3, n_estimators=10;, score=0.771 total time= 0.0s\n",
+ "[CV 10/10; 1/4] START max_depth=3, n_estimators=10..............................\n",
+ "[CV 10/10; 1/4] END max_depth=3, n_estimators=10;, score=0.765 total time= 0.0s\n",
+ "[CV 1/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 1/10; 2/4] END max_depth=3, n_estimators=200;, score=0.783 total time= 0.5s\n",
+ "[CV 2/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 2/10; 2/4] END max_depth=3, n_estimators=200;, score=0.817 total time= 0.5s\n",
+ "[CV 3/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 3/10; 2/4] END max_depth=3, n_estimators=200;, score=0.828 total time= 0.5s\n",
+ "[CV 4/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 4/10; 2/4] END max_depth=3, n_estimators=200;, score=0.805 total time= 0.5s\n",
+ "[CV 5/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 5/10; 2/4] END max_depth=3, n_estimators=200;, score=0.795 total time= 0.5s\n",
+ "[CV 6/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 6/10; 2/4] END max_depth=3, n_estimators=200;, score=0.812 total time= 0.5s\n",
+ "[CV 7/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 7/10; 2/4] END max_depth=3, n_estimators=200;, score=0.807 total time= 0.5s\n",
+ "[CV 8/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 8/10; 2/4] END max_depth=3, n_estimators=200;, score=0.778 total time= 0.5s\n",
+ "[CV 9/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 9/10; 2/4] END max_depth=3, n_estimators=200;, score=0.795 total time= 0.5s\n",
+ "[CV 10/10; 2/4] START max_depth=3, n_estimators=200.............................\n",
+ "[CV 10/10; 2/4] END max_depth=3, n_estimators=200;, score=0.799 total time= 0.5s\n",
+ "[CV 1/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 1/10; 3/4] END max_depth=10, n_estimators=10;, score=0.769 total time= 0.1s\n",
+ "[CV 2/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 2/10; 3/4] END max_depth=10, n_estimators=10;, score=0.802 total time= 0.1s\n",
+ "[CV 3/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 3/10; 3/4] END max_depth=10, n_estimators=10;, score=0.803 total time= 0.1s\n",
+ "[CV 4/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 4/10; 3/4] END max_depth=10, n_estimators=10;, score=0.786 total time= 0.1s\n",
+ "[CV 5/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 5/10; 3/4] END max_depth=10, n_estimators=10;, score=0.767 total time= 0.1s\n",
+ "[CV 6/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 6/10; 3/4] END max_depth=10, n_estimators=10;, score=0.801 total time= 0.1s\n",
+ "[CV 7/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 7/10; 3/4] END max_depth=10, n_estimators=10;, score=0.778 total time= 0.1s\n",
+ "[CV 8/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 8/10; 3/4] END max_depth=10, n_estimators=10;, score=0.777 total time= 0.1s\n",
+ "[CV 9/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 9/10; 3/4] END max_depth=10, n_estimators=10;, score=0.784 total time= 0.1s\n",
+ "[CV 10/10; 3/4] START max_depth=10, n_estimators=10.............................\n",
+ "[CV 10/10; 3/4] END max_depth=10, n_estimators=10;, score=0.773 total time= 0.1s\n",
+ "[CV 1/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 1/10; 4/4] END max_depth=10, n_estimators=200;, score=0.775 total time= 1.9s\n",
+ "[CV 2/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 2/10; 4/4] END max_depth=10, n_estimators=200;, score=0.803 total time= 1.9s\n",
+ "[CV 3/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 3/10; 4/4] END max_depth=10, n_estimators=200;, score=0.798 total time= 1.9s\n",
+ "[CV 4/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 4/10; 4/4] END max_depth=10, n_estimators=200;, score=0.792 total time= 1.9s\n",
+ "[CV 5/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 5/10; 4/4] END max_depth=10, n_estimators=200;, score=0.784 total time= 1.9s\n",
+ "[CV 6/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 6/10; 4/4] END max_depth=10, n_estimators=200;, score=0.788 total time= 1.9s\n",
+ "[CV 7/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 7/10; 4/4] END max_depth=10, n_estimators=200;, score=0.786 total time= 1.9s\n",
+ "[CV 8/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 8/10; 4/4] END max_depth=10, n_estimators=200;, score=0.773 total time= 1.9s\n",
+ "[CV 9/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 9/10; 4/4] END max_depth=10, n_estimators=200;, score=0.777 total time= 1.9s\n",
+ "[CV 10/10; 4/4] START max_depth=10, n_estimators=200............................\n",
+ "[CV 10/10; 4/4] END max_depth=10, n_estimators=200;, score=0.769 total time= 1.9s\n",
+ "\n",
+ "\n",
+ "Time taken to find the best combination of hyperparameters among the given ones: 26.0840 seconds\n",
+ "\n",
+ "\n",
+ "The best combination of hyperparameters has been: {'max_depth': 3, 'n_estimators': 200}\n",
+ "The best CV accuracy is: 0.8020\n",
+ "The R2 confidence interval for the best combination of hyperparameters is: ( 0.7918, 0.8020, 0.8123) \n"
+ ]
+ }
+ ],
+ "source": [
+ "grid_search = GridSearchCV(gb_classif, param_grid=parameter_grid, cv=folds, verbose=10, error_score='raise')\n",
+ "\n",
+ "start_time = time.time()\n",
+ "grid_search.fit(X_train_full, y_train)\n",
+ "end_time = time.time()\n",
+ "\n",
+ "print(\"\\n\")\n",
+ "print(f\"Time taken to find the best combination of hyperparameters among the given ones: {end_time - start_time: .4f} seconds\")\n",
+ "print(\"\\n\")\n",
+ "\n",
+ "\n",
+ "print(f\"The best combination of hyperparameters has been: {grid_search.best_params_}\")\n",
+ "print(f\"The best CV accuracy is: {grid_search.best_score_: .4f}\")\n",
+ "\n",
+ "results_gs_df = pd.DataFrame(grid_search.cv_results_).sort_values(by=\"mean_test_score\", ascending=False)\n",
+ "\n",
+ "gs_mean_score = results_gs_df.iloc[0,-3]\n",
+ "gs_sem = results_gs_df.iloc[0,-2] / np.sqrt(folds)\n",
+ "\n",
+ "gs_tc = st.t.ppf(1-((1-confidence_level)/2), df=folds-1)\n",
+ "gs_lower_bound = gs_mean_score - ( gs_tc * gs_sem )\n",
+ "gs_upper_bound = gs_mean_score + ( gs_tc * gs_sem )\n",
+ "\n",
+ "print(f\"The R2 confidence interval for the best combination of hyperparameters is: \\\n",
+ " ({gs_lower_bound: .4f}, {gs_mean_score: .4f}, {gs_upper_bound: .4f}) \")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "- Evaluate your model"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ "\n",
+ "accuracy 78.52\n",
+ "accuracy_train 43.53\n",
+ "recall 0.82\n",
+ "F1-score 0.79\n",
+ "Precision_score 0.77\n",
+ "\n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "best_model = grid_search.best_estimator_\n",
+ "y_pred_test_df = best_model.predict(X_test_full)\n",
+ "y_pred_train_df = best_model.predict(X_train_full_np_df)\n",
+ "\n",
+ "print(\"\\n\")\n",
+ "print(f\"accuracy {accuracy_score(y_test, y_pred_test_df)*100: .2f}\")\n",
+ "print(f\"accuracy_train {accuracy_score(y_train, y_pred_train_df)*100: .2f}\")\n",
+ "\n",
+ "print(f\"recall {recall_score(y_test, y_pred_test_df): .2f}\")\n",
+ "print(f\"F1-score {f1_score(y_test, y_pred_test_df): .2f}\")\n",
+ "print(f\"Precision_score {precision_score(y_test, y_pred_test_df): .2f}\")\n",
+ "print(\"\\n\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "**Conclusion**\n",
+ "We gain a bit better results with \"best model\" parameters and we also can see, that model wasn't overfitted ('accuracy_train' metric is below 'accuracy')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "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.14.6"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 4
+}
diff --git a/lab-hyper-tuning.ipynb b/lab-hyper-tuning.ipynb
index 847d487..127f2d8 100644
--- a/lab-hyper-tuning.ipynb
+++ b/lab-hyper-tuning.ipynb
@@ -42,7 +42,23 @@
"#Libraries\n",
"import pandas as pd\n",
"import numpy as np\n",
- "from sklearn.model_selection import train_test_split"
+ "import time\n",
+ "\n",
+ "import scipy.stats as st\n",
+ "\n",
+ "from sklearn.model_selection import train_test_split\n",
+ "from sklearn.preprocessing import OneHotEncoder\n",
+ "\n",
+ "\n",
+ "from sklearn.model_selection import train_test_split\n",
+ "from sklearn.tree import DecisionTreeClassifier\n",
+ "from sklearn.ensemble import BaggingClassifier, RandomForestClassifier,AdaBoostClassifier, GradientBoostingClassifier\n",
+ "\n",
+ "from sklearn.preprocessing import MinMaxScaler, StandardScaler\n",
+ "\n",
+ "from sklearn.metrics import accuracy_score, recall_score, f1_score, precision_score\n",
+ "\n",
+ "from sklearn.model_selection import GridSearchCV, RandomizedSearchCV"
]
},
{
@@ -221,11 +237,115 @@
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 3,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "PassengerId 0.000000\n",
+ "HomePlanet 2.312205\n",
+ "CryoSleep 2.496261\n",
+ "Cabin 2.289198\n",
+ "Destination 2.093639\n",
+ "Age 2.059128\n",
+ "VIP 2.335212\n",
+ "RoomService 2.082135\n",
+ "FoodCourt 2.105142\n",
+ "ShoppingMall 2.392730\n",
+ "Spa 2.105142\n",
+ "VRDeck 2.162660\n",
+ "Name 2.300702\n",
+ "Transported 0.000000\n",
+ "dtype: float64"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "#your code here\n",
+ "missing_pct = spaceship.isna().mean() * 100\n",
+ "missing_pct"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
- "#your code here"
+ "spaceship = spaceship.dropna(subset=[\"HomePlanet\", \"CryoSleep\", \"Cabin\", \"Destination\", \"Age\", \"VIP\", \"RoomService\", \"FoodCourt\", \"ShoppingMall\", \"Spa\", \"VRDeck\", \"Name\"])\n",
+ "spaceship = spaceship.drop(['PassengerId', 'Name'], axis=1)\n",
+ "spaceship[\"Transported\"] = np.where(spaceship[\"Transported\"] == True, 1, 0)\n",
+ "spaceship[\"Cabin\"] = spaceship[\"Cabin\"].str[0]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "features = spaceship.drop(columns=[\"Transported\"])\n",
+ "target = spaceship[\"Transported\"]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "X_train, X_test, y_train, y_test = train_test_split(features, target, test_size = 0.20, random_state=0)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "/var/folders/1q/2t5621cd5kz7blvfr_nw9dl80000gn/T/ipykernel_2877/1876233162.py:1: 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",
+ " categ_cols = X_train.select_dtypes(include=['object'])\n"
+ ]
+ }
+ ],
+ "source": [
+ "categ_cols = X_train.select_dtypes(include=['object'])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "ohe = OneHotEncoder(sparse_output=False, drop='first') \n",
+ "ohe.fit(X_train[categ_cols.columns])\n",
+ "X_train_trans_np = ohe.transform(X_train[categ_cols.columns])\n",
+ "X_test_trans_np = ohe.transform(X_test[categ_cols.columns])\n",
+ "X_train_trans_df = pd.DataFrame(X_train_trans_np, columns=ohe.get_feature_names_out(), index=X_train.index)\n",
+ "X_test_trans_np = ohe.transform(X_test[categ_cols.columns])\n",
+ "X_test_trans_df = pd.DataFrame(X_test_trans_np, columns=ohe.get_feature_names_out(), index=X_test.index)\n",
+ "X_train_num = X_train.select_dtypes(include='number')\n",
+ "X_train_num = X_train_num[X_train_num.columns]\n",
+ "X_test_num = X_test.select_dtypes('number')\n",
+ "X_test_num = X_test_num[X_test_num.columns]\n",
+ "X_train_full = pd.concat([X_train_trans_df, X_train_num], axis=1)\n",
+ "X_test_full = pd.concat([X_test_trans_df, X_test_num], axis=1)\n",
+ "std_scaler = StandardScaler()\n",
+ "std_scaler.fit(X_train_full)\n",
+ "X_train_full_np = std_scaler.transform(X_train_full)\n",
+ "X_test_full_np = std_scaler.transform(X_test_full)\n",
+ "X_train_full_np_df = pd.DataFrame(X_train_full_np, columns=X_train_full.columns, index=X_train_full.index)\n",
+ "X_test_full_np_df = pd.DataFrame(X_test_full_np, columns=X_test_full.columns, index=X_test_full.index)"
]
},
{
@@ -237,11 +357,13 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
- "#your code here"
+ "#your code here\n",
+ "gb_reg = GradientBoostingClassifier(max_depth=20,\n",
+ " n_estimators=100)"
]
},
{
@@ -253,11 +375,1535 @@
},
{
"cell_type": "code",
- "execution_count": 1,
+ "execution_count": 10,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "GradientBoostingClassifier(max_depth=20) 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. GradientBoostingClassifier
\n",
+ "
\n",
+ "
\n",
+ " Parameters \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " max_depth\n",
+ " \n",
+ " max_depth: int or None, default=3 Maximum depth of the individual regression estimators. The maximum depth limits the number of nodes in the tree. Tune this parameter for best performance; the best value depends on the interaction of the input variables. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. If int, values must be in the range `[1, inf)`. \n",
+ " \n",
+ " \n",
+ " 20 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " loss\n",
+ " \n",
+ " loss: {'log_loss', 'exponential'}, default='log_loss' The loss function to be optimized. 'log_loss' refers to binomial and multinomial deviance, the same as used in logistic regression. It is a good choice for classification with probabilistic outputs. For loss 'exponential', gradient boosting recovers the AdaBoost algorithm. \n",
+ " \n",
+ " \n",
+ " 'log_loss' \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " learning_rate\n",
+ " \n",
+ " learning_rate: float, default=0.1 Learning rate shrinks the contribution of each tree by `learning_rate`. There is a trade-off between learning_rate and n_estimators. Values must be in the range `[0.0, inf)`. For an example of the effects of this parameter and its interaction with ``subsample``, see :ref:`sphx_glr_auto_examples_ensemble_plot_gradient_boosting_regularization.py`. \n",
+ " \n",
+ " \n",
+ " 0.1 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " n_estimators\n",
+ " \n",
+ " n_estimators: int, default=100 The number of boosting stages to perform. Gradient boosting is fairly robust to over-fitting so a large number usually results in better performance. Values must be in the range `[1, inf)`. \n",
+ " \n",
+ " \n",
+ " 100 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " subsample\n",
+ " \n",
+ " subsample: float, default=1.0 The fraction of samples to be used for fitting the individual base learners. If smaller than 1.0 this results in Stochastic Gradient Boosting. `subsample` interacts with the parameter `n_estimators`. Choosing `subsample < 1.0` leads to a reduction of variance and an increase in bias. Values must be in the range `(0.0, 1.0]`. \n",
+ " \n",
+ " \n",
+ " 1.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " criterion\n",
+ " \n",
+ " criterion: {'friedman_mse', 'squared_error'}, default='friedman_mse' This parameter has no effect. .. versionadded:: 0.18 .. deprecated:: 1.9 `criterion` is deprecated and will be removed in 1.11. \n",
+ " \n",
+ " \n",
+ " 'deprecated' \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " min_samples_split\n",
+ " \n",
+ " min_samples_split: int or float, default=2 The minimum number of samples required to split an internal node: - If int, values must be in the range `[2, inf)`. - If float, values must be in the range `(0.0, 1.0]` and `min_samples_split` will be `ceil(min_samples_split * n_samples)`. .. versionchanged:: 0.18 Added float values for fractions. \n",
+ " \n",
+ " \n",
+ " 2 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " min_samples_leaf\n",
+ " \n",
+ " min_samples_leaf: int or float, default=1 The minimum number of samples required to be at a leaf node. A split point at any depth will only be considered if it leaves at least ``min_samples_leaf`` training samples in each of the left and right branches. This may have the effect of smoothing the model, especially in regression. - If int, values must be in the range `[1, inf)`. - If float, values must be in the range `(0.0, 1.0)` and `min_samples_leaf` will be `ceil(min_samples_leaf * n_samples)`. .. versionchanged:: 0.18 Added float values for fractions. \n",
+ " \n",
+ " \n",
+ " 1 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " min_weight_fraction_leaf\n",
+ " \n",
+ " min_weight_fraction_leaf: float, default=0.0 The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided. Values must be in the range `[0.0, 0.5]`. \n",
+ " \n",
+ " \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " min_impurity_decrease\n",
+ " \n",
+ " min_impurity_decrease: float, default=0.0 A node will be split if this split induces a decrease of the impurity greater than or equal to this value. Values must be in the range `[0.0, inf)`. The weighted impurity decrease equation is the following:: N_t / N * (impurity - N_t_R / N_t * right_impurity - N_t_L / N_t * left_impurity) where ``N`` is the total number of samples, ``N_t`` is the number of samples at the current node, ``N_t_L`` is the number of samples in the left child, and ``N_t_R`` is the number of samples in the right child. ``N``, ``N_t``, ``N_t_R`` and ``N_t_L`` all refer to the weighted sum, if ``sample_weight`` is passed. .. versionadded:: 0.19 \n",
+ " \n",
+ " \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " init\n",
+ " \n",
+ " init: estimator or 'zero', default=None An estimator object that is used to compute the initial predictions. ``init`` has to provide :term:`fit` and :term:`predict_proba`. If 'zero', the initial raw predictions are set to zero. By default, a ``DummyEstimator`` predicting the classes priors is used. \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " random_state\n",
+ " \n",
+ " random_state: int, RandomState instance or None, default=None Controls the random seed given to each Tree estimator at each boosting iteration. In addition, it controls the random permutation of the features at each split (see Notes for more details). It also controls the random splitting of the training data to obtain a validation set if `n_iter_no_change` is not None. Pass an int for reproducible output across multiple function calls. See :term:`Glossary <random_state>`. \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " max_features\n",
+ " \n",
+ " max_features: {'sqrt', 'log2'}, int or float, default=None The number of features to consider when looking for the best split: - If int, values must be in the range `[1, inf)`. - If float, values must be in the range `(0.0, 1.0]` and the features considered at each split will be `max(1, int(max_features * n_features_in_))`. - If 'sqrt', then `max_features=sqrt(n_features)`. - If 'log2', then `max_features=log2(n_features)`. - If None, then `max_features=n_features`. Choosing `max_features < n_features` leads to a reduction of variance and an increase in bias. Note: the search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than ``max_features`` features. \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " verbose\n",
+ " \n",
+ " verbose: int, default=0 Enable verbose output. If 1 then it prints progress and performance once in a while (the more trees the lower the frequency). If greater than 1 then it prints progress and performance for every tree. Values must be in the range `[0, inf)`. \n",
+ " \n",
+ " \n",
+ " 0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " max_leaf_nodes\n",
+ " \n",
+ " max_leaf_nodes: int, default=None Grow trees with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. Values must be in the range `[2, inf)`. If `None`, then unlimited number of leaf nodes. \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " warm_start\n",
+ " \n",
+ " warm_start: bool, default=False When set to ``True``, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just erase the previous solution. See :term:`the Glossary <warm_start>`. \n",
+ " \n",
+ " \n",
+ " False \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " validation_fraction\n",
+ " \n",
+ " validation_fraction: float, default=0.1 The proportion of training data to set aside as validation set for early stopping. Values must be in the range `(0.0, 1.0)`. Only used if ``n_iter_no_change`` is set to an integer. .. versionadded:: 0.20 \n",
+ " \n",
+ " \n",
+ " 0.1 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " n_iter_no_change\n",
+ " \n",
+ " n_iter_no_change: int, default=None ``n_iter_no_change`` is used to decide if early stopping will be used to terminate training when validation score is not improving. By default it is set to None to disable early stopping. If set to a number, it will set aside ``validation_fraction`` size of the training data as validation and terminate training when validation score is not improving in all of the previous ``n_iter_no_change`` numbers of iterations. The split is stratified. Values must be in the range `[1, inf)`. See :ref:`sphx_glr_auto_examples_ensemble_plot_gradient_boosting_early_stopping.py`. .. versionadded:: 0.20 \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " tol\n",
+ " \n",
+ " tol: float, default=1e-4 Tolerance for the early stopping. When the loss is not improving by at least tol for ``n_iter_no_change`` iterations (if set to a number), the training stops. Values must be in the range `[0.0, inf)`. .. versionadded:: 0.20 \n",
+ " \n",
+ " \n",
+ " 0.0001 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " ccp_alpha\n",
+ " \n",
+ " ccp_alpha: non-negative float, default=0.0 Complexity parameter used for Minimal Cost-Complexity Pruning. The subtree with the largest cost complexity that is smaller than ``ccp_alpha`` will be chosen. By default, no pruning is performed. Values must be in the range `[0.0, inf)`. See :ref:`minimal_cost_complexity_pruning` for details. See :ref:`sphx_glr_auto_examples_tree_plot_cost_complexity_pruning.py` for an example of such pruning. .. versionadded:: 0.22 \n",
+ " \n",
+ " \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ " \n",
+ "
\n",
+ " \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ " Fitted attributes \n",
+ " \n",
+ " \n",
+ " \n",
+ " Name \n",
+ " Type \n",
+ " Value \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " classes_\n",
+ " \n",
+ " classes_: ndarray of shape (n_classes,) The classes labels. \n",
+ " \n",
+ " \n",
+ " ndarray[int64](2,) \n",
+ " [0,1] \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " estimators_\n",
+ " \n",
+ " estimators_: ndarray of DecisionTreeRegressor of shape (n_estimators, ``n_trees_per_iteration_``) The collection of fitted sub-estimators. ``n_trees_per_iteration_`` is 1 for binary classification, otherwise ``n_classes``. \n",
+ " \n",
+ " \n",
+ " ndarray[object](100, 1) \n",
+ " [[DecisionTreeRegressor(max_depth=20,\n",
+ " random_state=RandomState(MT19937) at 0x10B895C40)],\n",
+ " [DecisionTreeRegressor(max_depth=20,\n",
+ " random_state=RandomState(MT19937) at 0x10B895C40)],\n",
+ " [DecisionTreeRegressor(max_depth=20,\n",
+ " random_state=RandomState(MT19937) at 0x10B895C40)],\n",
+ " ...,\n",
+ " [DecisionTreeRegressor(max_depth=20,\n",
+ " random_state=RandomState(MT19937) at 0x10B895C40)],\n",
+ " [DecisionTreeRegressor(max_depth=20,\n",
+ " random_state=RandomState(MT19937) at 0x10B895C40)],\n",
+ " [DecisionTreeRegressor(max_depth=20,\n",
+ " random_state=RandomState(MT19937) at 0x10B895C40)]] \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " feature_importances_\n",
+ " \n",
+ " feature_importances_: ndarray of shape (n_features,) The impurity-based feature importances. The higher, the more important the feature. The importance of a feature is computed as the (normalized) total reduction of the MSE brought by that feature. It is also known as the Gini importance. Warning: impurity-based feature importances can be misleading for high cardinality features (many unique values). See :func:`sklearn.inspection.permutation_importance` as an alternative. \n",
+ " \n",
+ " \n",
+ " ndarray[float64](19,) \n",
+ " [0.01,0.01,0.25,...,0.08,0.1 ,0.12] \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " feature_names_in_\n",
+ " \n",
+ " feature_names_in_: ndarray of shape (`n_features_in_`,) Names of features seen during :term:`fit`. Defined only when `X` has feature names that are all strings. .. versionadded:: 1.0 \n",
+ " \n",
+ " \n",
+ " ndarray[object](19,) \n",
+ " ['HomePlanet_Europa','HomePlanet_Mars','CryoSleep_True',...,'ShoppingMall',\n",
+ " 'Spa','VRDeck'] \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " init_\n",
+ " \n",
+ " init_: estimator The estimator that provides the initial predictions. Set via the ``init`` argument. \n",
+ " \n",
+ " \n",
+ " DummyClassifier \n",
+ " DummyClassifier() \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " max_features_\n",
+ " \n",
+ " max_features_: int The inferred value of max_features. \n",
+ " \n",
+ " \n",
+ " int \n",
+ " 19 \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " n_classes_\n",
+ " \n",
+ " n_classes_: int The number of classes. \n",
+ " \n",
+ " \n",
+ " int \n",
+ " 2 \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " n_estimators_\n",
+ " \n",
+ " n_estimators_: int The number of estimators as selected by early stopping (if ``n_iter_no_change`` is specified). Otherwise it is set to ``n_estimators``. .. versionadded:: 0.20 \n",
+ " \n",
+ " \n",
+ " int \n",
+ " 100 \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " n_features_in_\n",
+ " \n",
+ " n_features_in_: int Number of features seen during :term:`fit`. .. versionadded:: 0.24 \n",
+ " \n",
+ " \n",
+ " int \n",
+ " 19 \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " n_trees_per_iteration_\n",
+ " \n",
+ " n_trees_per_iteration_: int The number of trees that are built at each iteration. For binary classifiers, this is always 1. .. versionadded:: 1.4.0 \n",
+ " \n",
+ " \n",
+ " int \n",
+ " 1 \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " train_score_\n",
+ " \n",
+ " train_score_: ndarray of shape (n_estimators,) The i-th score ``train_score_[i]`` is the loss of the model at iteration ``i`` on the in-bag sample. If ``subsample == 1`` this is the loss on the training data. \n",
+ " \n",
+ " \n",
+ " ndarray[float64](100,) \n",
+ " [1.24,1.11,1.01,...,0.22,0.22,0.22] \n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ "GradientBoostingClassifier(max_depth=20)"
+ ]
+ },
+ "execution_count": 10,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
- "#your code here"
+ "#your code here\n",
+ "gb_reg.fit(X_train_full, y_train)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "accuracy 78.37\n",
+ "recall 0.80\n",
+ "F1-score 0.79\n",
+ "Precision_score 0.77\n"
+ ]
+ }
+ ],
+ "source": [
+ "y_pred_test_gb = gb_reg.predict(X_test_full)\n",
+ "\n",
+ "print(f\"accuracy {accuracy_score(y_test, y_pred_test_gb)*100: .2f}\")\n",
+ "print(f\"recall {recall_score(y_test, y_pred_test_gb): .2f}\")\n",
+ "print(f\"F1-score {f1_score(y_test, y_pred_test_gb): .2f}\")\n",
+ "print(f\"Precision_score {precision_score(y_test, y_pred_test_gb): .2f}\")"
]
},
{
@@ -283,11 +1929,18 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
- "#your code here"
+ "#your code here\n",
+ "parameter_grid = {\"max_depth\": [3, 10],\n",
+ " \"n_estimators\": [10, 200]}\n",
+ "\n",
+ "gb_classif = GradientBoostingClassifier(random_state=123)\n",
+ "\n",
+ "confidence_level = 0.95\n",
+ "folds = 10"
]
},
{
@@ -299,10 +1952,132 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 13,
"metadata": {},
- "outputs": [],
- "source": []
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Fitting 10 folds for each of 4 candidates, totalling 40 fits\n",
+ "[CV 1/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 1/10; 1/4] END max_depth=3, n_estimators=10;, score=0.737 total time= 0.0s\n",
+ "[CV 2/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 2/10; 1/4] END max_depth=3, n_estimators=10;, score=0.777 total time= 0.0s\n",
+ "[CV 3/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 3/10; 1/4] END max_depth=3, n_estimators=10;, score=0.798 total time= 0.0s\n",
+ "[CV 4/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 4/10; 1/4] END max_depth=3, n_estimators=10;, score=0.771 total time= 0.0s\n",
+ "[CV 5/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 5/10; 1/4] END max_depth=3, n_estimators=10;, score=0.758 total time= 0.0s\n",
+ "[CV 6/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 6/10; 1/4] END max_depth=3, n_estimators=10;, score=0.775 total time= 0.0s\n",
+ "[CV 7/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 7/10; 1/4] END max_depth=3, n_estimators=10;, score=0.758 total time= 0.0s\n",
+ "[CV 8/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 8/10; 1/4] END max_depth=3, n_estimators=10;, score=0.771 total time= 0.0s\n",
+ "[CV 9/10; 1/4] START max_depth=3, n_estimators=10...............................\n",
+ "[CV 9/10; 1/4] END max_depth=3, n_estimators=10;, score=0.771 total time= 0.0s\n",
+ "[CV 10/10; 1/4] START max_depth=3, n_estimators=10..............................\n",
+ "[CV 10/10; 1/4] END max_depth=3, n_estimators=10;, score=0.765 total time= 0.0s\n",
+ "[CV 1/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 1/10; 2/4] END max_depth=3, n_estimators=200;, score=0.783 total time= 0.5s\n",
+ "[CV 2/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 2/10; 2/4] END max_depth=3, n_estimators=200;, score=0.817 total time= 0.5s\n",
+ "[CV 3/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 3/10; 2/4] END max_depth=3, n_estimators=200;, score=0.828 total time= 0.5s\n",
+ "[CV 4/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 4/10; 2/4] END max_depth=3, n_estimators=200;, score=0.805 total time= 0.5s\n",
+ "[CV 5/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 5/10; 2/4] END max_depth=3, n_estimators=200;, score=0.795 total time= 0.5s\n",
+ "[CV 6/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 6/10; 2/4] END max_depth=3, n_estimators=200;, score=0.812 total time= 0.5s\n",
+ "[CV 7/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 7/10; 2/4] END max_depth=3, n_estimators=200;, score=0.807 total time= 0.5s\n",
+ "[CV 8/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 8/10; 2/4] END max_depth=3, n_estimators=200;, score=0.778 total time= 0.5s\n",
+ "[CV 9/10; 2/4] START max_depth=3, n_estimators=200..............................\n",
+ "[CV 9/10; 2/4] END max_depth=3, n_estimators=200;, score=0.795 total time= 0.5s\n",
+ "[CV 10/10; 2/4] START max_depth=3, n_estimators=200.............................\n",
+ "[CV 10/10; 2/4] END max_depth=3, n_estimators=200;, score=0.799 total time= 0.5s\n",
+ "[CV 1/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 1/10; 3/4] END max_depth=10, n_estimators=10;, score=0.769 total time= 0.1s\n",
+ "[CV 2/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 2/10; 3/4] END max_depth=10, n_estimators=10;, score=0.802 total time= 0.1s\n",
+ "[CV 3/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 3/10; 3/4] END max_depth=10, n_estimators=10;, score=0.803 total time= 0.1s\n",
+ "[CV 4/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 4/10; 3/4] END max_depth=10, n_estimators=10;, score=0.786 total time= 0.1s\n",
+ "[CV 5/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 5/10; 3/4] END max_depth=10, n_estimators=10;, score=0.767 total time= 0.1s\n",
+ "[CV 6/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 6/10; 3/4] END max_depth=10, n_estimators=10;, score=0.801 total time= 0.1s\n",
+ "[CV 7/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 7/10; 3/4] END max_depth=10, n_estimators=10;, score=0.778 total time= 0.1s\n",
+ "[CV 8/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 8/10; 3/4] END max_depth=10, n_estimators=10;, score=0.777 total time= 0.1s\n",
+ "[CV 9/10; 3/4] START max_depth=10, n_estimators=10..............................\n",
+ "[CV 9/10; 3/4] END max_depth=10, n_estimators=10;, score=0.784 total time= 0.1s\n",
+ "[CV 10/10; 3/4] START max_depth=10, n_estimators=10.............................\n",
+ "[CV 10/10; 3/4] END max_depth=10, n_estimators=10;, score=0.773 total time= 0.1s\n",
+ "[CV 1/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 1/10; 4/4] END max_depth=10, n_estimators=200;, score=0.775 total time= 1.9s\n",
+ "[CV 2/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 2/10; 4/4] END max_depth=10, n_estimators=200;, score=0.803 total time= 1.9s\n",
+ "[CV 3/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 3/10; 4/4] END max_depth=10, n_estimators=200;, score=0.798 total time= 1.9s\n",
+ "[CV 4/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 4/10; 4/4] END max_depth=10, n_estimators=200;, score=0.792 total time= 1.9s\n",
+ "[CV 5/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 5/10; 4/4] END max_depth=10, n_estimators=200;, score=0.784 total time= 1.9s\n",
+ "[CV 6/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 6/10; 4/4] END max_depth=10, n_estimators=200;, score=0.788 total time= 1.9s\n",
+ "[CV 7/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 7/10; 4/4] END max_depth=10, n_estimators=200;, score=0.786 total time= 1.9s\n",
+ "[CV 8/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 8/10; 4/4] END max_depth=10, n_estimators=200;, score=0.773 total time= 1.9s\n",
+ "[CV 9/10; 4/4] START max_depth=10, n_estimators=200.............................\n",
+ "[CV 9/10; 4/4] END max_depth=10, n_estimators=200;, score=0.777 total time= 1.9s\n",
+ "[CV 10/10; 4/4] START max_depth=10, n_estimators=200............................\n",
+ "[CV 10/10; 4/4] END max_depth=10, n_estimators=200;, score=0.769 total time= 1.9s\n",
+ "\n",
+ "\n",
+ "Time taken to find the best combination of hyperparameters among the given ones: 26.0840 seconds\n",
+ "\n",
+ "\n",
+ "The best combination of hyperparameters has been: {'max_depth': 3, 'n_estimators': 200}\n",
+ "The best CV accuracy is: 0.8020\n",
+ "The R2 confidence interval for the best combination of hyperparameters is: ( 0.7918, 0.8020, 0.8123) \n"
+ ]
+ }
+ ],
+ "source": [
+ "grid_search = GridSearchCV(gb_classif, param_grid=parameter_grid, cv=folds, verbose=10, error_score='raise')\n",
+ "\n",
+ "start_time = time.time()\n",
+ "grid_search.fit(X_train_full, y_train)\n",
+ "end_time = time.time()\n",
+ "\n",
+ "print(\"\\n\")\n",
+ "print(f\"Time taken to find the best combination of hyperparameters among the given ones: {end_time - start_time: .4f} seconds\")\n",
+ "print(\"\\n\")\n",
+ "\n",
+ "\n",
+ "print(f\"The best combination of hyperparameters has been: {grid_search.best_params_}\")\n",
+ "print(f\"The best CV accuracy is: {grid_search.best_score_: .4f}\")\n",
+ "\n",
+ "results_gs_df = pd.DataFrame(grid_search.cv_results_).sort_values(by=\"mean_test_score\", ascending=False)\n",
+ "\n",
+ "gs_mean_score = results_gs_df.iloc[0,-3]\n",
+ "gs_sem = results_gs_df.iloc[0,-2] / np.sqrt(folds)\n",
+ "\n",
+ "gs_tc = st.t.ppf(1-((1-confidence_level)/2), df=folds-1)\n",
+ "gs_lower_bound = gs_mean_score - ( gs_tc * gs_sem )\n",
+ "gs_upper_bound = gs_mean_score + ( gs_tc * gs_sem )\n",
+ "\n",
+ "print(f\"The R2 confidence interval for the best combination of hyperparameters is: \\\n",
+ " ({gs_lower_bound: .4f}, {gs_mean_score: .4f}, {gs_upper_bound: .4f}) \")\n"
+ ]
},
{
"cell_type": "markdown",
@@ -311,6 +2086,50 @@
"- Evaluate your model"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ "\n",
+ "accuracy 78.52\n",
+ "accuracy_train 43.53\n",
+ "recall 0.82\n",
+ "F1-score 0.79\n",
+ "Precision_score 0.77\n",
+ "\n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "best_model = grid_search.best_estimator_\n",
+ "y_pred_test_df = best_model.predict(X_test_full)\n",
+ "y_pred_train_df = best_model.predict(X_train_full_np_df)\n",
+ "\n",
+ "print(\"\\n\")\n",
+ "print(f\"accuracy {accuracy_score(y_test, y_pred_test_df)*100: .2f}\")\n",
+ "print(f\"accuracy_train {accuracy_score(y_train, y_pred_train_df)*100: .2f}\")\n",
+ "\n",
+ "print(f\"recall {recall_score(y_test, y_pred_test_df): .2f}\")\n",
+ "print(f\"F1-score {f1_score(y_test, y_pred_test_df): .2f}\")\n",
+ "print(f\"Precision_score {precision_score(y_test, y_pred_test_df): .2f}\")\n",
+ "print(\"\\n\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "**Conclusion**\n",
+ "We gain a bit better results with \"best model\" parameters and we also can see, that model wasn't overfitted ('accuracy_train' metric is below 'accuracy')"
+ ]
+ },
{
"cell_type": "code",
"execution_count": null,
@@ -321,7 +2140,7 @@
],
"metadata": {
"kernelspec": {
- "display_name": "Python 3",
+ "display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -335,9 +2154,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.10.9"
+ "version": "3.14.6"
}
},
"nbformat": 4,
- "nbformat_minor": 2
+ "nbformat_minor": 4
}