From c5e884575ee2e42293f69c95fbc4d205a6646297 Mon Sep 17 00:00:00 2001 From: SaurabhIndi <116150732+SaurabhIndi@users.noreply.github.com> Date: Tue, 8 Oct 2024 00:37:05 +0530 Subject: [PATCH 1/2] simple ensemble method using random forest and AdaBoost --- Stock_Price_Prediction(Updated).ipynb | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Stock_Price_Prediction(Updated).ipynb b/Stock_Price_Prediction(Updated).ipynb index c108568..84a90cd 100644 --- a/Stock_Price_Prediction(Updated).ipynb +++ b/Stock_Price_Prediction(Updated).ipynb @@ -5215,6 +5215,46 @@ "plt.tight_layout()\n", "plt.show()\n" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This code demonstrates a simple ensemble method using random forest and AdaBoost. It combines the predictions of these two models by taking their average. You can experiment with different ensemble techniques like stacking or voting, and adjust the hyperparameters of the individual models to find the best combination for your specific problem." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.ensemble import RandomForestRegressor, AdaBoostRegressor\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.metrics import mean_squared_error\n", + "\n", + "# Assuming you have your features (X) and target variable (y)\n", + "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n", + "\n", + "# Create individual models\n", + "rf_model = RandomForestRegressor(n_estimators=100, random_state=42)\n", + "adaboost_model = AdaBoostRegressor(n_estimators=100, random_state=42)\n", + "\n", + "# Train the models\n", + "rf_model.fit(X_train, y_train)\n", + "adaboost_model.fit(X_train, y_train)\n", + "\n", + "# Make predictions\n", + "rf_predictions = rf_model.predict(X_test)\n", + "adaboost_predictions = adaboost_model.predict(X_test)\n", + "\n", + "# Combine predictions (simple averaging)\n", + "ensemble_predictions = (rf_predictions + adaboost_predictions) / 2\n", + "\n", + "# Evaluate the ensemble model\n", + "ensemble_mse = mean_squared_error(y_test, ensemble_predictions)\n", + "print(\"Ensemble MSE:\", ensemble_mse)" + ] } ], "metadata": { From 333db66920fb79b32742e3287f98dd4ca395cde4 Mon Sep 17 00:00:00 2001 From: SaurabhIndi <116150732+SaurabhIndi@users.noreply.github.com> Date: Fri, 11 Oct 2024 15:25:34 +0530 Subject: [PATCH 2/2] Ensemble method added with validation --- Stock_Price_Prediction(Updated).ipynb | 38 ++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/Stock_Price_Prediction(Updated).ipynb b/Stock_Price_Prediction(Updated).ipynb index 84a90cd..15f1fc8 100644 --- a/Stock_Price_Prediction(Updated).ipynb +++ b/Stock_Price_Prediction(Updated).ipynb @@ -5220,7 +5220,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This code demonstrates a simple ensemble method using random forest and AdaBoost. It combines the predictions of these two models by taking their average. You can experiment with different ensemble techniques like stacking or voting, and adjust the hyperparameters of the individual models to find the best combination for your specific problem." + "### Ensemble method using random forest and AdaBoost" ] }, { @@ -5255,6 +5255,42 @@ "ensemble_mse = mean_squared_error(y_test, ensemble_predictions)\n", "print(\"Ensemble MSE:\", ensemble_mse)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Validation" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.metrics import accuracy_score, mean_squared_error, mean_absolute_error\n", + "\n", + "# Assuming we have the true labels (y_test) and the ensemble predictions (ensemble_predictions)\n", + "\n", + "# Calculate accuracy\n", + "accuracy = accuracy_score(y_test, ensemble_predictions.round())\n", + "print(\"Accuracy:\", accuracy)\n", + "\n", + "# Calculate RMSE\n", + "rmse = mean_squared_error(y_test, ensemble_predictions, squared=False)\n", + "print(\"RMSE:\", rmse)\n", + "\n", + "# Calculate MAE\n", + "mae = mean_absolute_error(y_test, ensemble_predictions)\n", + "print(\"MAE:\", mae)\n", + "\n", + "# Other relevant metrics\n", + "# For example, if your target variable is categorical:\n", + "# precision = precision_score(y_test, ensemble_predictions.round())\n", + "# recall = recall_score(y_test, ensemble_predictions.round())\n", + "# f1_score = f1_score(y_test, ensemble_predictions.round())" + ] } ], "metadata": {