{"id":13551,"date":"2020-03-23T15:48:09","date_gmt":"2020-03-23T10:18:09","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/"},"modified":"2024-10-24T19:11:39","modified_gmt":"2024-10-24T13:41:39","slug":"logistic-regression-with-examples-in-python-and-r","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/","title":{"rendered":"Logistic Regression With Examples in Python and R"},"content":{"rendered":"\n<p>In every algorithm of machine learning, there is an approach that is unique yet easily interpretable. Logistic regression is one such algorithm with an easy and unique approach. It is very often used in the credit and risk industry for its easy intuition on predicting the chances of default and risk cases. It is indeed quite a challenge to break down most of the algorithms due to their black-box nature and their hard to find parameters, but logistic regression outperforms all. So it is time to break down the entire algorithm and draw some inferences.<\/p>\n\n\n\n<p>We can say that logistic regression solely depends on maximum likelihood estimation. Now let us focus on this and after that, we will jump back to the core concepts including parameters. Learn more about the <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/logistic-regression-algorithm\" target=\"_blank\" rel=\"noreferrer noopener\">Logistic regression algorithm<\/a> here.<\/p>\n\n\n\n<p><em>Maximum likelihood estimation maximises the probability that classifies the event being 1 or 0 by estimating certain parameters. Every probability equation goes by the following.<\/em><\/p>\n\n\n\n<p class=\"has-text-align-center\"><em><strong>probability (event + non-event) = 1<\/strong><\/em><\/p>\n\n\n\n<p>Logistic regression is capable of finding out the probability only after transforming the dependent variable into a logit variable with respect to the independent variable or the features present in the data.<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg1-2.png\"><img decoding=\"async\" width=\"626\" height=\"96\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg1-2.png\" alt=\"\" class=\"wp-image-13552\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg1-2.png 626w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg1-2-300x46.png 300w\" sizes=\"(max-width: 626px) 100vw, 626px\" \/><\/figure>\n\n\n\n<p>The reason being using log function is to increase the range of probability of classification of events. Let us explain how it works.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><em>Range of Probability = [0,1]<\/em><\/p>\n\n\n\n<p class=\"has-text-align-center\"><em>Range of Odds = [0, + \u221e] as Odds = <\/em>(Probability)\/(1-Probability)<\/p>\n\n\n\n<p class=\"has-text-align-center\"><em>Range of log(odds) = [- \u221e, + \u221e]<\/em><\/p>\n\n\n\n<p>Here as we can see how the range of [0,1] increased to [-,+] which is what we wanted.<\/p>\n\n\n\n<p>Now the entire mechanism behind the logistic regression has been covered, let us break further to the terminologies involved in it.<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Advance Data Science with MIT<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/idss-gl.mit.edu\/mit-idss-data-science-machine-learning-online-program\" class=\"courses-cta-title-link\">MIT Data Science and Machine Learning Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Unlock the power of data. Build hands-on data science and machine learning skills to drive innovation in your career.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>Duration: 12 weeks<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>4.62\/5 Rating<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/idss-gl.mit.edu\/mit-idss-data-science-machine-learning-online-program\" class=\"courses-cta-button\">\n                Discover the Program\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"information-value\"><strong>Information Value<\/strong><\/h2>\n\n\n\n<p>This parameter is considered even before building the model. This is useful in eliminating those variables which seem to be weak for prediction and is considered a weak contributor to the dependent variable. Let us understand the following equation.<\/p>\n\n\n<figure class=\"wp-block-image aligncenter size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg-2-2.png\"><img decoding=\"async\" width=\"489\" height=\"104\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg-2-2.png\" alt=\"\" class=\"wp-image-13553\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg-2-2.png 489w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg-2-2-300x64.png 300w\" sizes=\"(max-width: 489px) 100vw, 489px\" \/><\/figure>\n\n\n\n<p>Now this equation seems to be tricky at first, so let us go with an example and get a clear idea of how it works.<\/p>\n\n\n\n<p>The bins given above are the price range followed by the events and non-events. After we calculated the information value, we found a value of 0.0356 which is 3.5% and we can say it is a weak predictor as it contributes only 3.5% to the dependent variable.<\/p>\n\n\n\n<p>Let us see the predictive power of the variables based on their information value<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Information Value<\/strong><\/td><td><strong>Predictive Power<\/strong><\/td><\/tr><tr><td><strong>&lt; 0.02<\/strong><\/td><td>Useless of prediction<\/td><\/tr><tr><td><strong>0.02 - 0.1<\/strong><\/td><td>Weak Predictor<\/td><\/tr><tr><td><strong>0.1 - 0.3<\/strong><\/td><td>Medium Predictor<\/td><\/tr><tr><td><strong>0.3 \u2013 0.5<\/strong><\/td><td>Strong Predictor<\/td><\/tr><tr><td>&gt;<strong> 0.5<\/strong><\/td><td>Suspicious or too good Predictor<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">Also Read: <a href=\"https:\/\/www.mygreatlearning.com\/blog\/linear-regression-in-machine-learning\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Linear Regression in Machine Learning (opens in a new tab)\">Linear Regression in Machine Learning<\/a><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"akaike-information-criteria\"><strong>Akaike Information Criteria<\/strong><\/h2>\n\n\n\n<p>This parameter helps us to find the relatedness of our model by acting as a tradeoff between bias and variance.<\/p>\n\n\n\n<p>Key point - The lower the AIC the better is the model.<\/p>\n\n\n\n<p>AIC can be calculated by the following equation&nbsp;<\/p>\n\n\n\n<p class=\"has-text-align-center\"><em>AIC = -2*ln(L) + 2*k<\/em><\/p>\n\n\n\n<p class=\"has-text-align-center\"><em>L = Maximum value of Likelihood (log transformation applied for mathematical convenience)<\/em><\/p>\n\n\n\n<p class=\"has-text-align-center\"><em>k = number of variables in the model<\/em><\/p>\n\n\n\n<p>Here the parameter k is used to penalise the overfitting issue in a model.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"roc-curve\"><strong>ROC Curve<\/strong><\/h2>\n\n\n\n<p>ROC or Receiver Operating Characteristics curve is a graphical representation of the performance of a binary classification model.&nbsp; It shows the variation between True positive and False positive rate at different threshold values.<\/p>\n\n\n\n<p>The ROC curve can give us a clear idea to set a threshold value to classify the label and also help in model optimisation.<\/p>\n\n\n\n<p>A low threshold value we will put most of the predicted observations under the positive category, even when some of them should be placed under the negative category. On the other hand, keeping the threshold at a very high level penalises the positive category, but the negative category will improve.<\/p>\n\n\n\n<p>For such case an optimum threshold value can give a better accuracy which can be found on ROC curve<\/p>\n\n\n\n<p>ROC curve will look as follow:<\/p>\n\n\n\n<p>To gain more information on sensitivity and specificity, we need to go through the following formula.<\/p>\n\n\n\n<p><em>Recall (Sensitivity or TPR) = <\/em>TP\/(TP+FN)<\/p>\n\n\n\n<p><em>Specificity (TNR) = <\/em>TN\/(FP+TN)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"evaluation-parameters\"><strong>Evaluation parameters<\/strong><\/h3>\n\n\n\n<p>Following are the evaluation parameters considered in logistic regression<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Precision<\/li>\n\n\n\n<li>Recall<\/li>\n\n\n\n<li>F1-Score<\/li>\n\n\n\n<li>ROC curve<\/li>\n<\/ol>\n\n\n\n<p>All the above parameters are considered on positive and negative rates of the classes which are tabulated below<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><br><\/td><td><strong>Predicted: YES<\/strong><\/td><td><strong>Predicted: NO<\/strong><\/td><\/tr><tr><td><strong>Actual: YES<\/strong><\/td><td>TP<br>( True Positive)<\/td><td>FN<br>( True Positive)<\/td><\/tr><tr><td><strong>Actual: NO<\/strong><\/td><td>FP<br>( True Positive)<\/td><td>TN<br>( True Positive)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>(a) Precision:<\/strong> Precision is an evaluation measure which is the combination of relevant as well as retrieved items over the total number of retrieved results. It is basically used when the case of false-positive prediction is high.<\/p>\n\n\n\n<p><em>Precision (TNR) = <\/em>TPTP+FP<\/p>\n\n\n\n<p><strong>b) Recall: <\/strong>Recall is a measure when False negative is considered.<\/p>\n\n\n\n<p><em>Recall (Sensitivity or TPR) = <\/em>TP\/(TP+FN)<\/p>\n\n\n\n<p><strong>c) ROC Curve <\/strong>(Explained above)<\/p>\n\n\n\n<p><strong>d)F1-Score:<\/strong> F1-Score is an evaluation technique that maintains a balance between precision and recall.<\/p>\n\n\n\n<p><em>F1 Score = <\/em>2 * (Precision*Recall)\/(Precision+Recall)<\/p>\n\n\n\n<p>Now in some cases precision and recall can be a dangerous call. Let us understand that further.<\/p>\n\n\n\n<p>Precision and recall are two of the evaluation techniques used in a <a aria-label=\"machine learning (opens in a new tab)\" href=\"https:\/\/www.mygreatlearning.com\/blog\/what-is-machine-learning\/\" target=\"_blank\" rel=\"noreferrer noopener\">machine learning<\/a> model which tells us how better a model can perform. But there are certain business domains where these two evaluation techniques can cause a business risk.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"medical-industry\"><strong>Medical Industry<\/strong><\/h3>\n\n\n\n<p>Let us suppose we are analysing the records of patients suffering from cancer and we are trying to predict whether the upcoming patients will suffer from cancer or not. Now, the precision is considered when the false positive rates are high which means the patient is not suffering from cancer but the machine predicts as a cancer patient.<\/p>\n\n\n\n<p>In case of recall, false negatives are high which means the patient is suffering from cancer and the model predicts the patient as cancer-free.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Cancer patients<\/strong><\/td><td><strong>Predicted: Yes (Having cancer)<\/strong><\/td><td><strong>Predicted: No (Not having cancer)<\/strong><\/td><\/tr><tr><td><strong>Actual: Yes (Having cancer)<\/strong><\/td><td><strong>90<\/strong><\/td><td><strong>10<\/strong><\/td><\/tr><tr><td><strong>Actual: No (Not having cancer)<\/strong><\/td><td><strong>80<\/strong><\/td><td><strong>20<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Accuracy of the model= (90+20)\/200=0.55<\/p>\n\n\n\n<p>Precision of the model=90\/(90+80)=0.53<\/p>\n\n\n\n<p>Recall of the model=90\/(90+10)=0.9<\/p>\n\n\n\n<p>Now the model is 55% accurate but if we see the recall, then it is 90% accurate. But if we consider recall as accuracy then it will declare many patients as cancer-free although they are suffering from cancer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"banking-sector\"><strong>Banking Sector<\/strong><\/h3>\n\n\n\n<p>In the banking sector, let us assume building a model to check whether a customer is eligible for a loan or not. Let us again build a model on 200 customers and evaluate based on accuracy, precision and recall. Check out this <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/logistic-regression-on-customer-data\" target=\"_blank\" rel=\"noreferrer noopener\">Logistic regression on customer data<\/a><\/p>\n\n\n\n<p> course.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Customer<\/strong><\/td><td><strong>Predicted: Yes (Eligible for a loan)<\/strong><\/td><td><strong>Predicted: No (Not eligible)<\/strong><\/td><\/tr><tr><td><strong>Actual: Yes (Eligible for a loan)<\/strong><\/td><td><strong>80<\/strong><\/td><td><strong>20<\/strong><\/td><\/tr><tr><td><strong>Actual: No (Not eligible)<\/strong><\/td><td><strong>8<\/strong><\/td><td><strong>92<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Accuracy of the model= (80+92)\/200=0.86<\/p>\n\n\n\n<p>Precision of the model=80\/(80+8)=0.9<\/p>\n\n\n\n<p>Recall of the model=80\/(80+20)=0.8<\/p>\n\n\n\n<p>Now the model is 86% accurate but if we consider the precision we see it is 90%. Now if we evaluate the model by precision, then there is a high chance that a person is not eligible for loan but the bank is sanctioning loan for them. In that case, the bank may incur huge losses.<\/p>\n\n\n\n<p>Precision works fine when false-positive cases are high. On the other hand, recall works fine when false-negative cases are high. Many domains such as medical, educational or legal fields are delicate, where evaluating entities with such measures can cost more risk. In such cases, F1-Score performs better as it is the only measure that maintains a balance between precision and recall.<\/p>\n\n\n\n<p>F1-Score is the harmonic mean of precision and recall. Multiplying the constant of <em>2 <\/em>scales the score to <em>1 <\/em>when both precision and recall are <em>1<\/em>.<\/p>\n\n\n\n<p>In such cases, F1-score can be a good evaluation technique because it maintains a balance between precision and recall and can tell almost exactly whether a person is eligible for a loan or not.<\/p>\n\n\n\n<p>The best evaluation technique is accuracy which can tell us the best prediction because it gives the ratio of true prediction to total actual values.<\/p>\n\n\n\n<p><em>Accuracy = <\/em>TP+TNTP+FN+TN+FP<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"merits-of-logistic-regression\"><strong>Merits of Logistic Regression<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Logistic Regression has less chance of overfitting<\/li>\n\n\n\n<li>It is much easier to implement compared to other algorithms<\/li>\n\n\n\n<li>Tuning of parameters is not required much<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"demerits-of-logistic-regression\"><strong>Demerits<\/strong> <strong>of Logistic Regression<\/strong> <\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>They fail to play good in large datasets<\/li>\n\n\n\n<li>The algorithm only works fine in linearly separable data<\/li>\n\n\n\n<li>They are not flexible with continuous data<\/li>\n<\/ul>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Master Gen AI Skills<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/online.lifelonglearning.jhu.edu\/jhu-certificate-program-applied-generative-ai\" class=\"courses-cta-title-link\">Certificate Program in Applied Generative AI<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Master the tools and techniques behind generative AI with expert-led, project-based training from Johns Hopkins University.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>Duration: 16 weeks<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>Weekly Live Sessions<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/online.lifelonglearning.jhu.edu\/jhu-certificate-program-applied-generative-ai\" class=\"courses-cta-button\">\n                Discover the Program\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"applications-of-logistic-regression\"><strong>Applications<\/strong> <strong>of Logistic Regression<\/strong> <\/h2>\n\n\n\n<p>Logistic regression is a well-applied algorithm that is widely used in many sectors. Some of them are:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"medical-sector\"><strong>Medical sector<\/strong><\/h3>\n\n\n\n<p>Logistic regression is mostly used to analyse the risk of patients suffering from various diseases. Also, it can predict the risk of various diseases that are difficult to treat.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"banking-sector\"><strong>Banking sector<\/strong><\/h3>\n\n\n\n<p>Logistic regression is one of the most used algorithms in banking sectors as we can set various threshold values to expect the probabilities of a person eligible for loan or not. Also, they play a huge role in analysing credit and risk of fraudulent activities in the industry.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-of-logistic-regression-in-python\"><strong>Example of Logistic Regression in Python<\/strong><\/h2>\n\n\n\n<p>Now let us take a case study in Python. We will be taking data from social network ads which tell us whether a person will purchase the ad or not based on the features such as age and salary.<\/p>\n\n\n\n<p>First, we will import all the libraries:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\n<\/code><\/pre>\n\n\n\n<p>Now we will import the dataset and select only age and salary as the features<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dataset = pd.read_csv('Social_Network_Ads.csv')\ndataset.head()<\/code><\/pre>\n\n\n<figure class=\"wp-block-image aligncenter size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg3-2.png\"><img decoding=\"async\" width=\"589\" height=\"437\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg3-2.png\" alt=\"\" class=\"wp-image-13554\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg3-2.png 589w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg3-2-300x223.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg3-2-566x420.png 566w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg3-2-80x60.png 80w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg3-2-265x198.png 265w\" sizes=\"(max-width: 589px) 100vw, 589px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>X = dataset.iloc&#091;:, &#091;2, 3]].values\ny = dataset.iloc&#091;:, 4].values\n\n\n<\/code><\/pre>\n\n\n\n<p>Now we will perform splitting for training and testing. We will take 75% of the data for training, and test on the remaining data<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.model_selection  import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)<\/code><\/pre>\n\n\n\n<p>Next, we scale the features to avoid variation and let the features follow a normal distribution<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.preprocessing import StandardScaler\nsc = StandardScaler()\nX_train = sc.fit_transform(X_train)\nX_test = sc.transform(X_test)<\/code><\/pre>\n\n\n\n<p>The preprocessing part is over. It is time to fit the model<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.linear_model import LogisticRegression\nclassifier = LogisticRegression(random_state = 0)\nclassifier.fit(X_train, y_train)\n<\/code><\/pre>\n\n\n\n<p>We fitted the model on training data. We will predict the labels of test data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>y_pred = classifier.predict(X_test)<\/code><\/pre>\n\n\n\n<p>The prediction is over. Now we will evaluate the performance of our model.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>From sklearn.metrics import confusion_matrix,classification_report\ncm = confusion_matrix(y_test, y_pred)<\/code><\/pre>\n\n\n<figure class=\"wp-block-image aligncenter size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg4-2.png\"><img decoding=\"async\" width=\"212\" height=\"84\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg4-2.png\" alt=\"\" class=\"wp-image-13555\"><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>cl_report=classification_report(y_test,y_pred)<\/code><\/pre>\n\n\n<figure class=\"wp-block-image aligncenter size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg-5-2.png\"><img decoding=\"async\" width=\"381\" height=\"254\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg-5-2.png\" alt=\"\" class=\"wp-image-13556\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg-5-2.png 381w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg-5-2-300x200.png 300w\" sizes=\"(max-width: 381px) 100vw, 381px\" \/><\/figure>\n\n\n\n<p>Now we can see that our model performed really good based on precision and recall.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-of-logistic-regression-in-r\"><strong>Example of Logistic Regression in R&nbsp;<\/strong><\/h2>\n\n\n\n<p>We will perform the application in R and look into the performance as compared to Python<\/p>\n\n\n\n<p>First, we will import the dataset<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dataset = read.csv('Social_Network_Ads.csv')\nWe will select only Age and Salary \ndataset = dataset &#091;3:5]\n<\/code><\/pre>\n\n\n\n<p>Now we will encode the target variable as a factor<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dataset$Purchased = factor (dataset$Purchased, levels = c (0, 1))<\/code><\/pre>\n\n\n\n<p>Next, we will split the data into training and testing.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># install.packages('caTools')\nlibrary(caTools)\nset.seed(123)\nsplit = sample.split(dataset$Purchased, SplitRatio = 0.75)\ntraining_set = subset (dataset, split == TRUE)\ntest_set = subset (dataset, split == FALSE)<\/code><\/pre>\n\n\n\n<p>Then we will perform scaling only on the feature variables<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>training_set&#091;-3] = scale(training_set&#091;-3])\ntest_set&#091;-3] = scale(test_set&#091;-3])<\/code><\/pre>\n\n\n\n<p>After preprocessing is done, we will fit the model and get ready for prediction<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Fitting Logistic Regression to the Training set\nclassifier = glm(formula = Purchased ~ .,\n                 family = binomial,\n                 data = training_set)<\/code><\/pre>\n\n\n\n<p>The fitting is done. Now we predict the values by keeping the threshold as 0.5. It means that the probability above 0.5 is counted as 1 and the rest as 0.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>prob_pred = predict (classifier, type = 'response', newdata = test_set&#091;-3])\ny_pred = ifelse (prob_pred &gt; 0.5, 1, 0)<\/code><\/pre>\n\n\n\n<p>Now we will evaluate our model based on the confusion matrix and make a comparison with Python.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cm = table (test_set&#091;, 3], y_pred &gt; 0.5)<\/code><\/pre>\n\n\n<figure class=\"wp-block-image aligncenter size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg6-2.png\"><img decoding=\"async\" width=\"156\" height=\"80\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/logireg6-2.png\" alt=\"\" class=\"wp-image-13557\"><\/figure>\n\n\n\n<p>As we see the rate of misclassification is much higher in R than in Python. But we can only challenge it when the data splitting will be the same in both cases.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Logistic Regression works fine only when the target variable is discrete in nature. They do not have the flexibility to act as regression analysis. Also, they have less chance of overfitting but in data having a higher dimension, logistic can overfit. For such cases, there are regularising techniques called L1 and L2 which shrink the coefficients of the algorithm to avoid overfitting.&nbsp;Logistic regression can also work fine with the discretised data as they do not follow a decision-based approach. Learn more with the help of a <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/logistic-regression\" target=\"_blank\" rel=\"noreferrer noopener\">logistic regression course<\/a>. Join now!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In every algorithm of machine learning, there is an approach that is unique yet easily interpretable. Logistic regression is one such algorithm with an easy and unique approach. It is very often used in the credit and risk industry for its easy intuition on predicting the chances of default and risk cases. It is indeed [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":13558,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[2],"tags":[36799],"content_type":[],"class_list":["post-13551","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence","tag-machine-learning"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Logistic Regression examples in python &amp; R<\/title>\n<meta name=\"description\" content=\"Logistic Regression examples: Logistic Regression is one such Machine Learning algorithm with an easy and unique approach. Read this article to know how it is applied in Python and R.\" \/>\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.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Logistic Regression With Examples in Python and R\" \/>\n<meta property=\"og:description\" content=\"Logistic Regression examples: Logistic Regression is one such Machine Learning algorithm with an easy and unique approach. Read this article to know how it is applied in Python and R.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-23T10:18:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-24T13:41:39+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"667\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/logistic-regression-with-examples-in-python-and-r\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/logistic-regression-with-examples-in-python-and-r\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Logistic Regression With Examples in Python and R\",\"datePublished\":\"2020-03-23T10:18:09+00:00\",\"dateModified\":\"2024-10-24T13:41:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/logistic-regression-with-examples-in-python-and-r\\\/\"},\"wordCount\":1877,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/logistic-regression-with-examples-in-python-and-r\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/shutterstock_1046205154.jpg\",\"keywords\":[\"Machine Learning\"],\"articleSection\":[\"AI and Machine Learning\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/logistic-regression-with-examples-in-python-and-r\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/logistic-regression-with-examples-in-python-and-r\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/logistic-regression-with-examples-in-python-and-r\\\/\",\"name\":\"Logistic Regression examples in python & R\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/logistic-regression-with-examples-in-python-and-r\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/logistic-regression-with-examples-in-python-and-r\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/shutterstock_1046205154.jpg\",\"datePublished\":\"2020-03-23T10:18:09+00:00\",\"dateModified\":\"2024-10-24T13:41:39+00:00\",\"description\":\"Logistic Regression examples: Logistic Regression is one such Machine Learning algorithm with an easy and unique approach. Read this article to know how it is applied in Python and R.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/logistic-regression-with-examples-in-python-and-r\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/logistic-regression-with-examples-in-python-and-r\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/logistic-regression-with-examples-in-python-and-r\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/shutterstock_1046205154.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/shutterstock_1046205154.jpg\",\"width\":1000,\"height\":667,\"caption\":\"logistic regression\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/logistic-regression-with-examples-in-python-and-r\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"AI and Machine Learning\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/artificial-intelligence\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Logistic Regression With Examples in Python and R\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Logistic Regression examples in python & R","description":"Logistic Regression examples: Logistic Regression is one such Machine Learning algorithm with an easy and unique approach. Read this article to know how it is applied in Python and R.","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.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/","og_locale":"en_US","og_type":"article","og_title":"Logistic Regression With Examples in Python and R","og_description":"Logistic Regression examples: Logistic Regression is one such Machine Learning algorithm with an easy and unique approach. Read this article to know how it is applied in Python and R.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2020-03-23T10:18:09+00:00","article_modified_time":"2024-10-24T13:41:39+00:00","og_image":[{"width":1000,"height":667,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Logistic Regression With Examples in Python and R","datePublished":"2020-03-23T10:18:09+00:00","dateModified":"2024-10-24T13:41:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/"},"wordCount":1877,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154.jpg","keywords":["Machine Learning"],"articleSection":["AI and Machine Learning"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/","url":"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/","name":"Logistic Regression examples in python & R","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154.jpg","datePublished":"2020-03-23T10:18:09+00:00","dateModified":"2024-10-24T13:41:39+00:00","description":"Logistic Regression examples: Logistic Regression is one such Machine Learning algorithm with an easy and unique approach. Read this article to know how it is applied in Python and R.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154.jpg","width":1000,"height":667,"caption":"logistic regression"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/logistic-regression-with-examples-in-python-and-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"AI and Machine Learning","item":"https:\/\/www.mygreatlearning.com\/blog\/artificial-intelligence\/"},{"@type":"ListItem","position":3,"name":"Logistic Regression With Examples in Python and R"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154.jpg",1000,667,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154-768x512.jpg",768,512,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154.jpg",1000,667,false],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154.jpg",1000,667,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154.jpg",1000,667,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154.jpg",640,427,false],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154.jpg",96,64,false],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/03\/shutterstock_1046205154.jpg",150,100,false]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"In every algorithm of machine learning, there is an approach that is unique yet easily interpretable. Logistic regression is one such algorithm with an easy and unique approach. It is very often used in the credit and risk industry for its easy intuition on predicting the chances of default and risk cases. It is indeed&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/13551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=13551"}],"version-history":[{"count":14,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/13551\/revisions"}],"predecessor-version":[{"id":110558,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/13551\/revisions\/110558"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/13558"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=13551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=13551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=13551"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=13551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}