Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/lessons/lesson_05/code/solution-code/solution-code-6.ipynb
1904 views
Kernel: Python 3

Lesson 6 - Solution Code

%matplotlib inline import numpy as np import pandas as pd from matplotlib import pyplot as plt import seaborn as sns sns.set_style("darkgrid") import sklearn.linear_model # read in the mammal dataset wd = '../../assets/dataset/msleep/' mammals = pd.read_csv(wd+'msleep.csv') mammals = mammals[mammals.brainwt.notnull()].copy()

Explore our mammals dataset

mammals.head()

Lets check out a scatter plot of body wieght and brain weight

# create a matplotlib figure plt.figure() # generate a scatterplot inside the figure plt.plot(mammals.bodywt, mammals.brainwt, '.') # show the plot plt.show()
Image in a Jupyter notebook
sns.lmplot('bodywt', 'brainwt', mammals)
<seaborn.axisgrid.FacetGrid at 0xb49fb38>
Image in a Jupyter notebook
log_columns = ['bodywt', 'brainwt',] log_mammals = mammals.copy() log_mammals[log_columns] = log_mammals[log_columns].apply(np.log10)
sns.lmplot('bodywt', 'brainwt', log_mammals)
<seaborn.axisgrid.FacetGrid at 0x3743a90>
Image in a Jupyter notebook

Guided Practice: Using Seaborn to generate single variable linear model plots (15 mins)

Update and complete the code below to use lmplot and display correlations between body weight and two dependent variables: sleep_rem and awake.

log_columns = ['bodywt', 'brainwt',] # any others? log_mammals = mammals.copy() log_mammals[log_columns] = log_mammals[log_columns].apply(np.log10)
Complete below for sleep_rem and awake as a y, with variables you've already used as x.
sns.lmplot(x, y, mammals) sns.lmplot(x, y, log_mammals)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-8-2e69c76a87ed> in <module>() ----> 1 sns.lmplot(x, y, mammals) 2 sns.lmplot(x, y, log_mammals) NameError: name 'x' is not defined

Solution:

log_columns = ['bodywt', 'brainwt', 'awake', 'sleep_rem'] # any others? log_mammals = mammals.copy() log_mammals[log_columns] = log_mammals[log_columns].apply(np.log10) # one other example, using brainwt and awake. x = 'brainwt' y = 'awake' sns.lmplot(x, y, mammals) sns.lmplot(x, y, log_mammals)

Introduction: Single Regression Analysis in statsmodels & scikit (10 mins)

# this is the standard import if you're using "formula notation" (similar to R) import statsmodels.formula.api as smf X = mammals[['bodywt']] y = mammals['brainwt'] # create a fitted model in one line #formula notiation is the equivalent to writting out our models such that 'outcome = predictor' #with the follwing syntax formula = 'outcome ~ predictor1 + predictor2 ... predictorN' lm = smf.ols(formula='y ~ X', data=mammals).fit() #print the full summary lm.summary()

use Statsmodels to make the prediction

# you have to create a DataFrame since the Statsmodels formula interface expects it X_new = pd.DataFrame({'X': [50]}) X_new.head()
lm.predict(X_new)

Repeat in Scikit with handy plotting

When modeling with sklearn, you'll use the following base principals.

  • All sklearn estimators (modeling classes) are based on this base estimator. This allows you to easily rotate through estimators without changing much code.

  • All estimators take a matrix, X, either sparse or dense.

  • Many estimators also take a vector, y, when working on a supervised machine learning problem. Regressions are supervised learning because we already have examples of y given X.

  • All estimators have parameters that can be set. This allows for customization and higher level of detail to the learning process. The parameters are appropriate to each estimator algorithm.

from sklearn import feature_selection, linear_model def get_linear_model_metrics(X, y, algo): # get the pvalue of X given y. Ignore f-stat for now. pvals = feature_selection.f_regression(X, y)[1] # start with an empty linear regression object # .fit() runs the linear regression function on X and y algo.fit(X,y) residuals = (y-algo.predict(X)).values # print the necessary values print('P Values:', pvals) print ('Coefficients:', algo.coef_) print ('y-intercept:', algo.intercept_) print ('R-Squared:', algo.score(X,y)) plt.figure() plt.hist(residuals, bins=np.ceil(np.sqrt(len(y)))) # keep the model return algo X = mammals[['bodywt']] y = mammals['brainwt'] lm = linear_model.LinearRegression() lm = get_linear_model_metrics(X, y, lm)

