{"id":641,"date":"2018-05-29T03:19:43","date_gmt":"2018-05-29T03:19:43","guid":{"rendered":"http:\/\/muthu.co\/?p=641"},"modified":"2021-05-24T03:47:18","modified_gmt":"2021-05-24T03:47:18","slug":"evaluating-the-fitness-of-a-modal-using-a-cost-function","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/evaluating-the-fitness-of-a-modal-using-a-cost-function\/","title":{"rendered":"Evaluating the fitness of a modal with a cost function"},"content":{"rendered":"\n

Previously<\/a> we derived a simple linear regression modal for our Pizza price dataset. We built a modal that predicted a price of $13.68 <\/strong>for a 12 inch pizza. <\/strong>When the same modal is used to predict the price of an 8 inch pizza, we get $9.78 which is around $0.78 more than the known price of $9. This difference between the price predicted by the modal and the observed price in the training set is called the residual or training error. Below table shows the residuals<\/strong> for our training set.<\/p>\n\n\n\n

\"\"<\/a><\/figure><\/div>\n\n\n\n

A much better pizza price predictor would be a modal where the sum of training error is as least as possible. We can produce the best pizza-price predictor by minimizing the sum of the residuals. That is, our model fits if the values it predicts for the response variable are close to the observed values for all of the training examples. This measure of model’s fitness is called the residual sum of squares cost function. <\/strong>The residual sum is calculated using the below formula where yi<\/sub><\/em> is the observed value and f(xi<\/sub>)<\/em> is the predicted value.<\/p>\n\n\n\n

\"\"<\/a><\/figure><\/div>\n\n\n\n

Lets extend our program from our previous article<\/a> to calculate the residual sum of squares.<\/p>\n\n\n\n

import numpy as np\nfrom sklearn.linear_model import LinearRegression\n\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\n\nprint np.mean((model.predict(X) - y) ** 2)\n#Residual sum of squares: 1.74956896552<\/code><\/pre>\n\n\n\n

We get 1.74 as our cost function or loss function which gives us the measure of error in our modal. The smaller the residual sum of squares, the better your model fits your data; The greater the residual sum of squares, the poorer your model fits your data. A value of zero means your model is a perfect fit.<\/p>\n","protected":false},"excerpt":{"rendered":"

Previously we derived a simple linear regression modal for our Pizza price dataset. We built a modal that predicted a price of $13.68 for a 12 inch pizza. When the same modal is used to predict the price of an 8 inch pizza, we get $9.78 which is around $0.78 more than the known price of $9. […]<\/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\/641"}],"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=641"}],"version-history":[{"count":2,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/641\/revisions"}],"predecessor-version":[{"id":1897,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/641\/revisions\/1897"}],"wp:attachment":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media?parent=641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/categories?post=641"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/tags?post=641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}