Logistic Regression
Logistic Regression
•The curve from the logistic function indicates the likelihood of something
such as whether the cells are cancerous or not, a mouse is obese or not
based on its weight, etc.
•It maps any real value into another value within a range of 0 and 1.
•In logistic regression, we use the concept of the threshold value, which
defines the probability of either 0 or 1. Such as values above the
threshold value tends to 1, and a value below the threshold values tends
to 0
•We know the equation of the straight line can be written as:
•In Logistic Regression y can be between 0 and 1 only, so for this let's
divide the above equation by (1-y):
import numpy as np
from sklearn.linear_model import LinearRegression
X = numpy.array([0,1,2,3,4,5,6,7,8,9]).reshape(-1,1)
y = numpy.array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1])
print(X)
print(y)
model = LogisticRegression(solver='liblinear', random_state=0)
model.fit(X, y)
print(model.intercept_)
print( model.coef_)
y_pred = model.predict(X)
print(y_pred)
cm = confusion_matrix(y,y_pred)
ac = accuracy_score(y,y_pred)
pr= precision_score(y,y_pred)
re= recall_score(y,y_pred)
print(cm)
print(ac)
print(pr)
print(re)
OUTPUT:
[[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]]
[0 0 0 0 1 1 1 1 1 1]
[-1.04608067]
[[0.51491375]]
[0 0 0 1 1 1 1 1 1 1]
[[3 1]
[0 6]]
0.9
0.8571428571428571
1.0
Output Analysi:
https://keisan.casio.com/exec/system/1223447896
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size =
0.20, random_state = 0)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print(X_train)
print(X_test)
print(classifier.intercept_)
print( classifier.coef_)
cm = confusion_matrix(y_test, y_pred)
ac = accuracy_score(y_test, y_pred)
pr= precision_score(y_test, y_pred)
re= recall_score(y_test, y_pred)
print(cm)
print(ac)
print(pr)
print(re)
OUTPUT:
[-0.84765641]
[[1.99162298 1.07582244]]
[[56 2]
[ 5 17]]
0.9125
0.8947368421052632
0.7727272727272727