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

Class 7- solution

import numpy as np import pandas as pd from sklearn import linear_model, metrics

###Create sample data and fit a model

df = pd.DataFrame({'x': range(100), 'y': range(100)}) biased_df = df.copy() biased_df.loc[:20, 'x'] = 1 biased_df.loc[:20, 'y'] = 1 def append_jitter(series): jitter = np.random.random_sample(size=100) return series + jitter df['x'] = append_jitter(df.x) df['y'] = append_jitter(df.y) biased_df['x'] = append_jitter(biased_df.x) biased_df['y'] = append_jitter(biased_df.y)
## fit lm = linear_model.LinearRegression().fit(df[['x']], df['y']) print(metrics.mean_squared_error(df['y'], lm.predict(df[['x']])))
0.1944825532752072
## biased fit lm = linear_model.LinearRegression().fit(biased_df[['x']], biased_df['y']) print metrics.mean_squared_error(df['y'], lm.predict(df[['x']]))
File "<ipython-input-6-f61f2d8c60eb>", line 3 print metrics.mean_squared_error(df['y'], lm.predict(df[['x']])) ^ SyntaxError: invalid syntax

Cross validation

Intro to cross validation with bike share data from last time. We will be modeling casual ridership.

from sklearn import cross_validation wd = '../../assets/dataset/' bikeshare = pd.read_csv(wd + 'bikeshare.csv')

####Create dummy variables and set outcome (dependent) variable

weather = pd.get_dummies(bikeshare.weathersit, prefix='weather') modeldata = bikeshare[['temp', 'hum']].join(weather[['weather_1', 'weather_2', 'weather_3']]) y = bikeshare.casual

Create a cross valiation with 5 folds

kf = cross_validation.KFold(len(modeldata), n_folds=5, shuffle=True)
mse_values = [] scores = [] n= 0 print "~~~~ CROSS VALIDATION each fold ~~~~" for train_index, test_index in kf: lm = linear_model.LinearRegression().fit(modeldata.iloc[train_index], y.iloc[train_index]) mse_values.append(metrics.mean_squared_error(y.iloc[test_index], lm.predict(modeldata.iloc[test_index]))) scores.append(lm.score(modeldata, y)) n+=1 print 'Model', n print 'MSE:', mse_values[n-1] print 'R2:', scores[n-1] print "~~~~ SUMMARY OF CROSS VALIDATION ~~~~" print 'Mean of MSE for all folds:', np.mean(mse_values) print 'Mean of R2 for all folds:', np.mean(scores)
lm = linear_model.LinearRegression().fit(modeldata, y) print "~~~~ Single Model ~~~~" print 'MSE of single model:', metrics.mean_squared_error(y, lm.predict(modeldata)) print 'R2: ', lm.score(modeldata, y)

Check

While the cross validated approach here generated more overall error, which of the two approaches would predict new data more accurately: the single model or the cross validated, averaged one? Why?

Answer: this score will be lower with the single model in the case, but we're trading off bias error for generalized error

###Advanced: There are ways to improve our model with regularization. Let's check out the effects on MSE and R2

lm = linear_model.LinearRegression().fit(modeldata, y) print "~~~ OLS ~~~" print 'OLS MSE: ', metrics.mean_squared_error(y, lm.predict(modeldata)) print 'OLS R2:', lm.score(modeldata, y) lm = linear_model.Lasso().fit(modeldata, y) print "~~~ Lasso ~~~" print 'Lasso MSE: ', metrics.mean_squared_error(y, lm.predict(modeldata)) print 'Lasso R2:', lm.score(modeldata, y) lm = linear_model.Ridge().fit(modeldata, y) print "~~~ Ridge ~~~" print 'Ridge MSE: ', metrics.mean_squared_error(y, lm.predict(modeldata)) print 'Ridge R2:', lm.score(modeldata, y)

Figuring out the alphas can be done by "hand"

