Learn practical skills, build real-world projects, and advance your career

Linear regression is unbounded,it has infinite possibilities and can be used only when the response variable is continuous, and this brings logistic regression to frame.Logistic Regression is used to describe data and to explain the relationship between dependent categorical variable with one or more nominal, ordinal, interval or ratio independent variable. Here categorical variable represent the type of data which may be divided into groups or levels. Example marital status, gender these questions might have answer yes/no or m/f in case of gender.

Types of Logistic Regression:-

  1. Binary Logistic Regression - It deals with categorical variables with two possible outcomes.

  2. Multi-nominal Logistic Regression - It deals with categorical variables with three or more nominal categories.

  3. Ordinal Logistic Regression - It deals with categorical variables with three or more ordinal categories. Ordinal means the categories will be in order. Example:- Rating(1-5).

Logistic regression model is similar to the Linear Regression model, that is, there are weights and biases and out put is obtained using simple matrix operations. For this notebook we would be considering the MNIST dataset, which consists of 28px by 28 px gray-scale images of hand-written digits(0 to 9) along with labels for each image, indicating which digit it represents. It contains 60000 images which is used to train the model.

import torch
import torchvision
from torchvision.datasets import MNIST

dataset = MNIST(root='data/', download=True)
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to data/MNIST/raw/train-images-idx3-ubyte.gz
HBox(children=(FloatProgress(value=1.0, bar_style='info', max=1.0), HTML(value='')))
Extracting data/MNIST/raw/train-images-idx3-ubyte.gz to data/MNIST/raw Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to data/MNIST/raw/train-labels-idx1-ubyte.gz
HBox(children=(FloatProgress(value=1.0, bar_style='info', max=1.0), HTML(value='')))
Extracting data/MNIST/raw/train-labels-idx1-ubyte.gz to data/MNIST/raw Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to data/MNIST/raw/t10k-images-idx3-ubyte.gz
HBox(children=(FloatProgress(value=1.0, bar_style='info', max=1.0), HTML(value='')))
Extracting data/MNIST/raw/t10k-images-idx3-ubyte.gz to data/MNIST/raw Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to data/MNIST/raw/t10k-labels-idx1-ubyte.gz
HBox(children=(FloatProgress(value=1.0, bar_style='info', max=1.0), HTML(value='')))
Extracting data/MNIST/raw/t10k-labels-idx1-ubyte.gz to data/MNIST/raw Processing... Done!
/pytorch/torch/csrc/utils/tensor_numpy.cpp:141: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program.

In the above code block we import torchvision which consists of popular datasets, model architectures and common image transformations for computer vision. It contains utilities to download and import popular datasets.

As we can see when the statement is executed for the first time, it downloads the data to the data/ directory next to the notebook and creates a PyTorch Dataset. On subsequent executions, the download is skipped as the data is already downloaded.

len(dataset)
60000