<\/a><\/figure><\/div>\n\n\n\nThe blue line represents our linear regression model line and the dots the training data set.<\/p>\n\n\n\n
Using training data to learn the values of the parameters for simple linear regression that produce the best fitting model is called ordinary least squares or linear least squares.<\/strong> We will discuss more about evaluating the fitness of a model with cost functions in our next article.<\/p>\n\n\n\nPython program showing the actual mathematics of Linear Regression:<\/p>\n\n\n\n
import numpy as np\n\n#Training Data set\nx = [6, 8, 10, 14, 18]\ny = [7, 9, 13, 17.5, 18]\n\n#find sum(x) , sum(y) , sum (xy), sum(x*x) sum(y*y)\nsum_x = np.sum(x)\nsum_y = np.sum(y)\nsum_xy = np.sum(np.multiply(x,y))\nsum_xx = np.sum(np.multiply(x,x))\nsum_yy = np.sum(np.multiply(y,y))\n\n#print sum_x, sum_y,sum_xx, sum_xy, sum_yy\n\n# #find the intercept values in the slope equation (Y = A + BX)\nnumber_of_records = len(x)\nA = (sum_y*sum_xx - sum_x*sum_xy)\/(number_of_records*sum_xx - sum_x*sum_x)\nB = (number_of_records*sum_xy - sum_x*sum_y)\/(number_of_records*sum_xx - sum_x*sum_x)\n\n#print A, B\n\n# find the price of pizaa (predict it)\n# x = 12 inch\n# y = A + BX\n\ndiameter = 12\nprice = A + B*diameter\n\nprint price #prints 13.6810344818\n<\/code><\/pre>\n\n\n\nPython Program using scikit machine learning library:<\/p>\n\n\n\n
from sklearn.linear_model import LinearRegression\n# Training data\nX = [[6], [8], [10], [14], [18]]\ny = [[7], [9], [13], [17.5], [18]]\n\n# Create and fit the model\nmodel = LinearRegression()\nmodel.fit(X, y)\n\nprint 'A 12\" pizza should cost: $%.2f' % model.predict(12)[0]\n#A 12\" pizza should cost: $13.68<\/code><\/pre>\n\n\n\nSome examples of real world use of Linear Regression :<\/p>\n\n\n\n
- The number of calories burnt versus the number of miles you run.<\/li>
- The amount of electricity that is needed to run a house based on the size of the house.<\/li>
- Height and weight relationship, As height increases, you’d expect the weight to increase, but not perfectly.<\/li>
- Amount of alcohol consumed and the amount of alcohol in your bloodstream.<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"
Simple linear regression is a statistical method you can use to study relationships between two continuous (quantitative) variables: independent variable (x) – also referred to as predictor or explanatory variable dependant variable (y) – also referred to as response or outcome The goal of any regression model is to predict the value of y (dependant variable) based on the […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,6,32,31],"tags":[46,54,58,62],"_links":{"self":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/630"}],"collection":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/comments?post=630"}],"version-history":[{"count":2,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/630\/revisions"}],"predecessor-version":[{"id":1898,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/630\/revisions\/1898"}],"wp:attachment":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media?parent=630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/categories?post=630"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/tags?post=630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}