diff --git a/lab-hyper-tuning.ipynb b/lab-hyper-tuning.ipynb index 847d487..c16d13a 100644 --- a/lab-hyper-tuning.ipynb +++ b/lab-hyper-tuning.ipynb @@ -42,7 +42,9 @@ "#Libraries\n", "import pandas as pd\n", "import numpy as np\n", - "from sklearn.model_selection import train_test_split" + "from sklearn.model_selection import train_test_split\n", + "from sklearn.impute import SimpleImputer\n", + "from sklearn.preprocessing import StandardScaler" ] }, { @@ -221,11 +223,34 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "((6954, 6), (1739, 6))" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#your code here" + "#your code here\n", + "\n", + "x = spaceship.select_dtypes(include=\"number\")\n", + "imputer = SimpleImputer(strategy=\"median\")\n", + "x_imputed = imputer.fit_transform(x)\n", + "\n", + "scaler = StandardScaler()\n", + "X = scaler.fit_transform(x_imputed)\n", + "y = spaceship[\"Transported\"]\n", + "\n", + "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n", + "\n", + "X_train.shape, X_test.shape" ] }, { @@ -237,11 +262,759 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
GradientBoostingClassifier(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": [ + "GradientBoostingClassifier(random_state=42)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#your code here" + "#your code here\n", + "\n", + "from sklearn.ensemble import GradientBoostingClassifier\n", + "\n", + "gb_base = GradientBoostingClassifier(random_state=42)\n", + "gb_base.fit(X_train,y_train)" ] }, { @@ -253,11 +1026,35 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Baseline Gradient Boosting accuracy: 0.7814836112708453\n", + " precision recall f1-score support\n", + "\n", + " False 0.83 0.70 0.76 861\n", + " True 0.75 0.86 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": [ - "#your code here" + "#your code here\n", + "from sklearn.metrics import accuracy_score, classification_report\n", + "\n", + "y_pred_baseline = gb_base.predict(X_test)\n", + "baseline_acc = accuracy_score(y_test, y_pred_baseline)\n", + "\n", + "print(\"Baseline Gradient Boosting accuracy:\", baseline_acc)\n", + "print(classification_report(y_test, y_pred_baseline))" ] }, { @@ -283,11 +1080,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "#your code here" + "#your code here\n", + "from sklearn.model_selection import GridSearchCV\n", + "\n", + "param_grid = {\"n_estimators\": [100, 200], \"learning_rate\": [0.01, 0.1, 0.2], \"max_depth\": [2, 3, 4],\"min_samples_split\": [2, 5]}\n", + "\n", + "grid_search = GridSearchCV(estimator=GradientBoostingClassifier(random_state=42),\n", + " param_grid=param_grid,\n", + " cv=5,\n", + " scoring=\"accuracy\",\n", + " n_jobs=-1,\n", + " verbose=1\n", + ")\n" ] }, { @@ -299,10 +1107,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Fitting 5 folds for each of 36 candidates, totalling 180 fits\n", + "Best hyperparameters: {'learning_rate': 0.1, 'max_depth': 2, 'min_samples_split': 2, 'n_estimators': 100}\n", + "Best cross-validated accuracy: 0.7963784658829371\n" + ] + } + ], + "source": [ + "grid_search.fit(X_train, y_train)\n", + "\n", + "print(\"Best hyperparameters:\", grid_search.best_params_)\n", + "print(\"Best cross-validated accuracy:\", grid_search.best_score_)\n", + "\n", + "best_gb = grid_search.best_estimator_" + ] }, { "cell_type": "markdown", @@ -311,6 +1136,41 @@ "- Evaluate your model" ] }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " precision recall f1-score support\n", + "\n", + " False 0.83 0.70 0.76 861\n", + " True 0.75 0.86 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", + "Confusion matrix:\n", + "[[606 255]\n", + " [127 751]]\n" + ] + } + ], + "source": [ + "from sklearn.metrics import confusion_matrix\n", + "\n", + "y_pred_tuned = best_gb.predict(X_test)\n", + "tuned_acc = accuracy_score(y_test, y_pred_tuned)\n", + "\n", + "print(classification_report(y_test, y_pred_tuned))\n", + "print(\"Confusion matrix:\")\n", + "print(confusion_matrix(y_test, y_pred_tuned))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -321,7 +1181,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "base", "language": "python", "name": "python3" }, @@ -335,7 +1195,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.13.9" } }, "nbformat": 4,