Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Regression Analysis with Python
Regression Analysis with Python

Regression Analysis with Python: Discover everything you need to know about the art of regression analysis with Python, and change how you view data

Arrow left icon
Profile Icon Luca Massaron Profile Icon Alberto Boschetti
Arrow right icon
$9.99 $39.99
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3 (4 Ratings)
eBook Feb 2016 312 pages 1st Edition
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Luca Massaron Profile Icon Alberto Boschetti
Arrow right icon
$9.99 $39.99
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3 (4 Ratings)
eBook Feb 2016 312 pages 1st Edition
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Regression Analysis with Python

Chapter 2. Approaching Simple Linear Regression

Having set up all your working tools (directly installing Python and IPython or using a scientific distribution), you are now ready to start using linear models to incorporate new abilities into the software you plan to build, especially predictive capabilities. Up to now, you have developed software solutions based on certain specifications you defined (or specifications that others have handed to you). Your approach has always been to tailor the response of the program to particular inputs, by writing code carefully mapping every single situation to a specific, predetermined response. Reflecting on it, by doing so you were just incorporating practices that you (or others) have learned from experience.

However, the world is complex, and sometimes your experience is not enough to make your software smart enough to make a difference in a fairly competitive business or in challenging problems with many different and mutable facets.

In...

Defining a regression problem

Thanks to machine learning algorithms, deriving knowledge from data is possible. Machine learning has solid roots in years of research: it has really been a long journey since the end of the fifties, when Arthur Samuel clarified machine learning as being a "field of study that gives computers the ability to learn without being explicitly programmed."

The data explosion (the availability of previously unrecorded amounts of data) has enabled the widespread usage of both recent and classic machine learning techniques and made them high-performance techniques. If nowadays you can talk by voice to your mobile phone and expect it to answer properly to you, acting as your secretary (such as Siri or Google Now), it is uniquely because of machine learning. The same holds true for every application based on machine learning such as face recognition, search engines, spam filters, recommender systems for books/music/movies, handwriting recognition, and automatic...

Starting from the basics

We will start exploring the first dataset, the Boston dataset, but before delving into numbers, we will upload a series of helpful packages that will be used during the rest of the chapter:

In: import numpy as np
  import pandas as pd
  import matplotlib.pyplot as plt
  import matplotlib as mpl

If you are working from an IPython Notebook, running the following command in a cell will instruct the Notebook to represent any graphic output in the Notebook itself (otherwise, if you are not working on IPython, just ignore the command because it won't work in IDEs such as Python's IDLE or Spyder):

In: %matplotlib inline
  # If you are using IPython, this will make the images available in the Notebook

To immediately select the variables that we need, we just frame all the data available into a Pandas data structure, DataFrame.

Inspired by a similar data structure present in the R statistical language, a DataFrame renders data vectors of different types easy to handle...

Extending to linear regression

Linear regression tries to fit a line through a given set of points, choosing the best fit. The best fit is the line that minimizes the summed squared difference between the value dictated by the line for a certain value of x and its corresponding y values. (It is optimizing the same squared error that we met before when checking how good a mean was as a predictor.)

Since linear regression is a line; in bi-dimensional space (x, y), it takes the form of the classical formula of a line in a Cartesian plane: y = mx + q, where m is the angular coefficient (expressing the angle between the line and the x axis) and q is the intercept between the line and the x axis.

Formally, machine learning indicates the correct expression for a linear regression as follows:

Extending to linear regression

