-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
143 lines (102 loc) · 3.83 KB
/
Copy pathtrain.py
File metadata and controls
143 lines (102 loc) · 3.83 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
134
135
136
137
138
139
140
141
142
143
import os
import pandas as pd
import mlflow
import mlflow.sklearn
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
mlflow.set_tracking_uri("sqlite:///mlflow.db")
mlflow.set_experiment("titanic-experiment")
# -------------------------------
# 1. Load Data
# -------------------------------
def load_data():
return pd.read_csv("data/train.csv")
# -------------------------------
# 2. Preprocessing
# -------------------------------
def preprocess(df):
df = df.copy()
# Fill Embarked
df['Embarked'] = df['Embarked'].fillna(df['Embarked'].mode()[0])
# Convert categorical
df['Sex'] = df['Sex'].map({'male': 0, 'female': 1})
# Title extraction
df['Title'] = df['Name'].str.extract(r' ([A-Za-z]+)\.', expand=False)
df['Title'] = df['Title'].replace(['Lady','Countess','Capt','Col',
'Don','Dr','Major','Rev','Sir','Jonkheer','Dona'], 'Rare')
df['Title'] = df['Title'].replace(['Mlle','Ms'], 'Miss')
df['Title'] = df['Title'].replace('Mme', 'Mrs')
df['Title'] = df['Title'].map({
'Mr': 0, 'Miss': 1, 'Mrs': 2, 'Master': 3, 'Rare': 4
})
# Fill Age smarter (based on Title)
df['Age'] = df.groupby('Title')['Age'].transform(
lambda x: x.fillna(x.median())
)
# Fill Fare for test.csv
df['Fare'] = df['Fare'].fillna(df['Fare'].median())
# Feature Engineering
df['FamilySize'] = df['SibSp'] + df['Parch'] + 1
df['IsAlone'] = (df['FamilySize'] == 1).astype(int)
# 🔥 NEW FEATURE
df['FarePerPerson'] = df['Fare'] / df['FamilySize']
return df
# -------------------------------
# 3. Train Model
# -------------------------------
def train_model(X_train, y_train, max_depth, n_estimators):
model = RandomForestClassifier(
n_estimators=n_estimators,
max_depth=max_depth,
random_state=42
)
model.fit(X_train, y_train)
return model
# -------------------------------
# 4. Main Pipeline
# -------------------------------
def main():
df = load_data()
df = preprocess(df)
features = [
'Pclass', 'Sex', 'Age', 'Fare',
'FamilySize', 'IsAlone', 'Title',
'FarePerPerson'
]
X = df[features]
y = df['Survived']
X_train, X_val, y_train, y_val = train_test_split(
X, y, test_size=0.2, random_state=42
)
# 🔥 MORE POWERFUL SEARCH SPACE
for max_depth in [6, 7, 8]:
for n_estimators in [180, 200, 220]:
run_name = f"depth_{max_depth}_trees_{n_estimators}"
with mlflow.start_run(run_name=run_name):
model = train_model(X_train, y_train, max_depth, n_estimators)
preds = model.predict(X_val)
acc = accuracy_score(y_val, preds)
# ---------------- MLflow ----------------
mlflow.log_param("max_depth", max_depth)
mlflow.log_param("n_estimators", n_estimators)
mlflow.log_metric("accuracy", acc)
# 🔥 Feature Importance Plot
importances = model.feature_importances_
plt.figure()
plt.barh(features, importances)
plt.title("Feature Importance")
plt.tight_layout()
plt.savefig("feature_importance.png")
mlflow.log_artifact("feature_importance.png")
# Save model
mlflow.sklearn.log_model(model, "model")
print(f"{run_name} → Accuracy: {acc}")
# Save locally
os.makedirs("models", exist_ok=True)
model_path = f"models/{run_name}.pkl"
pd.to_pickle(model, model_path)
# -------------------------------
if __name__ == "__main__":
main()