-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrain_model.py
More file actions
230 lines (179 loc) · 6.09 KB
/
Copy pathtrain_model.py
File metadata and controls
230 lines (179 loc) · 6.09 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import os
import pandas as pd
import joblib
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import (
classification_report,
accuracy_score,
confusion_matrix,
ConfusionMatrixDisplay
)
import matplotlib.pyplot as plt
DATA_PATH = "data/listings.csv"
MODEL_PATH = "models/deal_model.pkl"
def train_model():
df = pd.read_csv(DATA_PATH)
print("\nDataset loaded")
print(df.head())
print("\nDataset shape:", df.shape)
# New pattern features
df["price_per_sqm"] = df["price"] / df["sqm"]
df["deposit_months"] = df["deposit"] / df["price"]
df["room_size"] = df["sqm"] / df["rooms"]
df["area_avg_price_per_sqm"] = df.groupby("area")["price_per_sqm"].transform("mean")
df["discount_vs_area"] = (
df["area_avg_price_per_sqm"] - df["price_per_sqm"]
) / df["area_avg_price_per_sqm"]
df["is_cheap_for_area"] = (df["discount_vs_area"] >= 0.15).astype(int)
df["is_deposit_reasonable"] = (df["deposit_months"] <= 3).astype(int)
df["value_score"] = (
df["discount_vs_area"] * 100
+ df["bills_included"] * 5
+ df["near_transport"] * 5
+ df["viewing_available"] * 10
- df["deposit_months"] * 5
)
X = df[
[
"area",
"price",
"sqm",
"rooms",
"deposit",
"furnished",
"bills_included",
"near_transport",
"viewing_available",
"price_per_sqm",
"deposit_months",
"room_size",
"area_avg_price_per_sqm",
"discount_vs_area",
"is_cheap_for_area",
"is_deposit_reasonable",
"value_score",
]
]
y = df["label"]
print("\nLabel balance:")
print(y.value_counts())
categorical_features = ["area"]
numeric_features = [
"price",
"sqm",
"rooms",
"deposit",
"furnished",
"bills_included",
"near_transport",
"viewing_available",
"price_per_sqm",
"deposit_months",
"room_size",
"area_avg_price_per_sqm",
"discount_vs_area",
"is_cheap_for_area",
"is_deposit_reasonable",
"value_score",
]
preprocessor = ColumnTransformer(
transformers=[
("area_encoder", OneHotEncoder(handle_unknown="ignore"), categorical_features),
("numbers", "passthrough", numeric_features),
]
)
model = RandomForestClassifier(
n_estimators=200,
max_depth=None,
random_state=42,
class_weight="balanced"
)
pipeline = Pipeline(
steps=[
("preprocessor", preprocessor),
("model", model),
]
)
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.4,
random_state=42,
stratify=y
)
pipeline.fit(X_train, y_train)
predictions = pipeline.predict(X_test)
print("\nModel trained.")
print("\nAccuracy:")
print(accuracy_score(y_test, predictions))
print("\nClassification Report:")
print(classification_report(y_test, predictions))
print("\nConfusion Matrix:")
cm = confusion_matrix(y_test, predictions)
print(cm)
disp = ConfusionMatrixDisplay(confusion_matrix=cm)
disp.plot()
plt.show()
print("\nCross Validation Accuracy:")
scores = cross_val_score(pipeline, X, y, cv=5, scoring="accuracy")
print(scores)
print("Mean CV Accuracy:", scores.mean())
print("\nExample prediction test:")
sample_listing = pd.DataFrame([
{
"area": "Berlin Mitte",
"price": 850,
"sqm": 45,
"rooms": 2,
"deposit": 1700,
"furnished": 1,
"bills_included": 1,
"near_transport": 1,
"viewing_available": 1,
}
])
# Add same pattern features to sample listing
sample_listing["price_per_sqm"] = sample_listing["price"] / sample_listing["sqm"]
sample_listing["deposit_months"] = sample_listing["deposit"] / sample_listing["price"]
sample_listing["room_size"] = sample_listing["sqm"] / sample_listing["rooms"]
area_avg_map = df.groupby("area")["price_per_sqm"].mean()
sample_listing["area_avg_price_per_sqm"] = sample_listing["area"].map(area_avg_map)
sample_listing["discount_vs_area"] = (
sample_listing["area_avg_price_per_sqm"] - sample_listing["price_per_sqm"]
) / sample_listing["area_avg_price_per_sqm"]
sample_listing["is_cheap_for_area"] = (
sample_listing["discount_vs_area"] >= 0.15
).astype(int)
sample_listing["is_deposit_reasonable"] = (
sample_listing["deposit_months"] <= 3
).astype(int)
sample_listing["value_score"] = (
sample_listing["discount_vs_area"] * 100
+ sample_listing["bills_included"] * 5
+ sample_listing["near_transport"] * 5
+ sample_listing["viewing_available"] * 10
- sample_listing["deposit_months"] * 5
)
sample_prediction = pipeline.predict(sample_listing)
sample_probability = pipeline.predict_proba(sample_listing)
print("Prediction:", sample_prediction[0])
print("Prediction probabilities:", sample_probability)
print("\nFeature Importance:")
trained_preprocessor = pipeline.named_steps["preprocessor"]
trained_model = pipeline.named_steps["model"]
area_names = trained_preprocessor.named_transformers_["area_encoder"].get_feature_names_out(["area"])
feature_names = list(area_names) + numeric_features
importance_df = pd.DataFrame({
"feature": feature_names,
"importance": trained_model.feature_importances_
}).sort_values(by="importance", ascending=False)
print(importance_df)
os.makedirs("models", exist_ok=True)
joblib.dump(pipeline, MODEL_PATH)
print(f"\nModel saved to {MODEL_PATH}")
if __name__ == "__main__":
train_model()