0
0
ML Pythonprogramming~5 mins

Logistic regression in ML Python

Choose your learning style9 modes available
Introduction

Logistic regression helps us predict yes/no answers. It tells us the chance of something happening.

To decide if an email is spam or not spam.
To predict if a patient has a disease based on symptoms.
To check if a customer will buy a product or not.
To classify if a photo contains a cat or not.
To determine if a loan application should be approved or rejected.
Syntax
ML Python
from sklearn.linear_model import LogisticRegression

model = LogisticRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)

fit trains the model using data and answers.

predict uses the trained model to guess new answers.

Examples
Basic training and prediction with default settings.
ML Python
model = LogisticRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Increase max_iter to allow more training steps if the model needs it.
ML Python
model = LogisticRegression(max_iter=200)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Use a different solver for small datasets or binary classification.
ML Python
model = LogisticRegression(solver='liblinear')
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Sample Program

This example uses the iris flower data to predict if a flower is not class 0 (binary yes/no). It trains the logistic regression model and shows accuracy and predictions.

ML Python
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load iris data
iris = load_iris()
X = iris.data
# Use only two classes for binary classification
y = (iris.target != 0).astype(int)

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Create and train model
model = LogisticRegression(max_iter=200)
model.fit(X_train, y_train)

# Predict
predictions = model.predict(X_test)

# Check accuracy
acc = accuracy_score(y_test, predictions)

print(f"Accuracy: {acc:.2f}")
print(f"Predictions: {predictions}")
OutputSuccess
Important Notes

Logistic regression works well for yes/no questions but not for many categories.

Make sure your data is clean and scaled for better results.

Check if the model converges by increasing max_iter if needed.

Summary

Logistic regression predicts yes/no outcomes using input data.

It is simple and fast for binary classification tasks.

Training means learning from examples; prediction means guessing new answers.