-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_car_price_xgboost.py
More file actions
404 lines (311 loc) · 9.41 KB
/
Copy pathtrain_car_price_xgboost.py
File metadata and controls
404 lines (311 loc) · 9.41 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
print("SCRIPT STARTED - CAR PRICE PREDICTION")
import os
import json
import joblib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from xgboost import XGBRegressor
from sklearn.model_selection import train_test_split
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.pipeline import Pipeline
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent
# =========================
# SETTINGS
# =========================
DATA_DIR = BASE_DIR / "archiveprices"
RESULTS_DIR = BASE_DIR / "results_xgboost_price"
RESULTS_DIR.mkdir(exist_ok=True)
MODEL_PATH = RESULTS_DIR / "car_price_xgboost_model.pkl"
CSV_FILES = {
"audi.csv": "Audi",
"bmw.csv": "BMW",
"ford.csv": "Ford",
"hyundi.csv": "Hyundai",
"merc.csv": "Mercedes",
"skoda.csv": "Skoda",
"toyota.csv": "Toyota",
"vauxhall.csv": "Vauxhall",
"vw.csv": "Volkswagen"
}
TARGET = "price"
NUMERIC_FEATURES = [
"year",
"mileage",
"tax",
"mpg",
"engineSize"
]
CATEGORICAL_FEATURES = [
"brand",
"model",
"transmission",
"fuelType"
]
# =========================
# LOAD AND MERGE DATASET
# =========================
all_dfs = []
for file_name, brand in CSV_FILES.items():
file_path = os.path.join(DATA_DIR, file_name)
if not os.path.exists(file_path):
raise FileNotFoundError(f"Could not find file: {file_path}")
temp_df = pd.read_csv(file_path)
temp_df["brand"] = brand
all_dfs.append(temp_df)
df = pd.concat(all_dfs, ignore_index=True)
print("\nMerged dataset shape:", df.shape)
print("\nColumns:")
print(df.columns)
print("\nFirst rows:")
print(df.head())
print("\nMissing values:")
print(df.isnull().sum())
print("\nBrand distribution:")
print(df["brand"].value_counts())
# =========================
# BASIC CLEANING
# =========================
# Some files use "tax", while Hyundai uses "tax(£)"
# We merge both into one common "tax" column
if "tax(£)" in df.columns:
df["tax"] = df["tax"].fillna(df["tax(£)"])
needed_columns = CATEGORICAL_FEATURES + NUMERIC_FEATURES + [TARGET]
df = df[needed_columns].copy()
# Clean text columns
for col in CATEGORICAL_FEATURES:
df[col] = df[col].astype(str).str.strip()
# Remove missing rows
df = df.dropna()
# Remove unrealistic / noisy values
df = df[df["price"] > 500]
df = df[df["price"] < 100000]
df = df[df["year"] >= 1990]
df = df[df["year"] <= 2025]
df = df[df["mileage"] >= 0]
df = df[df["mileage"] < 300000]
df = df[df["engineSize"] > 0]
df = df[df["engineSize"] < 8]
print("\nDataset shape after cleaning:", df.shape)
print("\nBrand distribution after cleaning:")
print(df["brand"].value_counts())
# Save cleaned dataset
df.to_csv(os.path.join(RESULTS_DIR, "clean_used_car_dataset.csv"), index=False)
# =========================
# BASIC DATASET GRAPHS
# =========================
plt.figure(figsize=(8, 5))
df["price"].hist(bins=50)
plt.title("Used Car Price Distribution")
plt.xlabel("Price")
plt.ylabel("Number of Cars")
plt.grid(True)
plt.tight_layout()
plt.savefig(os.path.join(RESULTS_DIR, "price_distribution.png"), dpi=300, bbox_inches="tight")
plt.show()
plt.figure(figsize=(8, 5))
df["brand"].value_counts().plot(kind="bar")
plt.title("Number of Cars per Brand")
plt.xlabel("Brand")
plt.ylabel("Count")
plt.xticks(rotation=45)
plt.grid(axis="y")
plt.tight_layout()
plt.savefig(os.path.join(RESULTS_DIR, "brand_distribution.png"), dpi=300, bbox_inches="tight")
plt.show()
plt.figure(figsize=(8, 5))
plt.scatter(df["mileage"], df["price"], alpha=0.25)
plt.title("Price vs Mileage")
plt.xlabel("Mileage")
plt.ylabel("Price")
plt.grid(True)
plt.tight_layout()
plt.savefig(os.path.join(RESULTS_DIR, "price_vs_mileage.png"), dpi=300, bbox_inches="tight")
plt.show()
plt.figure(figsize=(8, 5))
plt.scatter(df["year"], df["price"], alpha=0.25)
plt.title("Price vs Year")
plt.xlabel("Year")
plt.ylabel("Price")
plt.grid(True)
plt.tight_layout()
plt.savefig(os.path.join(RESULTS_DIR, "price_vs_year.png"), dpi=300, bbox_inches="tight")
plt.show()
# =========================
# TRAIN / TEST SPLIT
# =========================
X = df[CATEGORICAL_FEATURES + NUMERIC_FEATURES]
y = df[TARGET]
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)
print("\nTrain shape:", X_train.shape)
print("Test shape:", X_test.shape)
# =========================
# BUILD XGBOOST PIPELINE
# =========================
try:
onehot = OneHotEncoder(handle_unknown="ignore", sparse_output=False)
except TypeError:
onehot = OneHotEncoder(handle_unknown="ignore", sparse=False)
preprocessor = ColumnTransformer(
transformers=[
("categorical", onehot, CATEGORICAL_FEATURES),
("numeric", "passthrough", NUMERIC_FEATURES)
]
)
xgb_model = XGBRegressor(
n_estimators=800,
learning_rate=0.03,
max_depth=6,
subsample=0.85,
colsample_bytree=0.85,
objective="reg:squarederror",
random_state=42,
n_jobs=-1
)
model = Pipeline(
steps=[
("preprocessor", preprocessor),
("regressor", xgb_model)
]
)
# =========================
# TRAIN MODEL
# =========================
print("\nTraining XGBoost model...")
model.fit(X_train, y_train)
print("\nTraining finished.")
# =========================
# EVALUATE MODEL
# =========================
y_pred = model.predict(X_test)
mae = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
r2 = r2_score(y_test, y_pred)
print("\nTest Results:")
print("MAE:", mae)
print("RMSE:", rmse)
print("R2 score:", r2)
metrics = {
"MAE": float(mae),
"RMSE": float(rmse),
"R2_score": float(r2)
}
with open(os.path.join(RESULTS_DIR, "price_model_metrics.json"), "w") as f:
json.dump(metrics, f, indent=4)
# =========================
# RESULT GRAPHS
# =========================
plt.figure(figsize=(8, 6))
plt.scatter(y_test, y_pred, alpha=0.35)
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()])
plt.title("Actual Price vs Predicted Price")
plt.xlabel("Actual Price")
plt.ylabel("Predicted Price")
plt.grid(True)
plt.tight_layout()
plt.savefig(os.path.join(RESULTS_DIR, "actual_vs_predicted_price.png"), dpi=300, bbox_inches="tight")
plt.show()
residuals = y_test - y_pred
plt.figure(figsize=(8, 5))
plt.scatter(y_pred, residuals, alpha=0.35)
plt.axhline(0)
plt.title("Residual Plot")
plt.xlabel("Predicted Price")
plt.ylabel("Residual: Actual - Predicted")
plt.grid(True)
plt.tight_layout()
plt.savefig(os.path.join(RESULTS_DIR, "residual_plot.png"), dpi=300, bbox_inches="tight")
plt.show()
plt.figure(figsize=(8, 5))
plt.hist(residuals, bins=50)
plt.title("Residual Distribution")
plt.xlabel("Prediction Error")
plt.ylabel("Count")
plt.grid(True)
plt.tight_layout()
plt.savefig(os.path.join(RESULTS_DIR, "residual_distribution.png"), dpi=300, bbox_inches="tight")
plt.show()
# =========================
# FEATURE IMPORTANCE
# =========================
trained_preprocessor = model.named_steps["preprocessor"]
trained_xgb = model.named_steps["regressor"]
cat_feature_names = trained_preprocessor.named_transformers_["categorical"].get_feature_names_out(CATEGORICAL_FEATURES)
feature_names = list(cat_feature_names) + NUMERIC_FEATURES
importances = trained_xgb.feature_importances_
importance_df = pd.DataFrame({
"feature": feature_names,
"importance": importances
}).sort_values(by="importance", ascending=False)
importance_df.to_csv(os.path.join(RESULTS_DIR, "feature_importance.csv"), index=False)
top_importance = importance_df.head(20)
plt.figure(figsize=(10, 7))
plt.barh(top_importance["feature"][::-1], top_importance["importance"][::-1])
plt.title("Top 20 Feature Importances - XGBoost")
plt.xlabel("Importance")
plt.ylabel("Feature")
plt.tight_layout()
plt.savefig(os.path.join(RESULTS_DIR, "feature_importance_top20.png"), dpi=300, bbox_inches="tight")
plt.show()
# =========================
# SAVE MODEL
# =========================
joblib.dump(model, MODEL_PATH)
print("\nSaved XGBoost price model:", MODEL_PATH)
# =========================
# EXAMPLE CLEAN PRICE PREDICTION
# =========================
example_car = pd.DataFrame([{
"brand": "Ford",
"model": "Focus",
"transmission": "Manual",
"fuelType": "Petrol",
"year": 2016,
"mileage": 60000,
"tax": 145,
"mpg": 55.0,
"engineSize": 1.6
}])
clean_price = model.predict(example_car)[0]
print("\nExample clean used-car price prediction:")
print(example_car)
print("Predicted clean price:", round(clean_price, 2))
# =========================
# DAMAGE PRICE ADJUSTMENT EXAMPLE
# =========================
# Later these values will come from your ResNet50 damage model.
damage_probs = {
"dent": 0.90,
"scratch": 0.80,
"crack": 0.10,
"glass_shatter": 0.05,
"lamp_broken": 0.20,
"tire_flat": 0.05
}
damage_weights = {
"scratch": 0.08,
"dent": 0.12,
"crack": 0.15,
"glass_shatter": 0.25,
"lamp_broken": 0.15,
"tire_flat": 0.10
}
damage_reduction = 0
for label, prob in damage_probs.items():
damage_reduction += prob * damage_weights[label]
# Cap the reduction so the formula does not become too extreme
damage_reduction = min(damage_reduction, 0.50)
damaged_price = clean_price * (1 - damage_reduction)
print("\nDamage adjustment example:")
print("Damage probabilities:", damage_probs)
print("Damage reduction:", round(damage_reduction * 100, 2), "%")
print("Final damaged price:", round(damaged_price, 2))
print("\nSCRIPT FINISHED")