diff --git a/.saropa/diagnostics/history.json b/.saropa/diagnostics/history.json new file mode 100644 index 0000000..596480a --- /dev/null +++ b/.saropa/diagnostics/history.json @@ -0,0 +1,15 @@ +{ + "version": 1, + "snapshots": [ + { + "commitSha": "b4fae1248121146facc49dde43c36b2ff4d0edc1", + "generatedAt": "2026-08-07T03:30:12.625Z", + "total": 0, + "errors": 0, + "warnings": 0, + "advisor": 0, + "lints": 0, + "logCapture": 0 + } + ] +} \ No newline at end of file diff --git a/lab-hyper-tuning.ipynb b/lab-hyper-tuning.ipynb index 847d487..9c7635b 100644 --- a/lab-hyper-tuning.ipynb +++ b/lab-hyper-tuning.ipynb @@ -35,7 +35,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ @@ -47,11 +47,182 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 32, "metadata": {}, "outputs": [ { "data": { + "application/vnd.microsoft.datawrangler.viewer.v0+json": { + "columns": [ + { + "name": "index", + "rawType": "int64", + "type": "integer" + }, + { + "name": "PassengerId", + "rawType": "str", + "type": "string" + }, + { + "name": "HomePlanet", + "rawType": "str", + "type": "string" + }, + { + "name": "CryoSleep", + "rawType": "object", + "type": "unknown" + }, + { + "name": "Cabin", + "rawType": "str", + "type": "string" + }, + { + "name": "Destination", + "rawType": "str", + "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": "str", + "type": "string" + }, + { + "name": "Transported", + "rawType": "bool", + "type": "boolean" + } + ], + "ref": "a9e5a9a0-96d8-4f96-863a-4248ad90cb97", + "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": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "- Evaluate your model" + "spaceship.head()" ] }, { "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "#your code here" - ] - }, - { - "cell_type": "markdown", + "execution_count": 35, "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "C:\\Users\\teres\\AppData\\Local\\Temp\\ipykernel_24644\\3838193136.py:14: 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", + " cat_cols = spaceship.select_dtypes(include=[\"object\", \"bool\"]).columns\n" + ] + } + ], "source": [ - "**Grid/Random Search**" + "from sklearn.preprocessing import LabelEncoder\n", + "\n", + "# Make a copy\n", + "spaceship = spaceship.copy()\n", + "\n", + "# Drop columns that won't help the model\n", + "spaceship = spaceship.drop(columns=[\"PassengerId\", \"Name\", \"Cabin\"])\n", + "\n", + "# Fill missing numerical values\n", + "num_cols = spaceship.select_dtypes(include=[\"float64\", \"int64\"]).columns\n", + "spaceship[num_cols] = spaceship[num_cols].fillna(spaceship[num_cols].median())\n", + "\n", + "# Fill missing categorical values\n", + "cat_cols = spaceship.select_dtypes(include=[\"object\", \"bool\"]).columns\n", + "for col in cat_cols:\n", + " spaceship[col] = spaceship[col].fillna(spaceship[col].mode()[0])\n", + "\n", + "# Encode categorical columns\n", + "encoder = LabelEncoder()\n", + "\n", + "for col in spaceship.columns:\n", + " if spaceship[col].dtype == \"object\" or spaceship[col].dtype == \"bool\":\n", + " spaceship[col] = encoder.fit_transform(spaceship[col])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "For this lab we will use Grid Search." + "Now perform the same as before:\n", + "- Feature Scaling\n", + "- Feature Selection\n" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 36, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HomePlanet str\n", + "CryoSleep int64\n", + "Destination str\n", + "Age float64\n", + "VIP int64\n", + "RoomService float64\n", + "FoodCourt float64\n", + "ShoppingMall float64\n", + "Spa float64\n", + "VRDeck float64\n", + "Transported int64\n", + "dtype: object\n" + ] + } + ], "source": [ - "- Define hyperparameters to fine tune." + "print(spaceship.dtypes)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, "outputs": [], "source": [ - "#your code here" + "from sklearn.preprocessing import LabelEncoder\n", + "\n", + "le = LabelEncoder()\n", + "\n", + "spaceship[\"HomePlanet\"] = le.fit_transform(spaceship[\"HomePlanet\"])\n", + "spaceship[\"Destination\"] = le.fit_transform(spaceship[\"Destination\"])" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 42, "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "HomePlanet int64\n", + "CryoSleep int64\n", + "Destination int64\n", + "Age float64\n", + "VIP int64\n", + "RoomService float64\n", + "FoodCourt float64\n", + "ShoppingMall float64\n", + "Spa float64\n", + "VRDeck float64\n", + "Transported int64\n", + "dtype: object\n" + ] + } + ], "source": [ - "- Run Grid Search" + "print(spaceship.dtypes)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from sklearn.model_selection import train_test_split\n", + "from sklearn.preprocessing import StandardScaler\n", + "\n", + "X = spaceship.drop(\"Transported\", axis=1)\n", + "y = spaceship[\"Transported\"]\n", + "\n", + "X_train, X_test, y_train, y_test = train_test_split(\n", + " X,\n", + " y,\n", + " test_size=0.2,\n", + " random_state=42\n", + ")\n", + "\n", + "scaler = StandardScaler()\n", + "\n", + "X_train_scaled = scaler.fit_transform(X_train)\n", + "X_test_scaled = scaler.transform(X_test)" + ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "- Evaluate your model" + "- 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, + "execution_count": 50, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/html": [ + "
RandomForestClassifier(random_state=42)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ], + "text/plain": [ + "RandomForestClassifier(random_state=42)" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sklearn.ensemble import RandomForestClassifier\n", + "\n", + "rf = RandomForestClassifier(random_state=42)\n", + "\n", + "rf.fit(X_train_scaled, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
RandomForestClassifier(random_state=42)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ], + "text/plain": [ + "RandomForestClassifier(random_state=42)" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sklearn.ensemble import RandomForestClassifier\n", + "\n", + "rf = RandomForestClassifier(random_state=42)\n", + "\n", + "rf.fit(X_train_scaled, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Accuracy: - lab-hyper-tuning.ipynb:5 0.780333525014376\n" + ] + } + ], + "source": [ + "from sklearn.metrics import accuracy_score\n", + "\n", + "pred = rf.predict(X_test_scaled)\n", + "\n", + "print(\"Accuracy: - lab-hyper-tuning.ipynb:5\", accuracy_score(y_test, pred))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Evaluate your model" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Accuracy: - lab-hyper-tuning.ipynb:5 0.780333525014376\n" + ] + } + ], + "source": [ + "from sklearn.metrics import accuracy_score\n", + "\n", + "pred = rf.predict(X_test_scaled)\n", + "\n", + "print(\"Accuracy: - lab-hyper-tuning.ipynb:5\", accuracy_score(y_test, pred))" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
RandomForestClassifier(random_state=42)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ], + "text/plain": [ + "RandomForestClassifier(random_state=42)" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sklearn.ensemble import RandomForestClassifier\n", + "\n", + "rf = RandomForestClassifier(random_state=42)\n", + "\n", + "rf.fit(X_train_scaled, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Accuracy: - lab-hyper-tuning.ipynb:5 0.780333525014376\n", + " precision recall f1-score support\n", + "\n", + " 0 0.79 0.77 0.78 861\n", + " 1 0.78 0.79 0.79 878\n", + "\n", + " accuracy 0.78 1739\n", + " macro avg 0.78 0.78 0.78 1739\n", + "weighted avg 0.78 0.78 0.78 1739\n", + "\n" + ] + } + ], + "source": [ + "from sklearn.metrics import accuracy_score, classification_report\n", + "\n", + "y_pred = rf.predict(X_test_scaled)\n", + "\n", + "print(\"Accuracy: - lab-hyper-tuning.ipynb:5\", accuracy_score(y_test, y_pred))\n", + "print(classification_report(y_test, y_pred))" + ] + }, + { + "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": 76, + "metadata": {}, + "outputs": [], + "source": [ + "param_grid = {\n", + " \"n_estimators\": [100, 200, 300],\n", + " \"max_depth\": [None, 10, 20, 30],\n", + " \"min_samples_split\": [2, 5, 10],\n", + " \"min_samples_leaf\": [1, 2, 4],\n", + " \"bootstrap\": [True, False]\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Run Grid Search" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
GridSearchCV(cv=5, estimator=RandomForestClassifier(random_state=42), n_jobs=-1,\n",
+       "             param_grid={'bootstrap': [True, False],\n",
+       "                         'max_depth': [None, 10, 20, 30],\n",
+       "                         'min_samples_leaf': [1, 2, 4],\n",
+       "                         'min_samples_split': [2, 5, 10],\n",
+       "                         'n_estimators': [100, 200, 300]},\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=5, estimator=RandomForestClassifier(random_state=42), n_jobs=-1,\n", + " param_grid={'bootstrap': [True, False],\n", + " 'max_depth': [None, 10, 20, 30],\n", + " 'min_samples_leaf': [1, 2, 4],\n", + " 'min_samples_split': [2, 5, 10],\n", + " 'n_estimators': [100, 200, 300]},\n", + " scoring='accuracy')" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sklearn.model_selection import GridSearchCV\n", + "\n", + "grid_search = GridSearchCV(\n", + " estimator=rf,\n", + " param_grid=param_grid,\n", + " cv=5,\n", + " scoring=\"accuracy\",\n", + " n_jobs=-1\n", + ")\n", + "\n", + "grid_search.fit(X_train_scaled, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Best Parameters: - lab-hyper-tuning.ipynb:1\n", + "{'bootstrap': False, 'max_depth': 10, 'min_samples_leaf': 4, 'min_samples_split': 2, 'n_estimators': 100}\n", + "Best CV Score: - lab-hyper-tuning.ipynb:4\n", + "0.8011233572451888\n" + ] + } + ], + "source": [ + "print(\"Best Parameters: - lab-hyper-tuning.ipynb:1\")\n", + "print(grid_search.best_params_)\n", + "\n", + "print(\"Best CV Score: - lab-hyper-tuning.ipynb:4\")\n", + "print(grid_search.best_score_)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Evaluate your model" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Accuracy: - lab-hyper-tuning.ipynb:5 0.7849338700402531\n", + " precision recall f1-score support\n", + "\n", + " 0 0.81 0.74 0.77 861\n", + " 1 0.76 0.83 0.80 878\n", + "\n", + " accuracy 0.78 1739\n", + " macro avg 0.79 0.78 0.78 1739\n", + "weighted avg 0.79 0.78 0.78 1739\n", + "\n" + ] + } + ], + "source": [ + "best_rf = grid_search.best_estimator_\n", + "\n", + "y_pred_best = best_rf.predict(X_test_scaled)\n", + "\n", + "print(\"Accuracy: - lab-hyper-tuning.ipynb:5\", accuracy_score(y_test, y_pred_best))\n", + "print(classification_report(y_test, y_pred_best))" + ] } ], "metadata": { @@ -335,7 +7239,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.14.6" } }, "nbformat": 4,