{"id":3684,"date":"2018-08-14T05:26:18","date_gmt":"2018-08-14T05:26:18","guid":{"rendered":"https:\/\/www.botreetechnologies.com\/blog\/?p=3684"},"modified":"2018-08-14T05:26:18","modified_gmt":"2018-08-14T05:26:18","slug":"simple-linear-regression-in-machine-learning-using-python","status":"publish","type":"post","link":"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/","title":{"rendered":"Simple linear regression in Machine Learning using Python"},"content":{"rendered":"<p>Machine learning is a field of computer science that gives computer systems the ability to learn with data, without being explicitly programmed.<\/p>\n<p>There are many algorithms available in <a href=\"https:\/\/www.botreetechnologies.com\/machine-learning-solutions\" target=\"_blank\" rel=\"noopener noreferrer\">python to use with machine learning<\/a>. Linear regression is one of them.<\/p>\n<p>We all know that linear regression is a popular technique and you might as well seen the mathematical equation of linear regression which is <i>y=mx+b<\/i>. where <i>m<\/i> is the slope of line and <i>b<\/i> is y-intercept.<\/p>\n<p>But here we are going to use python implementation of linear regression.<\/p>\n<blockquote><p>In order to understand linear regression, you should have knowledge of statistics.<\/p><\/blockquote>\n<p>Data is the most important thing in the machine learning. If we do not have enough data we cannot predict a result or make a decision even if we implemented our algorithm correctly.<\/p>\n<p>Once we have the data set, we need to identify our features and label.<\/p>\n<p>Here, feature means input data and label represents validated output and our target output when we predict the result.<\/p>\n<p>We are going to use diabetes dataset available in sklearn library.<\/p>\n<blockquote><p><code>import matplotlib.pyplot as plt<br \/>\nimport numpy as np<br \/>\nfrom sklearn import datasets, linear_model<br \/>\nfrom sklearn.metrics import mean_squared_error, r2_score<br \/>\ndiabetes_data = datasets.load_diabetes()<\/code><\/p><\/blockquote>\n<p>You can get dataset information using following code.<\/p>\n<blockquote><p><code>diabetes_data.keys,diabetes_data.data.shape<\/code><\/p><\/blockquote>\n<p>You can get all the features names available in our dataset:<\/p>\n<blockquote><p><code>diabetes_data.feature_names<\/code><\/p><\/blockquote>\n<p>Here, we are creating a data frame of our diabetes data using <i>pandas<\/i><\/p>\n<blockquote><p><code>di = pd.DataFrame(diabetes_data.data)<\/code><\/p><\/blockquote>\n<p>We are mapping data set features names to columns of our data frame.<\/p>\n<blockquote><p><code>di.columns = diabetes_data.feature_names<\/code><\/p><\/blockquote>\n<p>We are creating new column <i>target<\/i> of diabetes dataset.<\/p>\n<blockquote><p><code>di['target'] = diabetes_data.target<\/code><\/p><\/blockquote>\n<p>Here we stored our data into <i>x<\/i> which is containing our independent data.<\/p>\n<blockquote><p><code>x=di.drop('target',axis=1)<\/code><\/p><\/blockquote>\n<p>Now, we created the object of our linear model.<\/p>\n<blockquote><p><code>rm = linear_model.LinearRegression()<\/code><\/p><\/blockquote>\n<p>You will get following output:<\/p>\n<blockquote><p><code>LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)<\/code><\/p><\/blockquote>\n<p>We can fit our linear regression model using our dataset that we have already generated.<\/p>\n<blockquote><p><code>rm.fit(x,di.target)<\/code><\/p><\/blockquote>\n<p>Following will return <i>intercept<\/i> and <i>coefficient<\/i> of our linear model. The <i>coefficient<\/i> is used to find out the relationship between a feature and our target output.<\/p>\n<p>Coefficient will be as positive as it gets higher than 0 and negative if it is less than 0.<\/p>\n<p>Positive coefficient represents a strong relationship between feature and label.<\/p>\n<blockquote><p><code>print(rm.intercept_)<br \/>\nprint(rm.coef_)<\/code><\/p><\/blockquote>\n<p>Once we fit values in our model we can predict values by using <i>predict()<\/i> method available in <i>sklearn<\/i>. Here, we are predicting values for the first 10 records.<\/p>\n<blockquote><p><code>rm.predict(x)[:10]<\/code><\/p><\/blockquote>\n<p>We can use as many as a feature we think are enough for getting our desired result.<\/p>\n<p>We are using <i>matplotlib<\/i> to plot out points around the linear line and get the visual idea of our model.<\/p>\n<p><i>matplotlib<\/i> is a Python 2D plotting library which produces publication quality figures in a variety of hard copy formats and interactive environments across platforms. <i>matplotlib<\/i> can be used in Python scripts, the Python and IPython shell, the jupyter notebook, web application servers, and four graphical user interface toolkits.<\/p>\n<h2>Training and Testing Data-set<\/h2>\n<p>Here, first, we need to select our training and testing data set. Training data set will always be bigger than the testing dataset.<\/p>\n<p>We need to get the difference between actual output and our model generated output with the training dataset and to reduce that difference.<\/p>\n<p>Once we finished training our model, we need to test it with test data and check the score of our model.<\/p>\n<p>You should select training and testing dataset randomly because when we hand-picked dataset we may have a different type of data on the training set an entirely different on a testing dataset. In this situation even if you trained your model properly you can never get the desired outcome from your model. For this example, we are selecting a dataset manually.<\/p>\n<p>Now we need to check validation of our regression model. Here, we will use <i>MSE<\/i> (mean squared error).<\/p>\n<p>The mean squared error tells you how close a regression line is to a set of points. It does this by taking the distances from the points to the regression line (these distances are the &#8220;errors&#8221;) and squaring them. The squaring is necessary to remove any negative signs. It also gives more weight to larger differences. It&#8217;s called the mean squared error as you&#8217;re finding the average of a set of errors.<\/p>\n<p>You can also use <i>numpy<\/i> to generate MSE:<\/p>\n<blockquote><p>np.mean(di.target &#8211; rm(x)**2)<\/p><\/blockquote>\n<p>You can use the following script to start with linear regression in Python.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/yogendra-btc\/9c721d6569af0afc57b701d4cb0a33bd.js\"><\/script><\/p>\n<p>Thanks for reading!<\/p>\n<h3><a href=\"https:\/\/www.botreetechnologies.com\/python-development\" target=\"_blank\" rel=\"noopener noreferrer\">Click here for more details&#8230;<\/a><\/h3>\n<hr \/>\n<p>At <a href=\"https:\/\/www.botreetechnologies.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">BoTree Technologies<\/a>, we build enterprise applications with our Python team of 15+ engineers.<\/p>\n<p>We also specialize in RPA, AI, Django, JavaScript and ReactJS.<\/p>\n<h3><a href=\"https:\/\/www.botreetechnologies.com\/contact\" target=\"_blank\" rel=\"noopener noreferrer\">Consulting is free<\/a> &#8211; let us help you grow!<\/h3>\n","protected":false},"excerpt":{"rendered":"<p>Machine learning is a field of computer science that gives&#8230;<\/p>\n","protected":false},"author":27,"featured_media":3685,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70,10],"tags":[],"class_list":["post-3684","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-technology"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Simple linear regression in Machine Learning using Python<\/title>\n<meta name=\"description\" content=\"There are many algorithms available in python to use with machine learning. Learn more about simple linear regression in machine learning using python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Simple linear regression in Machine Learning using Python\" \/>\n<meta property=\"og:description\" content=\"There are many algorithms available in python to use with machine learning. Learn more about simple linear regression in machine learning using python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/\" \/>\n<meta property=\"og:site_name\" content=\"BoTree Technologies\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BoTreeTechnologies\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-08-14T05:26:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/logo-black.png\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"46\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Yogendra Katewa\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@BoTreeTech\" \/>\n<meta name=\"twitter:site\" content=\"@BoTreeTech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yogendra Katewa\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/\"},\"author\":{\"name\":\"Yogendra Katewa\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/6d6e7cb5fcfc8a2d634d736b2e972d42\"},\"headline\":\"Simple linear regression in Machine Learning using Python\",\"datePublished\":\"2018-08-14T05:26:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/\"},\"wordCount\":740,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#primaryimage\"},\"thumbnailUrl\":\"\",\"articleSection\":[\"Python\",\"Technology\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/\",\"name\":\"Simple linear regression in Machine Learning using Python\",\"isPartOf\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#primaryimage\"},\"thumbnailUrl\":\"\",\"datePublished\":\"2018-08-14T05:26:18+00:00\",\"author\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/6d6e7cb5fcfc8a2d634d736b2e972d42\"},\"description\":\"There are many algorithms available in python to use with machine learning. Learn more about simple linear regression in machine learning using python.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#primaryimage\",\"url\":\"\",\"contentUrl\":\"\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.botreetechnologies.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Simple linear regression in Machine Learning using Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#website\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/\",\"name\":\"BoTree Technologies\",\"description\":\"Committed to inspire generation.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.botreetechnologies.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/6d6e7cb5fcfc8a2d634d736b2e972d42\",\"name\":\"Yogendra Katewa\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/yogendra-katewa-150x150.png\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/yogendra-katewa-150x150.png\",\"contentUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/yogendra-katewa-150x150.png\",\"caption\":\"Yogendra Katewa\"},\"description\":\"Yogendra is a Python developer. He also loves working in JavaScript frameworks. He is a foody, loves to read and is also a fitness enthusiast.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Simple linear regression in Machine Learning using Python","description":"There are many algorithms available in python to use with machine learning. Learn more about simple linear regression in machine learning using python.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/","og_locale":"en_US","og_type":"article","og_title":"Simple linear regression in Machine Learning using Python","og_description":"There are many algorithms available in python to use with machine learning. Learn more about simple linear regression in machine learning using python.","og_url":"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/","og_site_name":"BoTree Technologies","article_publisher":"https:\/\/www.facebook.com\/BoTreeTechnologies\/","article_published_time":"2018-08-14T05:26:18+00:00","og_image":[{"width":150,"height":46,"url":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/logo-black.png","type":"image\/png"}],"author":"Yogendra Katewa","twitter_card":"summary_large_image","twitter_creator":"@BoTreeTech","twitter_site":"@BoTreeTech","twitter_misc":{"Written by":"Yogendra Katewa","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#article","isPartOf":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/"},"author":{"name":"Yogendra Katewa","@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/6d6e7cb5fcfc8a2d634d736b2e972d42"},"headline":"Simple linear regression in Machine Learning using Python","datePublished":"2018-08-14T05:26:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/"},"wordCount":740,"commentCount":0,"image":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#primaryimage"},"thumbnailUrl":"","articleSection":["Python","Technology"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/","url":"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/","name":"Simple linear regression in Machine Learning using Python","isPartOf":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#primaryimage"},"image":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#primaryimage"},"thumbnailUrl":"","datePublished":"2018-08-14T05:26:18+00:00","author":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/6d6e7cb5fcfc8a2d634d736b2e972d42"},"description":"There are many algorithms available in python to use with machine learning. Learn more about simple linear regression in machine learning using python.","breadcrumb":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/www.botreetechnologies.com\/blog\/simple-linear-regression-in-machine-learning-using-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.botreetechnologies.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Simple linear regression in Machine Learning using Python"}]},{"@type":"WebSite","@id":"https:\/\/www.botreetechnologies.com\/blog\/#website","url":"https:\/\/www.botreetechnologies.com\/blog\/","name":"BoTree Technologies","description":"Committed to inspire generation.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.botreetechnologies.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/6d6e7cb5fcfc8a2d634d736b2e972d42","name":"Yogendra Katewa","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/yogendra-katewa-150x150.png","url":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/yogendra-katewa-150x150.png","contentUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/yogendra-katewa-150x150.png","caption":"Yogendra Katewa"},"description":"Yogendra is a Python developer. He also loves working in JavaScript frameworks. He is a foody, loves to read and is also a fitness enthusiast."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/3684","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=3684"}],"version-history":[{"count":0,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/3684\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=3684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=3684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=3684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}