diff --git a/lab-hyper-tuning.ipynb b/lab-hyper-tuning.ipynb index 847d487..976b5c8 100644 --- a/lab-hyper-tuning.ipynb +++ b/lab-hyper-tuning.ipynb @@ -221,11 +221,29 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "#your code here" + "#feature scaling\n", + "spaceship = spaceship.dropna().reset_index(drop=True)\n", + "spaceship['Cabin'] = spaceship['Cabin'].str.split('/').str[0]\n", + "spaceship = spaceship.drop(columns=['PassengerId', 'Name'])\n", + "spaceship = pd.get_dummies(spaceship, drop_first=True)\n", + "y = spaceship['Transported_True']\n", + "X = spaceship.drop(columns=['Transported_True'])\n", + "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)\n", + "print(f\"X_train shape: {X_train.shape}\")\n", + "print(f\"X_test shape: {X_test.shape}\")\n", + "\n", + "#feature selection\n", + "from sklearn.preprocessing import StandardScaler\n", + "scaler = StandardScaler()\n", + "X_train_scaled = scaler.fit_transform(X_train)\n", + "X_test_scaled = scaler.transform(X_test)\n", + "X_train_scaled = pd.DataFrame(X_train_scaled, columns=X_train.columns, index=X_train.index)\n", + "X_test_scaled = pd.DataFrame(X_test_scaled, columns=X_test.columns, index=X_test.index)\n", + "X_train_scaled.head()" ] }, { @@ -241,7 +259,26 @@ "metadata": {}, "outputs": [], "source": [ - "#your code here" + "param_grid = {\n", + " 'n_estimators': [100, 200, 300],\n", + " 'learning_rate': [0.01, 0.05, 0.1, 0.2],\n", + " 'max_depth': [2, 3, 4],\n", + " 'min_samples_split': [2, 5, 10],\n", + " 'subsample': [0.8, 1.0]\n", + "}\n", + "\n", + "gb = GradientBoostingClassifier(random_state=42)\n", + "grid_search = GridSearchCV(\n", + " estimator=gb,\n", + " param_grid=param_grid,\n", + " cv=5,\n", + " scoring='accuracy',\n", + " n_jobs=-1,\n", + " verbose=1\n", + ")\n", + "grid_search.fit(X_train, y_train)\n", + "print(f\"Best parameters: {grid_search.best_params_}\")\n", + "print(f\"Best cross-validation accuracy: {grid_search.best_score_:.4f}\")" ] }, { @@ -253,11 +290,18 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "#your code here" + "best_gb = grid_search.best_estimator_\n", + "y_pred_best = best_gb.predict(X_test)\n", + "accuracy = accuracy_score(y_test, y_pred_best)\n", + "print(f\"Tuned Gradient Boosting Test Accuracy: {accuracy:.4f}\")\n", + "print(\"\\nClassification Report:\")\n", + "print(classification_report(y_test, y_pred_best))\n", + "print(\"Confusion Matrix:\")\n", + "print(confusion_matrix(y_test, y_pred_best))" ] }, { @@ -287,7 +331,15 @@ "metadata": {}, "outputs": [], "source": [ - "#your code here" + "from sklearn.ensemble import GradientBoostingClassifier\n", + "from sklearn.model_selection import GridSearchCV\n", + "param_grid = {\n", + " 'n_estimators': [100, 200, 300],\n", + " 'learning_rate': [0.01, 0.05, 0.1, 0.2],\n", + " 'max_depth': [2, 3, 4],\n", + " 'min_samples_split': [2, 5, 10],\n", + " 'subsample': [0.8, 1.0]\n", + "}" ] }, { @@ -302,7 +354,20 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gb = GradientBoostingClassifier(random_state=42)\n", + "grid_search = GridSearchCV(\n", + " estimator=gb,\n", + " param_grid=param_grid,\n", + " cv=5,\n", + " scoring='accuracy',\n", + " n_jobs=-1,\n", + " verbose=2\n", + ")\n", + "grid_search.fit(X_train, y_train)\n", + "print(f\"Best parameters: {grid_search.best_params_}\")\n", + "print(f\"Best cross-validation accuracy: {grid_search.best_score_:.4f}\")" + ] }, { "cell_type": "markdown", @@ -316,7 +381,17 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from sklearn.metrics import accuracy_store, classification_report, confusion_matrix\n", + "best_model = grid_search.best_estimator_\n", + "y_pred = best_model.predict(X_test)\n", + "accuracy = accuracy_score(y_test, y_pred)\n", + "print(f\"Test Accuracy: {accuracy:.4f}\")\n", + "print(\"\\nClassification Report:\")\n", + "print(classification_report(y_test, y_pred))\n", + "print(\"Confusion Matrix:\")\n", + "print(confusion_matrix(y_test, y_pred))" + ] } ], "metadata": {