<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Data Analysis of NBA Players
This iPython notebook is meant to challenge Georgetown Data Science Certificate students to ensure they have understood or acknowledged the technologies and techniques taught througout the semester. We’ll be analyzing a dataset of NBA players and their performance in the 2013-2014 season. You can download the file here.
Read in a CSV File
In the first step, load the dataset into a dataframe.
Step1:  Remember to import the pandas library to get access to Dataframes. Dataframes are two-dimensional arrays (matrices) where each column can be of a different datatype.
Find the number of Players
How many players are in the dataset?
Step2:  Look at the First Row of Data
What does the first row look like?
Step3:  Find the Average of Each Statistic
Find the average value for each statistic. The columns have names like PER (player efficiency rating) and GP (Games Played) that represent the season statistics for each player. For more on the various statistics look here.
Step4:  Make Pairwise Scatterplots
One common way to explore a dataset is to see how different columns correlate to others. We’ll compare the ast, mpg, and to columns. Create a scatter matrix plot of the dataset.
Step5:  In Python, matplotlib is the primary plotting package, and seaborn is a widely used layer over matplotlib. You could have also used the pandas scatter_matrix for a similar result. 
Make Clusters of the Players
One good way to explore this kind of data is to generate cluster plots. These will show which players are most similar. Use Scikit-Learn to cluster the data.
Step6:  We can use the main Python machine learning package, scikit-learn, to fit a k-means clustering model and get our cluster labels. In order to cluster properly, we remove any non-numeric columns, or columns with missing values (NA, Nan, etc) with the get_numeric_data and dropna methods.
Plot Players by Cluster
We can now plot out the players by cluster to discover patterns. One way to do this is to first use PCA to make our data 2-dimensional, then plot it, and shade each point according to cluster association.
Step7:  With Python, we used the PCA class in the scikit-learn library. We used matplotlib to create the plot.
Split into Training and Testing Sets
If we want to do supervised machine learning, it’s a good idea to split the data into training and testing sets so we don’t overfit.
Step8:  In Python, the recent version of pandas came with a sample method that returns a certain proportion of rows randomly sampled from a source dataframe – this makes the code much more concise. We could also use Scikit-Learns KFolds and train_test_splits for different types of shuffle and splits in the data set. In both cases, we set a random seed to make the results reproducible.
Univariate Linear Regression
Let’s say we want to predict number of assists per player from the turnovers made per player.
Step9:  Scikit-learn has a linear regression model that we can fit and generate predictions from. Note also the use of Lasso and Ridge regressions, though this doesn't apply in the univariate case. 
Calculate Summary Statistics for the Model
Evaluate the above model using your test set, how well does it perform?
Step10:  If we want to get summary statistics about the fit, like r-squared value, we can use the score method of the Sckit-Learn model. However, if we want more advanced regression statistics we’ll need to do a bit more. The statsmodels package enables many statistical methods to be used in Python and is a good tool to know.
Fit a Random Forest Model
Our linear regression worked well in the single variable case, but we suspect there may be nonlinearities in the data. Thus, we want to fit a random forest model.
Step11:  Calculate Error
Now that we’ve fit two models, let’s calculate error. We’ll use MSE.
Step12:  The scikit-learn library has a variety of error metrics that we can use
Download a Webpage
Now that we have data on NBA players from 2013-2014, let’s scrape some additional data to supplement it. We’ll just look at one box score from the NBA Finals here to save time.
Step13:  Extract Player Box Scores
Now that we have the web page, we’ll need to parse it to extract scores for players.
<ASSISTANT_TASK:>
Python Code:
# Load CSV into a dataframe named nba
Explanation: Data Analysis of NBA Players
This iPython notebook is meant to challenge Georgetown Data Science Certificate students to ensure they have understood or acknowledged the technologies and techniques taught througout the semester. We’ll be analyzing a dataset of NBA players and their performance in the 2013-2014 season. You can download the file here.
Read in a CSV File
In the first step, load the dataset into a dataframe.
End of explanation
# Print the number of rows and columns in the dataframe
Explanation: Remember to import the pandas library to get access to Dataframes. Dataframes are two-dimensional arrays (matrices) where each column can be of a different datatype.
Find the number of Players
How many players are in the dataset?
End of explanation
# Print the first row of data
Explanation: Look at the First Row of Data
What does the first row look like?
End of explanation
# Print the mean of each column
Explanation: Find the Average of Each Statistic
Find the average value for each statistic. The columns have names like PER (player efficiency rating) and GP (Games Played) that represent the season statistics for each player. For more on the various statistics look here.
End of explanation
%matplotlib inline 
# Use seaborn or pandas to plot the scatter matrix
Explanation: Make Pairwise Scatterplots
One common way to explore a dataset is to see how different columns correlate to others. We’ll compare the ast, mpg, and to columns. Create a scatter matrix plot of the dataset.
End of explanation
# Use a clustering model like K-means to cluster the players 
Explanation: In Python, matplotlib is the primary plotting package, and seaborn is a widely used layer over matplotlib. You could have also used the pandas scatter_matrix for a similar result. 
Make Clusters of the Players
One good way to explore this kind of data is to generate cluster plots. These will show which players are most similar. Use Scikit-Learn to cluster the data.
End of explanation
# Use PCA to plot the clusters in 2 dimensions
Explanation: We can use the main Python machine learning package, scikit-learn, to fit a k-means clustering model and get our cluster labels. In order to cluster properly, we remove any non-numeric columns, or columns with missing values (NA, Nan, etc) with the get_numeric_data and dropna methods.
Plot Players by Cluster
We can now plot out the players by cluster to discover patterns. One way to do this is to first use PCA to make our data 2-dimensional, then plot it, and shade each point according to cluster association.
End of explanation
# Create train (80%) and test (20%) splits
Explanation: With Python, we used the PCA class in the scikit-learn library. We used matplotlib to create the plot.
Split into Training and Testing Sets
If we want to do supervised machine learning, it’s a good idea to split the data into training and testing sets so we don’t overfit.
End of explanation
# Compute the univariate regression of TO to AST
Explanation: In Python, the recent version of pandas came with a sample method that returns a certain proportion of rows randomly sampled from a source dataframe – this makes the code much more concise. We could also use Scikit-Learns KFolds and train_test_splits for different types of shuffle and splits in the data set. In both cases, we set a random seed to make the results reproducible.
Univariate Linear Regression
Let’s say we want to predict number of assists per player from the turnovers made per player.
End of explanation
# Compute the regression results
Explanation: Scikit-learn has a linear regression model that we can fit and generate predictions from. Note also the use of Lasso and Ridge regressions, though this doesn't apply in the univariate case. 
Calculate Summary Statistics for the Model
Evaluate the above model using your test set, how well does it perform?
End of explanation
# Compute random forest from the predictors "AGE", "MPG", "TO", "HT", "WT", "REBR" to the target, "AST"
Explanation: If we want to get summary statistics about the fit, like r-squared value, we can use the score method of the Sckit-Learn model. However, if we want more advanced regression statistics we’ll need to do a bit more. The statsmodels package enables many statistical methods to be used in Python and is a good tool to know.
Fit a Random Forest Model
Our linear regression worked well in the single variable case, but we suspect there may be nonlinearities in the data. Thus, we want to fit a random forest model.
End of explanation
# Compute the MSE of the classifier
Explanation: Calculate Error
Now that we’ve fit two models, let’s calculate error. We’ll use MSE.
End of explanation
# Download "http://www.basketball-reference.com/boxscores/201506140GSW.html"
Explanation: The scikit-learn library has a variety of error metrics that we can use
Download a Webpage
Now that we have data on NBA players from 2013-2014, let’s scrape some additional data to supplement it. We’ll just look at one box score from the NBA Finals here to save time.
End of explanation
# Use BeautifulSoup to parse the table from the web page
Explanation: Extract Player Box Scores
Now that we have the web page, we’ll need to parse it to extract scores for players.
End of explanation
<END_TASK>