Path: blob/master/april_18/lessons/lesson-06-alt/code/starter-code/starter-code-6.ipynb
1905 views
Lesson 6 - Starter Code
Part 1:
Explore our mammals dataset
Check 1. Distribution
Lets check out a scatter plot of body wieght and brain weight
Log transformation can help here.
Curious about the math? http://onlinestatbook.com/2/transformations/log.html
Woohoo! This looks much better.
#Part 1- Student: Update and complete the code below to use lmplot and display correlations between body weight and two dependent variables: sleep_rem and awake.
Complete below for 2 new models:
With body weight as the x and y set as:
sleep_rem
awake
Create the lmplots
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-18-5329563f75c7> in <module>()
----> 1 g = sns.lmplot(X, Y, mammals)
2 g.set_axis_labels( "Body Weight", "REM")
3 g = sns.lmplot(X, Y, log_mammals)
4 g.set_axis_labels( "Log Body Weight", "Log REM ")
NameError: name 'X' is not defined
####play around with other outcomes
Decision for Check 1. Distributrion
Answer:
We decided above that we will need a log transformation. Let's take a look at both models to compare
Our output tells us that:
The relationship between bodywt and brainwt isn't random (p value approaching 0)
With this current model, log(brainwt) is roughly log(bodywt) * 0.0010
The model explains, roughly, 87% of the variance of the dataset
Student: repeat with the log transformation
File "<ipython-input-21-4798b1fa002f>", line 2
X =
^
SyntaxError: invalid syntax
What does our output tell us?
Our output tells us that:
Bonus: Use Statsmodels to make the prediction
Predict X_new
Part 2: Multiple Regression Analysis using citi bike data
In the previous example, one variable explained the variance of another; however, more often than not, we will need multiple variables.
For example, a house's price may be best measured by square feet, but a lot of other variables play a vital role: bedrooms, bathrooms, location, appliances, etc.
For a linear regression, we want these variables to be largely independent of each other, but all of them should help explain the y variable.
We'll work with bikeshare data to showcase what this means and to explain a concept called multicollinearity.
##Check 2. Multicollinearity What is Multicollinearity?
With the bike share data, let's compare three data points: actual temperature, "feel" temperature, and guest ridership.
Our data is already normalized between 0 and 1, so we'll start off with the correlations and modeling.
Students:
using the code from the demo create a correlation heat map comparing 'temp', 'atemp', 'casual'
####Question: What did we find?
The correlation matrix explains that:
###Demo: We can measure this effect in the coefficients:
Side note: this is a sneak peak at scikit learn
Intrepretation:
What happens if we use a second variable that isn't highly correlated with temperature, like humidity?
Guided Practice: Multicollinearity with dummy variables (15 mins)
There can be a similar effect from a feature set that is a singular matrix, which is when there is a clear relationship in the matrix (for example, the sum of all rows = 1).
Run through the following code on your own.
What happens to the coefficients when you include all weather situations instead of just including all except one?
Similar in Statsmodels
Students: Now drop one
Interpretation:
This model makes more sense, because we can more easily explain the variables compared to the one we left out.
For example, this suggests that a clear day (weathersit:1) on average brings in about 38 more riders hourly than a day with heavy snow.
In fact, since the weather situations "degrade" in quality (1 is the nicest day, 4 is the worst), the coefficients now reflect that well.
However at this point, there is still a lot of work to do, because weather on its own fails to explain ridership well.
Checkout our data again
#Part 3- Building a model to predict guest ridership With a partner, complete this code together and visualize the correlations of all the numerical features built into the data set.
We want to:
Id categorical variables
Create dummies (weather situation is done for you in the starter code)
Find at least two more features that are not correlated with current features, but could be strong indicators for predicting guest riders.
Independent Practice: Building model to predict guest ridership
Pay attention to:
Which variables would make sense to dummy (because they are categorical, not continuous)?
the distribution of riders (should we rescale the data?)
checking correlations with variables and guest riders
having a feature space (our matrix) with low multicollinearity
the linear assumption -- given all feature values being 0, should we have no ridership? negative ridership? positive ridership?
What features might explain ridership but aren't included in the data set?
###You're done when: If your model has an r-squared above .4, this a relatively effective model for the data available. Kudos! Move on to the bonus!
####1: What's the strongest predictor?
Answer:
####2: How well did your model do?
Answer:
####3: How can you improve it?
Answer:
###Bonus:
We've completed a model that explains casual guest riders. Now it's your turn to build another model, using a different y (outcome) variable: registered riders.
Bonus 1: What's the strongest predictor?
Bonus 2: How well did your model do?
Bonus 3: How can you improve it?