-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathanalysis.py
More file actions
149 lines (126 loc) · 3.54 KB
/
Copy pathanalysis.py
File metadata and controls
149 lines (126 loc) · 3.54 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import spacy
import pandas as pd
from datetime import date
from data.text_data import nba_words
from modules.scraper import get_player_stats
from sklearn.preprocessing import LabelEncoder
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
nlp = spacy.load("en_core_web_sm")
"""
Function to determine if query is NBA related
Parameters
----------
query : string
String representing user query
Returns
-------
score : int
The certainty with which the query is NBA related
1 ~ NBA query, 0 ~ unsure, -1 ~ Random query
"""
def isNBA(query):
temp_query = query.lower()
query_list = temp_query.split()
for word in query_list:
if word in nba_words:
return 1
doc = nlp(query)
for ent in doc.ents:
if ent.label_ == "ORG" or ent.label_ == "PERSON":
return 0
return -1
"""
Function to generate ranked list
of players based on fantasy score.
Parameters
----------
n/a
Returns
-------
player_scores : list
The ranked list of players with tuples containing
name and score.
"""
def fantasy_recommendations():
year = int(date.today().year)
players = get_player_stats(year)
player_scores = []
for player in players:
p_t = (player.name, player.get_fantasy_score())
player_scores.append(p_t)
player_scores.sort(key=lambda x:x[1], reverse=True)
return player_scores
"""
Function to generate ranked list
of players based on fantasy score.
Parameters
----------
n/a
Returns
-------
df : pandas.DataFrame
The dataframe of player stats
player_map : dict
The mapping of player name to dataframe index
"""
def create_player_dataframe():
year = int(date.today().year)
players = get_player_stats(year)
# Arrays for player categories
ns, pnts, rbs, asts, blks, fgp = [], [], [], [], [], []
player_map = {}
for i, p in enumerate(players):
player_map[i] = p.name
ns.append(p.name)
pnts.append(p.points)
rbs.append(p.total_reb)
asts.append(p.assists)
blks.append(p.blocks)
fgp.append(p.field_goal_percent)
# Creating a pandas dataframe based on player categories
df = pd.DataFrame({
'points': pnts,
'rebounds': rbs,
'assists': asts,
'blocks': blks,
'field goal percent': fgp
})
return df, player_map
"""
Function to cluster NBA players based
on attributes such as points, rebounds,
assists, blocks, and field goal percentage.
The idea behind clustering players is to
gain insights on potential trade options
and tier lists. The clustering algorithm
of choice is KMeans.
Parameters
----------
clusters : int
The final number of clusters desired
Returns
-------
player_clusters : list
A 2D matrix representing the clusters
that were formed after KMeans
"""
def build_stat_clusters(clusters):
data, p_map = create_player_dataframe()
km = KMeans(n_clusters=clusters).fit(data)
cluster_map = pd.DataFrame()
cluster_map['data_index'] = data.index.values
cluster_map['cluster'] = km.labels_
# Check Silhouette Score
silhouette_scr = silhouette_score(data, km.labels_, metric='euclidean')
print('Silhouette Score: %.3f' % silhouette_scr)
# cluster list
player_clusters = []
# Iterate over cluster map
for i in range(clusters):
c = cluster_map[cluster_map.cluster == i]
group = []
for id, cluster in c.iterrows():
group.append(p_map[cluster['data_index']])
player_clusters.append(group)
return player_clusters