Computing correlation – The NumPy way

Now, NumPy can actually compute correlation for you using the corrcoef() function. Let's look at the following code:

np.corrcoef(pageseeds, purchaseAmount) 

This single line gives the following output:

array([(1.         ,-046728788], 
      [-0.46728788], 1.       ]) 

So, if we wanted to do this the easy way, we could just use np.corrcoef(pageSpeeds, purchaseAmount), and what that gives you back is an array that gives you the correlation between every possible combination of the sets of data that you pass in. The way to read the output is: the 1 implies there is a perfect correlation between comparing pageSpeeds to itself and purchaseAmount to itself, which is expected. But when you start comparing pageSpeeds to purchaseAmount or purchaseAmount to the pageSpeeds, you end up with the -0.4672 value, which is roughly what we got when we did it the hard way. There's going to be little precision errors, but it's not really important.

Now we could force a perfect correlation by fabricating a totally linear relationship, so let's take a look at an example of that:

purchaseAmount = 100 - pageSpeeds * 3 
 
scatter(pageSpeeds, purchaseAmount) 
 
correlation (pageSpeeds, purchaseAmount) 

And again, here we would expect the correlation to come out to -1 for a perfect negative correlation, and in fact, that's what we end up with:

Again, a reminder: Correlation does not imply causality. Just because people might spend more if they have faster page speeds, maybe that just means that they can afford a better Internet connection. Maybe that doesn't mean that there's actually a causation between how fast your pages render and how much people spend, but it tells you there's an interesting relationship that's worth investigating more. You cannot say anything about causality without running an experiment, but correlation can tell you what experiments you might want to run.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.82.191