top of page
  • Facebook
  • Twitter
  • Spotify
  • Apple Music

Win or Loss: Fine-Tuning Predictions with Logistic Regression on Day

Writer: Chinmay Bharathulwar
Chinmay Bharathulwar
Apr 14, 2024
4 min read

From Points to Predictions: Logistic Regression Takes the Court

On Day 1, we played with linear regression, predicting the points a player might score. For Day 2, we've upped our game with logistic regression — a model that's all about classification. Our goal today is not just to forecast numbers but outcomes: will the team win or lose based on one player’s stats?

Logistic Regression: The Math Behind the Method

Logistic regression is used when our dependent variable is categorical — in our case, a win (1) or a loss (0) for the player's team. It estimates the probability of an event occurring by fitting data to a logistic curve.

It works on the principle of odds, which can be expressed as the probability of winning divided by the probability of losing. But since probabilities are bounded between 0 and 1, logistic regression uses the log of odds to map predictions to a continuous scale:



The logistic function then converts these log-odds back into a probability score between 0 and 1, giving us the predicted probability of the team winning or losing.

The Code on the Court

First, we sought the data — fetching the player's game logs for a chosen season using the NBA API:

# Fetching player data using NBA API
df = fetch_player_data('Kyrie Irving', '2021')

With the data in hand, we engineered our target variable — win or loss — based on game outcomes:

# Creating a binary target variable for wins and losses
df['target'] = (df['WL'] == 'W').astype(int)

We picked our features — the player's stats that might sway a game's outcome:

# Selecting features
features = df[['REB', 'AST', 'MIN', 'PTS']]

Next up, training our logistic model using the player's past performances to learn patterns:

# Training the logistic regression model
model = LogisticRegression()
model.fit(X_train, y_train)

And finally, we measured the model's foresight with precision, recall, and the confusion matrix:

# Model's predictions
predictions = model.predict(X_test)

# Evaluating the model's performance
print(classification_report(y_test, predictions))

Reading the Model's Results

Our model gave us a classification report and an ROC AUC score, quantifying its predictive prowess. With an AUC of 0.69, the model is better than a coin toss but not clairvoyant.

The confusion matrix and ROC curve provided visual feedback on our model’s performance:

# Confusion Matrix and ROC AUC Score
print(confusion_matrix(y_test, predictions))
print("ROC AUC score:", roc_auc_score(y_test, probabilities))

Decoding the Model's Stats

User Input

You've run the model for James Harden, specifying the 2019 NBA season. There's a warning about the date format which suggests that pandas had to guess how to parse the dates in your dataset. This is a common warning and can usually be fixed by specifying the date format directly in the code if needed.

Classification Report

The classification report provides several key metrics:

  • Precision measures the accuracy of positive predictions. For the 'loss' class (0), the precision is 0.75, indicating that when the model predicts a loss, it's correct 75% of the time. For the 'win' class (1), the precision is 0.70.

  • Recall shows the model’s ability to find all positive instances. For the 'loss' class, the recall is 0.50, which means the model correctly identifies 50% of the actual losses. For the 'win' class, the recall is much higher at 0.88, indicating the model identifies 88% of the actual wins.

  • F1-Score is a harmonic mean of precision and recall and is a balance between the two. The f1-scores are 0.60 for losses and 0.78 for wins, suggesting the model is better at identifying wins.

  • Support is the number of actual occurrences of the class in the dataset, showing there were 6 losses and 8 wins.

Confusion Matrix

The confusion matrix provides a tabular representation of the actual vs. predicted classes:

  • There were 3 true negatives (correctly predicted losses) and 3 false negatives (wins that were incorrectly predicted as losses).

  • There was 1 false positive (a loss incorrectly predicted as a win) and 7 true positives (correctly predicted wins).

ROC AUC Score

The ROC AUC score is 0.67 (or about 2/3), which indicates that the model has a fair ability to distinguish between the classes. It's better than a random guess but not exceptionally high.

ROC Curve

The Receiver Operating Characteristic (ROC) curve is a plot that illustrates the diagnostic ability of a binary classifier system. The curve is created by plotting the true positive rate (TPR) against the false positive rate (FPR) at various threshold settings.

  • The area under the curve (AUC) is 0.67, which confirms the score we discussed earlier.

  • The curve shows some capability of the model to distinguish between the classes, but it also indicates there’s room for improvement.

In summary, while the model demonstrates a decent ability to predict wins (high recall for wins), it struggles with accurately identifying losses (low recall for losses). The moderate ROC AUC score suggests that while the model is learning from the data and making predictions better than random, there is potential for enhancement, perhaps by including more features or by tuning the model further.

The Gains and Fallbakcs

Advantages:

  • Logistic regression provides a clear probability score, offering more nuanced insights than a simple win/loss output.

  • Its robustness across different thresholds enables us to fine-tune our predictions based on our tolerance for false positives or negatives.

Challenges:

  • The model assumes a linear relationship between feature variables and the outcome's log odds, which may not capture the complex interplay of factors in sports.

  • The effectiveness of the model hinges on the breadth and quality of the data it's trained on.

What's Next?

As we dribble into Day 3, we'll be exploring ensemble methods that can capture the complexity of sports outcomes even better. Stay tuned as our prediction model goes for the three-pointer!


 
 
 

Comments


bottom of page