Chad's Portfolio

Welcome to my portfolio showcasing my data analytics and cloud engineering projects.

View My GitHub Profile

๐Ÿ“Š MLB Wins Predictor

โšพ Overview

The MLB Wins Predictor is a machine learning project that forecasts the number of wins a Major League Baseball (MLB) team will achieve in a season based on key performance metrics. This project utilizes a Keras neural network model trained on historical team data, with a FastAPI backend serving real-time predictions via a RESTful API.


๐Ÿš€ Features


๐Ÿ’ฝ Project Structure

mlb-wins-predictor/
โ”‚
โ”œโ”€โ”€ app.py                # FastAPI application for serving predictions
โ”œโ”€โ”€ mlb_wins_model.h5     # Trained Keras model (HDF5 format)
โ”œโ”€โ”€ scaler.pkl            # StandardScaler for data normalization
โ”œโ”€โ”€ pitching_batting_combined_df.csv  # Combined dataset for model training
โ”œโ”€โ”€ README.md             # Project documentation
โ”œโ”€โ”€ requirements.txt      # Python dependencies
โ””โ”€โ”€ tuner_results/        # Keras Tuner results for hyperparameter tuning

๐Ÿ“ˆ Model Training Workflow

  1. Data Preparation:
    • The dataset pitching_batting_combined_df.csv contains various performance metrics (batting and pitching).
    • Features are selected based on their importance to predict wins (W).
  2. Feature Selection:
    • Used a Random Forest model to identify the top 10 features contributing to win predictions:
      • WAR, RS, SO, SV, ER, R, SH, GB, ERA, BB
  3. Model Training:
    • Neural network with two dense layers, dropout for regularization, and ReLU activations.
    • Hyperparameter tuning with Keras Tuner to optimize model architecture.
  4. Model Evaluation:
    • Evaluated on a test set using Mean Absolute Error (MAE) and Mean Squared Error (MSE).

๐Ÿ› ๏ธ Tech Stack


โš™๏ธ Setup Instructions

1. Clone the Repository

git clone https://github.com/ChadB12/mlbwinspredictor.git
cd mlbwinspredictor

2. Create a Virtual Environment

python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

3. Install Dependencies

pip install -r requirements.txt

4. Ensure Model and Scaler Files Are Present

Place the following files in the root directory:

5. Run the FastAPI App

uvicorn app:app --host 0.0.0.0 --port 8000

The API should now be running at http://0.0.0.0:8000.


๐Ÿ”ฎ Usage

Endpoint: /predict

Using curl:

curl -X 'POST' \
  'http://your_server_ip:8000/predict' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "data": [[45, 850, 1300, 45, 520, 650, 30, 1400, 3.50, 450]]
  }'

๐Ÿ“Š Example Data

Typical input values for the model based on an above average MLB teamโ€™s stats:

Feature Description Example Value
WAR Wins Above Replacement 45
RS Runs Scored 850
SO Strikeouts by pitchers 1300
SV Saves 45
ER Earned Runs 520
R Runs allowed by pitchers 650
SH Sacrifice Hits 30
GB Ground Balls 1400
ERA Earned Run Average 3.50
BB Walks allowed by pitchers 450

๐Ÿ‘Œ Testing the API

  1. Go to Docs: Open the Swagger UI to test the API via a user-friendly interface:

    http://18.219.97.159:8000/docs
    
  2. Interactive API Documentation: Use the Swagger interface to send POST requests and view responses.

Dataset Used

The dataset used for training the model includes a combination of pitching and batting statistics. The dataset can be found in the following path within the repository:

Dataset

๐Ÿ‘จโ€๐Ÿ’ป Author


๐ŸŒŸ Acknowledgments