-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogisticregression.py
More file actions
134 lines (91 loc) · 3.73 KB
/
Copy pathlogisticregression.py
File metadata and controls
134 lines (91 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
"""logisticRegression.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1LvIxp2rzUgQognGg7XuatdDUQqBc0-hD
"""
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
import datasets
# data = {
# 'Age': [22, 25, 47, 52, 46, 56, 23, 39, 45, 35],
# 'Fare': [7.25, 71.83, 7.92, 8.05, 26.0, 83.16, 8.05, 31.28, 35.5, 8.05],
# 'Survived': [0, 1, 0, 0, 1, 1, 0, 1, 0, 0] # Target variable (0 = No, 1 = Yes)
# }
!pip install datasets
from datasets import load_dataset
dataset = load_dataset('victor/titanic')
dataset
df1=dataset['train'].to_pandas()
df1
df1=df1.rename(columns={'2urvived':'Survived'})
X = df1[['Age', 'Fare']] #independent variables
y = df1['Survived'] #dependent variables
X
y
df2=dataset['test'].to_pandas()
df2=df2.rename(columns={'2urvived':'Survived'})
XX = df1[['Age', 'Fare']] #independent variables
yy = df1['Survived'] #dependent variables
# X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3, random_state=42)
X_train ,y_train = X,y
X_test, y_test = XX,yy
log_reg = LogisticRegression()
log_reg.fit(X_train, y_train)
y_pred = log_reg.predict(X_test)
#evaluate
print("Accuracy:", accuracy_score(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("Classification Report:\n", classification_report(y_test, y_pred))
"""The model predicts 73% overall correctly. Sounds okay at first glance, but accuracy is misleading because of imbalance (many more class 0 than class 1).
1. 945 True Negatives (TN) → correctly predicted class 0.
2. 22 False Positives (FP) → predicted 1 but actual was 0.
3. 327 False Negatives (FN) → predicted 0 but actual was 1.
4. 15 True Positives (TP) → correctly predicted class 1.
Class 0 (did not survive):
precision = 0.74 → 74% of predicted 0’s were correct
recall = 0.98 → model found almost all actual 0’s
Class 1 (survived):
precision = 0.41 → when it predicted survived, only 41% were truly survivors
recall = 0.04 → it found **almost none** of the survivors
f1-score = 0.08 → very poor balance for class 1
"""
new_passenger = pd.DataFrame({'Age': [30], 'Fare': [100]})
prediction = log_reg.predict(new_passenger)
print("Prediction:", prediction[0])
"""⚠ Problem
The dataset is imbalanced: far more class 0 than class 1.
Logistic regression with only Age and Fare cannot separate survivors well.
"""
# Plot decision boundary
import matplotlib.pyplot as plt
x_min, x_max = X['Age'].min() - 5, X['Age'].max() + 5
y_min, y_max = X['Fare'].min() - 5, X['Fare'].max() + 5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 200),
np.linspace(y_min, y_max, 200))
Z = log_reg.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.3)
plt.scatter(X['Age'], X['Fare'], c=y, edgecolors='k', marker='o')
plt.xlabel("Age")
plt.ylabel("Fare")
plt.title("Logistic Regression Decision Boundary")
plt.show()
model = LogisticRegression(max_iter=1000, class_weight="balanced")
model.fit(X_train, y_train)
y_predd = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, y_predd))
print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_predd))
print("\nClassification Report:\n", classification_report(y_test, y_predd, zero_division=0))
import seaborn as sns
cm = confusion_matrix(y_test, y_pred)
labels = [0, 1]
plt.figure(figsize=(5,4))
sns.heatmap(cm, annot=True, fmt='d', cmap="Blues", xticklabels=labels, yticklabels=labels)
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix Heatmap")
plt.show()