Welcome to the Election Prediction API, a Flask-powered service that predicts the election outcome for a given state and year. The API uses pre-trained machine learning models to deliver accurate predictions, enabling insightful analyses.
-
Predict Election Outcomes: Predict whether a state leans Republican or Democratic for a specified year.
-
Dynamic Model Integration: Plug in various ML models and preprocessors seamlessly.
-
State and Year Filters: Use state and year data to refine predictions.
-
Error Handling: Built-in robust validation and user-friendly error messages.
- Python 3.9 recommended or above
- Deps in Requirments.txt
-
Clone the repository:
https://github.com/sjhaleprogrammer/CSCI6020-final-project.git cd CSCI6020-final-project -
Create a virtual environment:
py -m venv venv # For Windows python3 -m venv venv # For Linux/Mac source venv/bin/activate # For Linux/Mac venv\Scripts\activate # For Windows -
Install dependencies:
pip install -r requirements.txt -
Prepare your models and data:
- Place your pre-trained models and preprocessors in the
models/directory. - Add your processed dataset (e.g.,
all_states_prediction.csv) to thedata_processed/directory.
- Place your pre-trained models and preprocessors in the
-
Start the Flask server:
flask --app frontend/app.py run
-
Access the API at:
http://127.0.0.1:5000
Predicts the election outcome for a given state and year.
- Headers:
Content-Type: application/json - Body:
{ "model_name": "example_model", "state": "North Carolina", "year": 2024 }
- Success (200):
{ "state": "North Carolina", "year": 2024, "predicted_result": "Democratic" } - Error:
- 400: Missing or invalid input fields.
- 404: Dataset or model files not found.
- 500: Internal server error.
- Models: Built using robust machine learning algorithms, each tailored for election prediction tasks.
- Data Preprocessing: Features standardized and normalized using a custom preprocessor.
- Prediction Logic: Combines state-year data with pre-trained models for high-accuracy results.
Imagine you're a political analyst preparing for an election year. Using this API, you can:
- Quickly assess the likelihood of a state leaning Democratic or Republican.
- Integrate predictions into dashboards or visualizations.
- Add support for more advanced ML models (e.g., neural networks).
- Introduce visual dashboards for predictions.
- Expand dataset support for county-level predictions.
Feel free to contribute! Fork the repository, make your changes, and submit a pull request.
This project is licensed under the MIT License.