Here, again, X is a matrix of the predictors, β is a matrix of coefficients, and β0 is a constant value called the bias (it is the same as the Cartesian formulation, only the notation is different...

Minimizing the cost function

At the core of linear regression, there is the search for a line's equation that it is able to minimize the sum of the squared errors of the difference between the line's y values and the original ones. As a reminder, let's say our regression function is called h, and its predictions h(X), as in this formulation:

Minimizing the cost function

Consequently, our cost function to be minimized is as follows:

Minimizing the cost function

There are quite a few methods to minimize it, some performing better than others in the presence of large quantities of data. Among the better performers, the most important ones are Pseudoinverse (you can find this in books on statistics), QR factorization, and gradient descent.

Explaining the reason for using squared errors

Looking under the hood of a linear regression analysis, at first it could be puzzling to realize that we are striving to minimize the squared differences between our estimates and the data from which we are building the model. Squared differences are not...

Summary

In this chapter, we introduced linear regression as a supervised machine learning algorithm. We explained its functional form, its relationship with the statistical measures of mean and correlation, and we tried to build a simple linear regression model on the Boston house prices data. After doing that we finally glanced at how regression works under the hood by proposing its key mathematical formulations and their translation into Python code.

In the next chapter, we will continue our discourse about linear regression, extending our predictors to multiple variables and carrying on our explanation where we left it suspended during our initial illustration with a single variable. We will also point out the most useful transformations you can apply to data to make it suitable for processing by a linear regression algorithm.

Left arrow icon Right arrow icon

Key benefits

  • Become competent at implementing regression analysis in Python
  • Solve some of the complex data science problems related to predicting outcomes
  • Get to grips with various types of regression for effective data analysis

Description

Regression is the process of learning relationships between inputs and continuous outputs from example data, which enables predictions for novel inputs. There are many kinds of regression algorithms, and the aim of this book is to explain which is the right one to use for each set of problems and how to prepare real-world data for it. With this book you will learn to define a simple regression problem and evaluate its performance. The book will help you understand how to properly parse a dataset, clean it, and create an output matrix optimally built for regression. You will begin with a simple regression algorithm to solve some data science problems and then progress to more complex algorithms. The book will enable you to use regression models to predict outcomes and take critical business decisions. Through the book, you will gain knowledge to use Python for building fast better linear models and to apply the results in Python or in any computer language you prefer.

Who is this book for?

The book targets Python developers, with a basic understanding of data science, statistics, and math, who want to learn how to do regression analysis on a dataset. It is beneficial if you have some knowledge of statistics and data science.

What you will learn

  • * Format a dataset for regression and evaluate its performance
  • * Apply multiple linear regression to real-world problems
  • * Learn to classify training points
  • * Create an observation matrix, using different techniques of data analysis and cleaning
  • * Apply several techniques to decrease (and eventually fix) any overfitting problem
  • * Learn to scale linear models to a big dataset and deal with incremental data

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 29, 2016
Length: 312 pages
Edition : 1st
Language : English
ISBN-13 : 9781783980741
Category :
Languages :
Concepts :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Feb 29, 2016
Length: 312 pages
Edition : 1st
Language : English
ISBN-13 : 9781783980741
Category :
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 152.97
Regression Analysis with Python
$48.99
Python Data Visualization Cookbook (Second Edition)
$48.99
Learning Predictive Analytics with Python
$54.99
Total $ 152.97 Stars icon

Table of Contents

10 Chapters
1. Regression – The Workhorse of Data Science Chevron down icon Chevron up icon
2. Approaching Simple Linear Regression Chevron down icon Chevron up icon
3. Multiple Regression in Action Chevron down icon Chevron up icon
4. Logistic Regression Chevron down icon Chevron up icon
5. Data Preparation Chevron down icon Chevron up icon
6. Achieving Generalization Chevron down icon Chevron up icon
7. Online and Batch Learning Chevron down icon Chevron up icon
8. Advanced Regression Methods Chevron down icon Chevron up icon
9. Real-world Applications for Regression Models Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
(4 Ratings)
5 star 25%
4 star 25%
3 star 0%
2 star 25%
1 star 25%
Amazon Customer Jul 23, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book will teach you the basics of Machine Learning - with focus on regression techniques, and show how to apply it to real-world problems. It starts from the simplest statistical concept of correlation, then goes into linear regression and gradually you'll find yourself reading about advanced techniques like Gradient Boosting. The book doesn't just show how to use off-the-shelf solutions like scikit-learn or statsmodels, but also gives enough theoretical grounds to understand what's going on in the library. Of course, it's all illustrated with nice examples.I recommend this book to anyone who would like to start with Machine Learning in Python
Amazon Verified review Amazon
Abacus Nov 08, 2018
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
For the targeted audience, the book teaches a lot about Python and regression analysis.Their coverage of the latter is surprisingly deep; especially within chapters 6 & 8 where they cover regularization methods and other advanced regression methods. Theoretical subjects such as how to treat outliers, overfitting vs underfitting, bias vs. variance, and many other topics are covered in a top-notch professorial manner.Their specific explanation of what the various main packages do is excellent. The numerous examples using either statsmodels or scikit-learn are very good.For my part, the book had a somewhat limited use because I work mainly with longitudinal data (econometrics time series). Meanwhile, the authors demonstrated regressions mainly using panel data (at the end they show a time series analysis which is not a full fledge multiple regression). Also, I found the coding at times burdensome (standardizing variables and graphs demanded a lot of codes). This may be in part due to the Python language itself. I come from R that is far more efficient for regression modeling.The book has a few questionable statements that do not detract from its overall quality. On pg. 39, the authors indicate that variables have to be standardized to get representative correlations. That’s inaccurate. I have tested this assumption with 10 different macroeconomic variables on different scales. And, I got the exact same correlation matrix whether the variables were in their original transformation or standardized. On pg. 51, the authors indicate that kurtosis is centered around 0. Within Python statsmodels, it is actually centered around 3. On page 132 & 133, when they either standardize or normalize variables to run regressions, based on the coding it seems that they only do so for the Xs variables and not for Y. If that is the case, that’s not a good model specification. On pg. 216, the explanation of stepwise forward selection seems inaccurate (in step 2, they indicate all other variables are projected on the first one selected; but, instead all other variables are correlated to the residual of the model using the first selected variables. And, you select the variable that has the highest absolute correlation with the mentioned residuals).In conclusion, this is a good book on the subject. Just like for any programing language, you don’t learn from just a single source. You will find yourself studying books, on-line documentation, YouTube videos, and maybe take online courses.
Amazon Verified review Amazon
B. Henderson Jul 12, 2023
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
This is actually a good book and fairly well written for a Packt, covering the reasons as well as the code. Unfortunately, the code is from 2016 so you will spend a lot of time looking through docs to replicate their methods. Also, Packt no longer supports this book, so code and errata are not available. Once good, old junk now, but useful for learning how things are/were done with less developed packages.
Amazon Verified review Amazon
Sean McGhee May 24, 2017
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
Filled with typos: F statistic had a "|" instead of a "+". For a math book this is impossible to learn from when there are so many error.s
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.

Modal Close icon
Modal Close icon