πŸ—³οΈ Election Prediction App

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.


πŸš€ Features

  • 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.


πŸ“‹ Prerequisites

  • Python 3.9 recommended or above
  • Deps in Requirments.txt

πŸ› οΈ Installation

  1. Clone the repository:

    https://github.com/sjhaleprogrammer/CSCI6020-final-project.git
    cd CSCI6020-final-project
    
  2. 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
    
  3. Install dependencies:

    pip install -r requirements.txt
    
  4. 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 the data_processed/ directory.

πŸƒβ€β™‚οΈ Run the API

  1. Start the Flask server:

    flask --app frontend/app.py run
  2. Access the API at:

    http://127.0.0.1:5000
    

πŸ“‘ Endpoints

POST /predict

Predicts the election outcome for a given state and year.

Request

  • Headers: Content-Type: application/json
  • Body:
    {
        "model_name": "example_model",
        "state": "North Carolina",
        "year": 2024
    }

Response

  • 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.

🧠 Behind the Scenes

  1. Models: Built using robust machine learning algorithms, each tailored for election prediction tasks.
  2. Data Preprocessing: Features standardized and normalized using a custom preprocessor.
  3. Prediction Logic: Combines state-year data with pre-trained models for high-accuracy results.

πŸ“– Example Use Case

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.

🌟 Future Enhancements

  • Add support for more advanced ML models (e.g., neural networks).
  • Introduce visual dashboards for predictions.
  • Expand dataset support for county-level predictions.

🀝 Contributions

Feel free to contribute! Fork the repository, make your changes, and submit a pull request.


πŸ“œ License

This project is licensed under the MIT License.


✨ Let’s Predict the Future!