Demo: Significance is Key (20 mins)

What does our output tell us?

Our output tells us that:

  • The relationship between bodywt and brainwt isn't random (p value approaching 0)

  • The model explains, roughly, 87% of the variance of the dataset (the largest errors being in the large brain and body sizes)

  • With this current model, brainwt is roughly bodywt * 0.00096395

  • The residuals, or error in the prediction, is not normal, with outliers on the right. A better with will have similar to normally distributed error.

Evaluating Fit, Evaluating Sense

Although we know there is a better solution to the model, we should evaluate some other sense things first. For example, given this model, what is an animal's brainwt if their bodywt is 0?

# prediction at 0? print(lm.predict([[0]]))
lm = linear_model.LinearRegression(fit_intercept=False) lm = get_linear_model_metrics(X, y, lm) # prediction at 0? print(lm.predict([[0]]))

Intrepretation

With linear modeling we call this part of the linear assumption. Consider it a test to the model. If an animal's body weights nothing, we expect their brain to be nonexistent. That given, we can improve the model by telling sklearn's LinearRegression object we do not want to fit a y intercept.

Now, the model fits where brainwt = 0, bodywt = 0. Because we start at 0, the large outliers have a greater effect, so the coefficient has increased. Fitting the this linear assumption also explains slightly less of the variance.

Guided Practice: Using the LinearRegression object (15 mins)

We learned earlier that the the data in its current state does not allow for the best linear regression fit.

With a partner, generate two more models using the log-transformed data to see how this transform changes the model's performance.

Complete the following code to update X and y to match the log-transformed data. Complete the loop by setting the list to be one True and one False.

#starter X = y = loop = [] for boolean in loop: print 'y-intercept:', boolean lm = linear_model.LinearRegression(fit_intercept=boolean) get_linear_model_metrics(X, y, lm) print
#solution X = log_mammals[['bodywt']] y = log_mammals['brainwt'] loop = [True, False] for boolean in loop: print 'y-intercept:', boolean lm = linear_model.LinearRegression(fit_intercept=boolean) get_linear_model_metrics(X, y, lm) print

Check: Which model performed the best? The worst? Why?

Advanced Methods!

We will go over different estimators in detail in the future but check it out in the docs if you're curious...

# loading other sklearn regression estimators X = log_mammals[['bodywt']] y = log_mammals['brainwt'] estimators = [ linear_model.Lasso(), linear_model.Ridge(), linear_model.ElasticNet(), ] for est in estimators: print est get_linear_model_metrics(X, y, est) print

Introduction: Multiple Regression Analysis using citi bike data (10 minutes)

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.

wd = '../../assets/dataset/bikeshare/' bike_data = pd.read_csv(wd+'bikeshare.csv') bike_data.head()

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.

cmap = sns.diverging_palette(220, 10, as_cmap=True) correlations = bike_data[['temp', 'atemp', 'casual']].corr() print correlations print sns.heatmap(correlations, cmap=cmap)

The correlation matrix explains that:

  • both temperature fields are moderately correlated to guest ridership;

  • the two temperature fields are highly correlated to each other.

Including both of these fields in a model could introduce a pain point of multicollinearity, where it's more difficult for a model to determine which feature is effecting the predicted value.

We can measure this effect in the coefficients:

y = bike_data['casual'] x_sets = ( ['temp'], ['atemp'], ['temp', 'atemp'], ) for x in x_sets: print ', '.join(x) get_linear_model_metrics(bike_data[x], y, linear_model.LinearRegression()) print

Intrepretation:

Even though the 2-variable model temp + atemp has a higher explanation of variance than two variables on their own, and both variables are considered significant (p values approaching 0), we can see that together, their coefficients are wildly different.

This can introduce error in how we explain models.

