{"id":417,"date":"2020-04-12T13:25:36","date_gmt":"2020-04-12T07:55:36","guid":{"rendered":"https:\/\/dphi.tech\/?p=417"},"modified":"2022-04-04T09:54:36","modified_gmt":"2022-04-04T04:24:36","slug":"tutorial-on-logistic-regression-using-python","status":"publish","type":"post","link":"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/","title":{"rendered":"Tutorial on Logistic Regression using Gradient Descent with Python"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"417\" class=\"elementor elementor-417\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-23236a47 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"23236a47\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-5433b89a\" data-id=\"5433b89a\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-42fa0f90 elementor-widget elementor-widget-text-editor\" data-id=\"42fa0f90\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><\/p>\n<h1 class=\"wp-block-heading\">Learn how logistic regression works and ways to implement it from scratch as well as using sklearn library in Python<\/h1>\n<div>)<\/div>\n<p><\/p>\n<p class=\"wp-block-paragraph\">In statistics, logistic regression is used to model the probability of a certain class or event. I will be focusing more on the basics and implementation of the model, and not go too deep into the math part in this post. Just to give you a heads up, this article is a written version of the video tutorial that can found <span style=\"text-decoration: underline;\"><a href=\"https:\/\/dphi.tech\/blog\/tutorial-on-logistic-regression-using-python\/?preview_id=417&amp;preview_nonce=c78fd656ff&amp;preview=true#video-logistic-regression\">here<\/a><\/span>.&nbsp;<\/p>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_1.gif\" alt=\"\"><p><\/p>\n<figcaption>The value of weights b0 and b1 are updated at each iteration<\/figcaption>\n<\/figure>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Logistic regression is similar to&nbsp;<a href=\"https:\/\/dphi.tech\/blog\/tutorial-on-linear-regression-using-least-squares\/\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"text-decoration: underline;\">linear regression<\/span><\/a>&nbsp;because both of these involve estimating the values of parameters used in the prediction equation based on the given training data. Linear regression predicts the value of a continuous dependent variable. Whereas logistic regression predicts the probability of an event or class that is dependent on other factors. Thus the output of logistic regression always lies between 0 and 1. Because of this property, it is commonly used for classification purpose.<\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">[Learn Data Science from this <a href=\"https:\/\/dphi.tech\/learn\/5-week_Data_Science_Bootcamp?utm_source=dphi-blog&amp;utm_medium=first-link&amp;utm_campaign=learn-platform\">5-Week Online Bootcamp<\/a> materials.]<\/p>\n<p><\/p>\n<h1 class=\"wp-block-heading\">Logistic Model<\/h1>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Consider a model with features&nbsp;<em>x1, x2, x3 \u2026 xn<\/em>. Let the binary output be denoted by&nbsp;<em>Y<\/em>, that can take the values 0 or 1.<br>Let&nbsp;<em>p<\/em>&nbsp;be the probability of&nbsp;<em>Y = 1<\/em>, we can denote it as&nbsp;<em>p = P(Y=1)<\/em>.<br>The mathematical relationship between these variables can be denoted as<\/p>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_2.png\" alt=\"\"><\/figure>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Here the term&nbsp;<em>p\/(1\u2212p)<\/em>&nbsp;is known as the&nbsp;<em>odds<\/em>&nbsp;and denotes the likelihood of the event taking place. Thus<em>&nbsp;ln(p\/(1\u2212p))<\/em>&nbsp;is known as the&nbsp;<em>log odds<\/em>&nbsp;and is simply used to map the probability that lies between 0 and 1 to a range between (\u2212\u221e,+\u221e). The terms&nbsp;<em>b0, b1, b2\u2026&nbsp;<\/em>are parameters (or weights) that we will estimate during training.<\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">So this is just the basic math behind what we are going to do. We are interested in the probability&nbsp;<em>p<\/em>&nbsp;in this equation. So we simplify the equation to obtain the value of p:<\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">1. The log term<em>&nbsp;ln<\/em>&nbsp;on the LHS can be removed by raising the RHS as a power of<em>&nbsp;e<\/em>:<\/p>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_3.png\" alt=\"\"><\/figure>\n<p><\/p>\n<p class=\"wp-block-paragraph\">2. Now we can easily simplify to obtain the value of&nbsp;<em>p&nbsp;<\/em>:<\/p>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_4.png\" alt=\"\"><\/figure>\n<p><\/p>\n<p class=\"wp-block-paragraph\">This actually turns out to be the equation of the&nbsp;<em>Sigmoid Function<\/em>&nbsp;which is widely used in other machine learning applications. The&nbsp;<em>Sigmoid Function<\/em>&nbsp;is given by:<\/p>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_5.png\" alt=\"\"><\/figure>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_6.png\" alt=\"\"><p><\/p>\n<figcaption>The Sigmoid Curve (Wikipedia)<\/figcaption>\n<\/figure>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Now we will be using the derived equation above to make our predictions. Before that, we will train our model to obtain the values of our parameters&nbsp;b0, b1, b2\u2026&nbsp;that results in the least error. This is where the error or loss function comes in.<\/p>\n<p><\/p>\n<h1 class=\"wp-block-heading\">Logistic regression loss function<\/h1><div>The loss is basically the error in our predicted value. In other words, it is a difference between our predicted value and the actual value. We will be using the L2 Loss Function to calculate the error. Theoretically, you can use any function to calculate the error. This function can be broken down as:<br><\/div>\n\n<p><\/p>\n<ol class=\"wp-block-list\">\n<li>Let the actual value be y\u1d62. Let the value predicted using our model be denoted as \u0233\u1d62. Find the difference between the actual and predicted value.<\/li>\n<li>Square this difference.<\/li>\n<li>Find the sum across all the values in training data.<\/li>\n<\/ol>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_7.png\" alt=\"\"><\/figure>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Now that we have the error, we need to update the values of our parameters to minimize this error. This is where the \u201clearning\u201d actually happens since our model is updating itself based on its previous output to obtain a more accurate output in the next step. Hence with each iteration, our model becomes more and more accurate. We will be using the&nbsp;<em>Gradient Descent Algorithm<\/em>&nbsp;to estimate our parameters. Another commonly used algorithm is the&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Maximum_likelihood_estimation\" target=\"_blank\" rel=\"noreferrer noopener\">Maximum Likelihood Estimation<\/a>.<\/p>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_8.jpg\" alt=\"\"><p><\/p>\n<figcaption>The loss or error on the y axis and number of iterations on the x axis.<\/figcaption>\n<\/figure>\n<p><\/p>\n<h1 class=\"wp-block-heading\">The Gradient Descent Algorithm<\/h1>\n<p><\/p>\n<p class=\"wp-block-paragraph\">You might know that the partial derivative of a function at its minimum value is equal to 0. So gradient descent basically uses this concept to estimate the parameters or weights of our model by minimizing the loss function.&nbsp;Check out the below video&nbsp;for a more detailed explanation on how gradient descent works.<\/p>\n<p><\/p>\n<figure>\n<div>https:\/\/www.youtube.com\/watch?v=4PHI11lX11I<\/div>\n<figcaption>Explainer tutorial about Gradient Descent.<\/figcaption>\n<\/figure>\n<p><\/p>\n<p class=\"wp-block-paragraph\">[Learn <a href=\"https:\/\/dphi.tech\/learn\/introduction-to-data-visualization?utm_source=dphi-blog&amp;utm_medium=first-link&amp;utm_campaign=learn-platform\">Data Visualization with Matplotlib<\/a> and <a href=\"https:\/\/dphi.tech\/learn\/introduction-to-exploratory-data-analysis?utm_source=dphi-blog&amp;utm_medium=first-link&amp;utm_campaign=learn-platform\">Exploratory Data Analysis<\/a>]<\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">For simplicity, for the rest of this tutorial let us assume that our output depends only on a single feature&nbsp;<em>x<\/em>. So we can rewrite our equation as:<\/p>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_9.png\" alt=\"\"><\/figure>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Thus we need to estimate the values of weights b0 and b1 using our given training data.<\/p>\n<p><\/p>\n<ol class=\"wp-block-list\">\n<li>Initially let b0=0 and b1=0. Let L be the learning rate. The learning rate controls by how much the values of b0 and b1 are updated at each step in the learning process. Here let L=0.001.<\/li>\n<li>Calculate the partial derivative with respect to b0 and b1. The value of the partial derivative will tell us how far the loss function is from its minimum value. It is a measure of how much our weights need to be updated to attain minimum or ideally 0 error. In case you have more than one feature, you need to calculate the partial derivative for each weight b0, b1 \u2026 bn where n is the number of features. For a detailed explanation on the math behind calculating the partial derivatives, check out&nbsp;<a href=\"https:\/\/youtu.be\/l8VEth6leXA\" target=\"_blank\" rel=\"noreferrer noopener\">my video.<\/a><\/li>\n<\/ol>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_10.png\" alt=\"\"><\/figure>\n<p><\/p>\n<p class=\"wp-block-paragraph\">3. Next we update the values of b0 and b1:<\/p>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_11.png\" alt=\"\"><\/figure>\n<p><\/p>\n<p class=\"wp-block-paragraph\">4. We repeat this process until our loss function is a very small value or ideally reaches 0 (meaning no errors and 100% accuracy). The number of times we repeat this learning process is known as iterations or epochs.<\/p>\n<p><\/p>\n<h1 class=\"wp-block-heading\">Implementing the Model<\/h1>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Import the necessary libraries and download the data set&nbsp;<a href=\"https:\/\/drive.google.com\/uc?id=15WAD9_4CpUK6EWmgWVXU8YMnyYLKQvW8&amp;export=download\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>. The data was taken from&nbsp;<a href=\"https:\/\/www.kaggle.com\/rakeshrau\/social-network-ads\" target=\"_blank\" rel=\"noreferrer noopener\">kaggle<\/a>&nbsp;and describes information about a product being purchased through an advertisement on social media. We will be predicting the value of&nbsp;<em>Purchased&nbsp;<\/em>and consider a single feature,&nbsp;<em>Age<\/em>&nbsp;to predict the values of&nbsp;<em>Purchased<\/em>. You can have multiple features as well.<\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_12.png\" alt=\"\"><\/figure>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_13.jpg\" alt=\"\"><\/figure>\n<p><\/p>\n<p class=\"wp-block-paragraph\">We need to normalize our training data and shift the mean to the origin. This is important to get accurate results because of the nature of the logistic equation. This is done by the&nbsp;<em>normalize&nbsp;<\/em>method. The&nbsp;<em>predict<\/em>&nbsp;method simply plugs in the value of the weights into the logistic model equation and returns the result. This returned value is the required probability.<\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">The model is trained for 300 epochs or iterations. The partial derivatives are calculated at each iteration and the weights are updated. You can even calculate the loss at each step and see how it approaches zero with each step.<\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Since the prediction equation returns a probability, we need to convert it into a binary value to be able to make classifications. To do this, we select a threshold, say 0.5 and all predicted values above 0.5 will be treated as 1 and everything else will be 0. You can choose a suitable threshold depending on the problem you are solving.<\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Here for each value of age in the testing data, we predict if the product was purchased or not and plot the graph. The accuracy can be calculated by checking how many correct predictions we made and dividing it by the total number of test cases. Our accuracy seems to be 85%.<\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_14.jpg\" alt=\"\"><p><\/p>\n<figcaption>Accuracy = 0.85<\/figcaption>\n<\/figure>\n<p><\/p>\n<p class=\"wp-block-paragraph\">[Get Started with Deep Learning with this free <a href=\"https:\/\/dphi.tech\/learn\/getting-started-with-deep-learning?utm_source=dphi-blog&amp;utm_medium=first-link&amp;utm_campaign=learn-platform\">Bootcamp materials<\/a>.]<\/p>\n<p><\/p>\n<h1 class=\"wp-block-heading\">Implementing using Sklearn<\/h1>\n<p><\/p>\n<p class=\"wp-block-paragraph\">The library sklearn can be used to perform logistic regression in a few lines as shown using the&nbsp;<em>LogisticRegression&nbsp;<\/em>class. It also supports multiple features. It requires the input values to be in a specific format hence they have been reshaped before training using the&nbsp;<em>fit&nbsp;<\/em>method.<\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">The accuracy using this is 86.25%, which is very close to the accuracy of our model that we implemented from scratch!<\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/dphi.tech\/blog\/wp-content\/uploads\/2020\/04\/LR_15.jpg\" alt=\"\"><p><\/p>\n<figcaption>Accuracy = 0.8625<\/figcaption>\n<\/figure>\n<p><\/p>\n<p class=\"wp-block-paragraph\">Thus we have implemented a seemingly complicated algorithm easily using python from scratch and also compared it with a standard model in sklearn that does the same. I think the most crucial part here is the gradient descent algorithm, and learning how to the weights are updated at each step. Once you have learned this basic concept, then you will be able to estimate parameters for any function.<\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/colab.research.google.com\/drive\/1qmdfU8tzZ08D3O84qaD11Ffl9YuNUvlD\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Click Here<\/strong><\/a><strong>&nbsp;for the entire code and explanation in a Google Colaboratory. You can use it to explore and play around with the code easily.<\/strong><\/p>\n<p><\/p>\n<h1><strong><a href=\"#video-logistic-regression\">Video Tutorial on Logistic Regression<\/a><\/strong><\/h1>\n<section><\/section>\n<p><\/p>\n<figure>\n<div>https:\/\/www.youtube.com\/watch?v=l8VEth6leXA<\/div>\n<figcaption>Tutorial on Logistic Regression in Python<\/figcaption>\n<\/figure>\n<p><\/p>\n<p class=\"wp-block-paragraph\"><em><strong>Note:<\/strong> This article was originally published on <a href=\"https:\/\/towardsdatascience.com\/logistic-regression-explained-and-implemented-in-python-880955306060\">towardsdatascience.com<\/a>, and kindly contributed to DPhi to spread the knowledge.<\/em><\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\">[<a href=\"https:\/\/dphi.tech\/challenges\/datathon\/?utm_source=dphi-blog&amp;utm_medium=first-link&amp;utm_campaign=learn-platform\">Join<\/a> our community solve problem based on real-world datasets.]<\/p>\n<p><\/p>\n<p class=\"wp-block-paragraph\"><strong>References<\/strong><\/p>\n<p><\/p>\n<ul class=\"wp-block-list\">\n<li>Artificial Intelligence, a modern approach \u2014 pg 726, 727<\/li>\n<li><a href=\"https:\/\/machinelearningmastery.com\/logistic-regression-for-machine-learning\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/machinelearningmastery.com\/logistic-regression-for-machine-learning\/<\/a><\/li>\n<li><a href=\"https:\/\/towardsdatascience.com\/logit-of-logistic-regression-understanding-the-fundamentals-f384152a33d1\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/towardsdatascience.com\/logit-of-logistic-regression-understanding-the-fundamentals-f384152a33d1<\/a><\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Logistic_regression\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/en.wikipedia.org\/wiki\/Logistic_regression<\/a><\/li>\n<\/ul>\n<p><\/p>\n<blockquote class=\"is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong><em>Become a guide. Become a mentor.<\/em><\/strong><br><em>We at DPhi, welcome you to share your experience in data science \u2013 be it your learning journey, experience while participating in Data Science Challenges, data science&nbsp;projects, tutorials and anything that is related to Data Science. Your learnings could help a large number of aspiring data scientists!&nbsp;Interested? Submit&nbsp;<a href=\"https:\/\/docs.google.com\/forms\/d\/e\/1FAIpQLSdOMt4yfkgkWxWIkvQMuuJiwsc21mCV8IxPqGLhj1uZ6oLJ9Q\/viewform\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.&nbsp;<\/em><\/p>\n<\/blockquote>\n<p><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>The article talks about how logistic regression works and how you can easily implement it from scratch using python as well as using sklearn<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[20,23,22,24],"class_list":["post-417","post","type-post","status-publish","format-standard","hentry","category-datascience","tag-datascience","tag-logisctic-regression","tag-machine-learning","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Logistic Regression in Python Explained - Gradient Descent | Free Tutorial<\/title>\n<meta name=\"description\" content=\"Logistic regression is used to model the probability of a certain class or event. We&#039;ll be focusing more on the basics and implementation of the model.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Logistic Regression in Python Explained - Gradient Descent | Free Tutorial\" \/>\n<meta property=\"og:description\" content=\"Logistic regression is used to model the probability of a certain class or event. We&#039;ll be focusing more on the basics and implementation of the model.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/\" \/>\n<meta property=\"og:site_name\" content=\"AI Planet\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/aiplanet\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-12T07:55:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-04T04:24:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/LR_1.gif\" \/>\n<meta name=\"author\" content=\"Adarsh Menon\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Adarsh Menon\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-logistic-regression-using-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-logistic-regression-using-python\\\/\"},\"author\":{\"name\":\"Adarsh Menon\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#\\\/schema\\\/person\\\/b121996cdbe68ef54fbc4683ff368a27\"},\"headline\":\"Tutorial on Logistic Regression using Gradient Descent with Python\",\"datePublished\":\"2020-04-12T07:55:36+00:00\",\"dateModified\":\"2022-04-04T04:24:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-logistic-regression-using-python\\\/\"},\"wordCount\":1560,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-logistic-regression-using-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/LR_1.gif\",\"keywords\":[\"datascience\",\"logisctic regression\",\"machine learning\",\"python\"],\"articleSection\":[\"datascience\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-logistic-regression-using-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-logistic-regression-using-python\\\/\",\"url\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-logistic-regression-using-python\\\/\",\"name\":\"Logistic Regression in Python Explained - Gradient Descent | Free Tutorial\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-logistic-regression-using-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-logistic-regression-using-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/LR_1.gif\",\"datePublished\":\"2020-04-12T07:55:36+00:00\",\"dateModified\":\"2022-04-04T04:24:36+00:00\",\"description\":\"Logistic regression is used to model the probability of a certain class or event. We'll be focusing more on the basics and implementation of the model.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-logistic-regression-using-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-logistic-regression-using-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-logistic-regression-using-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/LR_1.gif\",\"contentUrl\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/LR_1.gif\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/tutorial-on-logistic-regression-using-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorial on Logistic Regression using Gradient Descent with Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/\",\"name\":\"AI Planet, Formerly DPhi\",\"description\":\"AI Planet (formerly DPhi) is a global ecosystem that aims to educate and empower individuals and organizations to build and utilize artificial intelligence (AI). With a community of over 300,000 AI specialists and enthusiasts, we offer a range of products and have helped drive innovative solutions for various industries. Our ultimate goal is to make AI education accessible to everyone and use AI to tackle important business and social challenges.\",\"publisher\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#organization\",\"name\":\"AI Planet, Formerly DPhi\",\"url\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/Size48-Large-1.png\",\"contentUrl\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/Size48-Large-1.png\",\"width\":48,\"height\":48,\"caption\":\"AI Planet, Formerly DPhi\"},\"image\":{\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/aiplanet\\\/\",\"https:\\\/\\\/www.instagram.com\\\/aiplanet_community\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/aiplanet\\\/\",\"https:\\\/\\\/www.youtube.com\\\/@aiplanet\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/#\\\/schema\\\/person\\\/b121996cdbe68ef54fbc4683ff368a27\",\"name\":\"Adarsh Menon\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c60cd7eab96e2afd47ba36714163d1f75f4f470b5a5da376a761e1a0f74b12bf?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c60cd7eab96e2afd47ba36714163d1f75f4f470b5a5da376a761e1a0f74b12bf?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c60cd7eab96e2afd47ba36714163d1f75f4f470b5a5da376a761e1a0f74b12bf?s=96&d=mm&r=g\",\"caption\":\"Adarsh Menon\"},\"description\":\"Adarsh is a tech &amp; data science enthusiast. In his own words, \u201cI make websites and teach machines to predict stuff. I also make YouTube videos \u2014 https:\\\/\\\/www.youtube.com\\\/adarshmenon\\\"\",\"url\":\"https:\\\/\\\/aiplanet.com\\\/blog\\\/author\\\/adarsh_menon\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Logistic Regression in Python Explained - Gradient Descent | Free Tutorial","description":"Logistic regression is used to model the probability of a certain class or event. We'll be focusing more on the basics and implementation of the model.","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:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/","og_locale":"en_US","og_type":"article","og_title":"Logistic Regression in Python Explained - Gradient Descent | Free Tutorial","og_description":"Logistic regression is used to model the probability of a certain class or event. We'll be focusing more on the basics and implementation of the model.","og_url":"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/","og_site_name":"AI Planet","article_publisher":"https:\/\/www.facebook.com\/aiplanet\/","article_published_time":"2020-04-12T07:55:36+00:00","article_modified_time":"2022-04-04T04:24:36+00:00","og_image":[{"url":"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/LR_1.gif","type":"","width":"","height":""}],"author":"Adarsh Menon","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Adarsh Menon","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/#article","isPartOf":{"@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/"},"author":{"name":"Adarsh Menon","@id":"https:\/\/aiplanet.com\/blog\/#\/schema\/person\/b121996cdbe68ef54fbc4683ff368a27"},"headline":"Tutorial on Logistic Regression using Gradient Descent with Python","datePublished":"2020-04-12T07:55:36+00:00","dateModified":"2022-04-04T04:24:36+00:00","mainEntityOfPage":{"@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/"},"wordCount":1560,"commentCount":4,"publisher":{"@id":"https:\/\/aiplanet.com\/blog\/#organization"},"image":{"@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/#primaryimage"},"thumbnailUrl":"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/LR_1.gif","keywords":["datascience","logisctic regression","machine learning","python"],"articleSection":["datascience"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/","url":"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/","name":"Logistic Regression in Python Explained - Gradient Descent | Free Tutorial","isPartOf":{"@id":"https:\/\/aiplanet.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/#primaryimage"},"image":{"@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/#primaryimage"},"thumbnailUrl":"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/LR_1.gif","datePublished":"2020-04-12T07:55:36+00:00","dateModified":"2022-04-04T04:24:36+00:00","description":"Logistic regression is used to model the probability of a certain class or event. We'll be focusing more on the basics and implementation of the model.","breadcrumb":{"@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/#primaryimage","url":"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/LR_1.gif","contentUrl":"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2020\/04\/LR_1.gif"},{"@type":"BreadcrumbList","@id":"https:\/\/aiplanet.com\/blog\/tutorial-on-logistic-regression-using-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/aiplanet.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Tutorial on Logistic Regression using Gradient Descent with Python"}]},{"@type":"WebSite","@id":"https:\/\/aiplanet.com\/blog\/#website","url":"https:\/\/aiplanet.com\/blog\/","name":"AI Planet, Formerly DPhi","description":"AI Planet (formerly DPhi) is a global ecosystem that aims to educate and empower individuals and organizations to build and utilize artificial intelligence (AI). With a community of over 300,000 AI specialists and enthusiasts, we offer a range of products and have helped drive innovative solutions for various industries. Our ultimate goal is to make AI education accessible to everyone and use AI to tackle important business and social challenges.","publisher":{"@id":"https:\/\/aiplanet.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/aiplanet.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/aiplanet.com\/blog\/#organization","name":"AI Planet, Formerly DPhi","url":"https:\/\/aiplanet.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/aiplanet.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2022\/11\/Size48-Large-1.png","contentUrl":"https:\/\/aiplanet.com\/blog\/wp-content\/uploads\/2022\/11\/Size48-Large-1.png","width":48,"height":48,"caption":"AI Planet, Formerly DPhi"},"image":{"@id":"https:\/\/aiplanet.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/aiplanet\/","https:\/\/www.instagram.com\/aiplanet_community\/","https:\/\/www.linkedin.com\/company\/aiplanet\/","https:\/\/www.youtube.com\/@aiplanet"]},{"@type":"Person","@id":"https:\/\/aiplanet.com\/blog\/#\/schema\/person\/b121996cdbe68ef54fbc4683ff368a27","name":"Adarsh Menon","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c60cd7eab96e2afd47ba36714163d1f75f4f470b5a5da376a761e1a0f74b12bf?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c60cd7eab96e2afd47ba36714163d1f75f4f470b5a5da376a761e1a0f74b12bf?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c60cd7eab96e2afd47ba36714163d1f75f4f470b5a5da376a761e1a0f74b12bf?s=96&d=mm&r=g","caption":"Adarsh Menon"},"description":"Adarsh is a tech &amp; data science enthusiast. In his own words, \u201cI make websites and teach machines to predict stuff. I also make YouTube videos \u2014 https:\/\/www.youtube.com\/adarshmenon\"","url":"https:\/\/aiplanet.com\/blog\/author\/adarsh_menon\/"}]}},"_links":{"self":[{"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/posts\/417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/comments?post=417"}],"version-history":[{"count":31,"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/posts\/417\/revisions"}],"predecessor-version":[{"id":2852,"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/posts\/417\/revisions\/2852"}],"wp:attachment":[{"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/media?parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/categories?post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aiplanet.com\/blog\/wp-json\/wp\/v2\/tags?post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}