Logistic Regression

Python · Machine Learning (Scikit-Learn)

Theory

Despite its name, logistic regression is a classification model. It computes a weighted sum of the features like linear regression, then passes it through the sigmoid function, which squeezes any number into a probability between 0 and 1. predict_proba(X) returns those probabilities, one column per class, and predict(X) returns class 1 whenever the probability is at least 0.5.

0.5 is only a default threshold. If missing a positive is costly (a fraudulent payment, a sick patient), lower it to catch more positives; if false alarms are costly, raise it. Apply your own with (model.predict_proba(X)[:, 1] >= 0.3).astype(int). The sign of each coefficient shows whether a feature pushes the probability up or down. C controls regularisation (smaller C means a simpler model), and scaling the features helps the solver converge.

Why we use it

It gives well-behaved probabilities, trains fast and is easy to explain, which makes it the standard baseline for yes/no problems such as churn, default or pass/fail, and a common choice in regulated industries.

When to use it

Use it for binary or multi-class classification when a mostly linear boundary is reasonable and you want probabilities. Tune the threshold to the cost of errors. For complex non-linear patterns, try random forests or gradient boosting.

How it works

  1. Features X and 0/1 labels y
  2. Weighted sum z = coef x X + intercept
  3. Sigmoid turns z into a probability from 0 to 1
  4. Probability >= threshold? Yes: Predict class 1. No: Predict class 0.
  5. Tune the threshold to the cost of each mistake
  6. Class predictions with probabilities

Syntax

from sklearn.linear_model import LogisticRegression
model = LogisticRegression(C=1.0, max_iter=1000).fit(X_train, y_train)
model.predict_proba(X_test)[:, 1]
(model.predict_proba(X_test)[:, 1] >= 0.3).astype(int)

Example

model = LogisticRegression().fit(X_train, y_train)
prob = model.predict_proba(X_test)[:, 1]
print((prob >= 0.3).astype(int))

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic