Logistic Regression 📉
Classification, Not Regression!
Despite its name, Logistic Regression is used for Classification tasks. It predicts the probability that an instance belongs to a specific class (e.g., Yes/No, True/False, Spam/Not Spam).
✨ The Sigmoid Function
Logistic Regression uses the Sigmoid (S-curve) function to map any input value to a probability between 0 and 1.
python
# Probability = 1 / (1 + e^-x)🛠️ Example: Tumor Prediction
Predicting if a tumor is malignant (1) or benign (0) based on size.
python
import numpy
from sklearn import linear_model
# Tumor sizes (features)
X = numpy.array([3.78, 2.44, 2.09, 0.14, 1.72, 1.65, 4.92, 4.37, 4.96, 4.52, 3.69, 5.88]).reshape(-1,1)
# Diagnosis (0=Benign, 1=Malignant)
y = numpy.array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1])
logr = linear_model.LogisticRegression()
logr.fit(X,y)
# Predict for a new tumor of size 3.46mm
predicted = logr.predict(numpy.array([3.46]).reshape(-1,1))
print(predicted) # Output: [0] (Benign)✨ Pro Tip: Probability
You can get the actual probability score instead of just the class label:
python
# Returns probability for [Class 0, Class 1]
print(logr.predict_proba(numpy.array([3.46]).reshape(-1,1)))