DEV Community

Sachin Patel
Sachin Patel

Posted on • Originally published at techreactlearning.blogspot.com

Logistic Regression Explained: Will This Engine Fail?

Originally published at Programming Tech Lab.


Back in the Garage: From Numbers to Yes/No Choices

In standard Linear Regression, we predict continuous numeric values—such as estimating a used car's exact market price based on mileage. But as a software engineer or data analyst working on diagnostic systems, you often face a completely different question:

"Is this engine going to fail in the next 10,000 miles? (Yes or No)"

Predicting continuous dollar amounts or temperatures requires a straight line. But answering binary classification questions—Yes or No, Pass or Fail, Spam or Ham, Malignant or Benign—requires Logistic Regression.


Why Linear Regression Fails at Binary Classification

Why can’t we just fit a straight linear regression line to binary outcomes?

If you map "No Failure" to 0 and "Engine Failure" to 1 on a graph, a straight regression line will inevitably overshoot 1.0 (predicting a 150% chance of failure) or drop below 0.0 (predicting a -40% probability).

Probabilities must strictly remain bounded between 0% (0.0) and 100% (1.0).

The Sigmoid Function (S-Curve Pressure Valve)

To solve this, Logistic Regression takes the linear combination of inputs ($z = \beta_0 + \beta_1 x_1 + \dots$) and passes it through a mathematical function called the Sigmoid Function ($\sigma(z)$):

$$\sigma(z) = \frac{1}{1 + e^{-z}}$$

Think of the Sigmoid function as a diagnostic pressure-release valve. No matter how large or small the raw input value is, it squashes the result into a smooth S-shaped curve bounded strictly between 0.0 and 1.0.


The Decision Threshold

Once the Sigmoid function outputs a probability score (e.g., "This engine has an 82% risk of failure"), how does the model make a final binary classification?

It uses a Decision Threshold (by default set at 0.5):

  • Probability < 0.5: Classified as 0 (Engine Safe / Pass)
  • Probability ≥ 0.5: Classified as 1 (Engine Danger / Fail)

Adjusting Sensitivity in Critical Systems

In real-world applications where failure consequences are high (like an automotive engine failing at high speeds), you shouldn't wait for a 50% risk threshold before taking action.

By lowering the decision threshold to 0.20 (20%), the model flags the car for inspection if even a 21% risk is detected. In machine learning, tweaking this threshold allows you to balance Precision and Recall.


Multi-Factor Diagnostics: Multiple Logistic Regression

Predicting engine failure rarely relies on a single sensor reading. Diagnostic scanners aggregate telemetry data across multiple features:

  • Engine Temperature: High heat increases failure probability.
  • Oil Pressure Drop: Low pressure increases failure probability.
  • Engine Vibration: Excessive rattling increases failure probability.

Logistic Regression assigns a weight ($\beta_i$) to each sensor feature, sums them up, and runs the linear combination through the Sigmoid curve to output a unified probability percentage.


Quick Implementation (Python / Scikit-Learn)

Here is how you can train a Logistic Regression model for engine diagnostics:


python
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

# Synthetic Telemetry Data: [Temperature (°C), Oil Pressure (PSI), Vibration (mm/s)]
X = np.array([
    [85, 45, 1.2],
    [92, 40, 1.5],
    [115, 20, 4.8],
    [120, 15, 5.2],
    [88, 42, 1.1],
    [110, 22, 4.1]
])

# Labels: 0 = Normal, 1 = Failure Risk
y = np.array([0, 0, 1, 1, 0, 1])

# Train Logistic Regression Model
model = LogisticRegression()
model.fit(X, y)

# Predict probability on new sensor reading
sample_sensor_data = [[108, 25, 3.9]]
prob_failure = model.predict_proba(sample_sensor_data)[0][1]

print(f"Engine Failure Probability: {prob_failure * 100:.2f}%")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)