Giter Site home page Giter Site logo

dsc-ridge-and-lasso-regression-lab-notredame-se-ds-preview's Introduction

Ridge and Lasso Regression - Lab

Introduction

In this lab, you'll practice your knowledge on Ridge and Lasso regression!

Objectives

You will be able to:

  • Use Lasso and ridge regression in Python
  • Compare Lasso and Ridge with standard regression

Housing Prices Data

Let's look at yet another house pricing data set.

import pandas as pd
import warnings
warnings.filterwarnings('ignore')

df = pd.read_csv('Housing_Prices/train.csv')

Look at df.info

# Your code here

First, make a selection of the data by removing some of the data with dtype = object, this way our first model only contains continuous features

Make sure to remove the SalesPrice column from the predictors (which you store in X), then replace missing inputs by the median per feature.

Store the target in y.

# Load necessary packages


# remove "object"-type features and SalesPrice from `X`


# Impute null values


# Create y

Look at the information of X again

Let's use this data to perform a first naive linear regression model

Compute the R squared and the MSE for both train and test set.

from sklearn.metrics import mean_squared_error, mean_squared_log_error

# Split in train and test

# Fit the model and print R2 and MSE for train and test

Normalize your data

We haven't normalized our data, let's create a new model that uses preprocessing.scale to scale our predictors!

from sklearn import preprocessing

# Scale the data and perform train test split

Perform the same linear regression on this data and print out R-squared and MSE.

# Your code here

Include dummy variables

Your model hasn't included dummy variables so far: let's use the "object" variables again and create dummies

# Create X_cat which contains only the categorical variables
# Make dummies

Merge x_cat together with our scaled X so you have one big predictor dataframe.

# Your code here

Perform the same linear regression on this data and print out R-squared and MSE.

# Your code here

Notice the severe overfitting above; our training R squared is quite high, but the testing R squared is negative! Our predictions are far off. Similarly, the scale of the Testing MSE is orders of magnitude higher than that of the training.

Perform Ridge and Lasso regression

Use all the data (normalized features and dummy categorical variables) and perform Lasso and Ridge regression for both! Each time, look at R-squared and MSE.

Lasso

With default parameter (alpha = 1)

# Your code here

With a higher regularization parameter (alpha = 10)

# Your code here

Ridge

With default parameter (alpha = 1)

# Your code here

With default parameter (alpha = 10)

# Your code here

Look at the metrics, what are your main conclusions?

Conclusions here

Compare number of parameter estimates that are (very close to) 0 for Ridge and Lasso

Compare with the total length of the parameter space and draw conclusions!

# number of Ridge params almost zero
# number of Lasso params almost zero

Lasso was very effective to essentially perform variable selection and remove about 25% of the variables from your model!

# your code here

Summary

Great! You now know how to perform Lasso and Ridge regression.

dsc-ridge-and-lasso-regression-lab-notredame-se-ds-preview's People

Contributors

loredirick avatar fpolchow avatar

Watchers

James Cloos avatar Kevin McAlear avatar  avatar Mohawk Greene avatar Victoria Thevenot avatar Yoan Ante avatar Belinda Black avatar Bernard Mordan avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton avatar  avatar  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.