Link Search Menu Expand Document

Here we are going to implement logistic regression in python. I tried implementing Logistic Regression using statsmodels, but i am getting some error, which i am analysis. So i implemented through sklearn library.

Loading Libraries

import pandas as pd import numpy as np import os from sklearn import tree from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score

Setting up directory and reading the file

os.chdir(“/Users/mac/Downloads/”) T1 = pd.read_excel(“Latest Covid-19 India Status.xlsx”) T1.head() T1 = T1.drop(‘State/UTs’, axis = 1) // Dropping off the first variable, as it does not add any value to the model.

Replacing ‘Yes’ with 1 and ‘No’ with 0(Target Variable)

T1[‘DR Above 1%’] = T1[‘DR Above 1%’].replace(‘No’,0) T1[‘DR Above 1%’] = T1[‘DR Above 1%’].replace(‘Yes’,1)

Train & Test Split.(Simple Random Sampling as we deal with numerical data)

train, test = train_test_split(T1, test_size=0.2)

Building Model And Predictions

from sklearn.linear_model import LogisticRegression
Model = LogisticRegression() classifier = Model.fit(train.iloc[:,0:7],train.iloc[:,7]) predictions = classifier.predict(test.iloc[:,0:7])

Checking Accuracy

Accuracy_score = accuracy_score(predictions, test.iloc[:,7])
Accuracy_score*100