So, speaking of data, let's create some data for our example. 1
I'll add a few more cells here so that I'm working more in the middle of the screen and then I'm gonna 2
add a comment, say "Make sample data" and I'm going to create two variables here - an x_5 variable 3
and a y_5 variable. 4
That's our fifth example, 5
after all. Our x_5 is going to be equal to a numpy array, so "np.array( 6
[])", 7
and I'm going to punch in here seven values. 8
So if you type in something else, then we're gonna get different results. 9
But I'm going to punch in 0.1, 1.2, 2.4, 3.2, 4.1, 10
5.7 and 6.5. 11
So these are gonna be our X values, 12
seven of them in total. Our y values, 13
so y_5 14
is gonna be equal to also an np array, 15
same thing parentheses, square brackets and here 16
we're going to punch in 1.7, 2.4, 3.5, 3.0, 6.1 17
9.4 and 8.2. 18
So we have two arrays and let's print out the shape of these arrays below. 19
So I'm going to say "print('Shape of x_5 array :', x_5. 20
shape)" and we're gonna do the same thing for our y array. 21
I'm going to fix my typo, shape and then I'm going to do the same thing for our y array - I'm going to say print and 22
then add a string "'Shape of y_5 array: ', y_5.shape". 23
So let's take a look at the shape of these arrays. 24
So they're both flattened meaning they only have one dimension and they've got seven values. 25
Brilliant. 26
So there's our fake sample data and we've added our print statements. 27
Now let's fit a linear regression to this sample data. 28
That way we can calculate the intercept and the slope parameter and we can check our work that we're 29
gonna do against what we know to be correct. 30
So scroll to the very, very top of the file where you're adding your import statements and then in 31
the cell add "from sklearn.linear_model 32
import LinearRegression", capital L, 33
capital R. Remember to hit Shift+Enter and then scroll back down to where you left off and let's run 34
a quick regression. So I'm going to add a comment 35
"Quick linear regression", we're gonna run it like so, we're gonna say "regr = LinearRegresson", 36
capital L, capital R, parentheses at the end and then we're gonna see "regr.fit()" then 37
we're gonna supply two arguments within the parentheses - x_5, comma and then 38
y_5. 39
So these are two arrays, our two pieces of data. 40
Now let's see what happens. 41
Because I'm going tell you right now this isn't gonna work just like that. When we hit Shift+Enter, 42
we can scroll down and take a look at the error - what we get is a value error - "Found input variables with 43
inconsistent number of samples: [1,7]". 44
So that's a very cryptic error message, but let's take a look at the quick documentation for this fit 45
method and see what we need. 46
So. 47
So our fit method takes an X and Y. 48
So the X is our training data and the Y are our target values. 49
Now the interesting thing to note here is that this method requires a two dimensional input. 50
So at the moment our shape of our x ray and our y array is actually one dimensional it's just got seven 51
values in it. 52
But we can see here that this method requires a two dimensional inputs. 53
It needs to know how many samples and how many features. 54
Now we've got only one feature right. 55
We've only got some x values but we still have to pay attention to the kind of array that we're giving. 56
So even though we've only got one feature namely our Xs we still have to pass in a two dimensional array 57
and the same is true for our wise. 58
So even though we're only predicting one value namely the y value we still have to pass in a two dimensional 59
array. 60
So let's go back up here and take a look at where we're creating these arrays. 61
We're going to have to add some additional Python code to reshape or transform our x's and our y array 62
to meet this requirement from our fit method from our regression. 63
I'll show you two possible ways we can solve this problem. 64
Here's the first one - for our x's. 65
If I add another pair of square brackets inside the parentheses and then I put a dot afterwards and 66
write transpose, yeah with parentheses at the end and hit Shift+Enter, 67
then you'll see in the print statement that our X array now is two dimensional. 68
It's got seven rows and one column. 69
So this is one way you can take a one dimensional array which was just seven values and make it into 70
two dimensions even though it's only got one column - two pairs of square brackets and then calling the 71
transpose method. Another way you can accomplish the very very same thing is by using a method called 72
reshape. 73
So writing ".reshape" and then in the parentheses I'm going to supply how many rows I want - 7 74
comma and then how many columns I want - namely 1; 7 rows 1 column. 75
Let me hit Shift+Enter and update the shape of our y array. 76
Voila! 77
I get exactly the same result. 78
So two ways you can do it. 79
Now with that in place I'm going to click into our cell below where we're doing our regression 80
and I'm going to hit Shift+Enter and voila! Our error goes away. 81
So we've managed to fit our regression perfectly. 82
Now what are we actually interested in? 83
We're interested in the theta values right - 84
the intercept and our slope parameter - our theta zero and our theta one. 85
Let's print these out. 86
I'm going to put a print statement here and write the string "'Theta 0:', regr.intercept_ 87
[0]". 88
That's how we got our intercept, our theta zero; and then I'm going to add another print statement "'Theta 89
91 1: ', regr.coef_" and this was two dimensional remember. 90
So we had s[0] and then another square brackets with a zero. 91
So first column first row of this two dimensional coefficient array. I'm going to hit Shift+Enter we can see 92
what the values. Our intercept is at 0.85 approximately and our theta one, our 93
slope has the value 1.2 approximately. 94
So now that we have the parameters that describe our best fit line let's throw them up on screen because 95
I'm a very, very visual person and I'm sure it'll help you as well. 96
So I'm going to add a few more cells, scroll down and now let's plot this stuff. First let's plot our data points. 97
So this 98
we can do with a scatter plot so "plt.scatter" then in the parentheses we'll have "x_5, 99
y_5", and then size for these points maybe at 50. 100
So that's our scatter plot and now we'll add our best fit line between these points. 101
So I'll write "plt.plot(x_5, )", our x values and then for our y values, the predicted 102
values, I'm going to write "regr", which is our linear regression, 103
then ".predict()" and then in the parentheses I'm going to supply again the x values. 104
So I'm going to predict the y values based on the x values. 105
I'm going to this line orange. 106
So I'm going to say "color='orange'" and maybe 'linewidth=3'. 107
Let's add some labels as well. 108
So "plt.xlabel()", 109
it's gonna be the string "x values" and "plt.ylabel()", 110
is gonna be the string "y values". 111
Last but not least "plt.show()". 112
Now let's take a gander what this looks like. 113
Voila! Brilliant. Here we can clearly see our data points and the best fit line. 114
Nothing new so far but I quickly want to mention something quite important and that's to do with a difference 115
in notation that we're gonna be using in this example than in the previous examples. 116
You see this isn't the cost function that we're looking at. 117
This is the best fit line and the data. The plots that we've been running previously have been the cost 118
functions and we're going to be plotting the mean square error shortly. 119
Now the cost functions plot the relationship between the parameters that we're trying to estimate and 120
how far off we were from the actual data. 121
So the cost functions don't plot the actual data itself. 122
That regression line right here, 123
this graph - that's plotting the data. 124
It's a function of x and y and not the theta parameters, theta zero and theta one, that we're gonna be 125
interested in and looking at for our cost function. 126
And the reason that I'm mentioning this is that previously we've been using x and y for our cost function 127
parameters because that's how we tend to learn about mathematics in school and functions. 128
And it's quite a comfortable sort of notation but when looking at this cost function that's coming up. 129
It's going depend on the theta values and the theta values are not our x's and y's so just keep that 130
in mind, x_5 and y_5 131
in this example have a different purpose than the data that we generated in the previous lesson.