Logistic Regression
    1. Logistic regression is a supervised machine learning algorithm.
    2. In the logistic regression, the outcome must be a categorical or discrete value, it can be either yes or no, True or False, or 0 or 1.
    3. Logistic regression gives probabilistic values between 0 and 1.
    4. Logistic regression is used to solve the classification problem.
    5. In logistic regression, the regression line fits an S-shaped logistic function that predicts two maximum values, 0 or 1.
    6. Logistic regression uses the concept of predictive modeling as regression, so it is called logistic regression.
    7. But logistic regression classifies the samples; therefore, it falls under the classification problem.
Types of Logistic Regression
    1. Binomial: In binomial logistic regression, there are only two possible types of dependent variables.
    2. Example: 0 or 1, pass or fail, male or female, etc.

    3. Multinomial: In multinomial logistic regression, there are three or more possible unordered types of dependent variables.
    4. Example: Laptop, monitor, CPU, etc.

    5. Ordinal: In ordinal logistic regression, there are three or more possible ordered types of dependent variables.
    6. Example: Low, medium, high.





import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

df = pd.read_csv("insurance_data.csv")

x = df[["age"]].values
y = df["bought_insurance"]

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

logistic_regressor = LogisticRegression()
logistic_regressor.fit(x_train, y_train)

print("Model score is")
print(logistic_regressor.score(x_test, y_test))

print("Customer buy insurance")
print(logistic_regressor.predict([[25]]))
print(logistic_regressor.predict([[35]]))
print(logistic_regressor.predict([[45]]))

Model score is
1.0
Customer buy insurance
[0]
[0]
[1]

# Cross verify using Logistic Regression formula
import math

def sigmoid(x):

  y = 1 / (1 + math.exp(-x))

  return y

def predict_using_formula(x):

  a0 = logistic_regressor.intercept_
  a1 = logistic_regressor.coef_

  predicted_y = a0 + a1 * x

  y = sigmoid(predicted_y)

  return y

def person_buy_insurance(age):

  y = predict_using_formula(age)

  buy_insurance = 0
  if y > 0.5:
      buy_insurance = 1
  else:
      buy_insurance = 0

  return buy_insurance


print("Customer buy insurance")

print(person_buy_insurance(25))
print(person_buy_insurance(35))
print(person_buy_insurance(45))

Customer buy insurance

0
0
1