Git Product home page Git Product logo

dsc-bias-variance-trade-off-lab's Introduction

Bias-Variance Tradeoff - Lab

Introduction

In this lab, you'll practice the concepts you learned in the last lesson, bias-variance tradeoff.

Objectives

In this lab you will:

  • Demonstrate the tradeoff between bias and variance by way of fitting a machine learning model

Let's get started!

In this lab, you'll try to predict some movie revenues based on certain factors, such as ratings and movie year. Start by running the following cell which imports all the necessary functions and the dataset:

import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
%matplotlib inline

df = pd.read_excel('movie_data_detailed_with_ols.xlsx')
df.head()

Subset the df DataFrame to only keep the 'domgross', 'budget', 'imdbRating', 'Metascore', and 'imdbVotes' columns.

# Subset the DataFrame
df = None

Split the data

  • First, assign the predictors to X and the outcome variable, 'domgross' to y
  • Split the data into training and test sets. Set the seed to 42 and the test_size to 0.25
# domgross is the outcome variable
X = None
y = None

X_train , X_test, y_train, y_test = None
# create a scaler
scaler = None

Use the MinMaxScaler to scale the training set. Remember you can fit and transform in a single method using .fit_transform().

Then, use .transform() to apply the scaler to the test set.

# fit and transform X_train
# X_train_scaled = scaler.fit_transform(X_train)
X_train_scaled = None

# transform X_test
X_test_scaled = None

Fit a regression model to the training data

# Your code 

# create an instance of linear regression
linreg = None

# fit the model to X_train and y_train

Use the model to make predictions on both the training and test sets:

# Training set predictions
lm_train_predictions = None

# Test set predictions 
lm_test_predictions = None

Plot predictions for the training set against the actual data:

# Run this cell - vertical distance between the points and the line denote the errors
plt.figure(figsize=(8, 5))
plt.scatter(y_train, lm_train_predictions, label='Model')
plt.plot(y_train, y_train, label='Actual data')
plt.title('Model vs data for training set')
plt.legend();

Plot predictions for the test set against the actual data:

# Run this cell - vertical distance between the points and the line denote the errors
plt.figure(figsize=(8, 5))
plt.scatter(y_test, lm_test_predictions, label='Model')
plt.plot(y_test, y_test, label='Actual data')
plt.title('Model vs data for test set')
plt.legend();

Bias

Create a function bias() to calculate the bias of a model's predictions given the actual data: $Bias(\hat{f}(x)) = E[\hat{f}(x)-f(x)]$
(The expected value can simply be taken as the mean or average value.)

import numpy as np
def bias(y, y_hat):
    pass

Variance

Create a function variance() to calculate the variance of a model's predictions: $Var(\hat{f}(x)) = E[\hat{f}(x)^2] - \big(E[\hat{f}(x)]\big)^2$

def variance(y_hat):
    pass

Calculate bias and variance

# Bias and variance for training set 
b = None
v = None
print(f'Train bias: {b} \nTrain variance: {v}')

# Train bias (approximate): -4.063953052867542e-09 
# Train variance (approximate): 3406811040986517.0
# Bias and variance for test set 
b = None
v = None
print(f'Test bias: {b} \nTest variance: {v}')

# Test bias: -10982393.918069275 
# Test variance: 1518678846127932.0

Overfit a new model

Use PolynomialFeatures with degree 3 and transform X_train_scaled and X_test_scaled.

Important note: By including this, you don't only take polynomials of single variables, but you also combine variables, eg:

Budget times MetaScore squared

What you're essentially doing is taking interactions and creating polynomials at the same time! Have a look at how many columns we get using np.shape()!

# Your code here
poly = None

X_train_poly = None
X_test_poly = None
# Check the shape

Fit a regression model to the training data:

# Your code here
polyreg = LinearRegression()

Use the model to make predictions on both the training and test sets:

# Training set predictions
poly_train_predictions = None

# Test set predictions 
poly_test_predictions = None

Plot predictions for the training set against the actual data:

# Run this cell - vertical distance between the points and the line denote the errors
plt.figure(figsize=(8, 5))
plt.scatter(y_train, poly_train_predictions, label='Model')
plt.plot(y_train, y_train, label='Actual data')
plt.title('Model vs data for training set')
plt.legend();

Plot predictions for the test set against the actual data:

# Run this cell - vertical distance between the points and the line denote the errors
plt.figure(figsize=(8, 5))
plt.scatter(y_test, poly_test_predictions, label='Model')
plt.plot(y_test, y_test, label='Actual data')
plt.title('Model vs data for test set')
plt.legend();

Calculate the bias and variance for the training set:

# Bias and variance for training set 
b = None 
v = None 
print('Train bias: {} \nTrain variance: {}'.format(b, v))

# Train bias (approximate): -2.0997090773148971e-07
# Train variance (approximate): 7394168636697528.0

Calculate the bias and variance for the test set:

# Bias and variance for test set 
b = None 
v = None 
print('Test bias: {} \nTest variance: {}'.format(b, v))

# Test bias: -68166032.47666144 
# Test variance: 4.798244829435879e+16

Interpret the overfit model

# Your description here

Level Up (Optional)

In this lab we went from 4 predictors to 35 by adding polynomials and interactions, using PolynomialFeatures. That being said, where 35 leads to overfitting, there are probably ways to improve by adding just a few polynomials. Feel free to experiment and see how bias and variance improve!

Summary

This lab gave you insight into how bias and variance change for a training and a test set by using both simple and complex models.

dsc-bias-variance-trade-off-lab's People

Contributors

loredirick avatar cheffrey2000 avatar alexgriff avatar mas16 avatar sumedh10 avatar fpolchow avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.