alphas = np.logspace(-10, 10, 21) for a in alphas: print 'Alpha:', a lm = linear_model.Ridge(alpha=a) lm.fit(modeldata, y) print lm.coef_ print metrics.mean_squared_error(y, lm.predict(modeldata))

Or we can use grid search to make this faster

from sklearn import grid_search alphas = np.logspace(-10, 10, 21) gs = grid_search.GridSearchCV( estimator=linear_model.Ridge(), param_grid={'alpha': alphas}, scoring='neg_mean_squared_error') gs.fit(modeldata, y)
Best score
print gs.best_score_
mean squared error here comes in negative, so let's make it positive.
print -gs.best_score_
explains which grid_search setup worked best
print gs.best_estimator_
shows all the grid pairings and their performances.
print gs.grid_scores_

Gradient Descent

num_to_approach, start, steps, optimized = 6.2, 0., [-1, 1], False while not optimized: current_distance = num_to_approach - start got_better = False next_steps = [start + i for i in steps] for n in next_steps: distance = np.abs(num_to_approach - n) if distance < current_distance: got_better = True print distance, 'is better than', current_distance current_distance = distance start = n if got_better: print 'found better solution! using', current_distance a += 1 else: optimized = True print start, 'is closest to', num_to_approach

For the DP example below, it might be a great idea for students to take the code and implement a stopping point, similar to what n_iter would do in gradient descent.

There can be a great conversation about stopping early and still kinda getting the right result vs taking a longer time to solve and having a more precise model.

That solution is below.

num_to_approach, start, steps, optimized = 6.2, 0., [-1, 1], False n_iter = 0 while not optimized: if n_iter > 3: print 'stopping iterations' break n_iter += 1 current_distance = num_to_approach - start got_better = False next_steps = [start + i for i in steps] for n in next_steps: distance = np.abs(num_to_approach - n) if distance < current_distance: got_better = True print distance, 'is better than', current_distance current_distance = distance start = n if got_better: print 'found better solution! using', current_distance a += 1 else: optimized = True print start, 'is closest to', num_to_approach

##Demo: Application of Gradient Descent

lm = linear_model.SGDRegressor() lm.fit(modeldata, y) print "Gradient Descent R2:", lm.score(modeldata, y) print "Gradient Descent MSE:", metrics.mean_squared_error(y, lm.predict(modeldata))

###Check: Untuned, how well did gradient descent perform compared to OLS?

Previous Result (from above):

Mean of MSE for all folds: 1780.97924083 Mean of R2 for all folds: 0.306643649561

Answer: similar R2, MSE is lower for GR

#Independent Practice: Bike data revisited

There are tons of ways to approach a regression problem. The regularization techniques appended to ordinary least squares optimizes the size of coefficients to best account for error. Gradient Descent also introduces learning rate (how aggressively do we solve the problem), epsilon (at what point do we say the error margin is acceptable), and iterations (when should we stop no matter what?)

For this deliverable, our goals are to:

  • implement the gradient descent approach to our bike-share modeling problem,

  • show how gradient descent solves and optimizes the solution,

  • demonstrate the grid_search module!

While exploring the Gradient Descent regressor object, you'll build a grid search using the stochastic gradient descent estimator for the bike-share data set. Continue with either the model you evaluated last class or the simpler one from today. In particular, be sure to implement the "param_grid" in the grid search to get answers for the following questions:

  • With a set of alpha values between 10^-10 and 10^-1, how does the mean squared error change?

  • Based on the data, we know when to properly use l1 vs l2 regularization. By using a grid search with l1_ratios between 0 and 1 (increasing every 0.05), does that statement hold true? If not, did gradient descent have enough iterations?

  • How do these results change when you alter the learning rate (eta0)?

Bonus: Can you see the advantages and disadvantages of using gradient descent after finishing this exercise?

Starter Code

