Cognitive Computing UCS420
Logistic Regression
What is Logistic Regression?
• Logistic regression is used for classification problems.
• Unlike linear regression, it predicts probabilities (values
between 0 and 1).
• Uses the sigmoid function to map predictions.
Understanding the Iris Dataset
• The Iris dataset is widely used in ML.
• It contains 150 samples of 3 flower species (Setosa,
Versicolor, Virginica).
• Each sample has 4 features: Sepal Length, Sepal Width,
Petal Length, Petal Width.
Understanding the Iris Dataset
• The Iris dataset is widely used in ML.
• It contains 150 samples of 3 flower species (Setosa,
Versicolor, Virginica).
• Each sample has 4 features: Sepal Length, Sepal Width,
Petal Length, Petal Width.
Loading the Iris Dataset in Python
• Import datasets from sklearn and load the dataset.
• from sklearn import datasets
• iris = datasets.load_iris()
• X = iris.data
• y = iris.target
• X contains features, y contains target labels (0, 1, 2).
Splitting the Dataset
Why do we split the data? To train and test separately.
Answer-We split the data into training and testing sets to ensure that our
model learns from one part of the data and is evaluated on another, unseen
part.
Code:
from sklearn.model_selection import train_test_split
80% training, 20% testing.
Feature Scaling (Standardization)
Feature Scaling- Why? To normalize data for better model performance.
from sklearn.preprocessing import StandardScaler
• scaler = StandardScaler()
Training the Logistic Regression Model
We use LogisticRegression from sklearn.
from sklearn.linear_model import LogisticRegression
multi_class='ovr' means One-vs-Rest classification.
ulti_class='ovr' (One-vs-Rest) is a strategy used in multi-class classification
problems where logistic regression is inherently designed for binary
classification.
Training the Logistic Regression Model
• How does it work?
• If there are N classes, the model trains N binary classifiers.
• Each classifier treats one class as positive (1) and the rest as negative (0).
• During prediction, the model assigns the sample to the class with the
highest probability.
• Example (Iris Dataset with 3 classes: 0, 1, 2)
• First classifier: Classifies class 0 vs. (class 1 and 2 together).
• Second classifier: Classifies class 1 vs. (class 0 and 2 together).
• Third classifier: Classifies class 2 vs. (class 0 and 1 together).
Making Predictions and Model Evaluation
Metrics
Use model.predict() to classify test samples.
y_pred = model.predict(X_test)
Predictions will be class labels (0, 1, or 2).
Accuracy Score: Measures overall correctness