Skip to content Skip to sidebar Skip to footer

Logistic Regression Implementation Python Code Download Only Using Loops UPDATED

Logistic Regression Implementation Python Code Download Only Using Loops

This article was published as a part of the Data Scientific discipline Blogathon

riables. For example, if you desire to predict the selling price of a business firm based on foursquare footage, area of location, no of rooms, etc, you can apply Linear Regression. But in some cases, nosotros don't desire to predict a number, but nosotros want to allot a category. For example, given the medical data data of a population, nosotros desire to classify them into cancer-affected patients or salubrious people.

These are 2 classes. Here, we use logistic regression. Other examples include email classification as spam/not spam etc.

 Logistic Regression with Pytorch 1

Source

In curt, the target variable we desire to predict in logistic regression is non continuous, it's discrete. An example where logistic regression can be applied is email classification: Identity equally Spam or non spam. Image classification, text classification all fall into the category.

I assume y'all are familiar with implementing logistic regression using the sklearn library. In this blog, we shall meet how to implement logistic regression in PyTorch. This will help you if yous trying to acquire PyTorch. I'll cover what are the steps to solve a problem in PyTorch while explaining every term.

Grab a coffee and charge up to de-mystify Pytorch!

ane. Dataset loading

Here, I'll exist using the Breast cancer dataset from the sklearn library. This is a unproblematic binary class classification dataset. You can load it from sklearn.datasets module. Next, you lot tin can excerpt X and Y from the dataset using the inbuilt role every bit shown beneath.

from sklearn import datasets breast_cancer=datasets.load_breast_cancer() x,y=iris.data,iris.target

x is the data with all features, while y has the target or the course. Next, we should find the size of the dataset and what is the no of features in x. The beneath code executes it in one line.

n_samples,n_features=ten.shape print(n_samples) print(n_features) #> 569  #> xxx

And then, we know the dataset has 569 samples and 30 features. Side by side, we will divide it into training and testing batches using sklearn's "train_test_split". This can be imported from the model selection module.

from sklearn.model_selection import train_test_split x_train,x_test,y_train,y_test= train_test_split(ten,y,test_size=0.ii)

In the above code, test size denotes the fraction of data you want to utilise as a test dataset. And then, eighty% for preparation and twenty% for testing.

two. Preprocessing

Since this is a classification problem, a expert preprocessing step would be to apply standard scaler transformation.

scaler=sklearn.preprocessing.StandardScaler() x_train=scaler.fit_transform(x_train) x_test=scaler.fit_transform(x_test)

Now, there is a last and crucial step of data processing earlier moving to models. Pytorch works with tensors. And so, we catechumen all four into tensors using the "torch.from_numpy()" method. It is important to convert the datatype into float32 before this. You can do that using "astype()" function.

import numpy equally np x_train=torch.from_numpy(x_train.astype(np.float32)) x_test=torch.from_numpy(x_test.astype(np.float32)) y_train=torch.from_numpy(y_train.astype(np.float32)) y_test=torch.from_numpy(y_test.astype(np.float32))

Merely, we know that y has to exist in the form of a cavalcade tensor and not a row tensor. We perform this alter using the "view" performance as shown in the code. Practice the same operation for y_test also.

y_train=y_train.view(y_train.shape[0],1) y_test=y_test.view(y_test.shape[0],i)

The preprocessing steps are completed, yous can move alee to model building.

3.Model Building

Now, we have the input information set up. Let'southward see how to write a custom model in PyTorch for logistic regression. The start footstep would exist to define a class with the model proper noun. This class should derive torch.nn.Module.

Inside the class, nosotros have the __init__ function and forward function.

class Logistic_Reg_model(torch.nn.Module):  def __init__(self,no_input_features):    super(Logistic_Reg_model,self).__init__()    self.layer1=torch.nn.Linear(no_input_features,20)    self.layer2=torch.nn.Linear(twenty,i)  def forward(self,x):    y_predicted=cocky.layer1(x)    y_predicted=torch.sigmoid(cocky.layer2(y_predicted))    render y_predicted