params = {} # put your gradient descent parameters here gs = grid_search.GridSearchCV( estimator=linear_model.SGDRegressor(), cv=cross_validation.KFold(len(modeldata), n_folds=5, shuffle=True), param_grid=params, scoring='neg_mean_squared_error', ) gs.fit(modeldata, y) print 'BEST ESTIMATOR' print -gs.best_score_ print gs.best_estimator_ print 'ALL ESTIMATORS' print gs.grid_scores_

Independent Practice Solution

This code shows the variety of challenges and some student gotchas. The plots will help showcase what should be learned.

  1. With a set of alpha values between 10^-10 and 10^-1, how does the mean squared error change?

  2. We know when to properly use l1 vs l2 regularization based on the data. By using a grid search with l1_ratios between 0 and 1 (increasing every 0.05), does that statement hold true?

    • (if it didn't look like it, did gradient descent have enough iterations?)

  3. How do results change when you alter the learning rate (power_t)?

%matplotlib inline
alphas = np.logspace(-10, -1, 10) print alphas params = {'alpha':alphas, } # put your gradient descent parameters here gs = grid_search.GridSearchCV( estimator=linear_model.SGDRegressor(), cv=cross_validation.KFold(len(modeldata), n_folds=5, shuffle=True), param_grid=params, scoring='neg_mean_squared_error', ) gs.fit(modeldata, y)
grid = pd.DataFrame(gs.grid_scores_) grid.iloc[:,0] = grid.iloc[:,0].apply(lambda x: x['alpha']) grid.iloc[:,1] = grid.iloc[:,1].apply(lambda x: -x) grid.columns = ['alpha', 'mean_squared_error', 'cv'] #grid.iloc[:,0]

With the alphas available, it looks like at mean squared error stays generally flat with incredibly small alpha values, but starting at 10310^{-3}, the error begins to elbow. We probably don't have much of a different in performance with other alpha values.

grid.plot('alpha', 'mean_squared_error', logx=True)

At alpha values of either .1 or 1, the l1_ratio works best closer to 1! Interesting. At other values of alpha they should see similar results, though the graphs aren't as clear.

l1_2_ratios = [float(i) / 100 for i in range(0, 101, 5)] print l1_2_ratios params = {'l1_ratio':l1_2_ratios, 'penalty': ['elasticnet'], 'alpha': [.1], 'tol': [50]} gs = grid_search.GridSearchCV( estimator=linear_model.SGDRegressor(), cv=cross_validation.KFold(len(modeldata), n_folds=5, shuffle=True), param_grid=params, scoring='neg_mean_squared_error', ) gs.fit(modeldata, y) grid = pd.DataFrame(gs.grid_scores_) grid.iloc[:,0] = grid.iloc[:,0].apply(lambda x: x['l1_ratio']) grid.iloc[:,1] = grid.iloc[:,1].apply(lambda x: -x) grid.columns = ['l1_ratio', 'mean_squared_error', 'cv']
gs.best_estimator_
grid.plot('l1_ratio', 'mean_squared_error')
learning = range(1, 50) print learning params = {'eta0':learning, 'tol': [50]} gs = grid_search.GridSearchCV( estimator=linear_model.SGDRegressor(), cv=cross_validation.KFold(len(modeldata), n_folds=5, shuffle=True), param_grid=params, scoring='neg_mean_squared_error', ) gs.fit(modeldata, y) grid = pd.DataFrame(gs.grid_scores_) grid.iloc[:,0] = grid.iloc[:,0].apply(lambda x: x['eta0']) grid.iloc[:,1] = grid.iloc[:,1].apply(lambda x: -x) grid.columns = ['eta0', 'mean_squared_error', 'cv']

Here it should be apparent that as the initial learning rate increases, the error should also increase. And what happens when the initial learning rate is too high? A dramatic increase in error. Students should recognize the importance of learning rate and what values it should be set at, the smaller generally the better.

grid.plot('eta0', 'mean_squared_error', logy=True)