<jupyter_start><jupyter_text>Since I was only interested in QB, RB, WR, TE, K, I created new DataFrames using only these positions. This step was not totally necessary but it did simplify my data nicely.<jupyter_code># Create new DataFrames using only QB, WR, RB, TE, K
nfl16_QB = nfl16.loc[nfl16.pos == 'QB']
nfl16_WR = nfl16.loc[nfl16.pos == 'WR']
nfl16_RB = nfl16.loc[nfl16.pos == 'RB']
nfl16_TE = nfl16.loc[nfl16.pos == 'TE']
nfl16_K = nfl16.loc[nfl16.pos == 'K']
# Concat the new DataFrames back together as "nfl16_off"
frames = [nfl16_QB, nfl16_WR, nfl16_RB, nfl16_TE, nfl16_K]
nfl16_off = pd.concat(frames)
nfl16_off.shape
# Perform a Sanity Check to make sure the expected players are still there
nfl16_off.head()
# Determine column headers
nfl16_off.columns.values.tolist()
nfl16.head()
# Backup Modeling - A player's position can be predicted from their stats.
nfl16_off.head()
nfl16_off.pos.unique()
# map positions to numbers and store in new column
nfl16_off['pos_num'] = nfl16_off.pos.map({'QB':0, 'WR':1, 'RB':2, 'TE':3, 'K':4})
nfl16_off.head()
# import KNN class we need from scikit-learn
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
# fill NaN's with 0
nfl16_zero = nfl16_off.copy()

for column in nfl16_zero:
    nfl16_zero[column].fillna(0, inplace = True)

nfl16_zero.head()
# Create Feature Matrix and Response Vector
# Only considering features that can be mapped to fantasy points
feature_cols_pos = ['fumbles_lost', 'passing_yds', 'passing_tds', 'passing_ints', 'rushing_yds', 'rushing_tds', 'receiving_yds',
                   'receiving_tds', 'passing_twoptm', 'receiving_twoptm','rushing_twoptm','kicking_xpmade','kicking_fgm']
X_pos = nfl16_zero[feature_cols_pos]
print X_pos.shape

y_pos = nfl16_zero['pos_num']
print y_pos.shape

X_pos_tr, X_pos_te, y_pos_tr, y_pos_te = train_test_split(X_pos, y_pos, random_state=1)
# Training the model using k = 1
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_pos_tr, y_pos_tr)
# STEP 3: test the model on the testing set, and check the accuracy
from sklearn import metrics
y_pos_pred = knn.predict(X_pos_te)
print metrics.accuracy_score(y_pos_te, y_pos_pred) #0.6923
# compute null accuracy
y_pos_te.value_counts()/len(y_pos_te)
# well nothing shows up more than about 1/3 of the time so we're more accurate than null
# Calculating best k and also testing/training error rates
k_range = range(1, 101)
training_error_rate = []
testing_error_rate = []

for k in k_range:

    # instantiate the model with the current K value
    knn = KNeighborsClassifier(n_neighbors=k)

    # calculate training error
    knn.fit(X_pos, y_pos)
    y_pos_pred_train = knn.predict(X_pos)
    training_accuracy = metrics.accuracy_score(y_pos, y_pos_pred_train)
    training_error_rate.append(1 - training_accuracy)
    
    # calculate testing error
    knn.fit(X_pos_tr, y_pos_tr)
    y_pos_pred_test = knn.predict(X_pos_te)
    testing_accuracy = metrics.accuracy_score(y_pos_te, y_pos_pred_test)
    testing_error_rate.append(1 - testing_accuracy)
column_dict = {'K': k_range, 'training error rate':training_error_rate, 'testing error rate':testing_error_rate}
df = pd.DataFrame(column_dict).set_index('K').sort_index(ascending=True)
df.sort_values(by='testing error rate').head()
# Looks like k = 5 is best
plt.style.use('fivethirtyeight');
df.plot(y='testing error rate')
plt.xlabel('Value of K for KNN')
plt.ylabel('Error rate (lower is better)')
# Retrain model using k = 5
knn5 = KNeighborsClassifier(n_neighbors=5)
knn5.fit(X_pos_tr, y_pos_tr)
y_pos_pred5 = knn.predict(X_pos_te)
print metrics.accuracy_score(y_pos_te, y_pos_pred5)
# just me or does this seem less accurate?
# Trying to predict position of with fake values
# create a list to represent a player

import random
player = random.sample(range(1, 100), 13)
player
# Using the model
print knn5.predict([player])
print knn5.predict_proba([player])
# Random player is most likely 2/RB with those stats
# nfl16_off.pos.map({'QB':0, 'WR':1, 'RB':2, 'TE':3, 'K':4})<jupyter_output>[2]
[[ 0.2  0.2  0.6  0.   0. ]]
{'fraction_non_alphanumeric': 0.11135693215339233, 'fraction_numerical': 0.032448377581120944, 'mean_word_length': 5.407874015748032, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '179651', 'license': 'MIT', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1541, 'n_words': 400, 'path': 'KNN-player-position.ipynb', 'repo_name': 'wendybo/NFLrank', 'size': '4068'}