In the __init__ method, you accept to define the layers yous want in your model. Here, nosotros use Linear layers, which can be alleged from the torch.nn module. You tin give any proper noun to the layer, like "layer1" in this case. So, I have declared 2 linear layers.

The syntax is: torch.nn.Linear(in_features, out_features, bias=True)

Side by side, we take the "forward()" function which is responsible for performing the forward pass/propagation. The input is passed through the 2 layers defined earlier. In add-on, the output from the second layer is passed through an activation function called sigmoid.

What is an activation part?

Activation functions are used to capture the complex relationships in linear data. In this case, we use a sigmoid activation office.

The reason we accept called the sigmoid function, in this case, is because it will restrict the value to (0 to ane). Below is a graph of sigmoid part forth with its formula

Logistic Regression with Pytorch 2Source: Imagelink

4. Grooming and optimization

After defining the course, you tin initialize a model.

model=Logistic_Reg_model(n_features)

Now, we demand to define the loss function and optimization algorithm. Luckily in Pytorch, you tin choose and import your desired loss office and optimization algorithm in simple steps. Here, we choose BCE as our loss benchmark.

What is BCE loss?

It stands for Binary Cross-Entropy loss. Its usually used for binary classification examples. A notable point is that, when using the BCE loss function, the output of the node should exist between (0–1). Nosotros need to apply an appropriate activation function for this.

For optimizer, we choose SGD or Stochastic gradient descent. I assume you lot are familiar with the SGD algorithm, which is unremarkably used as an optimizer. There are other optimizers like Adam, lars, etc. Depending upon the trouble, you have to decide on the optimizer. For optimizer, y'all should requite model parameters and learning rate every bit input.

What is a learning rate?

Optimization algorithms have a parameter called the learning rate. This basically decides the rate at which the algorithm approaches the local minima, where the loss volition be minimal. This value is crucial.

Changes in the loss function vs. the epoch by the learning rate [40]. | Download Scientific Diagram

Source: Imagelink

 Because if the value is too high, the algorithm might shoot up and miss the local minima. On the other hand, if information technology's too minor, it will have a lot of time and may not converge. Hence, the Learning rate "lr" is a hyperparameter that should be fine-tuned to an optimal value.

criterion=torch.nn.BCELoss() optimizer=torch.optim.SGD(model.parameters(),lr=0.01)

Next, you decide the number of epochs and we write the grooming loop.

number_of_epochs=100 for epoch in range(number_of_epochs):  y_prediction=model(x_train)  loss=criterion(y_prediction,y_train)  loss.backward()  optimizer.step()  optimizer.zero_grad()  if (epoch+1)%10 == 0:    print('epoch:', epoch+1,',loss=',loss.item())

Here, the starting time forward pass happens. Next, the loss is calculated. When loss.backward() is chosen, information technology computes the gradient of the loss with respect to the weights (of the layer). The weights are then updated by calling optimizer.step(). After this, the weights have to exist emptied for the side by side iteration. So the zero_grad() method is chosen.

The above code will print the loss at each 10th epoch.

epochSource: Screenshot from my Jupiter notebook

If you observe the loss is decreasing with the epoch, the algorithm is working correct. Yous can finetune the no of epochs and learning charge per unit and try to better the performance of the model!

v. Calculate Accurateness

How can y'all calculate accuracy?

with torch.no_grad():  y_pred=model(x_test)  y_pred_class=y_pred.round()  accurateness=(y_pred_class.eq(y_test).sum())/float(y_test.shape[0])  print(accuracy.item())

Nosotros have to utilize torch.no_grad() here. The aim is to basically skip the slope calculation over the weights. And then, whatever I write inside this loop will not cause a modify in the weights and hence not disturb the backpropagation procedure. In this case, I got an accuracy of 0.92105!

Hope this was useful. Thanks for the read!

Connect with me over email: [email protected]

The media shown in this article are non owned by Analytics Vidhya and are used at the Author's discretion.

DOWNLOAD HERE

Posted by: donnaphroodession.blogspot.com

Postar um comentário for "Logistic Regression Implementation Python Code Download Only Using Loops UPDATED"