What happens if we use a second variable that isn't highly correlated with temperature, like humidity?

y = bike_data['casual'] x = bike_data[['temp', 'hum']] get_linear_model_metrics(x, y, linear_model.LinearRegression())

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?

lm = linear_model.LinearRegression() weather = pd.get_dummies(bike_data.weathersit) get_linear_model_metrics(weather[[1, 2, 3, 4]], y, lm) print # drop the least significant, weather situation = 4 get_linear_model_metrics(weather[[1, 2, 3]], y, lm)

Similar in Statsmodels

# all dummies in the model lm_stats = smf.ols(formula='y ~ weather[[1, 2, 3, 4]]', data=bike_data).fit() lm_stats.summary()
#droping one lm_stats = smf.ols(formula='y ~ weather[[1, 2, 3]]', data=bike_data).fit() lm_stats.summary()

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.

Guided Practice: Combining non-correlated features into a better model (15 mins)

bike_data.dtypes

With a partner, complete this code together and visualize the correlations of all the numerical features built into the data set.

We want to:

  • Add the three significant weather situations into our current model

  • Find two more features that are not correlated with current features, but could be strong indicators for predicting guest riders.

#starter lm = linear_model.LinearRegression() bikemodel_data = bike_data.join() # add in the three weather situations cmap = sns.diverging_palette(220, 10, as_cmap=True) correlations = # what are we getting the correlations of? print correlations print sns.heatmap(correlations, cmap=cmap) columns_to_keep = [] #[which_variables?] final_feature_set = bikemodel_data[columns_to_keep] get_linear_model_metrics(final_feature_set, y, lm)
#solution lm = linear_model.LinearRegression() weather = pd.get_dummies(bike_data.weathersit) weather.columns = ['weather_' + str(i) for i in weather.columns] hours = pd.get_dummies(bike_data.hr) hours.columns = ['hour_' + str(i) for i in hours.columns] season = pd.get_dummies(bike_data.season) season.columns = ['season_' + str(i) for i in season.columns] bikemodel_data = bike_data.join(weather) # add in the three weather situations bikemodel_data = bikemodel_data.join(hours) bikemodel_data = bikemodel_data.join(season) cmap = sns.diverging_palette(220, 10, as_cmap=True) columns_to_keep = ['temp', 'hum', 'windspeed', 'weather_1', 'weather_2', 'weather_3', 'holiday',] columns_to_keep.extend(['hour_' + str(i) for i in range(1, 24)]) correlations = bikemodel_data[columns_to_keep].corr() print correlations print sns.heatmap(correlations, cmap=cmap)

Independent Practice: Building models for other y variables (25 minutes)

We've completely a model together that explains casual guest riders. Now it's your turn to build another model, using a different y variable: registered riders.

Pay attention to:

  • the distribution of riders (should we rescale the data?)

  • checking correlations with variables and registered riders

  • having a feature space (our matrix) with low multicollinearity

  • model complexity vs explanation of variance: at what point do features in a model stop improving r-squared?

  • the linear assumption -- given all feature values being 0, should we have no ridership? negative ridership? positive ridership?

Bonus

  • Which variables would make sense to dummy (because they are categorical, not continuous)?

  • What features might explain ridership but aren't included in the data set?

  • Is there a way to build these using pandas and the features available?

  • Outcomes: If your model at least improves upon the original model and the explanatory effects (coefficients) make sense, consider this a complete task.

If your model has an r-squared above .4, this a relatively effective model for the data available. Kudos!

bikemodel_data.columns
y = bike_data['registered'] log_y = np.log10(y+1) lm = smf.ols(formula=' log_y ~ temp + hum + windspeed + weather_1 + weather_2 + weather_3 + holiday + hour_1 + hour_2 + hour_3 + hour_4 + hour_5 + hour_6 + hour_7 + hour_8 + hour_9 + hour_10 + hour_11 + hour_12 + hour_13 + hour_14 + hour_15 + hour_16 + hour_18 + hour_19 + hour_20 + hour_21 + hour_22 + hour_23', data=bikemodel_data).fit() #print the full summary lm.summary()