Computing linear regression and r-squared using Python

Let's now play with linear regression and actually compute some linear regression and r-squared. We can start by creating a little bit of Python code here that generates some random-ish data that is in fact linearly correlated.

In this example I'm going to fake some data about page rendering speeds and how much people purchase, just like a previous example. We're going to fabricate a linear relationship between the amount of time it takes for a website to load and the amount of money people spend on that website:

%matplotlib inlineimport numpy as npfrom pylab import *pageSpeeds = np.random.normal(3.0, 1.0, 1000)purchaseAmount = 100 - (pageSpeeds + np.random.normal(0, 0.1,1000)) * 3

Get Hands-On Data Science and Python Machine Learning now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.