<jupyter_start><jupyter_text>Exercise 10 KNN exercise with NBA player data  Introduction- NBA player statistics from 2014-2015 (partial season): [data](https://github.com/justmarkham/DAT4-students/blob/master/kerry/Final/NBA_players_2015.csv), [data dictionary](https://github.com/justmarkham/DAT-project-examples/blob/master/pdf/nba_paper.pdf)- **Goal:** Predict player position using assists, steals, blocks, turnovers, and personal fouls  Read the data into Pandas<jupyter_code># read the data into a DataFrame
import pandas as pd
url = 'https://raw.githubusercontent.com/justmarkham/DAT4-students/master/kerry/Final/NBA_players_2015.csv'
nba = pd.read_csv(url, index_col=0)
# examine the columns
nba.columns
# examine the positions
nba.pos.value_counts()<jupyter_output><empty_output><jupyter_text>Create X and yUse the following features: assists, steals, blocks, turnovers, personal fouls<jupyter_code># map positions to numbers
nba['pos_num'] = nba.pos.map({'C':0, 'F':1, 'G':2})
# create feature matrix (X)
feature_cols = ['ast', 'stl', 'blk', 'tov', 'pf']
X = nba[feature_cols]
# alternative way to create X
X = nba.loc[:, 'ast':'pf']
# create response vector (y)
y = nba.pos_num<jupyter_output><empty_output><jupyter_text>Exercice 10.1* Split the data in train and test* Train a KNN model (K=5)* Evaluate the accuracy<jupyter_code>from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X.values, y.values, random_state=123)
from sklearn.neighbors import KNeighborsClassifier
# instantiate with K=5
knn = KNeighborsClassifier(n_neighbors=5)
# fit with data
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_pred)<jupyter_output><empty_output><jupyter_text>Exercice 10.2 Predict player position and calculate predicted probability of each positionPredict for a player with these statistics: 1 assist, 1 steal, 0 blocks, 1 turnover, 2 personal fouls<jupyter_code># create a list to represent a player
import numpy as np
player = np.array([1, 1, 0, 1, 2]).reshape(1, -1) 
# make a prediction
knn.predict(player)
# calculate predicted probabilities
knn.predict_proba(player)<jupyter_output><empty_output><jupyter_text>Exercice 10.3  Repeat steps 10.1 and 10.2 using K=50<jupyter_code># repeat for K=50
knn = KNeighborsClassifier(n_neighbors=50)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
confusion_matrix(y_test, y_pred)
knn.predict(player)
# calculate predicted probabilities
knn.predict_proba(player)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1113698098564222, 'fraction_numerical': 0.024447031431897557, 'mean_word_length': 6.9079754601226995, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 3, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '263838', 'license': 'MIT', 'n_tokens_mistral': 957, 'n_tokens_neox': 897, 'n_words': 231, 'path': 'exercises/10-nba_solution.ipynb', 'repo_name': 'S7evenrg/PracticalMachineLearningClass', 'size': '2577'}