{"id":74,"date":"2020-08-18T00:19:00","date_gmt":"2020-08-18T00:19:00","guid":{"rendered":""},"modified":"2021-01-10T01:29:20","modified_gmt":"2021-01-10T01:29:20","slug":"linear-regression-you-should-always-try-it-first","status":"publish","type":"post","link":"https:\/\/therenaissanceraven.com\/?p=74","title":{"rendered":"Linear Regression &#8211; You should always try it first!"},"content":{"rendered":"<p>You should always try to use linear regression first when you are faced with an unknown data source for regression analysis, the main and simple reason behind this is:<\/p>\n<p><b>Linear Regression creates a stable and accurate model, unlikely to fail miserably with erratic data.&nbsp;<\/b><\/p>\n<p>If you are able to model the data correctly to fit a linear model, it is very likely that you have derived the true equation to solve the problem. Lets see an example with a very basic example with a very linear equation:<\/p>\n<p style=\"text-align: center;\">Y = x1+x2<\/p>\n<p><!--HTML generated using hilite.me--><\/p>\n<div style=\"background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: medium solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;\">\n<pre style=\"line-height: 125%; margin: 0px;\"><span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">sklearn.linear_model<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> LinearRegression<br \/><span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">sklearn.ensemble<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> RandomForestRegressor<br \/><span style=\"color: #008800; font-weight: bold;\">from<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">sklearn.model_selection<\/span> <span style=\"color: #008800; font-weight: bold;\">import<\/span> train_test_split<br \/><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">numpy<\/span> <span style=\"color: #008800; font-weight: bold;\">as<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">np<\/span><br \/><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">matplotlib.pyplot<\/span> <span style=\"color: #008800; font-weight: bold;\">as<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">plt<\/span><br \/><br \/><span style=\"color: #888888;\"># generating data<\/span><br \/>x1 <span style=\"color: #333333;\">=<\/span> np<span style=\"color: #333333;\">.<\/span>random<span style=\"color: #333333;\">.<\/span>randint(<span style=\"color: #0000dd; font-weight: bold;\">100<\/span>,size<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">100<\/span>)<br \/>x2 <span style=\"color: #333333;\">=<\/span> np<span style=\"color: #333333;\">.<\/span>random<span style=\"color: #333333;\">.<\/span>randint(<span style=\"color: #0000dd; font-weight: bold;\">100<\/span>,size<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">100<\/span>)<br \/>X <span style=\"color: #333333;\">=<\/span> np<span style=\"color: #333333;\">.<\/span>stack((x1,x2),axis<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>)<br \/><br \/><span style=\"color: #888888;\"># purely linear equation<\/span><br \/>y <span style=\"color: #333333;\">=<\/span> x1<span style=\"color: #333333;\">+<\/span>x2<br \/><br \/><span style=\"color: #888888;\"># splitting data<\/span><br \/>X_train, X_test, y_train, y_test <span style=\"color: #333333;\">=<\/span> train_test_split(X,y,test_size<span style=\"color: #333333;\">=<\/span><span style=\"color: #6600ee; font-weight: bold;\">0.8<\/span>)<br \/><br \/><span style=\"color: #888888;\"># linear regression<\/span><br \/>linearRegressor <span style=\"color: #333333;\">=<\/span> LinearRegression()<br \/>linearRegressor<span style=\"color: #333333;\">.<\/span>fit(X_train,y_train)<br \/>y_pred_linear <span style=\"color: #333333;\">=<\/span> linearRegressor<span style=\"color: #333333;\">.<\/span>predict(X_test)<br \/><span style=\"color: #008800; font-weight: bold;\">print<\/span>(linearRegressor<span style=\"color: #333333;\">.<\/span>intercept_)  <span style=\"color: #888888;\"># prints near zero<\/span><br \/><span style=\"color: #008800; font-weight: bold;\">print<\/span>(linearRegressor<span style=\"color: #333333;\">.<\/span>coef_)  <span style=\"color: #888888;\"># prints [1,1]<\/span><br \/><br \/><span style=\"color: #888888;\"># random forest regression<\/span><br \/>randomForestRegressor <span style=\"color: #333333;\">=<\/span> RandomForestRegressor(max_depth<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">100<\/span>,n_estimators<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">100<\/span>)<br \/>randomForestRegressor<span style=\"color: #333333;\">.<\/span>fit(X_train,y_train)<br \/>y_pred_randomForest <span style=\"color: #333333;\">=<\/span> randomForestRegressor<span style=\"color: #333333;\">.<\/span>predict(X_test)<br \/><br \/><span style=\"color: #888888;\"># plotting<\/span><br \/>plt<span style=\"color: #333333;\">.<\/span>scatter(y_test,y_pred_linear,label<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'Linear'<\/span>)<br \/>plt<span style=\"color: #333333;\">.<\/span>scatter(y_test,y_pred_randomForest,label<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'Random Forest'<\/span>)<br \/>plt<span style=\"color: #333333;\">.<\/span>plot([np<span style=\"color: #333333;\">.<\/span>min(y_test),np<span style=\"color: #333333;\">.<\/span>max(y_test)],[np<span style=\"color: #333333;\">.<\/span>min(y_test),np<span style=\"color: #333333;\">.<\/span>max(y_test)],<span style=\"background-color: #fff0f0;\">'k-'<\/span>,label<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">'n=1'<\/span>)<br \/>plt<span style=\"color: #333333;\">.<\/span>legend()<br \/>plt<span style=\"color: #333333;\">.<\/span>show()<br \/><\/pre>\n<\/div>\n<p>The results are shown below for degree of fit, additionally from printing the linear regression intercept and slope we can see that it derived the following equation:<\/p>\n<p style=\"text-align: center;\">Y = 1*x1+1*x2+0<\/p>\n<p style=\"text-align: center;\">\n<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/therenaissanceraven.com\/wp-content\/uploads\/2020\/08\/SimpleLinearExample.png\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" border=\"0\" data-original-height=\"410\" data-original-width=\"547\" src=\"https:\/\/therenaissanceraven.com\/wp-content\/uploads\/2020\/08\/SimpleLinearExample-300x225.png\" \/><\/a><\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: left;\">Which is spot on.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You should always try to use linear regression first when you are faced with an unknown data source for regression analysis, the main and simple reason behind this is: Linear Regression creates a stable and accurate model, unlikely to fail miserably with erratic data.&nbsp; If you are able to model the data correctly to fit&#8230;<\/p>\n","protected":false},"author":2,"featured_media":109,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-74","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning"],"_links":{"self":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/posts\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=74"}],"version-history":[{"count":1,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/posts\/74\/revisions"}],"predecessor-version":[{"id":110,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/posts\/74\/revisions\/110"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/media\/109"}],"wp:attachment":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}