I am trying to predict an NBA player's position using Tensorflow. The dataset that I have doesn't classify each player under one specific position, but rather any position that they played in their career. For that reason I currently have this dictionary:
posdict = {'G-F' : 1, 'SG' : 2, 'SF' : 3, 'F' : 4, 'G' : 5, 'F-C' : 6, 'PG' : 7, 'F-G' : 8, 'C' : 9, 'PF' : 10, 'C-F' : 11, 'PF-C' : 12, 'SF-SG' : 13, 'C-PF' : 14, 'SG-SF' : 15, 'PF-SF' : 16, 'SF-PF' : 17, 'SG-PG' : 18, 'SF-PG' : 19, 'C-SF' : 20, 'PG-SG' : 21, 'PG-SF' : 22, 'SG-PF' : 23}
However, I want to set a number for each of the five positions and if a player played more than one position, then I want them to fit under multiple categories. For example if I set the positions with these numbers: {PG: 1, SG: 2, SF: 3, PF: 4, C: 5}, I want the above dictionary to be like this:
posdict = {'G-F' : [1,2,3,4], 'SG' : [2], 'SF' : [3], 'F' : [3,4], 'G' : [1,2], 'F-C' : [3,4,5], 'PG' : [1], 'F-G' : [1,2,3,4], 'C' : [5], 'PF' : [4], 'C-F' : [3,4,5], 'PF-C' : [4,5], 'SF-SG' : [2,3], 'C-PF' : [4,5], 'SG-SF' : [2,3], 'PF-SF' : [3,4], 'SF-PF' : [3,4], 'SG-PG' : [1,2], 'SF-PG' : [1,3], 'C-SF' : [3,5], 'PG-SG' : [1,2], 'PG-SF' : [1,3], 'SG-PF' : [2,4]}
When I get a prediction that is under any of the positions that a player was, then I want it to be considered as a correct prediciton. I tried using the second dictionary but I got a TypeError.
I am new to Tensorflow and not sure how to achieve what I want. Below is the code that I am using resulting in the error. (It works properly if I use the first dictionary, but I am not getting the results I want).
dftrain[('Pos')] = [posdict[item] for item in dftrain['Pos']]
dfeval[('Pos')] = [posdict[item] for item in dfeval['Pos']]
y_train = dftrain.pop('Pos')
y_eval = dfeval.pop('Pos')
NUMERICAL_COLUMS = ['G', 'PER', 'TS%', 'FTr', 'USG%', 'FG', 'FGA', 'FG%', '3P', '3PA', 'AST%', 'STL%', 'BLK%', 'TOV%', '2P', '2PA', 'OBPM', 'DBPM', 'BPM', '2P%', 'eFG%', 'FT', 'FTA', 'FT%', 'PTS', 'ORB%', 'DRB%', 'TRB%', 'OWS', 'DWS', 'WS']
feature_colums= []
for feature_name in NUMERICAL_COLUMS:
feature_colums.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float64))
def make_input_fn(data_df, label_df, num_epochs=10, shuffle=True, batch_size=32):
def input_function(): # inner function, this will be returned
ds = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df)) # create tf.data.Dataset object with data and its label
if shuffle:
ds = ds.shuffle(1000) # randomize order of data
ds = ds.batch(batch_size).repeat(num_epochs) # split dataset into batches of 32 and repeat process for number of epochs
return ds # return a batch of the dataset
return input_function # return a function object for use
train_input_fn = make_input_fn(dftrain, y_train) # here we will call the input_function that was returned to us to get a dataset object we can feed to the model
eval_input_fn = make_input_fn(dfeval, y_eval, num_epochs=1, shuffle=False)
linear_est = tf.estimator.LinearClassifier(feature_columns=feature_colums, n_classes=24)
linear_est.train(train_input_fn)
result = linear_est.evaluate(eval_input_fn)
print(result['accuracy'])