34. Logistic Regression ने Python मध्ये Classification
34.2 छोटे LogisticRegression Copy-Run
का गरजेचे आहे. Regression सारखीच शिस्त हवी: split, फक्त train वर fit, test वर predict.
हे काय आहे. sklearn.linear_model.LogisticRegression features मधून class 1 ची probability शिकते, मग threshold (default 0.5) लावून 0 किंवा 1 देते.
कसे करायचे.
छोटे churn-style model copy-run
- X आणि y तयार करा.
- Classes असमान असताना
stratify=yसह train_test_split (Chapter 31). - LogisticRegression fit करा.
- Test set वर predict print करा.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
df = pd.DataFrame({
"orders_last_30d": [2, 8, 1, 12, 3, 9, 0, 11, 4, 7, 1, 10],
"complaints": [1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0],
"churned": [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
})
X = df[["orders_last_30d", "complaints"]]
y = df["churned"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.33, random_state=42, stratify=y
)
clf = LogisticRegression(max_iter=200)
clf.fit(X_train, y_train)
print("Predictions:", clf.predict(X_test))
print("True labels:", y_test.values)
तुम्हाला काय दिसायला हवे. खऱ्या labels शेजारी 0/1 predictions ची छोटी यादी. Scoring quality Chapter 37 मध्ये — इथे ध्येय clean fit/predict पाथ आहे.
काल्पनिक data
Churn flags शिकवण्यासाठी काल्पनिक आहेत, खरे app export नाहीत.