-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
41 lines (32 loc) · 1 KB
/
Copy pathapp.py
File metadata and controls
41 lines (32 loc) · 1 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
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# Load dataset
df = pd.read_csv("titanic.csv")
# Basic cleaning
df = df[['Survived', 'Pclass', 'Sex', 'Age']].dropna()
df['Sex'] = df['Sex'].map({'male': 0, 'female': 1})
# Features & target
X = df[['Pclass', 'Sex', 'Age']]
y = df['Survived']
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Train model
model = LogisticRegression()
model.fit(X_train, y_train)
# Predict
y_pred = model.predict(X_test)
# Accuracy
acc = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {acc:.2f}")
# 📊 Graph 1: Survival count
df['Survived'].value_counts().plot(kind='bar')
plt.title("Survival Count")
plt.savefig("survival.png")
plt.clf()
# 📊 Graph 2: Age distribution
df['Age'].hist()
plt.title("Age Distribution")
plt.savefig("age.png")