Tensorflow: Linear regression- understanding the concept.
In simple words linear regression is predicting the value of Y(dependent variable) based on some variable X(independednt variable) provided there is a relation between X and Y.
Linear regression between two varibles can be represented as :
The blue line in above fig is called as regression line. As we can see some points are on regresion line and some are not, this is because our regression line is a probabilistic model and our predictions are approximate.So there will be some error/ deviation from actual value of variable Y(Blue line):
In above fig the distance betweeen data points(red dots) and the regression line(blue line) is the error . If a data point is on the regression line then the error is zero and similary if the distance between data point and regression line is 'd' than error is 'd'.
Now how we can minimize the error or can how we can find the best fit or best regression line for the given data set?. For solving this problem we use method of "least square".
Mathematical representation of a regression line: Y= b0+b1*X+e
b0 –intercept of the regression line.
b1-slope of the regression line.
e- error/deviation from actual/observed value of variable Y.
Suppose we fit n points of the form (x1,y1) ,(x2,y2)…..(xn,yn)to the above regression line then:
ei= Yi-b0-b1* Xi
Where ei is the difference between ith observed response value and the ith response value that is predicted by our line.
Our aim here is to minimize this error so that we can get the best possible regression line.
Now this error ei can be positive or negative but we are only interested in the magnitude of the error and not in its sign. Hence we square the errors and minimize the sum of squared errors(SSE).
SSE= summation(ei^2) SSE= summation((Yi-b0-b1* Xi)^2)
In above fig the green line is best fir because the green line has least squared error.
Now lets understand the code of a simple regression usinf tensorflow.
Now we are importing the important libraries !
Create Features and Labels Dataframes
For this tutorial, we'll be doing the train test split first as we need to normalize only the train and test features and not the labels.
Train Test Split
The StandardScalar algorithm standardizes the features by removing the mean and scaling to unit variance. In this, the centering and scaling happens independently on each feature by computing the relevant statistics on the samples in the training set. Mean and standard deviation are then stored to be used on later data using the transform method.
Lets summerize what we have done till now . load the dataset, train test split, preprocessing etc. From here we start defining the Tensorflow code to train the model on this dataset and get some inference from it.
Lets Define the tensorflow model.
So, how do we define the placeholders in Tensorflow ?? And how do we define the shape of the placeholder ??
Well, remember the shape of the training data X_train i.e. (404,13). Since we might send the inputs in batches instead of all the samples at once, the number of columns is set to None so that it can be replace by the batch size. For columns, X_train has 13 columns, so that remains the same.
For the label placeholder, remember the shape of the labels y_train i.e. (404,1). Since, we might be using features in batches, the labels should equal to the batch size. Hence, we leave the rows i.e. the output values to None and leave the column to 1.
So, how do we define the hyperparameters i.e. the parameters for which we train the model to finetune them. So, we initialize the weights as an array of ones with a shape of "13". You may ask why 13 ??
Well, say if we send one row of values as input at a time. So, how many values do we get at the input ? It's 13 i.e. one value from each feature. So, number of weight values required for each input is equal to "13".
Similarly, the number of bias values required will be "13".
Train the model
Lets make some prediction for our test data.
So, we just trained a basic tensorflow model to predict the housing prices.