<jupyter_start><jupyter_text>Logistic Regression  Logistic regression is a fundamental classification technique. It belongs to the group of linear classifiers and is somewhat similar to polynomial and linear regression. Logistic regression is fast and relatively uncomplicated, and it’s convenient for you to interpret the results. Although it’s essentially a method for binary classification, it can also be applied to multiclass problems. You’ll need an understanding of the sigmoid function and the natural logarithm function to understand what logistic regression is and how it works.This image shows the sigmoid function (or S-shaped curve) of some variable 𝑥:  The sigmoid function has values very close to either 0 or 1 across most of its domain. This fact makes it suitable for application in classification methods.  Single-Variate Logistic Regression Single-variate logistic regression is the most straightforward case of logistic regression. There is only one independent variable (or feature), which is 𝐱 = 𝑥. This figure illustrates single-variate logistic regression:  Here, you have a given set of input-output (or 𝑥-𝑦) pairs, represented by green circles. These are your observations. Remember that 𝑦 can only be 0 or 1. For example, the leftmost green circle has the input 𝑥 = 0 and the actual output 𝑦 = 0. The rightmost observation has 𝑥 = 9 and 𝑦 = 1.Logistic regression finds the weights 𝑏₀ and 𝑏₁ that correspond to the maximum LLF. These weights define the logit 𝑓(𝑥) = 𝑏₀ + 𝑏₁𝑥, which is the dashed black line. They also define the predicted probability 𝑝(𝑥) = 1 / (1 + exp(−𝑓(𝑥))), shown here as the full black line. In this case, the threshold 𝑝(𝑥) = 0.5 and 𝑓(𝑥) = 0 corresponds to the value of 𝑥 slightly higher than 3. This value is the limit between the inputs with the predicted outputs of 0 and 1.  Logistic Regression in Python<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix
x = np.arange(10).reshape(-1, 1)
y = np.array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1])
model = LogisticRegression()
model.fit(x, y)<jupyter_output><empty_output><jupyter_text>LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,                   intercept_scaling=1, l1_ratio=None, max_iter=100,                   multi_class='warn', n_jobs=None, penalty='l2',                   random_state=0, solver='liblinear', tol=0.0001, verbose=0,                   warm_start=False)<jupyter_code>print("Classes: ", model.classes_)
print("Intercept: ",model.intercept_)
print("Coef: ",model.coef_)
print("Probability: ",model.predict_proba(x))
model.predict(x)
confusion_matrix(y, model.predict(x))
import seaborn as sns

cm = confusion_matrix(y, model.predict(x))
sns.heatmap(cm, annot=True)
print(classification_report(y, model.predict(x)))
model = LogisticRegression(solver='liblinear', C=0.5, random_state=0)
model.fit(x, y)
model.intercept_
model.coef_
model.predict_proba(x)
model.predict(x)
model.score(x, y)
confusion_matrix(y, model.predict(x))
sns.heatmap(confusion_matrix(y, model.predict(x)), annot=True)
print(classification_report(y, model.predict(x)))<jupyter_output>precision    recall  f1-score   support

           0       1.00      0.50      0.67         4
           1       0.75      1.00      0.86         6

    accuracy                           0.80        10
   macro avg       0.88      0.75      0.76        10
weighted avg       0.85      0.80      0.78        10
StarcoderdataJupyterStructuredCleanDedup