Last active
June 27, 2023 18:34
-
-
Save laurenashley/bc40fa45919c60a352cfa9daecb1a343 to your computer and use it in GitHub Desktop.
Create a linear regression model that predicts the outcome for a tennis player based on their playing habits. By analyzing and modeling the Association of Tennis Professionals (ATP) data, you will determine what it takes to be one of the best tennis players in the world.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import codecademylib3_seaborn | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
from sklearn.model_selection import train_test_split | |
from sklearn.linear_model import LinearRegression | |
# load and investigate the data here: | |
players = pd.read_csv('tennis_stats.csv') | |
print(players.head(5)) | |
# perform exploratory analysis here: | |
plt.scatter(players.BreakPointsOpportunities, players.Wins) | |
plt.title('Break Points Opportunities and Wins') | |
plt.show() | |
plt.clf() | |
plt.scatter(players.BreakPointsOpportunities, players.Losses) | |
plt.title('Break Points Opportunities and Losses') | |
plt.show() | |
plt.clf() | |
plt.scatter(players.Aces, players.Wins) | |
plt.title('Aces and Wins') | |
plt.show() | |
plt.clf() | |
# helpers | |
def write_model(features, outcome, title): | |
features = players[features] | |
outcome = players[outcome] | |
features_train, features_test, outcome_train, outcome_test = train_test_split(features, outcome, train_size=0.8) | |
model = LinearRegression() | |
model.fit(features_train, outcome_train) | |
print(model.score(features_test, outcome_test)) | |
prediction = model.predict(features_test) | |
plt.scatter(outcome_test, prediction, alpha=0.4) | |
plt.title(title) | |
plt.xlabel('Actual') | |
plt.ylabel('Predicted') | |
plt.legend() | |
plt.show() | |
plt.clf() | |
## perform single feature linear regressions here: | |
write_model(['BreakPointsOpportunities'], 'Winnings', 'Break Points Opportunities to Winnings') | |
#Aces | |
write_model(['Aces'], 'Winnings', 'Aces to Winnings') | |
## perform two feature linear regressions here: | |
#Aces | |
write_model(['BreakPointsOpportunities', 'Aces'], 'Winnings', 'Break Points Opportunities and Aces to Winnings') | |
# BP Ops & Ranking | |
write_model(['BreakPointsOpportunities', 'FirstServeReturnPointsWon'], 'Ranking', 'Break Points Opportunities and 1st Serve Return Points to Winnings') | |
# FirstServeReturnPointsWon | |
write_model(['BreakPointsOpportunities', 'FirstServeReturnPointsWon'], 'Winnings', 'Break Points Opportunities and 1st Serve Return Points to Winnings') | |
## perform multiple feature linear regressions here: | |
write_model(['FirstServe','FirstServePointsWon','FirstServeReturnPointsWon', | |
'SecondServePointsWon','SecondServeReturnPointsWon','Aces', | |
'BreakPointsConverted','BreakPointsFaced','BreakPointsOpportunities', | |
'BreakPointsSaved','DoubleFaults','ReturnGamesPlayed','ReturnGamesWon', | |
'ReturnPointsWon','ServiceGamesPlayed','ServiceGamesWon','TotalPointsWon', | |
'TotalServicePointsWon'], ['Winnings'], 'All Features') | |
write_model(['FirstServe','FirstServePointsWon','FirstServeReturnPointsWon', | |
'SecondServePointsWon','SecondServeReturnPointsWon','Aces', | |
'BreakPointsConverted','BreakPointsFaced','BreakPointsOpportunities', | |
'BreakPointsSaved','DoubleFaults','ReturnGamesPlayed','ReturnGamesWon', | |
'ReturnPointsWon','ServiceGamesPlayed','ServiceGamesWon','TotalPointsWon', | |
'TotalServicePointsWon'], ['Ranking'], 'All Features') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment