Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensemble method with validation on Updated ipynb file #108

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions Stock_Price_Prediction(Updated).ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5215,6 +5215,82 @@
"plt.tight_layout()\n",
"plt.show()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Ensemble method using random forest and AdaBoost"
]
},
{
"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)"
]
},
{
"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": {
Expand Down
Loading