-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNBA_2Points.py
More file actions
38 lines (29 loc) · 1.25 KB
/
NBA_2Points.py
File metadata and controls
38 lines (29 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
import plotly.graph_objects as go
import plotly.express as px
"""get the dataset"""
df = pd.read_excel("datasets/df_players_merged.xlsx")
print(df)
attempted = df['FGA']
scored = df['FGM']
"""make the linear regression"""
X = attempted.values.reshape(-1,1)
y = scored
regression = LinearRegression()
regression.fit(X,y)
x_range = np.linspace(X.min(), X.max(), 100)
y_range = regression.predict(x_range.reshape(-1, 1))
"""identifying best shooters"""
df['best_worst'] = (scored-attempted*regression.coef_)
df=df.sort_values(by=['best_worst'])
df['best_worst'][:5]=-100 #set the five worst to -1 -> in blue on the graphic
df['best_worst'][-5:]=100 #set the five best to 1 -> in yellow on the graphic
df['best_worst'][5:-5]=0 #set the other to 0
df = df.rename(columns={'FGA': 'Field Goals Attempted Per Match', 'FGM': 'Field Goals Scored Per Match'})
"""plot the graph"""
fig = px.scatter(df,x='Field Goals Attempted Per Match', y='Field Goals Scored Per Match', hover_name='PLAYER',title="2 Points",template="plotly_white",color='best_worst')
fig.add_traces(go.Scatter(x=x_range, y=y_range, name=' '))
fig.show()