diff --git a/lab-hyper-tuning.ipynb b/lab-hyper-tuning.ipynb
index 847d487..3cc9a78 100644
--- a/lab-hyper-tuning.ipynb
+++ b/lab-hyper-tuning.ipynb
@@ -221,11 +221,35 @@
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
- "#your code here"
+ "spaceship = spaceship.dropna()\n",
+ "spaceship[\"Cabin\"] = spaceship[\"Cabin\"].str.split(\"/\")\n",
+ "spaceship[\"Cabin\"] = spaceship[\"Cabin\"].str[0]\n",
+ "spaceship = spaceship.drop(columns = [\"PassengerId\", \"Name\"])\n",
+ "\n",
+ "x = spaceship.drop(columns = [\"Transported\"]) \n",
+ "y = spaceship[\"Transported\"]\n",
+ "\n",
+ "from sklearn.preprocessing import StandardScaler\n",
+ "scaler = StandardScaler()\n",
+ "\n",
+ "X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)\n",
+ "\n",
+ "from sklearn.preprocessing import OneHotEncoder\n",
+ "encoder = OneHotEncoder(sparse_output=False, handle_unknown=\"ignore\")\n",
+ "\n",
+ "categorical_cols = [\"HomePlanet\", \"CryoSleep\", \"Cabin\", \"Destination\", \"VIP\"] \n",
+ "x_train_encoded = encoder.fit_transform(X_train[categorical_cols])\n",
+ "x_test_encoded = encoder.transform(X_test[categorical_cols])\n",
+ "\n",
+ "x_train_scaled = scaler.fit_transform(X_train.select_dtypes(include=\"number\"))\n",
+ "x_test_scaled = scaler.transform(X_test.select_dtypes(include=\"number\"))\n",
+ "\n",
+ "x_train_final = np.hstack([x_train_scaled, x_train_encoded])\n",
+ "x_test_final = np.hstack([x_test_scaled, x_test_encoded])"
]
},
{
@@ -237,11 +261,939 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 6,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Fitting 5 folds for each of 20 candidates, totalling 100 fits\n"
+ ]
+ },
+ {
+ "data": {
+ "text/html": [
+ "
RandomizedSearchCV(cv=5, estimator=RandomForestClassifier(random_state=0),\n",
+ " n_iter=20, n_jobs=-1,\n",
+ " param_distributions={'max_depth': [None, 10, 20, 30],\n",
+ " 'max_features': ['sqrt', 'log2'],\n",
+ " 'min_samples_leaf': [1, 2, 4],\n",
+ " 'min_samples_split': [2, 5, 10],\n",
+ " 'n_estimators': [100, 200, 300, 500]},\n",
+ " random_state=42, scoring='accuracy', verbose=2) 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. \n",
+ "
\n",
+ "
\n",
+ " Parameters \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " estimator \n",
+ " RandomForestC...andom_state=0) \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " param_distributions \n",
+ " {'max_depth': [None, 10, ...], 'max_features': ['sqrt', 'log2'], 'min_samples_leaf': [1, 2, ...], 'min_samples_split': [2, 5, ...], ...} \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " n_iter \n",
+ " 20 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " scoring \n",
+ " 'accuracy' \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " n_jobs \n",
+ " -1 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " refit \n",
+ " True \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " cv \n",
+ " 5 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " verbose \n",
+ " 2 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " pre_dispatch \n",
+ " '2*n_jobs' \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " random_state \n",
+ " 42 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " error_score \n",
+ " nan \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " return_train_score \n",
+ " False \n",
+ " \n",
+ " \n",
+ " \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ "
\n",
+ "
\n",
+ " Parameters \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " n_estimators \n",
+ " 500 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " criterion \n",
+ " 'gini' \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " max_depth \n",
+ " 30 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " min_samples_split \n",
+ " 2 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " min_samples_leaf \n",
+ " 4 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " min_weight_fraction_leaf \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " max_features \n",
+ " 'log2' \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " max_leaf_nodes \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " min_impurity_decrease \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " bootstrap \n",
+ " True \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " oob_score \n",
+ " False \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " n_jobs \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " random_state \n",
+ " 0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " verbose \n",
+ " 0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " warm_start \n",
+ " False \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " class_weight \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " ccp_alpha \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " max_samples \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " monotonic_cst \n",
+ " None \n",
+ " \n",
+ " \n",
+ " \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ "RandomizedSearchCV(cv=5, estimator=RandomForestClassifier(random_state=0),\n",
+ " n_iter=20, n_jobs=-1,\n",
+ " param_distributions={'max_depth': [None, 10, 20, 30],\n",
+ " 'max_features': ['sqrt', 'log2'],\n",
+ " 'min_samples_leaf': [1, 2, 4],\n",
+ " 'min_samples_split': [2, 5, 10],\n",
+ " 'n_estimators': [100, 200, 300, 500]},\n",
+ " random_state=42, scoring='accuracy', verbose=2)"
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
- "#your code here"
+ "from sklearn.ensemble import RandomForestClassifier\n",
+ "from sklearn.model_selection import RandomizedSearchCV\n",
+ "\n",
+ "rf = RandomForestClassifier(random_state=0)\n",
+ "\n",
+ "param_dist = {\n",
+ " \"n_estimators\": [100, 200, 300, 500],\n",
+ " \"max_depth\": [None, 10, 20, 30],\n",
+ " \"min_samples_split\": [2, 5, 10],\n",
+ " \"min_samples_leaf\": [1, 2, 4],\n",
+ " \"max_features\": [\"sqrt\", \"log2\"]\n",
+ "}\n",
+ "\n",
+ "random_search = RandomizedSearchCV(\n",
+ " estimator=rf,\n",
+ " param_distributions=param_dist,\n",
+ " n_iter=20, # Try 20 random parameter combinations\n",
+ " cv=5,\n",
+ " scoring=\"accuracy\",\n",
+ " random_state=42,\n",
+ " n_jobs=-1, # Use all CPU cores\n",
+ " verbose=2\n",
+ ")\n",
+ "\n",
+ "random_search.fit(x_train_final, y_train)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Best parameters: {'n_estimators': 500, 'min_samples_split': 2, 'min_samples_leaf': 4, 'max_features': 'log2', 'max_depth': 30}\n",
+ "Best score: 0.803179918867005\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(f\"Best parameters: {random_search.best_params_}\")\n",
+ "print(f\"Best score: {random_search.best_score_}\")"
]
},
{
@@ -253,11 +1205,24 @@
},
{
"cell_type": "code",
- "execution_count": 1,
+ "execution_count": 11,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Random Forest accuracy: 0.7844175491679274\n"
+ ]
+ }
+ ],
"source": [
- "#your code here"
+ "best_rf = random_search.best_estimator_\n",
+ "\n",
+ "pred = best_rf.predict(x_test_final)\n",
+ "\n",
+ "from sklearn.metrics import accuracy_score\n",
+ "print(f\"Random Forest accuracy: {accuracy_score(y_test, pred)}\")"
]
},
{
@@ -283,11 +1248,17 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
- "#your code here"
+ "param_grid = {\n",
+ " \"n_estimators\": [50, 100, 150],\n",
+ " \"max_depth\": [None, 3, 5],\n",
+ " \"min_samples_split\": [2, 4],\n",
+ " \"min_samples_leaf\": [1, 2],\n",
+ " \"criterion\": [\"gini\", \"entropy\"]\n",
+ "}"
]
},
{
@@ -299,10 +1270,875 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 16,
"metadata": {},
- "outputs": [],
- "source": []
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "GridSearchCV(cv=3, estimator=RandomForestClassifier(random_state=0), n_jobs=-1,\n",
+ " param_grid={'criterion': ['gini', 'entropy'],\n",
+ " 'max_depth': [None, 3, 5], 'min_samples_leaf': [1, 2],\n",
+ " 'min_samples_split': [2, 4],\n",
+ " 'n_estimators': [50, 100, 150]},\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. \n",
+ "
\n",
+ "
\n",
+ " Parameters \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " estimator \n",
+ " RandomForestC...andom_state=0) \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " param_grid \n",
+ " {'criterion': ['gini', 'entropy'], 'max_depth': [None, 3, ...], 'min_samples_leaf': [1, 2], 'min_samples_split': [2, 4], ...} \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " scoring \n",
+ " 'accuracy' \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " n_jobs \n",
+ " -1 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " refit \n",
+ " True \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " cv \n",
+ " 3 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " verbose \n",
+ " 0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " pre_dispatch \n",
+ " '2*n_jobs' \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " error_score \n",
+ " nan \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " return_train_score \n",
+ " False \n",
+ " \n",
+ " \n",
+ " \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ "
\n",
+ "
\n",
+ " Parameters \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " n_estimators \n",
+ " 150 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " criterion \n",
+ " 'gini' \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " max_depth \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " min_samples_split \n",
+ " 2 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " min_samples_leaf \n",
+ " 2 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " min_weight_fraction_leaf \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " max_features \n",
+ " 'sqrt' \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " max_leaf_nodes \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " min_impurity_decrease \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " bootstrap \n",
+ " True \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " oob_score \n",
+ " False \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " n_jobs \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " random_state \n",
+ " 0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " verbose \n",
+ " 0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " warm_start \n",
+ " False \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " class_weight \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " ccp_alpha \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " max_samples \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " monotonic_cst \n",
+ " None \n",
+ " \n",
+ " \n",
+ " \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "text/plain": [
+ "GridSearchCV(cv=3, estimator=RandomForestClassifier(random_state=0), n_jobs=-1,\n",
+ " param_grid={'criterion': ['gini', 'entropy'],\n",
+ " 'max_depth': [None, 3, 5], 'min_samples_leaf': [1, 2],\n",
+ " 'min_samples_split': [2, 4],\n",
+ " 'n_estimators': [50, 100, 150]},\n",
+ " scoring='accuracy')"
+ ]
+ },
+ "execution_count": 16,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "from sklearn.model_selection import GridSearchCV\n",
+ "\n",
+ "forest = RandomForestClassifier(random_state=0)\n",
+ "forest_grid = GridSearchCV(\n",
+ " estimator=forest,\n",
+ " param_grid=param_grid,\n",
+ " cv=3,\n",
+ " scoring=\"accuracy\",\n",
+ " n_jobs=-1)\n",
+ "\n",
+ "forest_grid.fit(x_train_final, y_train)"
+ ]
},
{
"cell_type": "markdown",
@@ -311,6 +2147,46 @@
"- Evaluate your model"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Best parameters: {'criterion': 'gini', 'max_depth': None, 'min_samples_leaf': 2, 'min_samples_split': 2, 'n_estimators': 150}\n",
+ "Best cross-validation accuracy: 0.7988235883070427\n",
+ "Best model: RandomForestClassifier(min_samples_leaf=2, n_estimators=150, random_state=0)\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(\"Best parameters:\", forest_grid.best_params_)\n",
+ "print(\"Best cross-validation accuracy:\", forest_grid.best_score_)\n",
+ "print(\"Best model:\", forest_grid.best_estimator_)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Random Forest Accuracy: 0.791981845688351\n"
+ ]
+ }
+ ],
+ "source": [
+ "best_forest = forest_grid.best_estimator_\n",
+ "pred = best_forest.predict(x_test_final) \n",
+ "print(f\"Random Forest Accuracy: {accuracy_score(y_test, pred)}\")"
+ ]
+ },
{
"cell_type": "code",
"execution_count": null,
@@ -321,7 +2197,7 @@
],
"metadata": {
"kernelspec": {
- "display_name": "Python 3",
+ "display_name": "base",
"language": "python",
"name": "python3"
},
@@ -335,7 +2211,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.10.9"
+ "version": "3.13.9"
}
},
"nbformat": 4,