Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Logistic Regression

This notebook will introduce you to the Logistic Regression algorithm and how to implement it in Python.

Practice

Loading...
Loading...

Dataset

In this implementation, we are going to use Pima Indians Diabetes Database.

Loading...
Loading...
Loading...

There are 268 patients that have diabetes and 500 patients that do not have diabetes.

Now, we split the data into training and testing sets. We will use 80% of the data for training and 20% for testing. By using function train_test_split from sklearn.model_selection, we can split the data easily.

To keep the ratio of the classes in the training and testing sets, we use the parameter stratify=y in the train_test_split function.

(614, 8) (154, 8) (614,) (154,)

Next, we will standardize the data using z-score normalization. In z-score normalization, the values are standardized based on the mean and standard deviation of the data. The formula for z-score normalization is given by:

z=x−μσz = \frac{x - \mu}{\sigma}

where xx is the original feature vector, μ\mu is the mean of the feature vector, and σ\sigma is the standard deviation of the feature vector.

Data standardization is a common requirement/technique in data preprocessing and it is important. It ensures that each feature contributes approximately proportionately to the final distance. Besides that, many machine learning algorithms perform better or converge faster when features are on a relatively similar scale and/or close to normally distributed.

Now, for each feature, we calculate the mean and standard deviation of the training set. Then, we use these values to standardize both the training and testing sets.

Loading...

Plot the training loss and accuracy.

Source
Loading...
Loading...

Now, we evaluate the model on the testing set. We calculate the accuracy, precision, recall, and F1-score of the model.

Test Accuracy: 70.78%
              precision    recall  f1-score   support

           0       0.74      0.85      0.79       100
           1       0.62      0.44      0.52        54

    accuracy                           0.71       154
   macro avg       0.68      0.65      0.65       154
weighted avg       0.70      0.71      0.69       154

Source
Loading...
Loading...